Skip to content

Commit 38fc289

Browse files
committed
refactoring
1 parent d466ff7 commit 38fc289

File tree

5 files changed

+62
-20
lines changed

5 files changed

+62
-20
lines changed

jcp/src/main/java/com/igormaznitsa/jcp/containers/FileInfoContainer.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.concurrent.atomic.AtomicReference;
4545
import java.util.regex.Matcher;
4646
import java.util.regex.Pattern;
47+
import java.util.stream.Collectors;
4748
import lombok.Data;
4849

4950
/**
@@ -447,28 +448,42 @@ private void flushTextBufferForRemovedComments(
447448

448449
if (textBuffer.length() > 0) {
449450
final List<CommentTextProcessor> processors = context.getCommentTextProcessors();
450-
final String origText = textBuffer.toString();
451+
final String textToProcess = textBuffer.toString();
451452
textBuffer.setLength(0);
452-
String text = origText;
453+
String text = textToProcess;
453454

454455
if (!processors.isEmpty()) {
455-
processors.forEach(x -> {
456-
try {
457-
final FilePositionInfo filePositionInfo =
458-
new FilePositionInfo(this.sourceFile, stringIndex);
459-
final String result = x.processUncommentedText(
460-
firstUncommentLine == null ? 0 : firstUncommentLine.getKey().length(), origText,
461-
this, filePositionInfo, context, state);
462-
textBuffer.append(result);
463-
} catch (Exception ex) {
464-
throw new PreprocessorException(
465-
"Error during external comment text processor call",
466-
origText, state.makeIncludeStack(), ex);
467-
}
468-
});
456+
final FilePositionInfo filePositionInfo =
457+
new FilePositionInfo(this.sourceFile, stringIndex);
458+
final int indent = firstUncommentLine == null ? 0 : firstUncommentLine.getKey().length();
459+
460+
final List<String> results = processors
461+
.stream()
462+
.filter(x -> x.isEnabled(this, filePositionInfo, context, state))
463+
.map(x -> {
464+
try {
465+
return x.processUncommentedText(
466+
indent,
467+
textToProcess,
468+
this,
469+
filePositionInfo,
470+
context,
471+
state
472+
);
473+
} catch (Exception ex) {
474+
throw new PreprocessorException(
475+
"Error during external comment text processor call: " +
476+
x.getClass().getCanonicalName(),
477+
textToProcess, state.makeIncludeStack(), ex);
478+
}
479+
}).collect(Collectors.toList());
469480

470-
text = textBuffer.toString();
471-
textBuffer.setLength(0);
481+
if (results.isEmpty()) {
482+
context.logDebug("No any result from processors for text block at " + filePositionInfo);
483+
text = textToProcess;
484+
} else {
485+
text = String.join("", results);
486+
}
472487
}
473488
resetablePrinter.print(text);
474489
}

jcp/src/main/java/com/igormaznitsa/jcp/context/CommentTextProcessor.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public interface CommentTextProcessor extends PreprocessorContextListener {
1616

1717
/**
1818
* Process uncommented text detected in //$ or //$$ sections. If processing not needed then the provided text must be returned.
19+
* Returned text should contain EOL in the end because will be printed into result directly.
1920
*
2021
* @param recommendedIndent indent to be recommended for the processed text if it will be processed
2122
* @param uncommentedText the text which was uncommented and needs processing, must not be null
@@ -29,7 +30,22 @@ public interface CommentTextProcessor extends PreprocessorContextListener {
2930
String processUncommentedText(
3031
int recommendedIndent,
3132
String uncommentedText,
32-
FileInfoContainer fileContainer, FilePositionInfo positionInfo,
33+
FileInfoContainer fileContainer,
34+
FilePositionInfo positionInfo,
3335
PreprocessorContext context,
3436
PreprocessingState state);
37+
38+
/**
39+
* Returns flag that the preprocessor is enabled and can be called. It gets full info about current context and file so can make decision on fly.
40+
* It will be called before every processor call.
41+
*
42+
* @return true if the preprocessor allowed, false if not
43+
* @since 7.2.1
44+
*/
45+
boolean isEnabled(
46+
FileInfoContainer fileContainer,
47+
FilePositionInfo positionInfo,
48+
PreprocessorContext context,
49+
PreprocessingState state
50+
);
3551
}

jcp/src/main/java/com/igormaznitsa/jcp/expression/functions/FunctionBINFILE.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ public ValueType[][] getAllowedArgumentTypes() {
180180
}
181181

182182
@Override
183-
184183
public ValueType getResultType() {
185184
return ValueType.STRING;
186185
}

jcp/src/test/java/com/igormaznitsa/jcp/directives/SpecialDirectivesBlockTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public void onContextStopped(PreprocessorContext context, Throwable error) {
6161
}
6262
}
6363

64+
@Override
65+
public boolean isEnabled(FileInfoContainer fileContainer, FilePositionInfo positionInfo,
66+
PreprocessorContext context, PreprocessingState state) {
67+
return true;
68+
}
69+
6470
@Override
6571
public String processUncommentedText(int recommendedIndent, String uncommentedText,
6672
FileInfoContainer fileContainer,

jcp/src/test/java/com/igormaznitsa/jcp/directives/SpecialDirectivesTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public void onContextStopped(PreprocessorContext context, Throwable error) {
5959
}
6060
}
6161

62+
@Override
63+
public boolean isEnabled(FileInfoContainer fileContainer, FilePositionInfo positionInfo,
64+
PreprocessorContext context, PreprocessingState state) {
65+
return true;
66+
}
67+
6268
@Override
6369
public String processUncommentedText(int recommendedIndent, String uncommentedText,
6470
FileInfoContainer fileContainer,

0 commit comments

Comments
 (0)