Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.codehaus.mojo.versions.api;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.TransformerException;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.Profile;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
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.utils.ModelNode;
import org.codehaus.mojo.versions.utils.RegexUtils;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.codehaus.stax2.XMLInputFactory2;
import static java.util.Collections.singletonMap;
import static java.util.Optional.ofNullable;
import static org.codehaus.mojo.versions.api.PomHelper.Marks.CHILD_START;
import static org.codehaus.mojo.versions.api.PomHelper.Marks.END_ELEMENT;
import static org.codehaus.mojo.versions.api.PomHelper.Marks.PARENT_START;
/**
* Helper class for modifying pom files.
*
* @author Stephen Connolly
* @since 1.0-alpha-3
*/
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.
*
* @param project The project to getModel the raw model for.
* @return The raw model.
* @throws IOException if the file is not found or if the file does not parse.
*/
public static Model getRawModel(MavenProject project) throws IOException {
return getRawModel(project.getFile());
}
/**
* Gets the raw model before any interpolation what-so-ever.
*
* @param moduleProjectFile The project file to getModel the raw model for.
* @return The raw model.
* @throws IOException if the file is not found or if the file does not parse.
*/
public static Model getRawModel(File moduleProjectFile) throws IOException {
try (Reader reader =
new BufferedReader(new InputStreamReader(Files.newInputStream(moduleProjectFile.toPath())))) {
Model result = getRawModel(reader);
result.setPomFile(moduleProjectFile);
return result;
}
}
/**
* Gets the current raw model before any interpolation what-so-ever.
*
* @param modelString a string containing the raw model
* @param modelPath the File containing the model
* @return The raw model.
* @throws IOException if the file is not found or if the file does not parse.
*/
public static Model getRawModel(String modelString, File modelPath) throws IOException {
try (Reader reader = new StringReader(modelString)) {
Model result = getRawModel(reader);
result.setPomFile(modelPath);
return result;
}
}
/**
* Gets the current raw model before any interpolation what-so-ever.
*
* @param reader The {@link Reader} to getModel the raw model for.
* @return The raw model.
* @throws IOException if the file is not found or if the file does not parse.
*/
public static Model getRawModel(Reader reader) throws IOException {
try {
return new MavenXpp3Reader().read(reader);
} catch (XmlPullParserException e) {
throw new IOException(e.getMessage(), e);
}
}
/**
* 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.
* @return true if a replacement was made.
* @throws XMLStreamException if somethinh went wrong.
*/
public static boolean setPropertyVersion(
final MutableXMLStreamReader pom, final String profileId, final String property, final String value)
throws XMLStreamException {
Stack stack = new Stack<>();
String path = "";
final Pattern propertyRegex;
final Pattern matchScopeRegex;
final Pattern projectProfileId;
boolean inMatchScope = false;
boolean madeReplacement = false;
if (profileId == null) {
propertyRegex = Pattern.compile("/project/properties/" + RegexUtils.quote(property));
matchScopeRegex = PATTERN_PROJECT_PROPERTIES;
projectProfileId = null;
} else {
propertyRegex = Pattern.compile("/project/profiles/profile/properties/" + RegexUtils.quote(property));
matchScopeRegex = PATTERN_PROJECT_PROFILE;
projectProfileId = PATTERN_PROJECT_PROFILE_ID;
}
pom.rewind();
while (pom.hasNext()) {
pom.next();
if (pom.isStartElement()) {
stack.push(path);
path = path + "/" + pom.getLocalName();
if (propertyRegex.matcher(path).matches()) {
pom.mark(0);
} else if (matchScopeRegex.matcher(path).matches()) {
// we're in a new match scope
// reset any previous partial matches
inMatchScope = profileId == null;
pom.clearMark(0);
pom.clearMark(1);
} else if (profileId != null && projectProfileId.matcher(path).matches()) {
String candidateId = pom.getElementText();
inMatchScope = profileId.trim().equals(candidateId.trim());
}
}
// for empty elements, pom can be both start- and end element
if (pom.isEndElement()) {
if (propertyRegex.matcher(path).matches()) {
pom.mark(1);
} else if (matchScopeRegex.matcher(path).matches()) {
if (inMatchScope && pom.hasMark(0) && pom.hasMark(1)) {
pom.replaceBetween(0, 1, value);
madeReplacement = true;
}
pom.clearMark(0);
pom.clearMark(1);
inMatchScope = false;
}
path = stack.pop();
}
}
return madeReplacement;
}
/**
* Searches the pom re-defining the project version to the specified version.
*
* @param pom The pom to modify.
* @param value The new value of the property.
* @return true if a replacement was made.
* @throws XMLStreamException if somethinh went wrong.
*/
public static boolean setProjectVersion(final MutableXMLStreamReader pom, final String value)
throws XMLStreamException {
return setElementValue(pom, "/project", "version", value, false);
}
/**
* Sets the value of the given element given its parent element path.
* Will only consider the first found occurrence of the parent element.
* If the element is not found in the parent element, the method will create the element.
*
* @param pom pom to modify
* @param parentPath path of the parent element
* @param elementName name of the element to set or create
* @param value the new value of the element
* @return {@code true} if the element was created or replaced
* @throws XMLStreamException if something went wrong
*/
public static boolean setElementValue(
MutableXMLStreamReader pom, String parentPath, String elementName, String value) throws XMLStreamException {
pom.rewind();
return setElementValue(pom, parentPath, elementName, value, true);
}
enum Marks {
CHILD_START,
PARENT_START,
END_ELEMENT
}
/**
* Sets the value of the given element given its parent element path.
* Will only consider the first found occurrence of the parent element.
* If the element is not found in the parent element, the method will create the element
* if {@code shouldCreate} is {@code true}.
*
* @param pom pom to modify
* @param parentPath path of the parent element
* @param elementName name of the element to set or create
* @param value the new value of the element
* @param shouldCreate should the element be created if it's not found in the first encountered parent element
* matching the parentPath
* @return {@code true} if the element was created or replaced
* @throws XMLStreamException if something went wrong
*/
public static boolean setElementValue(
MutableXMLStreamReader pom, String parentPath, String elementName, String value, boolean shouldCreate)
throws XMLStreamException {
class ElementValueInternal {
private final String parentName;
private final String superParentPath;
ElementValueInternal() {
int lastDelimeterIndex = parentPath.lastIndexOf('/');
parentName = parentPath.substring(lastDelimeterIndex + 1);
superParentPath = parentPath.substring(0, lastDelimeterIndex);
}
boolean process(String currentPath) throws XMLStreamException {
boolean replacementMade = false;
while (!replacementMade && pom.hasNext()) {
pom.next();
if (pom.isStartElement()) {
// here, we will only mark the beginning of the child or the parent element
if (currentPath.equals(parentPath) && elementName.equals(pom.getLocalName())) {
pom.mark(CHILD_START);
} else if (currentPath.equals(superParentPath)
&& pom.getLocalName().equals(parentName)) {
pom.mark(PARENT_START);
}
// process child element
replacementMade = process(currentPath + "/" + pom.getLocalName());
} else if (pom.isEndElement()) {
// here we're doing the replacement
if (currentPath.equals(parentPath + "/" + elementName)) {
// end of the child
replaceValueInChild();
replacementMade = true;
} else if (shouldCreate && currentPath.equals(parentPath)) {
// end of the parent
replaceValueInParent();
replacementMade = true;
} else {
return false;
}
}
}
return replacementMade;
}
private void replaceValueInChild() {
pom.mark(END_ELEMENT);
if (!pom.getBetween(CHILD_START, END_ELEMENT).isEmpty()) {
pom.replaceBetween(CHILD_START, END_ELEMENT, value);
} else {
pom.replace(String.format("<%1$s>%2$s%1$s>", elementName, value));
}
}
private void replaceValueInParent() {
pom.mark(END_ELEMENT);
if (pom.hasMark(PARENT_START)) {
if (!pom.getBetween(PARENT_START, END_ELEMENT).isEmpty()) {
pom.replace(String.format("<%2$s>%3$s%2$s>%1$s>", parentName, elementName, value));
} else {
pom.replace(String.format("<%1$s><%2$s>%3$s%2$s>%1$s>", parentName, elementName, value));
}
} else {
pom.replace(String.format("<%1$s><%2$s>%3$s%2$s>%1$s>", parentName, elementName, value));
}
}
}
pom.rewind();
return new ElementValueInternal().process("");
}
/**
* Retrieves the project version from the pom.
*
* @param pom The pom.
* @return the project version or null if the project version is not defined (i.e. inherited from
* parent version).
* @throws XMLStreamException if something went wrong.
*/
public static String getProjectVersion(final MutableXMLStreamReader pom) throws XMLStreamException {
Stack stack = new Stack<>();
String path = "";
pom.rewind();
while (pom.hasNext()) {
pom.next();
if (pom.isStartElement()) {
stack.push(path);
path = path + "/" + pom.getLocalName();
if (PATTERN_PROJECT_VERSION.matcher(path).matches()) {
pom.mark(0);
}
}
// for empty elements, pom can be both start- and end element
if (pom.isEndElement()) {
if (PATTERN_PROJECT_VERSION.matcher(path).matches()) {
pom.mark(1);
if (pom.hasMark(0) && pom.hasMark(1)) {
return pom.getBetween(0, 1).trim();
}
pom.clearMark(0);
pom.clearMark(1);
}
path = stack.pop();
}
}
return null;
}
/**
* Searches the pom re-defining the project version to the specified version.
*
* @param pom The pom to modify.
* @param value The new value of the property.
* @return true if a replacement was made.
* @throws XMLStreamException if somethinh went wrong.
*/
public static boolean setProjectParentVersion(final MutableXMLStreamReader pom, final String value)
throws XMLStreamException {
Stack stack = new Stack<>();
String path = "";
boolean madeReplacement = false;
pom.rewind();
while (pom.hasNext()) {
pom.next();
if (pom.isStartElement()) {
stack.push(path);
path = path + "/" + pom.getLocalName();
if (PATTERN_PROJECT_PARENT_VERSION.matcher(path).matches()) {
pom.mark(0);
}
}
// for empty elements, pom can be both start- and end element
if (pom.isEndElement()) {
if (PATTERN_PROJECT_PARENT_VERSION.matcher(path).matches()) {
pom.mark(1);
if (pom.hasMark(0) && pom.hasMark(1)) {
pom.replaceBetween(0, 1, value);
madeReplacement = true;
}
pom.clearMark(0);
pom.clearMark(1);
}
path = stack.pop();
}
}
return madeReplacement;
}
/**
* Searches the pom re-defining the specified dependency to the specified version.
*
* @param pom The pom to modify.
* @param groupId The groupId of the dependency.
* @param artifactId The artifactId of the dependency.
* @param oldVersion The old version of the dependency.
* @param newVersion The new version of the dependency.
* @param model The model to getModel the project properties from.
* @param logger The logger to use.
* @return true if a replacement was made.
* @throws XMLStreamException if something went wrong.
*/
@SuppressWarnings("checkstyle:MethodLength")
public static boolean setDependencyVersion(
final MutableXMLStreamReader pom,
final String groupId,
final String artifactId,
final String oldVersion,
final String newVersion,
final Model model,
final Log logger)
throws XMLStreamException {
Stack stack = new Stack<>();
String path = "";
Set implicitPaths = new HashSet<>(Arrays.asList(
"/project/parent/groupId", "/project/parent/artifactId",
"/project/parent/version", "/project/groupId",
"/project/artifactId", "/project/version"));
Map implicitProperties = new HashMap<>();
for (Map.Entry