Skip to content

Commit ddde808

Browse files
committed
#46 refactoring for removers
1 parent d7678ce commit ddde808

25 files changed

Lines changed: 136 additions & 101 deletions

jcp-tests/jcp-test-gradle-7/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ preprocess {
4949
verbose = true
5050
clearTarget = true
5151
careForLastEol = true
52-
keepComments = 'remove_all'
52+
keepComments = 'remove_c_style'
5353
excludeFolders = ['**/some1', '**/some2']
5454
configFiles = ['./configFile.txt']
5555
keepLines = true

jcp-tests/jcp-test-gradle-8/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ preprocess {
4949
verbose = true
5050
clearTarget = true
5151
careForLastEol = true
52-
keepComments = 'remove_jcp'
52+
keepComments = 'remove_jcp_only'
5353
excludeFolders = ['**/some1', '**/some2']
5454
configFiles = ['./configFile.txt']
5555
keepLines = true

jcp/src/main/java/com/igormaznitsa/jcp/ant/PreprocessTask.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
package com.igormaznitsa.jcp.ant;
2323

2424
import com.igormaznitsa.jcp.JcpPreprocessor;
25-
import com.igormaznitsa.jcp.context.KeepComments;
25+
import com.igormaznitsa.jcp.context.CommentRemoverType;
2626
import com.igormaznitsa.jcp.context.PreprocessorContext;
2727
import com.igormaznitsa.jcp.context.SpecialVariableProcessor;
2828
import com.igormaznitsa.jcp.exceptions.PreprocessorException;
2929
import com.igormaznitsa.jcp.expression.Value;
3030
import com.igormaznitsa.jcp.logger.PreprocessorLogger;
3131
import com.igormaznitsa.jcp.utils.GetUtils;
32+
import com.igormaznitsa.jcp.utils.PreprocessorUtils;
3233
import java.io.File;
3334
import java.nio.charset.Charset;
3435
import java.util.ArrayList;
@@ -69,7 +70,7 @@ public class PreprocessTask extends Task implements PreprocessorLogger, SpecialV
6970
private boolean verbose = false;
7071
private boolean clearTarget = false;
7172
private boolean careForLastEol = false;
72-
private String keepComments = KeepComments.REMOVE_ALL.name();
73+
private String keepComments = CommentRemoverType.REMOVE_C_STYLE.name();
7374
private Vars vars = null;
7475
private ExcludeFolders excludeFolders = null;
7576
private ConfigFiles configFiles = null;
@@ -146,7 +147,7 @@ PreprocessorContext makePreprocessorContext() {
146147
context.setDontOverwriteSameContent(this.isDontOverwriteSameContent());
147148
context.setClearTarget(this.isClearTarget());
148149
context.setDryRun(this.isDryRun());
149-
context.setKeepComments(KeepComments.findForText(this.getKeepComments()));
150+
context.setKeepComments(PreprocessorUtils.findCommentRemoverForId(this.getKeepComments()));
150151
context.setVerbose(this.isVerbose());
151152
context.setKeepLines(this.isKeepLines());
152153
context.setCareForLastEol(this.isCareForLastEol());

jcp/src/main/java/com/igormaznitsa/jcp/cmdline/KeepCommentsHandler.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
package com.igormaznitsa.jcp.cmdline;
2323

24-
import com.igormaznitsa.jcp.context.KeepComments;
24+
import static com.igormaznitsa.jcp.context.CommentRemoverType.makeListOfAllRemoverIds;
25+
26+
import com.igormaznitsa.jcp.context.CommentRemoverType;
2527
import com.igormaznitsa.jcp.context.PreprocessorContext;
2628
import com.igormaznitsa.jcp.utils.PreprocessorUtils;
2729
import java.util.Locale;
@@ -38,8 +40,8 @@ public class KeepCommentsHandler implements CommandLineHandler {
3840

3941
@Override
4042
public String getDescription() {
41-
return "select keep comments mode, for instance /M:remove_all, (allowed: " +
42-
KeepComments.makeStringForExpectedValues() + ')';
43+
return "select keep comments mode, for instance /M:remove_c_style, (allowed: true,false," +
44+
makeListOfAllRemoverIds() + ')';
4345
}
4446

4547
@Override
@@ -49,13 +51,13 @@ public boolean processCommandLineKey(final String key, final PreprocessorContext
4951
if (!key.isEmpty() && key.toUpperCase(Locale.ENGLISH).startsWith(ARG_NAME)) {
5052
final String tail = PreprocessorUtils.extractTrimmedTail(ARG_NAME, key);
5153

52-
final KeepComments mode;
54+
final CommentRemoverType mode;
5355
try {
54-
mode = KeepComments.findForText(tail);
56+
mode = PreprocessorUtils.findCommentRemoverForId(tail);
5557
} catch (IllegalArgumentException ex) {
5658
throw context.makeException(
57-
"Illegal keep comments mode '" + tail + "' in " + ARG_NAME + ", expected one of " +
58-
KeepComments.makeStringForExpectedValues(), null);
59+
"Illegal keep comments mode '" + tail + "' in " + ARG_NAME + ", expected one of: true,false," +
60+
makeListOfAllRemoverIds(), null);
5961
}
6062

6163
context.setKeepComments(mode);

jcp/src/main/java/com/igormaznitsa/jcp/cmdline/RemoveCommentsHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
package com.igormaznitsa.jcp.cmdline;
2323

24-
import com.igormaznitsa.jcp.context.KeepComments;
24+
import com.igormaznitsa.jcp.context.CommentRemoverType;
2525
import com.igormaznitsa.jcp.context.PreprocessorContext;
2626

2727
/**
@@ -45,7 +45,7 @@ public boolean processCommandLineKey(final String argument,
4545
boolean result = false;
4646

4747
if (ARG_NAME.equalsIgnoreCase(argument)) {
48-
configurator.setKeepComments(KeepComments.REMOVE_ALL);
48+
configurator.setKeepComments(CommentRemoverType.REMOVE_C_STYLE);
4949
result = true;
5050
}
5151

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.igormaznitsa.jcp.context;
2+
3+
import java.util.Arrays;
4+
import java.util.stream.Collectors;
5+
6+
/**
7+
* Type of comment remover.
8+
* @since 7.1.0
9+
*/
10+
public enum CommentRemoverType {
11+
/**
12+
* To not remove comments.
13+
*/
14+
KEEP_ALL,
15+
/**
16+
* Remove all single line and multiline comments defined in C style.
17+
*/
18+
REMOVE_C_STYLE,
19+
/**
20+
* Remove only comments contain JCP directives.
21+
*/
22+
REMOVE_JCP_ONLY;
23+
24+
/**
25+
* Make comma separated list of enum item names.
26+
* @return comma separated list of all enum items, without spaces.
27+
*/
28+
public static String makeListOfAllRemoverIds() {
29+
return Arrays.stream(CommentRemoverType.values())
30+
.map(Enum::toString).collect(Collectors.joining(","));
31+
}
32+
33+
}

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

Lines changed: 0 additions & 40 deletions
This file was deleted.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public void saveBuffersToStreams(final OutputStream prefix, final OutputStream n
420420
new BufferedWriter(new OutputStreamWriter(postfix, globalOutCharacterEncoding)));
421421
}
422422

423-
public boolean saveBuffersToFile(final File outFile, final KeepComments keepComments)
423+
public boolean saveBuffersToFile(final File outFile, final CommentRemoverType keepComments)
424424
throws IOException {
425425
final File path = outFile.getParentFile();
426426

@@ -439,7 +439,7 @@ public boolean saveBuffersToFile(final File outFile, final KeepComments keepComm
439439

440440
if (this.overrideOnlyIfContentChanged) {
441441
String content = writePrinterBuffers(new StringWriter(totatBufferedChars)).toString();
442-
if (keepComments != KeepComments.KEEP_ALL) {
442+
if (keepComments != CommentRemoverType.KEEP_ALL) {
443443
content = makeCommentRemover(
444444
keepComments,
445445
new StringReader(content),
@@ -464,7 +464,7 @@ public boolean saveBuffersToFile(final File outFile, final KeepComments keepComm
464464
this.context.logDebug(
465465
"Ignore writing data for " + outFile + " because its content has not been changed");
466466
}
467-
} else if (keepComments != KeepComments.KEEP_ALL) {
467+
} else if (keepComments != CommentRemoverType.KEEP_ALL) {
468468
final String joinedBufferContent =
469469
writePrinterBuffers(new StringWriter(totatBufferedChars)).toString();
470470
writer = new OutputStreamWriter(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class PreprocessorContext {
9898
.ensureNonNull(System.getProperty("jcp.line.separator", System.getProperty("line.separator")),
9999
"\n");
100100
private boolean verbose = false;
101-
private KeepComments keepComments = KeepComments.KEEP_ALL;
101+
private CommentRemoverType keepComments = CommentRemoverType.KEEP_ALL;
102102
private boolean clearTarget = false;
103103
private boolean dryRun = false;
104104
private boolean keepLines = false;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findFirstActiveFileContainer;
2626

2727
import com.igormaznitsa.jcp.containers.FileInfoContainer;
28-
import com.igormaznitsa.jcp.context.KeepComments;
28+
import com.igormaznitsa.jcp.context.CommentRemoverType;
2929
import com.igormaznitsa.jcp.context.PreprocessingState;
3030
import com.igormaznitsa.jcp.context.PreprocessorContext;
3131
import com.igormaznitsa.jcp.expression.Value;
@@ -79,7 +79,7 @@ private PreprocessorContext prepareContext(final PreprocessorContext base) {
7979
result.setDryRun(true);
8080
result.setKeepLines(false);
8181
result.setClearTarget(false);
82-
result.setKeepComments(KeepComments.REMOVE_ALL);
82+
result.setKeepComments(CommentRemoverType.REMOVE_C_STYLE);
8383
result.setCareForLastEol(true);
8484
return result;
8585
}

0 commit comments

Comments
 (0)