Skip to content

Commit f8c3d5f

Browse files
committed
refactoring
1 parent 99a5f0d commit f8c3d5f

5 files changed

Lines changed: 85 additions & 37 deletions

File tree

jcp/src/main/java/com/igormaznitsa/jcp/JcpPreprocessor.java

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.igormaznitsa.jcp.context.CommentTextProcessor;
5555
import com.igormaznitsa.jcp.context.PreprocessingState;
5656
import com.igormaznitsa.jcp.context.PreprocessorContext;
57+
import com.igormaznitsa.jcp.context.SpecialVariableProcessor;
5758
import com.igormaznitsa.jcp.directives.ExcludeIfDirectiveHandler;
5859
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
5960
import com.igormaznitsa.jcp.exceptions.PreprocessorException;
@@ -86,8 +87,6 @@
8687
*/
8788
public final class JcpPreprocessor {
8889

89-
private static final String PROPERTY_JCP_BASE_DIR = "jcp.base.dir";
90-
9190
static final CommandLineHandler[] COMMAND_LINE_HANDLERS = new CommandLineHandler[] {
9291
new HelpHandler(),
9392
new InCharsetHandler(),
@@ -113,6 +112,7 @@ public final class JcpPreprocessor {
113112
new AllowMergeBlockLineHandler(),
114113
new UnknownAsFalseHandler()
115114
};
115+
private static final String PROPERTY_JCP_BASE_DIR = "jcp.base.dir";
116116
private final PreprocessorContext context;
117117

118118
public JcpPreprocessor(final PreprocessorContext context) {
@@ -224,41 +224,66 @@ public PreprocessorContext getContext() {
224224

225225

226226
public Statistics execute() throws IOException {
227-
final long timeStart = System.currentTimeMillis();
227+
this.context.getCommentTextProcessors().forEach(x -> x.onContextStarted(this.context));
228+
this.context.getMapVariableNameToSpecialVarProcessor()
229+
.values().forEach(x -> x.onContextStarted(this.context));
228230

229-
this.context.getActivatedConfigFiles().addAll(processConfigFiles());
231+
final long timeStart = System.currentTimeMillis();
232+
Throwable throwable = null;
233+
Statistics stat = null;
234+
try {
235+
this.context.getActivatedConfigFiles().addAll(processConfigFiles());
230236

231-
this.context.logInfo(String
232-
.format("File extensions: %s excluded %s", this.context.getExtensions(),
233-
this.context.getExcludeExtensions()));
234-
final List<PreprocessorContext.SourceFolder> srcFolders = this.context.getSources();
235-
this.context.logDebug("Source folders: " + srcFolders);
237+
this.context.logInfo(String
238+
.format("File extensions: %s excluded %s", this.context.getExtensions(),
239+
this.context.getExcludeExtensions()));
240+
final List<PreprocessorContext.SourceFolder> srcFolders = this.context.getSources();
241+
this.context.logDebug("Source folders: " + srcFolders);
236242

237-
if (srcFolders.isEmpty()) {
238-
this.context.logWarning("Source folder list is empty!");
239-
}
243+
if (srcFolders.isEmpty()) {
244+
this.context.logWarning("Source folder list is empty!");
245+
}
240246

241-
final Collection<FileInfoContainer> filesToBePreprocessed =
242-
collectFilesToPreprocess(srcFolders, this.context.getExcludeFolders());
243-
this.context.addAllPreprocessedResources(filesToBePreprocessed);
247+
final Collection<FileInfoContainer> filesToBePreprocessed =
248+
collectFilesToPreprocess(srcFolders, this.context.getExcludeFolders());
249+
this.context.addAllPreprocessedResources(filesToBePreprocessed);
244250

245-
final List<PreprocessingState.ExcludeIfInfo> excludedIf =
246-
processGlobalDirectives(filesToBePreprocessed);
251+
final List<PreprocessingState.ExcludeIfInfo> excludedIf =
252+
processGlobalDirectives(filesToBePreprocessed);
247253

248-
processFileExclusion(excludedIf);
249-
if (!this.context.isDryRun()) {
250-
createTargetFolder();
251-
} else {
252-
this.context.logInfo("Dry run mode is ON");
254+
processFileExclusion(excludedIf);
255+
if (!this.context.isDryRun()) {
256+
createTargetFolder();
257+
} else {
258+
this.context.logInfo("Dry run mode is ON");
259+
}
260+
stat = preprocessFiles(filesToBePreprocessed);
261+
} catch (Throwable ex) {
262+
throwable = ex;
263+
if (ex instanceof IOException) {
264+
throw (IOException) ex;
265+
}
266+
} finally {
267+
try {
268+
for (final CommentTextProcessor p : this.context.getCommentTextProcessors()) {
269+
p.onContextStopped(this.context, throwable);
270+
}
271+
} finally {
272+
for (final SpecialVariableProcessor p : this.context.getMapVariableNameToSpecialVarProcessor()
273+
.values()) {
274+
p.onContextStopped(this.context, throwable);
275+
}
276+
}
277+
}
278+
if (stat != null) {
279+
final long elapsedTime = System.currentTimeMillis() - timeStart;
280+
this.context.logInfo("-----------------------------------------------------------------");
281+
this.context.logInfo(String
282+
.format("Preprocessed %d files, copied %d files, ignored %d files, elapsed time %d ms",
283+
stat.getPreprocessed(), stat.getCopied(), stat.getExcluded(), elapsedTime));
253284
}
254-
final Statistics stat = preprocessFiles(filesToBePreprocessed);
255-
256-
final long elapsedTime = System.currentTimeMillis() - timeStart;
257-
this.context.logInfo("-----------------------------------------------------------------");
258-
this.context.logInfo(String
259-
.format("Preprocessed %d files, copied %d files, ignored %d files, elapsed time %d ms",
260-
stat.getPreprocessed(), stat.getCopied(), stat.getExcluded(), elapsedTime));
261285
return stat;
286+
262287
}
263288

264289
private void processFileExclusion(final List<PreprocessingState.ExcludeIfInfo> foundExcludeIf) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ private void flushTextBufferForRemovedComments(final StringBuilder textBuffer,
415415
if (!processors.isEmpty()) {
416416
processors.forEach(x -> {
417417
try {
418-
final String result = x.processCommentText(origText, this, context, state);
418+
final String result = x.onUncommentText(origText, this, context, state);
419419
textBuffer.append(result);
420420
} catch (Exception ex) {
421421
throw new PreprocessorException(

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
*
1111
* @since 7.2.0
1212
*/
13-
@FunctionalInterface
14-
public interface CommentTextProcessor {
13+
public interface CommentTextProcessor extends PreprocessorContextListener {
1514
/**
1615
* Process text value.
1716
*
@@ -22,7 +21,7 @@ public interface CommentTextProcessor {
2221
* @return must return value as the same text or as the changed one.
2322
* @throws IOException if any IO error during operation
2423
*/
25-
String processCommentText(
24+
String onUncommentText(
2625
String text,
2726
FileInfoContainer fileInfoContainer,
2827
PreprocessorContext context,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.igormaznitsa.jcp.context;
2+
3+
/**
4+
* Listener for preprocessor context work states.
5+
*
6+
* @since 7.2.0
7+
*/
8+
public interface PreprocessorContextListener {
9+
/**
10+
* Called when context started.
11+
*
12+
* @param context the source context, must not be null
13+
*/
14+
default void onContextStarted(PreprocessorContext context) {
15+
16+
}
17+
18+
/**
19+
* Called when context work ended.
20+
*
21+
* @param context the source context, must not be null
22+
* @param error the error if it was thrown during context execution.
23+
*/
24+
default void onContextStopped(PreprocessorContext context, Throwable error) {
25+
26+
}
27+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@
2929
*
3030
* @author Igor Maznitsa (igor.maznitsa@igormaznitsa.com)
3131
*/
32-
public interface SpecialVariableProcessor {
32+
public interface SpecialVariableProcessor extends PreprocessorContextListener {
3333

3434
/**
3535
* Get all variable names allowed by the processor as an array, all names must
3636
* be in lower case
3737
*
3838
* @return allowed variable names as a String array
3939
*/
40-
41-
4240
String[] getVariableNames();
4341

4442
/**
@@ -49,7 +47,6 @@ public interface SpecialVariableProcessor {
4947
* @return the value, it must not return null because it will notified
5048
* preprocessor that it supports the variable
5149
*/
52-
5350
Value getVariable(String varName, PreprocessorContext context);
5451

5552
/**

0 commit comments

Comments
 (0)