Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Pattern.compile to static variable #1116

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.codehaus.stax2.XMLInputFactory2;

Expand All @@ -90,9 +90,39 @@
* @author Stephen Connolly
* @since 1.0-alpha-3
*/
public class PomHelper {
public final class PomHelper {
public static final String APACHE_MAVEN_PLUGINS_GROUPID = "org.apache.maven.plugins";

public static final Pattern PATTERN_PROJECT_PROPERTIES = Pattern.compile("/project/properties");

public static final Pattern PATTERN_PROJECT_PROFILE = Pattern.compile("/project/profiles/profile");

public static final Pattern PATTERN_PROJECT_PROFILE_ID = Pattern.compile("/project/profiles/profile/id");

public static final Pattern PATTERN_PROJECT_VERSION = Pattern.compile("/project/version");

public static final Pattern PATTERN_PROJECT_PARENT_VERSION = Pattern.compile("/project/parent/version");

public static final Pattern PATTERN_PROJECT_DEPENDENCY = Pattern.compile("/project" + "(/profiles/profile)?"
+ "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
+ "/dependencies/dependency");

public static final Pattern PATTERN_PROJECT_DEPENDENCY_VERSION = Pattern.compile("/project" + "(/profiles/profile)?"
+ "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
+ "/dependencies/dependency"
+ "((/groupId)|(/artifactId)|(/version))");

public static final Pattern PATTERN_PROJECT_PLUGIN = Pattern.compile(
"/project" + "(/profiles/profile)?" + "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin");

public static final Pattern PATTERN_PROJECT_PLUGIN_VERSION = Pattern.compile("/project" + "(/profiles/profile)?"
+ "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin"
+ "((/groupId)|(/artifactId)|(/version))");

private PomHelper() {
// utility class
}

/**
* Gets the raw model before any interpolation what-so-ever.
*
Expand Down Expand Up @@ -173,12 +203,12 @@ public static boolean setPropertyVersion(
boolean madeReplacement = false;
if (profileId == null) {
propertyRegex = Pattern.compile("/project/properties/" + RegexUtils.quote(property));
matchScopeRegex = Pattern.compile("/project/properties");
matchScopeRegex = PATTERN_PROJECT_PROPERTIES;
projectProfileId = null;
} else {
propertyRegex = Pattern.compile("/project/profiles/profile/properties/" + RegexUtils.quote(property));
matchScopeRegex = Pattern.compile("/project/profiles/profile");
projectProfileId = Pattern.compile("/project/profiles/profile/id");
matchScopeRegex = PATTERN_PROJECT_PROFILE;
projectProfileId = PATTERN_PROJECT_PROFILE_ID;
}

pom.rewind();
Expand Down Expand Up @@ -362,7 +392,6 @@ private void replaceValueInParent() {
public static String getProjectVersion(final ModifiedPomXMLEventReader pom) throws XMLStreamException {
Stack<String> stack = new Stack<>();
String path = "";
final Pattern matchScopeRegex = Pattern.compile("/project/version");

pom.rewind();

Expand All @@ -372,12 +401,12 @@ public static String getProjectVersion(final ModifiedPomXMLEventReader pom) thro
stack.push(path);
path = path + "/" + event.asStartElement().getName().getLocalPart();

if (matchScopeRegex.matcher(path).matches()) {
if (PATTERN_PROJECT_VERSION.matcher(path).matches()) {
pom.mark(0);
}
}
if (event.isEndElement()) {
if (matchScopeRegex.matcher(path).matches()) {
if (PATTERN_PROJECT_VERSION.matcher(path).matches()) {
pom.mark(1);
if (pom.hasMark(0) && pom.hasMark(1)) {
return pom.getBetween(0, 1).trim();
Expand All @@ -403,9 +432,7 @@ public static boolean setProjectParentVersion(final ModifiedPomXMLEventReader po
throws XMLStreamException {
Stack<String> stack = new Stack<>();
String path = "";
final Pattern matchScopeRegex;
boolean madeReplacement = false;
matchScopeRegex = Pattern.compile("/project/parent/version");

pom.rewind();

Expand All @@ -415,12 +442,12 @@ public static boolean setProjectParentVersion(final ModifiedPomXMLEventReader po
stack.push(path);
path = path + "/" + event.asStartElement().getName().getLocalPart();

if (matchScopeRegex.matcher(path).matches()) {
if (PATTERN_PROJECT_PARENT_VERSION.matcher(path).matches()) {
pom.mark(0);
}
}
if (event.isEndElement()) {
if (matchScopeRegex.matcher(path).matches()) {
if (PATTERN_PROJECT_PARENT_VERSION.matcher(path).matches()) {
pom.mark(1);
if (pom.hasMark(0) && pom.hasMark(1)) {
pom.replaceBetween(0, 1, value);
Expand Down Expand Up @@ -514,15 +541,6 @@ public static boolean setDependencyVersion(
boolean haveArtifactId = false;
boolean haveOldVersion = false;

final Pattern matchScopeRegex = Pattern.compile("/project" + "(/profiles/profile)?"
+ "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
+ "/dependencies/dependency");

final Pattern matchTargetRegex = Pattern.compile("/project" + "(/profiles/profile)?"
+ "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
+ "/dependencies/dependency"
+ "((/groupId)|(/artifactId)|(/version))");

pom.rewind();

while (pom.hasNext()) {
Expand All @@ -532,7 +550,7 @@ public static boolean setDependencyVersion(
final String elementName = event.asStartElement().getName().getLocalPart();
path = path + "/" + elementName;

if (matchScopeRegex.matcher(path).matches()) {
if (PATTERN_PROJECT_DEPENDENCY.matcher(path).matches()) {
// we're in a new match scope
// reset any previous partial matches
inMatchScope = true;
Expand All @@ -542,7 +560,8 @@ public static boolean setDependencyVersion(
haveGroupId = false;
haveArtifactId = false;
haveOldVersion = false;
} else if (inMatchScope && matchTargetRegex.matcher(path).matches()) {
} else if (inMatchScope
&& PATTERN_PROJECT_DEPENDENCY_VERSION.matcher(path).matches()) {
if ("groupId".equals(elementName)) {
haveGroupId =
groupId.equals(evaluate(pom.getElementText().trim(), implicitProperties));
Expand All @@ -557,7 +576,7 @@ public static boolean setDependencyVersion(
}
}
if (event.isEndElement()) {
if (matchTargetRegex.matcher(path).matches()
if (PATTERN_PROJECT_DEPENDENCY_VERSION.matcher(path).matches()
&& "version".equals(event.asEndElement().getName().getLocalPart())) {
pom.mark(1);
String compressedPomVersion =
Expand All @@ -570,7 +589,7 @@ public static boolean setDependencyVersion(
// fall back to string comparison
haveOldVersion = compressedOldVersion.equals(compressedPomVersion);
}
} else if (matchScopeRegex.matcher(path).matches()) {
} else if (PATTERN_PROJECT_DEPENDENCY.matcher(path).matches()) {
if (inMatchScope
&& pom.hasMark(0)
&& pom.hasMark(1)
Expand Down Expand Up @@ -720,22 +739,13 @@ public static boolean setPluginVersion(
throws XMLStreamException {
Stack<String> stack = new Stack<>();
String path = "";
final Pattern matchScopeRegex;
final Pattern matchTargetRegex;
boolean inMatchScope = false;
boolean madeReplacement = false;
boolean haveGroupId = false;
boolean needGroupId = groupId != null && !APACHE_MAVEN_PLUGINS_GROUPID.equals(groupId);
boolean haveArtifactId = false;
boolean haveOldVersion = false;

matchScopeRegex = Pattern.compile(
"/project" + "(/profiles/profile)?" + "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin");

matchTargetRegex = Pattern.compile("/project" + "(/profiles/profile)?"
+ "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin"
+ "((/groupId)|(/artifactId)|(/version))");

pom.rewind();

while (pom.hasNext()) {
Expand All @@ -745,7 +755,7 @@ public static boolean setPluginVersion(
final String elementName = event.asStartElement().getName().getLocalPart();
path = path + "/" + elementName;

if (matchScopeRegex.matcher(path).matches()) {
if (PATTERN_PROJECT_PLUGIN.matcher(path).matches()) {
// we're in a new match scope
// reset any previous partial matches
inMatchScope = true;
Expand All @@ -755,7 +765,8 @@ public static boolean setPluginVersion(
haveGroupId = false;
haveArtifactId = false;
haveOldVersion = false;
} else if (inMatchScope && matchTargetRegex.matcher(path).matches()) {
} else if (inMatchScope
&& PATTERN_PROJECT_PLUGIN_VERSION.matcher(path).matches()) {
if ("groupId".equals(elementName)) {
haveGroupId = pom.getElementText().trim().equals(groupId);
path = stack.pop();
Expand All @@ -768,7 +779,7 @@ public static boolean setPluginVersion(
}
}
if (event.isEndElement()) {
if (matchTargetRegex.matcher(path).matches()
if (PATTERN_PROJECT_PLUGIN_VERSION.matcher(path).matches()
&& "version".equals(event.asEndElement().getName().getLocalPart())) {
pom.mark(1);

Expand All @@ -779,7 +790,7 @@ public static boolean setPluginVersion(
// fall back to string comparison
haveOldVersion = oldVersion.equals(pom.getBetween(0, 1).trim());
}
} else if (matchScopeRegex.matcher(path).matches()) {
} else if (PATTERN_PROJECT_PLUGIN.matcher(path).matches()) {
if (inMatchScope
&& pom.hasMark(0)
&& pom.hasMark(1)
Expand Down Expand Up @@ -1524,7 +1535,7 @@ public static int getReactorParentCount(Map<File, Model> reactor, Model model) {
* @throws java.io.IOException when things go wrong.
*/
public static StringBuilder readXmlFile(File outFile) throws IOException {
try (Reader reader = ReaderFactory.newXmlReader(outFile)) {
try (Reader reader = new XmlStreamReader(outFile)) {
return new StringBuilder(IOUtil.toString(reader));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public class DisplayPluginUpdatesMojo extends AbstractVersionsDisplayMojo {
*/
private static final String FROM_SUPER_POM = "(from super-pom) ";

public static final Pattern PATTERN_PROJECT_PLUGIN = Pattern.compile(
"/project(/profiles/profile)?" + "((/build(/pluginManagement)?)|(/reporting))" + "/plugins/plugin");

/**
* @since 1.0-alpha-1
*/
Expand Down Expand Up @@ -201,9 +204,6 @@ private Map<String, String> getSuperPomPluginManagement() {
StringBuilder buf = new StringBuilder(IOUtil.toString(reader));
ModifiedPomXMLEventReader pom = newModifiedPomXER(buf, superPom.toString());

Pattern pathRegex = Pattern.compile("/project(/profiles/profile)?"
+ "((/build(/pluginManagement)?)|(/reporting))"
+ "/plugins/plugin");
Stack<StackState> pathStack = new Stack<>();
StackState curState = null;
while (pom.hasNext()) {
Expand All @@ -215,7 +215,9 @@ private Map<String, String> getSuperPomPluginManagement() {
if (curState != null) {
String elementName =
event.asStartElement().getName().getLocalPart();
if (pathRegex.matcher(curState.path).matches()) {
if (PATTERN_PROJECT_PLUGIN
.matcher(curState.path)
.matches()) {
if ("groupId".equals(elementName)) {
curState.groupId = pom.getElementText().trim();
continue;
Expand All @@ -236,7 +238,9 @@ private Map<String, String> getSuperPomPluginManagement() {
} else if (event.isEndElement()) {
if (curState != null
&& curState.artifactId != null
&& pathRegex.matcher(curState.path).matches()) {
&& PATTERN_PROJECT_PLUGIN
.matcher(curState.path)
.matches()) {
result.putIfAbsent(
Plugin.constructKey(
curState.groupId == null
Expand Down Expand Up @@ -755,8 +759,6 @@ private Set<String> findPluginsWithVersionsSpecified(StringBuilder pomContents,
Set<String> result = new HashSet<>();
ModifiedPomXMLEventReader pom = newModifiedPomXER(pomContents, path);

Pattern pathRegex = Pattern.compile(
"/project(/profiles/profile)?" + "((/build(/pluginManagement)?)|(/reporting))" + "/plugins/plugin");
Stack<StackState> pathStack = new Stack<>();
StackState curState = null;
while (pom.hasNext()) {
Expand All @@ -766,7 +768,8 @@ private Set<String> findPluginsWithVersionsSpecified(StringBuilder pomContents,
pathStack.clear();
} else if (event.isStartElement()) {
String elementName = event.asStartElement().getName().getLocalPart();
if (curState != null && pathRegex.matcher(curState.path).matches()) {
if (curState != null
&& PATTERN_PROJECT_PLUGIN.matcher(curState.path).matches()) {
if ("groupId".equals(elementName)) {
curState.groupId = pom.getElementText().trim();
continue;
Expand All @@ -783,7 +786,8 @@ private Set<String> findPluginsWithVersionsSpecified(StringBuilder pomContents,
pathStack.push(curState);
curState = new StackState(curState.path + "/" + elementName);
} else if (event.isEndElement()) {
if (curState != null && pathRegex.matcher(curState.path).matches()) {
if (curState != null
&& PATTERN_PROJECT_PLUGIN.matcher(curState.path).matches()) {
if (curState.artifactId != null && curState.version != null) {
if (curState.groupId == null) {
curState.groupId = PomHelper.APACHE_MAVEN_PLUGINS_GROUPID;
Expand Down