diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java
index 135f58fd9a..399db76add 100644
--- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java
+++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java
@@ -81,6 +81,7 @@
import org.apache.maven.project.ProjectBuildingResult;
import org.apache.maven.shared.utils.io.IOUtil;
import org.codehaus.mojo.versions.rewriting.MutableXMLStreamReader;
+import org.codehaus.mojo.versions.rewriting.PropertyVersionInternal;
import org.codehaus.mojo.versions.utils.ArtifactFactory;
import org.codehaus.mojo.versions.utils.ModelNode;
import org.codehaus.mojo.versions.utils.RegexUtils;
@@ -224,59 +225,24 @@ public static Model getRawModel(Reader reader) throws IOException {
*/
public static boolean setPropertyVersion(
MutableXMLStreamReader pom, String profileId, String property, String value) throws XMLStreamException {
- class PropertyVersionInternal {
- final Pattern propertyRegex = Pattern.compile(
- (profileId == null ? "/project/properties/" : "/project/profiles/profile/properties/")
- + RegexUtils.quote(property));
- final Pattern matchScopeRegex = profileId == null
- ? Pattern.compile("/project/properties")
- : Pattern.compile("/project/profiles/profile");
- final Pattern profileIdRegex = profileId == null ? null : Pattern.compile("/project/profiles/profile/id");
-
- boolean setPropertyVersion(String path, boolean inMatchScope) throws XMLStreamException {
- boolean replaced = false;
- while (!replaced && pom.hasNext()) {
- pom.next();
- if (pom.isStartElement()) {
- String elementPath = path + "/" + pom.getLocalName();
- if (propertyRegex.matcher(elementPath).matches()) {
- pom.mark(START);
- } else if (matchScopeRegex.matcher(elementPath).matches()) {
- // we're in a new match scope -> reset any previous partial matches
- inMatchScope = profileId == null;
- pom.clearMark(START);
- pom.clearMark(END);
- } else if (profileId != null
- && profileIdRegex.matcher(elementPath).matches()) {
- inMatchScope =
- profileId.trim().equals(pom.getElementText().trim());
- }
-
- // getElementText could've pushed the pointer to END_ELEMENT
- if (!pom.isEndElement()) {
- // inMatchScope will not change in the child element
- replaced = setPropertyVersion(elementPath, inMatchScope);
- }
- } else if (pom.isEndElement()) {
- if (propertyRegex.matcher(path).matches()) {
- pom.mark(END);
- } else if (matchScopeRegex.matcher(path).matches()) {
- if (inMatchScope && pom.hasMark(START) && pom.hasMark(END)) {
- pom.replaceBetween(START, END, value);
- replaced = true;
- }
- pom.clearMark(START);
- pom.clearMark(END);
- }
- return replaced;
- }
- }
- return replaced;
- }
- }
+ return setPropertyVersion(pom, profileId, property, value, false);
+ }
- pom.rewind();
- return new PropertyVersionInternal().setPropertyVersion("", false);
+ /**
+ * Searches the pom re-defining the specified property to the specified version.
+ *
+ * @param pom The pom to modify.
+ * @param profileId The profile in which to modify the property.
+ * @param property The property to modify.
+ * @param value The new value of the property.
+ * @param insert Whether to insert a new property or just replace an existing one.
+ * @return true if a replacement was made.
+ * @throws XMLStreamException if somethinh went wrong.
+ */
+ public static boolean setPropertyVersion(
+ MutableXMLStreamReader pom, String profileId, String property, String value, boolean insert)
+ throws XMLStreamException {
+ return new PropertyVersionInternal(pom, profileId, property, value).update(insert);
}
/**
diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/rewriting/MutableXMLStreamReader.java b/versions-common/src/main/java/org/codehaus/mojo/versions/rewriting/MutableXMLStreamReader.java
index 60d39c19e4..a0fef89164 100644
--- a/versions-common/src/main/java/org/codehaus/mojo/versions/rewriting/MutableXMLStreamReader.java
+++ b/versions-common/src/main/java/org/codehaus/mojo/versions/rewriting/MutableXMLStreamReader.java
@@ -30,6 +30,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.maven.shared.utils.io.IOUtil;
import org.codehaus.stax2.LocationInfo;
@@ -44,6 +46,7 @@
*/
public class MutableXMLStreamReader extends StreamReader2Delegate implements AutoCloseable {
private static final XMLInputFactory FACTORY = XMLInputFactory2.newInstance();
+ private static final Pattern INDENTATION_PATTERN = Pattern.compile("^(\\s+)<", Pattern.MULTILINE);
private StringBuilder source;
@@ -386,4 +389,35 @@ public String toString() {
return "MarkInfo[" + getStart() + ":" + getEnd() + "]";
}
}
+
+ /**
+ * Determine the line separator.
+ *
+ * @return line separator
+ */
+ public String getLineSeparator() {
+ String text = getSource();
+ if (text.contains("\r\n")) {
+ return "\r\n";
+ }
+ if (text.contains("\r")) {
+ return "\r";
+ }
+ return "\n";
+ }
+
+ /**
+ * Determine the indentation.
+ *
+ * @return indentation
+ */
+ public String getIndentation() {
+ Pattern indentPattern = INDENTATION_PATTERN;
+ Matcher matcher = indentPattern.matcher(getSource());
+
+ if (matcher.find()) {
+ return matcher.group(1);
+ }
+ return "";
+ }
}
diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/rewriting/PropertyVersionInternal.java b/versions-common/src/main/java/org/codehaus/mojo/versions/rewriting/PropertyVersionInternal.java
new file mode 100644
index 0000000000..656b5ec8fd
--- /dev/null
+++ b/versions-common/src/main/java/org/codehaus/mojo/versions/rewriting/PropertyVersionInternal.java
@@ -0,0 +1,380 @@
+package org.codehaus.mojo.versions.rewriting;
+
+/*
+ * Copyright MojoHaus and Contributors
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import javax.xml.stream.XMLStreamException;
+
+/**
+ * Searches the pom re-defining or inserting the specified version property.
+ *
+ * This is an internal implementation class used by {@link org.codehaus.mojo.versions.api.PomHelper}.
+ * It should not be used directly by client code.
+ *
+ * @since 2.20.1
+ */
+public class PropertyVersionInternal {
+
+ private final MutableXMLStreamReader pom;
+ private final String profileId;
+ private final String propertyName;
+ private final String value;
+ private final String indentation;
+ private final String lineSeparator;
+
+ private Template result = Template.NONE;
+ private boolean inProfileWithId;
+
+ /**
+ * Marks.
+ */
+ private enum Marks {
+ START,
+ END
+ }
+
+ /**
+ * Represents the state of the parser.
+ */
+ private enum State {
+ BEGIN_OF_STREAM,
+ PROJECT,
+ PROFILES,
+ PROFILE,
+ PROFILE_ID,
+ PROPERTIES,
+ PROPERTY,
+ PROJECT_END;
+ }
+
+ /**
+ * Result template.
+ *
+ * Decides what to render between start and end marks.
+ */
+ private enum Template {
+ /**
+ * Does not update anything.
+ */
+ NONE {
+ @Override
+ boolean apply(
+ MutableXMLStreamReader pom,
+ String propertyName,
+ String value,
+ String indentation,
+ String lineSeparator) {
+ return false;
+ }
+ },
+ /**
+ * Replaces an existing property value.
+ */
+ VALUE {
+ @Override
+ boolean apply(
+ MutableXMLStreamReader pom,
+ String propertyName,
+ String value,
+ String indentation,
+ String lineSeparator) {
+ pom.replaceBetween(Marks.START, Marks.END, value);
+ return true;
+ }
+ },
+ /**
+ * Inserts a missing property entry at the end of the {@code properties} container.
+ */
+ PROJECT_PROPERTY {
+ @Override
+ boolean apply(
+ MutableXMLStreamReader pom,
+ String propertyName,
+ String value,
+ String indentation,
+ String lineSeparator) {
+ pom.replaceBetween(
+ Marks.START,
+ Marks.END,
+ String.format(
+ "%3$s<%1$s>%2$s%1$s>%4$s%3$s", propertyName, value, indentation, lineSeparator));
+ return true;
+ }
+ },
+ /**
+ * Inserts a missing property entry at the end of the {@code profile}'s {@code properties} container.
+ */
+ PROFILE_PROPERTY {
+ @Override
+ boolean apply(
+ MutableXMLStreamReader pom,
+ String propertyName,
+ String value,
+ String indentation,
+ String lineSeparator) {
+ pom.replaceBetween(
+ Marks.START,
+ Marks.END,
+ String.format(
+ "%3$s<%1$s>%2$s%1$s>%4$s%3$s%3$s%3$s",
+ propertyName, value, indentation, lineSeparator));
+ return true;
+ }
+ },
+ /**
+ * Inserts a missing property container at the end of the {@code project} element.
+ */
+ PROJECT_PROPERTIES {
+ @Override
+ boolean apply(
+ MutableXMLStreamReader pom,
+ String propertyName,
+ String value,
+ String indentation,
+ String lineSeparator) {
+ pom.replaceBetween(
+ Marks.START,
+ Marks.END,
+ String.format(
+ "%3$s