Skip to content

Commit

Permalink
Resolves #931: Change the way to read the raw POM file into String to…
Browse files Browse the repository at this point in the history
… circumvent problems with encoding

Uses Plexus XmlStreamReader to guess file encoding.
  • Loading branch information
andrzejj0 committed May 11, 2023
1 parent 765e23f commit b881d37
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.MavenProjectUtils;
import org.codehaus.mojo.versions.utils.ModelNode;
import org.codehaus.mojo.versions.utils.RegexUtils;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
Expand Down Expand Up @@ -1364,9 +1365,7 @@ private static List<ModelNode> getRawModelTree(ModelNode rootNode, Log logger, X
.map(pomFile -> {
try {
ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader(
new StringBuilder(new String(Files.readAllBytes(pomFile))),
inputFactory,
pomFile.toString());
MavenProjectUtils.readFile(pomFile), inputFactory, pomFile.toString());
return new ModelNode(rootNode, getRawModel(pom), pom);
} catch (IOException e) {
throw new UncheckedIOException("Could not open " + pomFile, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* under the License.
*/

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Expand All @@ -29,6 +32,7 @@
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.plexus.util.xml.XmlStreamReader;

import static java.util.Collections.emptyList;
import static java.util.Optional.ofNullable;
Expand Down Expand Up @@ -150,4 +154,16 @@ public static Dependency interpolateVersion(final Dependency dependency, final M
}
return dependency;
}

/**
* Reads the given file to a StringBuilder using {@link XmlStreamReader} to determine the file encoding.
* @param path file path
* @return StringBuilder containing the given file
* @throws IOException thrown in case of an I/O error
*/
public static StringBuilder readFile(Path path) throws IOException {
try (XmlStreamReader reader = new XmlStreamReader(path.toFile())) {
return new StringBuilder(new String(Files.readAllBytes(path), reader.getEncoding()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:use-dep-version
invoker.mavenOpts = -Dfile.encoding=latin1 -Dincludes=localhost -DdepVersion=1.0.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test-group</groupId>
<artifactId>test-artifact</artifactId>
<version>DEVELOP-SNAPSHOT</version>

<description>Wörter mit Umlauten</description>
<!-- Vorläufige Container für Tests -->

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import groovy.xml.XmlSlurper

def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
assert project.dependencies.dependency.version == '1.0.1'
assert project.description == 'Wörter mit Umlauten'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:use-dep-version
invoker.mavenOpts = -Dfile.encoding=latin1 -Dincludes=localhost -DdepVersion=1.0.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test-group</groupId>
<artifactId>test-artifact</artifactId>
<version>DEVELOP-SNAPSHOT</version>

<description>Wörter mit Umlauten</description>
<!-- Vorläufige Container für Tests -->

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import groovy.xml.XmlSlurper

def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
assert project.dependencies.dependency.version == '1.0.1'
assert project.description == 'Wörter mit Umlauten'
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -54,6 +53,7 @@
import org.codehaus.mojo.versions.recording.DefaultPropertyChangeRecord;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.mojo.versions.utils.MavenProjectUtils;
import org.codehaus.mojo.versions.utils.ModelNode;
import org.codehaus.plexus.util.FileUtils;

Expand Down Expand Up @@ -136,10 +136,10 @@ protected void update(ModifiedPomXMLEventReader pom)
public void execute() throws MojoExecutionException, MojoFailureException {
validateInput();
List<ModelNode> rawModels;

try {
ModifiedPomXMLEventReader pomReader = newModifiedPomXER(
new StringBuilder(
new String(Files.readAllBytes(getProject().getFile().toPath()))),
MavenProjectUtils.readFile(getProject().getFile().toPath()),
getProject().getFile().toPath().toString());
ModelNode rootNode = new ModelNode(PomHelper.getRawModel(pomReader), pomReader);
rawModels = PomHelper.getRawModelTree(rootNode, getLog());
Expand Down

0 comments on commit b881d37

Please sign in to comment.