2323
2424import static java .util .Objects .requireNonNull ;
2525
26+ import com .igormaznitsa .jcp .context .CommentTextProcessor ;
2627import com .igormaznitsa .jcp .context .PreprocessingState ;
2728import com .igormaznitsa .jcp .context .PreprocessorContext ;
2829import com .igormaznitsa .jcp .directives .AbstractDirectiveHandler ;
@@ -133,12 +134,14 @@ private static String findTailRemover(final String str, final PreprocessorContex
133134
134135 /**
135136 * Check that text line starts with two commented dollar chars
136- * @param line text line to be examined, must not be null
137+ *
138+ * @param line text line to be examined, must not be null
137139 * @param allowedWhitespaces if true then whitespaces allowed after line comment
138140 * @return true if the line starts with two commented dollar chars, false otherwise
139141 * @since 7.0.6
140142 */
141- public static boolean isDoubleDollarPrefixed (final String line , final boolean allowedWhitespaces ) {
143+ public static boolean isDoubleDollarPrefixed (final String line ,
144+ final boolean allowedWhitespaces ) {
142145 if (allowedWhitespaces ) {
143146 return DIRECTIVE_TWO_DOLLARS_PREFIXED .matcher (line ).matches ();
144147 } else {
@@ -165,12 +168,14 @@ public static boolean isDoubleDollarBlockPrefixed(final String line,
165168
166169 /**
167170 * Check that text line starts with single dollar chars
168- * @param line text line to be examined, must not be null
171+ *
172+ * @param line text line to be examined, must not be null
169173 * @param allowedWhitespaces if true then whitespaces allowed after line comment
170174 * @return true if the line starts with single dollar chars, false otherwise
171175 * @since 7.0.6
172176 */
173- public static boolean isSingleDollarPrefixed (final String line , final boolean allowedWhitespaces ) {
177+ public static boolean isSingleDollarPrefixed (final String line ,
178+ final boolean allowedWhitespaces ) {
174179 if (allowedWhitespaces ) {
175180 return DIRECTIVE_SINGLE_DOLLAR_PREFIXED .matcher (line ).matches ();
176181 } else {
@@ -180,7 +185,8 @@ public static boolean isSingleDollarPrefixed(final String line, final boolean al
180185
181186 /**
182187 * Allows to check that a text line can be considered as a JCP directive or special line.
183- * @param line text line to be examined
188+ *
189+ * @param line text line to be examined
184190 * @param allowedWhitespaces if true then whitespaces allowed after line comment
185191 * @return true if the line can be considered as JCP one, false otherwise
186192 * @since 7.0.6
@@ -193,7 +199,7 @@ public static boolean isJcpCommentLine(final String line, final boolean allowedW
193199 /**
194200 * Check that a text line contains comment directive.
195201 *
196- * @param line string to be examined
202+ * @param line string to be examined
197203 * @param allowWhitespaces flag to allow spaces betwee hash and started comment chars
198204 * @return true if the line contains a directive, false otherwise
199205 * @since 7.0.6
@@ -395,13 +401,33 @@ private String extractSingleDollarPrefixedDirective(final String line,
395401 return tail ;
396402 }
397403
398- private void flushTextBufferForRemovedComments (final StringBuilder text ,
404+ private void flushTextBufferForRemovedComments (final StringBuilder textBuffer ,
399405 final ResetablePrinter resetablePrinter ,
406+ final PreprocessingState state ,
400407 final PreprocessorContext context )
401408 throws IOException {
402- if (text .length () > 0 ) {
403- resetablePrinter .print (text .toString ());
404- text .setLength (0 );
409+ if (textBuffer .length () > 0 ) {
410+ final List <CommentTextProcessor > processors = context .getCommentTextProcessors ();
411+ final String origText = textBuffer .toString ();
412+ textBuffer .setLength (0 );
413+ String text = origText ;
414+
415+ if (!processors .isEmpty ()) {
416+ processors .forEach (x -> {
417+ try {
418+ final String result = x .processCommentText (origText , this , context , state );
419+ textBuffer .append (result );
420+ } catch (Exception ex ) {
421+ throw new PreprocessorException (
422+ "Error during external comment text processor call" ,
423+ origText , state .makeIncludeStack (), ex );
424+ }
425+ });
426+
427+ text = textBuffer .toString ();
428+ textBuffer .setLength (0 );
429+ }
430+ resetablePrinter .print (text );
405431 }
406432 }
407433
@@ -457,7 +483,7 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
457483 }
458484
459485 if (rawString == null ) {
460- this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , context );
486+ this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , state , context );
461487 lastTextFileDataContainer = preprocessingState .popTextContainer ();
462488 if (preprocessingState .isIncludeStackEmpty ()) {
463489 break ;
@@ -485,7 +511,7 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
485511 final boolean doPrintLn = presentedNextLine || !context .isCareForLastEol ();
486512
487513 if (isHashPrefixed (stringToBeProcessed , context )) {
488- this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , context );
514+ this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , state , context );
489515 final String extractedDirective =
490516 extractHashPrefixedDirective (stringToBeProcessed , context );
491517 switch (processDirective (preprocessingState , extractedDirective , context , false )) {
@@ -522,7 +548,8 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
522548 if (preprocessingState .isDirectiveCanBeProcessed () &&
523549 !preprocessingState .getPreprocessingFlags ()
524550 .contains (PreprocessingFlag .TEXT_OUTPUT_DISABLED )) {
525- final boolean startsWithTwoDollars = isDoubleDollarPrefixed (leftTrimmedString , context .isAllowWhitespaces ());
551+ final boolean startsWithTwoDollars =
552+ isDoubleDollarPrefixed (leftTrimmedString , context .isAllowWhitespaces ());
526553
527554 if (!startsWithTwoDollars ) {
528555 stringToBeProcessed = PreprocessorUtils .processMacroses (leftTrimmedString , context );
@@ -540,12 +567,12 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
540567 textBlockBuffer .append (context .getEol ());
541568 }
542569 } else {
543- this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , context );
570+ this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , state , context );
544571 textBlockBuffer .append (stringPrefix ).append (text );
545572 if (doPrintLn ) {
546573 textBlockBuffer .append (context .getEol ());
547574 }
548- this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , context );
575+ this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , state , context );
549576 }
550577 } else if (isSingleDollarPrefixed (stringToBeProcessed , context .isAllowWhitespaces ())) {
551578 // Output the tail of the string to the output stream without comments
@@ -560,16 +587,16 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
560587 textBlockBuffer .append (context .getEol ());
561588 }
562589 } else {
563- this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , context );
590+ this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , state , context );
564591 textBlockBuffer .append (stringPrefix ).append (text );
565592 if (doPrintLn ) {
566593 textBlockBuffer .append (context .getEol ());
567594 }
568- this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , context );
595+ this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , state , context );
569596 }
570597 } else {
571598 // Just string
572- this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , context );
599+ this .flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , state , context );
573600
574601 final String strToOut = findTailRemover (stringToBeProcessed , context );
575602
@@ -588,7 +615,7 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
588615 }
589616 }
590617 } else if (context .isKeepLines ()) {
591- flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , context );
618+ flushTextBufferForRemovedComments (textBlockBuffer , thePrinter , state , context );
592619 final String text = AbstractDirectiveHandler .PREFIX_FOR_KEEPING_LINES + rawString ;
593620 if (doPrintLn ) {
594621 thePrinter .println (text , context .getEol ());
0 commit comments