supporting null (non-existing) properties and text

This commit is contained in:
Brian Long 2021-04-14 09:18:38 -04:00
parent efb1d75ca7
commit 49ef3dd8a2
3 changed files with 32 additions and 36 deletions

View File

@ -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,12 +85,11 @@ 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
} else {
this.getLog().debug("Operating on value: " + text);
Matcher matcher = pattern.matcher(text);
@ -100,7 +101,6 @@ public abstract class AbstractMatchMojo extends AbstractRegexMojo {
return true;
}
}
}
return false;
}

View File

@ -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);
}

View File

@ -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,21 +110,18 @@ 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<Pattern, String> 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());
}
}
return text;
}
@ -180,21 +181,18 @@ 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<Pattern, String> 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());
}
}
return text;
}