Skip to content

Commit 0c528ed

Browse files
committed
refactoring
1 parent 9dbd84d commit 0c528ed

8 files changed

Lines changed: 382 additions & 289 deletions

File tree

.projectKnowledge/documap.mmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,7 @@
15861586
> fillColor=`#FFC800`
15871587

15881588

1589-
####### Special case //$""" means merging of text lines<br/>before providing then to an external processor
1589+
####### Special case //$""" means merging of text lines<br/>before providing them to an external processor
15901590
> fillColor=`#FFC800`
15911591

15921592

@@ -1602,7 +1602,7 @@
16021602
> fillColor=`#FFC800`
16031603

16041604

1605-
####### Special case //$$""" means merging of text lines<br/>before providing then to an external processor
1605+
####### Special case //$$""" means merging of text lines<br/>before providing them to an external processor
16061606
> fillColor=`#FFC800`
16071607

16081608

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

Lines changed: 85 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
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;
5857
import com.igormaznitsa.jcp.directives.ExcludeIfDirectiveHandler;
5958
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
6059
import com.igormaznitsa.jcp.exceptions.PreprocessorException;
@@ -222,68 +221,41 @@ public PreprocessorContext getContext() {
222221
return this.context;
223222
}
224223

225-
226224
public Statistics execute() throws IOException {
227-
this.context.getCommentTextProcessors().forEach(x -> x.onContextStarted(this.context));
228-
this.context.getMapVariableNameToSpecialVarProcessor()
229-
.values().forEach(x -> x.onContextStarted(this.context));
230-
231225
final long timeStart = System.currentTimeMillis();
232-
Throwable throwable = null;
233-
Statistics stat = null;
234-
try {
235-
this.context.getActivatedConfigFiles().addAll(processConfigFiles());
226+
this.context.getActivatedConfigFiles().addAll(processConfigFiles());
236227

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);
228+
this.context.logInfo(String
229+
.format("File extensions: %s excluded %s", this.context.getExtensions(),
230+
this.context.getExcludeExtensions()));
231+
final List<PreprocessorContext.SourceFolder> srcFolders = this.context.getSources();
232+
this.context.logDebug("Source folders: " + srcFolders);
242233

243-
if (srcFolders.isEmpty()) {
244-
this.context.logWarning("Source folder list is empty!");
245-
}
234+
if (srcFolders.isEmpty()) {
235+
this.context.logWarning("Source folder list is empty!");
236+
}
246237

247-
final Collection<FileInfoContainer> filesToBePreprocessed =
248-
collectFilesToPreprocess(srcFolders, this.context.getExcludeFolders());
249-
this.context.addAllPreprocessedResources(filesToBePreprocessed);
238+
final Collection<FileInfoContainer> filesToBePreprocessed =
239+
collectFilesToPreprocess(srcFolders, this.context.getExcludeFolders());
240+
this.context.addAllPreprocessedResources(filesToBePreprocessed);
250241

251-
final List<PreprocessingState.ExcludeIfInfo> excludedIf =
252-
processGlobalDirectives(filesToBePreprocessed);
242+
final List<PreprocessingState.ExcludeIfInfo> excludedIf =
243+
processGlobalDirectives(filesToBePreprocessed);
253244

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));
245+
processFileExclusion(excludedIf);
246+
if (!this.context.isDryRun()) {
247+
createTargetFolder();
248+
} else {
249+
this.context.logInfo("Dry run mode is ON");
284250
}
285-
return stat;
251+
final Statistics stat = this.preprocessFiles(filesToBePreprocessed, true);
286252

253+
final long elapsedTime = System.currentTimeMillis() - timeStart;
254+
this.context.logInfo("-----------------------------------------------------------------");
255+
this.context.logInfo(String
256+
.format("Preprocessed %d files, copied %d files, ignored %d files, elapsed time %d ms",
257+
stat.getPreprocessed(), stat.getCopied(), stat.getExcluded(), elapsedTime));
258+
return stat;
287259
}
288260

289261
private void processFileExclusion(final List<PreprocessingState.ExcludeIfInfo> foundExcludeIf) {
@@ -353,54 +325,75 @@ private List<PreprocessingState.ExcludeIfInfo> processGlobalDirectives(
353325
}
354326

355327

356-
private Statistics preprocessFiles(final Collection<FileInfoContainer> files) throws IOException {
328+
private Statistics preprocessFiles(final Collection<FileInfoContainer> files,
329+
final boolean notifyProcessors) throws IOException {
330+
if (notifyProcessors) {
331+
context.fireNotificationStart();
332+
}
333+
357334
int preprocessedCounter = 0;
358335
int copiedCounter = 0;
359336
int excludedCounter = 0;
360337

361-
for (final FileInfoContainer fileRef : files) {
362-
if (fileRef.isExcludedFromPreprocessing()) {
363-
excludedCounter++;
364-
} else if (fileRef.isCopyOnly()) {
365-
if (!context.isDryRun()) {
366-
final File destinationFile =
367-
this.context.createDestinationFileForPath(fileRef.makeTargetFilePathAsString());
368-
boolean doCopy = true;
369-
370-
if (this.context.isDontOverwriteSameContent() &&
371-
PreprocessorUtils.isFileContentEquals(fileRef.getSourceFile(), destinationFile)) {
372-
doCopy = false;
373-
if (this.context.isVerbose()) {
374-
this.context.logForVerbose(String
375-
.format("Copy skipped because same content: %s -> {dst} %s",
376-
PreprocessorUtils.getFilePath(fileRef.getSourceFile()),
377-
fileRef.makeTargetFilePathAsString()));
338+
Throwable error = null;
339+
try {
340+
for (final FileInfoContainer fileRef : files) {
341+
if (fileRef.isExcludedFromPreprocessing()) {
342+
excludedCounter++;
343+
} else if (fileRef.isCopyOnly()) {
344+
if (!context.isDryRun()) {
345+
final File destinationFile =
346+
this.context.createDestinationFileForPath(fileRef.makeTargetFilePathAsString());
347+
boolean doCopy = true;
348+
349+
if (this.context.isDontOverwriteSameContent() &&
350+
PreprocessorUtils.isFileContentEquals(fileRef.getSourceFile(), destinationFile)) {
351+
doCopy = false;
352+
if (this.context.isVerbose()) {
353+
this.context.logForVerbose(String
354+
.format("Copy skipped because same content: %s -> {dst} %s",
355+
PreprocessorUtils.getFilePath(fileRef.getSourceFile()),
356+
fileRef.makeTargetFilePathAsString()));
357+
}
378358
}
379-
}
380359

381-
if (doCopy) {
382-
if (this.context.isVerbose()) {
383-
this.context.logForVerbose(String.format("Copy file %s -> {dst} %s",
384-
PreprocessorUtils.getFilePath(fileRef.getSourceFile()),
385-
fileRef.makeTargetFilePathAsString()));
360+
if (doCopy) {
361+
if (this.context.isVerbose()) {
362+
this.context.logForVerbose(String.format("Copy file %s -> {dst} %s",
363+
PreprocessorUtils.getFilePath(fileRef.getSourceFile()),
364+
fileRef.makeTargetFilePathAsString()));
365+
}
366+
PreprocessorUtils.copyFile(fileRef.getSourceFile(), destinationFile,
367+
this.context.isKeepAttributes());
368+
fileRef.getGeneratedResources().add(destinationFile);
369+
copiedCounter++;
386370
}
387-
PreprocessorUtils.copyFile(fileRef.getSourceFile(), destinationFile,
388-
this.context.isKeepAttributes());
389-
fileRef.getGeneratedResources().add(destinationFile);
390-
copiedCounter++;
391371
}
372+
} else {
373+
final long startTime = System.currentTimeMillis();
374+
fileRef.preprocessFileWithNotification(null, this.context, false);
375+
final long elapsedTime = System.currentTimeMillis() - startTime;
376+
if (this.context.isVerbose()) {
377+
this.context.logForVerbose(String
378+
.format("File preprocessing completed '%s', elapsed time %d ms",
379+
PreprocessorUtils.getFilePath(fileRef.getSourceFile()), elapsedTime));
380+
}
381+
preprocessedCounter++;
392382
}
393-
} else {
394-
final long startTime = System.currentTimeMillis();
395-
fileRef.preprocessFile(null, this.context);
396-
final long elapsedTime = System.currentTimeMillis() - startTime;
397-
if (this.context.isVerbose()) {
398-
this.context.logForVerbose(String
399-
.format("File preprocessing completed '%s', elapsed time %d ms",
400-
PreprocessorUtils.getFilePath(fileRef.getSourceFile()), elapsedTime));
401-
}
402-
preprocessedCounter++;
403383
}
384+
} catch (Throwable err) {
385+
error = err;
386+
if (error instanceof IOException) {
387+
throw (IOException) error;
388+
}
389+
if (error instanceof RuntimeException) {
390+
throw (RuntimeException) error;
391+
}
392+
if (error instanceof Error) {
393+
throw (Error) error;
394+
}
395+
} finally {
396+
context.fireNotificationStop(error);
404397
}
405398

406399
return new Statistics(

0 commit comments

Comments
 (0)