Skip to content

Commit 99a5f0d

Browse files
committed
provided allowBlocks parameter in interfaces and cli
1 parent f78f43a commit 99a5f0d

File tree

7 files changed

+191
-2
lines changed

7 files changed

+191
-2
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
package com.igormaznitsa.jcp;
2323

2424
import static com.igormaznitsa.jcp.InfoHelper.makeTextForHelpInfo;
25+
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findAndInstantiateAllServices;
2526
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.readWholeTextFileIntoArray;
2627
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.throwPreprocessorException;
2728

2829
import com.igormaznitsa.jcp.cmdline.ActionPreprocessorExtensionHandler;
30+
import com.igormaznitsa.jcp.cmdline.AllowMergeBlockLineHandler;
2931
import com.igormaznitsa.jcp.cmdline.AllowWhitespaceDirectiveHandler;
3032
import com.igormaznitsa.jcp.cmdline.CareForLastEolHandler;
3133
import com.igormaznitsa.jcp.cmdline.ClearTargetHandler;
@@ -49,6 +51,7 @@
4951
import com.igormaznitsa.jcp.cmdline.UnknownAsFalseHandler;
5052
import com.igormaznitsa.jcp.cmdline.VerboseHandler;
5153
import com.igormaznitsa.jcp.containers.FileInfoContainer;
54+
import com.igormaznitsa.jcp.context.CommentTextProcessor;
5255
import com.igormaznitsa.jcp.context.PreprocessingState;
5356
import com.igormaznitsa.jcp.context.PreprocessorContext;
5457
import com.igormaznitsa.jcp.directives.ExcludeIfDirectiveHandler;
@@ -70,6 +73,7 @@
7073
import java.util.Locale;
7174
import java.util.Objects;
7275
import java.util.Set;
76+
import java.util.stream.Collectors;
7377
import lombok.Data;
7478
import org.apache.commons.io.FileUtils;
7579
import org.apache.commons.io.FilenameUtils;
@@ -106,6 +110,7 @@ public final class JcpPreprocessor {
106110
new ExcludeFoldersHandler(),
107111
new KeepAttributesHandler(),
108112
new ActionPreprocessorExtensionHandler(),
113+
new AllowMergeBlockLineHandler(),
109114
new UnknownAsFalseHandler()
110115
};
111116
private final PreprocessorContext context;
@@ -188,6 +193,15 @@ private static PreprocessorContext processCommandLine(final File baseDir,
188193
}
189194
}
190195

196+
final List<CommentTextProcessor> commentTextProcessors = findAndInstantiateAllServices(
197+
CommentTextProcessor.class);
198+
if (!commentTextProcessors.isEmpty()) {
199+
System.out.printf("Detected comment text processors: %s%n",
200+
commentTextProcessors.stream().map(x -> x.getClass().getCanonicalName())
201+
.collect(Collectors.joining(",")));
202+
commentTextProcessors.forEach(result::addCommentTextProcessor);
203+
}
204+
191205
return result;
192206
}
193207

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121

2222
package com.igormaznitsa.jcp.ant;
2323

24+
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findAndInstantiateAllServices;
25+
2426
import com.igormaznitsa.jcp.JcpPreprocessor;
2527
import com.igormaznitsa.jcp.context.CommentRemoverType;
28+
import com.igormaznitsa.jcp.context.CommentTextProcessor;
2629
import com.igormaznitsa.jcp.context.PreprocessorContext;
2730
import com.igormaznitsa.jcp.context.SpecialVariableProcessor;
2831
import com.igormaznitsa.jcp.exceptions.PreprocessorException;
@@ -67,6 +70,7 @@ public class PreprocessTask extends Task implements PreprocessorLogger, SpecialV
6770
private Extensions extensions = null;
6871
private boolean unknownVarAsFalse = false;
6972
private boolean dryRun = false;
73+
private boolean allowBlocks = false;
7074
private boolean verbose = false;
7175
private boolean clearTarget = false;
7276
private boolean careForLastEol = false;
@@ -148,6 +152,7 @@ PreprocessorContext makePreprocessorContext() {
148152
context.setDontOverwriteSameContent(this.isDontOverwriteSameContent());
149153
context.setClearTarget(this.isClearTarget());
150154
context.setDryRun(this.isDryRun());
155+
context.setAllowsBlocks(this.isAllowBlocks());
151156
context.setKeepComments(PreprocessorUtils.findCommentRemoverForId(this.getKeepComments()));
152157
context.setVerbose(this.isVerbose());
153158
context.setKeepLines(this.isKeepLines());
@@ -177,6 +182,17 @@ PreprocessorContext makePreprocessorContext() {
177182
this.getActionPreprocessorExtension().trim()));
178183
}
179184

185+
final List<CommentTextProcessor> commentTextProcessors = findAndInstantiateAllServices(
186+
CommentTextProcessor.class);
187+
if (!commentTextProcessors.isEmpty()) {
188+
info(String.format("Detected %d external comment text processing services",
189+
commentTextProcessors.size()));
190+
info(String.format("Detected comment text processors: %s",
191+
commentTextProcessors.stream().map(x -> x.getClass().getCanonicalName())
192+
.collect(Collectors.joining(","))));
193+
commentTextProcessors.forEach(context::addCommentTextProcessor);
194+
}
195+
180196
this.registerConfigFiles(context);
181197
this.fillGlobalVars(context);
182198

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2002-2019 Igor Maznitsa (http://www.igormaznitsa.com)
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.igormaznitsa.jcp.cmdline;
23+
24+
import com.igormaznitsa.jcp.context.PreprocessorContext;
25+
26+
/**
27+
* Enable merging of text lines found in //$""" and //$$""" into single text block
28+
* for processing by external processors.
29+
*
30+
* @author Igor Maznitsa (igor.maznitsa@igormaznitsa.com)
31+
* @since 7.2.0
32+
*/
33+
public class AllowMergeBlockLineHandler implements CommandLineHandler {
34+
35+
private static final String ARG_NAME = "/B";
36+
37+
@Override
38+
public String getDescription() {
39+
return "interpret lines marked by //$\"\"\" and //$$\"\"\" as single text block";
40+
}
41+
42+
@Override
43+
public boolean processCommandLineKey(final String key, final PreprocessorContext context) {
44+
boolean result = false;
45+
46+
if (ARG_NAME.equalsIgnoreCase(key)) {
47+
context.setAllowsBlocks(true);
48+
result = true;
49+
}
50+
51+
return result;
52+
}
53+
54+
@Override
55+
public String getKeyName() {
56+
return ARG_NAME;
57+
}
58+
59+
}

jcp/src/main/java/com/igormaznitsa/jcp/gradle/JcpTask.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.igormaznitsa.jcp.gradle;
22

3+
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findAndInstantiateAllServices;
34
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findAndInstantiatePreprocessorExtensionForClassName;
45
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findCommentRemoverForId;
56
import static java.util.Collections.emptyMap;
67

78
import com.igormaznitsa.jcp.JcpPreprocessor;
9+
import com.igormaznitsa.jcp.context.CommentTextProcessor;
810
import com.igormaznitsa.jcp.context.PreprocessorContext;
911
import com.igormaznitsa.jcp.expression.Value;
1012
import com.igormaznitsa.jcp.logger.PreprocessorLogger;
@@ -98,6 +100,12 @@ public class JcpTask extends DefaultTask {
98100
* Dry run, making pre-processing but without output
99101
*/
100102
private final Property<Boolean> dryRun;
103+
/**
104+
* Allow merge preprocessed lines marked as block text for external processing.
105+
*
106+
* @since 7.2.0
107+
*/
108+
private final Property<Boolean> allowBlocks;
101109
/**
102110
* Verbose mode.
103111
*/
@@ -172,6 +180,7 @@ public JcpTask(final ObjectFactory factory) {
172180
this.clearTarget = factory.property(Boolean.class).convention(false);
173181
this.dontOverwriteSameContent = factory.property(Boolean.class).convention(false);
174182
this.dryRun = factory.property(Boolean.class).convention(false);
183+
this.allowBlocks = factory.property(Boolean.class).convention(false);
175184
this.ignoreMissingSources = factory.property(Boolean.class).convention(false);
176185
this.keepAttributes = factory.property(Boolean.class).convention(false);
177186
this.keepComments = factory.property(Object.class).convention(false);
@@ -273,6 +282,11 @@ public Property<Boolean> getDryRun() {
273282
return dryRun;
274283
}
275284

285+
@Input
286+
public Property<Boolean> getAllowBlocks() {
287+
return allowBlocks;
288+
}
289+
276290
@Input
277291
public Property<Boolean> getVerbose() {
278292
return verbose;
@@ -348,7 +362,7 @@ public void preprocessTask() throws IOException {
348362
} else {
349363
baseDirFile = this.getProject().getProjectDir();
350364
}
351-
logger.info("Base folder: " + baseDirFile);
365+
logger.info("Base folder: {}", baseDirFile);
352366
final PreprocessorContext preprocessorContext = new PreprocessorContext(baseDirFile);
353367

354368
preprocessorContext.setPreprocessorLogger(new PreprocessorLogger() {
@@ -419,6 +433,7 @@ public void warning(final String message) {
419433
preprocessorContext.setKeepComments(
420434
findCommentRemoverForId(String.valueOf(this.keepComments.get())));
421435
preprocessorContext.setDryRun(this.dryRun.get());
436+
preprocessorContext.setAllowsBlocks(this.allowBlocks.get());
422437
preprocessorContext.setKeepAttributes(this.keepAttributes.get());
423438
preprocessorContext.setKeepLines(this.keepLines.get());
424439
preprocessorContext.setAllowWhitespaces(this.allowWhitespaces.get());
@@ -438,6 +453,17 @@ public void warning(final String message) {
438453
findAndInstantiatePreprocessorExtensionForClassName(className));
439454
}
440455

456+
final List<CommentTextProcessor> commentTextProcessors = findAndInstantiateAllServices(
457+
CommentTextProcessor.class);
458+
if (!commentTextProcessors.isEmpty()) {
459+
logger.info("Detected {} external comment text processing services",
460+
commentTextProcessors.size());
461+
logger.info("Detected comment text processors: {}",
462+
commentTextProcessors.stream().map(x -> x.getClass().getCanonicalName())
463+
.collect(Collectors.joining(",")));
464+
commentTextProcessors.forEach(preprocessorContext::addCommentTextProcessor);
465+
}
466+
441467
this.vars.getOrElse(emptyMap()).entrySet().stream()
442468
.filter(e -> {
443469
if (e.getValue() == null) {

jcp/src/main/java/com/igormaznitsa/jcp/maven/PreprocessMojo.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
package com.igormaznitsa.jcp.maven;
2323

2424
import static com.igormaznitsa.jcp.utils.GetUtils.ensureNonNull;
25+
import static com.igormaznitsa.jcp.utils.PreprocessorUtils.findAndInstantiateAllServices;
2526

2627
import com.igormaznitsa.jcp.JcpPreprocessor;
2728
import com.igormaznitsa.jcp.context.CommentRemoverType;
29+
import com.igormaznitsa.jcp.context.CommentTextProcessor;
2830
import com.igormaznitsa.jcp.context.PreprocessorContext;
2931
import com.igormaznitsa.jcp.exceptions.PreprocessorException;
3032
import com.igormaznitsa.jcp.expression.Value;
@@ -115,8 +117,8 @@ public class PreprocessMojo extends AbstractMojo implements PreprocessorLogger {
115117
* Name of a class to be used as action preprocessor extension. The class must have default constructor.
116118
* Empty string will be recognized as missing class name.
117119
*
118-
* @since 7.1.2
119120
* @see com.igormaznitsa.jcp.extension.PreprocessorExtension
121+
* @since 7.1.2
120122
*/
121123
@Parameter(alias = "actionPreprocessorExtension", property = "jcp.action.preprocessor.extension", defaultValue = "")
122124
private String actionPreprocessorExtension = "";
@@ -199,6 +201,14 @@ public class PreprocessMojo extends AbstractMojo implements PreprocessorLogger {
199201
@Parameter(alias = "dryRun", defaultValue = "false")
200202
private boolean dryRun = false;
201203

204+
/**
205+
* Allows merge preprocessed text lines as single text block for external processing.
206+
*
207+
* @since 7.2.0.
208+
*/
209+
@Parameter(alias = "allowBlocks", defaultValue = "false")
210+
private boolean allowBlocks = false;
211+
202212
/**
203213
* Verbose mode.
204214
*/
@@ -418,6 +428,7 @@ PreprocessorContext makePreprocessorContext() throws AbstractMojoExecutionExcept
418428
context.setVerbose(getLog().isDebugEnabled() || this.isVerbose());
419429
context.setKeepLines(this.isKeepLines());
420430
context.setDryRun(this.isDryRun());
431+
context.setAllowsBlocks(this.isAllowBlocks());
421432
context.setAllowWhitespaces(this.isAllowWhitespaces());
422433
context.setPreserveIndents(this.isPreserveIndents());
423434
context.setExcludeFolders(this.getExcludeFolders());
@@ -466,6 +477,22 @@ public void execute() throws MojoExecutionException, MojoFailureException {
466477
final PreprocessorContext context;
467478
try {
468479
context = makePreprocessorContext();
480+
final List<CommentTextProcessor> commentTextProcessors = findAndInstantiateAllServices(
481+
CommentTextProcessor.class);
482+
if (!commentTextProcessors.isEmpty()) {
483+
getLog().info("Detected " + commentTextProcessors.size() +
484+
" external comment text processing services");
485+
if (this.isVerbose()) {
486+
getLog().info("Detected comment text processors: " +
487+
commentTextProcessors.stream().map(x -> x.getClass().getCanonicalName())
488+
.collect(Collectors.joining(",")));
489+
} else {
490+
getLog().debug("Detected comment text processors: " +
491+
commentTextProcessors.stream().map(x -> x.getClass().getCanonicalName())
492+
.collect(Collectors.joining(",")));
493+
}
494+
commentTextProcessors.forEach(context::addCommentTextProcessor);
495+
}
469496
} catch (Exception ex) {
470497
final PreprocessorException newException =
471498
PreprocessorException.extractPreprocessorException(ex);

jcp/src/main/java/com/igormaznitsa/jcp/utils/PreprocessorUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@
4747
import java.util.Locale;
4848
import java.util.Objects;
4949
import java.util.Optional;
50+
import java.util.ServiceLoader;
5051
import java.util.concurrent.atomic.AtomicBoolean;
5152
import java.util.regex.Matcher;
5253
import java.util.regex.Pattern;
54+
import java.util.stream.Collectors;
5355
import org.apache.commons.io.FileUtils;
5456
import org.apache.commons.io.FilenameUtils;
5557

@@ -244,6 +246,10 @@ public static String replacePartByChar(final String text, final char chr, final
244246
return result.toString();
245247
}
246248

249+
public static <T> List<T> findAndInstantiateAllServices(final Class<T> serviceClass) {
250+
final ServiceLoader<T> serviceLoader = ServiceLoader.load(serviceClass);
251+
return serviceLoader.stream().map(ServiceLoader.Provider::get).collect(Collectors.toList());
252+
}
247253

248254
public static String generateStringForChar(final char chr, final int length) {
249255
final StringBuilder buffer = new StringBuilder(Math.max(length, 1));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.igormaznitsa.jcp.cmdline;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.mockito.Mockito.verify;
7+
8+
import com.igormaznitsa.jcp.context.PreprocessorContext;
9+
10+
public class AllowMergeBlockLineHandlerTest extends AbstractCommandLineHandlerTest {
11+
12+
private static final AllowMergeBlockLineHandler HANDLER = new AllowMergeBlockLineHandler();
13+
14+
@Override
15+
public void testThatTheHandlerInTheHandlerList() {
16+
assertHandlerInTheHandlerList(HANDLER);
17+
}
18+
19+
@Override
20+
public void testExecution() throws Exception {
21+
final PreprocessorContext mock = prepareMockContext();
22+
23+
assertFalse(HANDLER.processCommandLineKey("", mock));
24+
assertFalse(HANDLER.processCommandLineKey("/b:", mock));
25+
assertFalse(HANDLER.processCommandLineKey("/BB", mock));
26+
27+
assertTrue(HANDLER.processCommandLineKey("/B", mock));
28+
verify(mock).setAllowsBlocks(true);
29+
}
30+
31+
@Override
32+
public void testName() {
33+
assertEquals("/B", HANDLER.getKeyName());
34+
}
35+
36+
@Override
37+
public void testDescription() {
38+
assertDescription(HANDLER);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)