2424import static com .igormaznitsa .jcp .removers .AbstractCommentRemover .makeCommentRemover ;
2525import static com .igormaznitsa .jcp .utils .IOUtils .closeQuietly ;
2626import static com .igormaznitsa .jcp .utils .PreprocessorUtils .findFirstActiveFileContainer ;
27+ import static java .util .Objects .requireNonNull ;
28+ import static java .util .Objects .requireNonNullElse ;
2729
2830import com .igormaznitsa .jcp .containers .FileInfoContainer ;
2931import com .igormaznitsa .jcp .containers .PreprocessingFlag ;
3032import com .igormaznitsa .jcp .containers .TextFileDataContainer ;
3133import com .igormaznitsa .jcp .exceptions .FilePositionInfo ;
3234import com .igormaznitsa .jcp .exceptions .PreprocessorException ;
3335import com .igormaznitsa .jcp .utils .PreprocessorUtils ;
34- import com .igormaznitsa .jcp .utils .ResetablePrinter ;
36+ import com .igormaznitsa .jcp .utils .ResettablePrinter ;
3537import java .io .BufferedInputStream ;
3638import java .io .BufferedOutputStream ;
3739import java .io .BufferedWriter ;
5254import java .util .EnumSet ;
5355import java .util .LinkedList ;
5456import java .util .List ;
55- import java .util .Objects ;
5657import java .util .Optional ;
5758import java .util .Set ;
5859import java .util .concurrent .atomic .AtomicBoolean ;
@@ -79,15 +80,15 @@ public final class PreprocessingState {
7980 private final LinkedList <TextFileDataContainer > ifStack = new LinkedList <>();
8081 private final LinkedList <TextFileDataContainer > includeStack = new LinkedList <>();
8182 private final LinkedList <ExcludeIfInfo > deferredExcludeStack = new LinkedList <>();
82- private final ResetablePrinter prefixPrinter = new ResetablePrinter (1024 );
83- private final ResetablePrinter postfixPrinter = new ResetablePrinter (64 * 1024 );
84- private final ResetablePrinter normalPrinter = new ResetablePrinter (1024 );
83+ private final ResettablePrinter prefixPrinter = new ResettablePrinter (1024 );
84+ private final ResettablePrinter postfixPrinter = new ResettablePrinter (64 * 1024 );
85+ private final ResettablePrinter normalPrinter = new ResettablePrinter (1024 );
8586 private final boolean overrideOnlyIfContentChanged ;
8687 private final EnumSet <PreprocessingFlag > preprocessingFlags =
8788 EnumSet .noneOf (PreprocessingFlag .class );
8889 private final PreprocessorContext context ;
8990 private final boolean mockMode ;
90- private ResetablePrinter currentPrinter ;
91+ private ResettablePrinter selectedPrinter ;
9192 private TextFileDataContainer activeIf ;
9293 private TextFileDataContainer activeWhile ;
9394 private String lastReadString ;
@@ -96,8 +97,8 @@ public final class PreprocessingState {
9697 PreprocessingState (final PreprocessorContext context , final Charset inEncoding ,
9798 final Charset outEncoding ) {
9899 this .mockMode = true ;
99- this .globalInCharacterEncoding = Objects . requireNonNull (inEncoding );
100- this .globalOutCharacterEncoding = Objects . requireNonNull (outEncoding );
100+ this .globalInCharacterEncoding = requireNonNull (inEncoding );
101+ this .globalOutCharacterEncoding = requireNonNull (outEncoding );
101102 this .rootReference = null ;
102103 this .lastReadString = "" ;
103104 this .rootFileInfo = new FileInfoContainer (new File ("global" ), "global" , true );
@@ -114,10 +115,10 @@ public final class PreprocessingState {
114115 this .context = context ;
115116
116117 this .overrideOnlyIfContentChanged = overrideOnlyIfContentChanged ;
117- this .globalInCharacterEncoding = Objects . requireNonNull (inEncoding );
118- this .globalOutCharacterEncoding = Objects . requireNonNull (outEncoding );
118+ this .globalInCharacterEncoding = requireNonNull (inEncoding );
119+ this .globalOutCharacterEncoding = requireNonNull (outEncoding );
119120
120- this .rootFileInfo = Objects . requireNonNull (rootFile , "The root file is null" );
121+ this .rootFileInfo = requireNonNull (rootFile , "The root file is null" );
121122 init ();
122123 rootReference = openFile (rootFile .getSourceFile ());
123124 }
@@ -129,11 +130,11 @@ public final class PreprocessingState {
129130
130131 this .context = context ;
131132
132- this .globalInCharacterEncoding = Objects . requireNonNull (inEncoding );
133- this .globalOutCharacterEncoding = Objects . requireNonNull (outEncoding );
133+ this .globalInCharacterEncoding = requireNonNull (inEncoding );
134+ this .globalOutCharacterEncoding = requireNonNull (outEncoding );
134135 this .overrideOnlyIfContentChanged = overrideOnlyIfContentChanged ;
135136
136- this .rootFileInfo = Objects . requireNonNull (rootFile , "The root file is null" );
137+ this .rootFileInfo = requireNonNull (rootFile , "The root file is null" );
137138 init ();
138139 rootReference = rootContainer ;
139140 includeStack .push (rootContainer );
@@ -161,8 +162,8 @@ public String getLastReadString() {
161162
162163 public void pushExcludeIfData (final FileInfoContainer infoContainer ,
163164 final String excludeIfCondition , final int stringIndex ) {
164- Objects . requireNonNull (infoContainer , "File info is null" );
165- Objects . requireNonNull (excludeIfCondition , "Condition is null" );
165+ requireNonNull (infoContainer , "File info is null" );
166+ requireNonNull (excludeIfCondition , "Condition is null" );
166167
167168 if (stringIndex < 0 ) {
168169 throw new IllegalArgumentException ("Unexpected string index [" + stringIndex + ']' );
@@ -171,6 +172,9 @@ public void pushExcludeIfData(final FileInfoContainer infoContainer,
171172 deferredExcludeStack .push (new ExcludeIfInfo (infoContainer , excludeIfCondition , stringIndex ));
172173 }
173174
175+ public ResettablePrinter getSelectedPrinter () {
176+ return this .selectedPrinter ;
177+ }
174178
175179 public List <ExcludeIfInfo > popAllExcludeIfInfoData () {
176180 final List <ExcludeIfInfo > result = new ArrayList <>(deferredExcludeStack );
@@ -188,22 +192,29 @@ public Set<PreprocessingFlag> getPreprocessingFlags() {
188192 return preprocessingFlags ;
189193 }
190194
191-
192- public ResetablePrinter getPrinter () throws IOException {
193- return currentPrinter ;
195+ public ResettablePrinter findPrinter (final PrinterType type ) {
196+ switch (requireNonNull (type , "Type is null" )) {
197+ case NORMAL :
198+ return this .normalPrinter ;
199+ case POSTFIX :
200+ return this .postfixPrinter ;
201+ case PREFIX :
202+ return this .prefixPrinter ;
203+ default :
204+ throw new IllegalArgumentException ("Unsupported type detected [" + type .name () + ']' );
205+ }
194206 }
195207
196- public void setPrinter (final PrinterType type ) {
197- Objects .requireNonNull (type , "Type is null" );
198- switch (type ) {
208+ public void selectPrinter (final PrinterType type ) {
209+ switch (requireNonNull (type , "Type is null" )) {
199210 case NORMAL :
200- currentPrinter = normalPrinter ;
211+ this . selectedPrinter = this . normalPrinter ;
201212 break ;
202213 case POSTFIX :
203- currentPrinter = postfixPrinter ;
214+ this . selectedPrinter = this . postfixPrinter ;
204215 break ;
205216 case PREFIX :
206- currentPrinter = prefixPrinter ;
217+ this . selectedPrinter = this . prefixPrinter ;
207218 break ;
208219 default :
209220 throw new IllegalArgumentException ("Unsupported type detected [" + type .name () + ']' );
@@ -217,7 +228,7 @@ public TextFileDataContainer getRootTextContainer() {
217228
218229
219230 public TextFileDataContainer openFile (final File file ) throws IOException {
220- Objects . requireNonNull (file , "The file is null" );
231+ requireNonNull (file , "The file is null" );
221232
222233 final AtomicBoolean endedByNextLineContainer = new AtomicBoolean ();
223234
@@ -276,7 +287,6 @@ public TextFileDataContainer popTextContainer() {
276287 return this .includeStack .pop ();
277288 }
278289
279-
280290 public FileInfoContainer getRootFileInfo () {
281291 return rootFileInfo ;
282292 }
@@ -422,15 +432,50 @@ private void init() {
422432 preprocessingFlags .clear ();
423433 resetPrinters ();
424434
425- setPrinter (PrinterType .NORMAL );
435+ selectPrinter (PrinterType .NORMAL );
426436 }
427437
428438 public void resetPrinters () {
429439 normalPrinter .reset ();
430440 prefixPrinter .reset ();
431441 postfixPrinter .reset ();
432442
433- currentPrinter = normalPrinter ;
443+ selectedPrinter = normalPrinter ;
444+ }
445+
446+ public void setBufferText (final String text ) {
447+ this .prefixPrinter .reset ();
448+ this .normalPrinter .reset ();
449+ this .postfixPrinter .reset ();
450+ this .setBufferText (text , PrinterType .NORMAL );
451+ }
452+
453+ public void setBufferText (final String text , final PrinterType printerType ) {
454+ switch (printerType ) {
455+ case NORMAL : {
456+ this .normalPrinter .reset ();
457+ this .normalPrinter .print (requireNonNullElse (text , "" ));
458+ }
459+ break ;
460+ case PREFIX : {
461+ this .prefixPrinter .reset ();
462+ this .prefixPrinter .print (requireNonNullElse (text , "" ));
463+ }
464+ break ;
465+ case POSTFIX : {
466+ this .postfixPrinter .reset ();
467+ this .postfixPrinter .print (requireNonNullElse (text , "" ));
468+ }
469+ break ;
470+ default :
471+ throw new IllegalArgumentException ("Unsupported printer type: " + printerType );
472+ }
473+ }
474+
475+ public String getCurrentText () {
476+ return this .prefixPrinter .getText ()
477+ + this .normalPrinter .getText ()
478+ + this .postfixPrinter .getText ();
434479 }
435480
436481 public void saveBuffersToStreams (final OutputStream prefix , final OutputStream normal ,
0 commit comments