From 49ef3dd8a2d87026eec50c7fc319fd2d6db548b5 Mon Sep 17 00:00:00 2001 From: Brian Long Date: Wed, 14 Apr 2021 09:18:38 -0400 Subject: [PATCH 1/2] supporting null (non-existing) properties and text --- .../maven/regex/AbstractMatchMojo.java | 26 +++++++------- .../maven/regex/AbstractRegexMojo.java | 8 ++--- .../maven/regex/AbstractReplaceMojo.java | 34 +++++++++---------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/inteligr8/maven/regex/AbstractMatchMojo.java b/src/main/java/com/inteligr8/maven/regex/AbstractMatchMojo.java index 2749459..e365a99 100644 --- a/src/main/java/com/inteligr8/maven/regex/AbstractMatchMojo.java +++ b/src/main/java/com/inteligr8/maven/regex/AbstractMatchMojo.java @@ -47,6 +47,8 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo { boolean matches = this.matches(text); if (matches) this.getLog().debug("Matches!"); + if (props == null) + props = this.project.getProperties(); props.setProperty(newPropertyName, String.valueOf(matches)); } @@ -83,22 +85,20 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo { } protected boolean matches(String text) { + if (text == null) + text = ""; + for (Pattern pattern : this.compiledPatterns) { this.getLog().debug("Applying regex pattern: " + pattern); - - if (text == null) { - // TODO we want to capture this somehow + this.getLog().debug("Operating on value: " + text); + + Matcher matcher = pattern.matcher(text); + if (this.allowPartialMatch) { + if (matcher.find()) + return true; } else { - this.getLog().debug("Operating on value: " + text); - - Matcher matcher = pattern.matcher(text); - if (this.allowPartialMatch) { - if (matcher.find()) - return true; - } else { - if (matcher.matches()) - return true; - } + if (matcher.matches()) + return true; } } diff --git a/src/main/java/com/inteligr8/maven/regex/AbstractRegexMojo.java b/src/main/java/com/inteligr8/maven/regex/AbstractRegexMojo.java index 302d583..c70872b 100644 --- a/src/main/java/com/inteligr8/maven/regex/AbstractRegexMojo.java +++ b/src/main/java/com/inteligr8/maven/regex/AbstractRegexMojo.java @@ -82,12 +82,10 @@ public abstract class AbstractRegexMojo extends AbstractMojo { this.getLog().debug("Finding property: " + propertyName); Properties props = this.propResolver.resolveScope(propertyName); - if (props == null) { - this.getLog().info("Property not found: " + propertyName); - return; - } + if (props == null) + this.getLog().debug("Property not found: " + propertyName); - String propertyValue = props.getProperty(propertyName); + String propertyValue = props == null ? null : props.getProperty(propertyName); this.executeOnText(props, propertyValue, newPropertyName); } diff --git a/src/main/java/com/inteligr8/maven/regex/AbstractReplaceMojo.java b/src/main/java/com/inteligr8/maven/regex/AbstractReplaceMojo.java index 3f0fd2f..65ffcda 100644 --- a/src/main/java/com/inteligr8/maven/regex/AbstractReplaceMojo.java +++ b/src/main/java/com/inteligr8/maven/regex/AbstractReplaceMojo.java @@ -45,11 +45,15 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo { @Override protected void executeOnText(Properties props, String text, String newPropertyName) { + if (text == null) + text = ""; String originalText = text; text = this.replaceAll(text); if (this.getLog().isDebugEnabled() && !text.equals(originalText)) this.getLog().debug("Manipulated value: " + text); + if (props == null) + props = this.project.getProperties(); props.setProperty(newPropertyName, text); } @@ -106,20 +110,17 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo { } protected String replaceFirst(String text) { + if (text == null) + text = ""; if (this.getLog().isDebugEnabled()) this.getLog().debug("replace first: " + text.length()); for (Pair regex : this.compiledRegexes) { this.getLog().debug("Applying regex pattern: " + regex.getLeft()); - - if (text == null) { - // TODO we want to capture this somehow - } else { - this.getLog().debug("Operating on value: " + text); - - Matcher matcher = regex.getLeft().matcher(text); - text = matcher.replaceFirst(regex.getRight()); - } + this.getLog().debug("Operating on value: " + text); + + Matcher matcher = regex.getLeft().matcher(text); + text = matcher.replaceFirst(regex.getRight()); } return text; @@ -180,20 +181,17 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo { } protected String replaceAll(String text) { + if (text == null) + text = ""; if (this.getLog().isDebugEnabled()) this.getLog().debug("replace all: " + text.length()); for (Pair regex : this.compiledRegexes) { this.getLog().debug("Applying regex pattern: " + regex.getLeft()); - - if (text == null) { - // TODO we want to capture this somehow - } else { - this.getLog().debug("Operating on value: " + text); - - Matcher matcher = regex.getLeft().matcher(text); - text = matcher.replaceAll(regex.getRight()); - } + this.getLog().debug("Operating on value: " + text); + + Matcher matcher = regex.getLeft().matcher(text); + text = matcher.replaceAll(regex.getRight()); } return text; From 9f5860181583a4ae079852d72719d5c04739bc0c Mon Sep 17 00:00:00 2001 From: Brian Long Date: Wed, 14 Apr 2021 09:19:07 -0400 Subject: [PATCH 2/2] supporting "negate" for matching --- .../com/inteligr8/maven/regex/AbstractMatchMojo.java | 11 +++++++---- .../inteligr8/maven/regex/MatchFileContentMojo.java | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/inteligr8/maven/regex/AbstractMatchMojo.java b/src/main/java/com/inteligr8/maven/regex/AbstractMatchMojo.java index e365a99..883d8ae 100644 --- a/src/main/java/com/inteligr8/maven/regex/AbstractMatchMojo.java +++ b/src/main/java/com/inteligr8/maven/regex/AbstractMatchMojo.java @@ -39,6 +39,9 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo { @Parameter( property = "patterns", required = true ) protected List patterns; + + @Parameter( property = "negate", required = true, defaultValue = "false" ) + protected boolean negate = false; private List compiledPatterns; @@ -81,7 +84,7 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo { if (this.allowMultiLineMatch) return this.matches(strbuilder.toString()); - return false; + return this.negate; } protected boolean matches(String text) { @@ -95,14 +98,14 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo { Matcher matcher = pattern.matcher(text); if (this.allowPartialMatch) { if (matcher.find()) - return true; + return !this.negate; } else { if (matcher.matches()) - return true; + return !this.negate; } } - return false; + return this.negate; } @Override diff --git a/src/main/java/com/inteligr8/maven/regex/MatchFileContentMojo.java b/src/main/java/com/inteligr8/maven/regex/MatchFileContentMojo.java index 8e7c0c4..e4864ba 100644 --- a/src/main/java/com/inteligr8/maven/regex/MatchFileContentMojo.java +++ b/src/main/java/com/inteligr8/maven/regex/MatchFileContentMojo.java @@ -69,7 +69,7 @@ public class MatchFileContentMojo extends AbstractMatchMojo { throw new MojoExecutionException("Execution failed due to an I/O related issue", ie); } - return false; + return this.negate; } @Override