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

[MNG-7830] Switch from plexus-xml to stax / woodstox #1185

Merged
merged 1 commit into from
Jun 29, 2023
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
@@ -0,0 1,3 @@
Copyright (c) 2008 FasterXML LLC [email protected]

This source code is licensed under standard BSD license, which is compatible with all Free and Open Software (OSS) licenses.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 23,7 @@
import java.nio.file.Files;

import org.apache.maven.api.settings.InputSource;
import org.apache.maven.settings.v4.SettingsXpp3ReaderEx;
import org.apache.maven.settings.v4.SettingsStaxReader;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -43,7 43,7 @@ void testValidGlobalSettings() throws Exception {
assertTrue(globalSettingsFile.isFile(), globalSettingsFile.getAbsolutePath());

try (InputStream is = Files.newInputStream(globalSettingsFile.toPath())) {
new SettingsXpp3ReaderEx().read(is, true, new InputSource(globalSettingsFile.getAbsolutePath()));
new SettingsStaxReader().read(is, true, new InputSource(globalSettingsFile.getAbsolutePath()));
}
}
}
5 changes: 5 additions & 0 deletions maven-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 147,11 @@ under the License.
<artifactId>maven-slf4j-wrapper</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-xml-impl</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 18,20 @@
*/
package org.apache.maven.artifact.repository.metadata;

import javax.xml.stream.XMLStreamException;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.WriterFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.apache.maven.artifact.repository.metadata.io.MetadataStaxReader;
import org.apache.maven.artifact.repository.metadata.io.MetadataStaxWriter;

/**
* Shared methods of the repository metadata handling.
Expand Down Expand Up @@ -59,15 59,13 @@ public void storeInLocalRepository(ArtifactRepository localRepository, ArtifactR
throws RepositoryMetadataStoreException {
try {
updateRepositoryMetadata(localRepository, remoteRepository);
} catch (IOException | XmlPullParserException e) {
} catch (IOException | XMLStreamException e) {
throw new RepositoryMetadataStoreException("Error updating group repository metadata", e);
}
}

protected void updateRepositoryMetadata(ArtifactRepository localRepository, ArtifactRepository remoteRepository)
throws IOException, XmlPullParserException {
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();

throws IOException, XMLStreamException {
Metadata metadata = null;

File metadataFile = new File(
Expand All @@ -85,8 83,8 @@ protected void updateRepositoryMetadata(ArtifactRepository localRepository, Arti
// to delete on exit
}
} else if (metadataFile.exists()) {
try (Reader reader = ReaderFactory.newXmlReader(metadataFile)) {
metadata = mappingReader.read(reader, false);
try (InputStream input = Files.newInputStream(metadataFile.toPath())) {
metadata = new Metadata(new MetadataStaxReader().read(input, false));
}
}

Expand All @@ -110,10 108,9 @@ protected void updateRepositoryMetadata(ArtifactRepository localRepository, Arti

if (changed || !metadataFile.exists()) {
metadataFile.getParentFile().mkdirs();
try (Writer writer = WriterFactory.newXmlWriter(metadataFile)) {
MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();

mappingWriter.write(writer, metadata);
try (OutputStream output = Files.newOutputStream(metadataFile.toPath())) {
MetadataStaxWriter mappingWriter = new MetadataStaxWriter();
mappingWriter.write(output, metadata.getDelegate());
}
} else {
metadataFile.setLastModified(System.currentTimeMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 21,7 @@
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.xml.stream.XMLStreamException;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -38,14 39,13 @@
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
import org.apache.maven.artifact.repository.metadata.io.MetadataStaxReader;
import org.apache.maven.artifact.repository.metadata.io.MetadataStaxWriter;
import org.apache.maven.repository.legacy.UpdateCheckManager;
import org.apache.maven.repository.legacy.WagonManager;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.apache.maven.wagon.TransferFailedException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* @author Jason van Zyl
Expand Down Expand Up @@ -270,13 270,11 @@ private boolean loadMetadata(
*/
protected Metadata readMetadata(File mappingFile) throws RepositoryMetadataReadException {

MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
try (InputStream in = Files.newInputStream(mappingFile.toPath())) {
Metadata result = mappingReader.read(in, false);
return result;
return new Metadata(new MetadataStaxReader().read(in, false));
} catch (FileNotFoundException e) {
throw new RepositoryMetadataReadException("Cannot read metadata from '" mappingFile "'", e);
} catch (IOException | XmlPullParserException e) {
} catch (IOException | XMLStreamException e) {
throw new RepositoryMetadataReadException(
"Cannot read metadata from '" mappingFile "': " e.getMessage(), e);
}
Expand Down Expand Up @@ -311,8 309,8 @@ private void fixTimestamp(File metadataFile, Metadata metadata, Metadata referen
getLogger().debug("Repairing metadata in " metadataFile);

try (OutputStream out = Files.newOutputStream(metadataFile.toPath())) {
new MetadataXpp3Writer().write(out, metadata);
} catch (IOException e) {
new MetadataStaxWriter().write(out, metadata.getDelegate());
} catch (IOException | XMLStreamException e) {
String msg = "Could not write fixed metadata to " metadataFile ": " e.getMessage();
if (getLogger().isDebugEnabled()) {
getLogger().warn(msg, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 19,7 @@
package org.apache.maven.project.interpolation;

import javax.inject.Inject;
import javax.xml.stream.XMLStreamException;

import java.io.File;
import java.io.IOException;
Expand All @@ -32,8 33,8 @@
import java.util.Properties;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.model.v4.MavenStaxReader;
import org.apache.maven.model.v4.MavenStaxWriter;
import org.apache.maven.project.DefaultProjectBuilderConfiguration;
import org.apache.maven.project.ProjectBuilderConfiguration;
import org.apache.maven.project.path.PathTranslator;
Expand All @@ -52,7 53,6 @@
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* Use a regular expression search to find and resolve expressions within the POM.
Expand Down Expand Up @@ -130,10 130,10 @@ public Model interpolate(Model model, File projectDir, ProjectBuilderConfigurati
throws ModelInterpolationException {
StringWriter sWriter = new StringWriter(1024);

MavenXpp3Writer writer = new MavenXpp3Writer();
MavenStaxWriter writer = new MavenStaxWriter();
try {
writer.write(sWriter, model);
} catch (IOException e) {
writer.write(sWriter, model.getDelegate());
} catch (IOException | XMLStreamException e) {
throw new ModelInterpolationException("Cannot serialize project model for interpolation.", e);
}

Expand All @@ -142,10 142,10 @@ public Model interpolate(Model model, File projectDir, ProjectBuilderConfigurati

StringReader sReader = new StringReader(serializedModel);

MavenXpp3Reader modelReader = new MavenXpp3Reader();
MavenStaxReader modelReader = new MavenStaxReader();
try {
model = modelReader.read(sReader);
} catch (IOException | XmlPullParserException e) {
model = new Model(modelReader.read(sReader));
} catch (XMLStreamException e) {
throw new ModelInterpolationException(
"Cannot read project model from interpolating filter of serialized version.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 22,11 @@
import javax.inject.Singleton;

import java.io.File;
import java.io.Reader;
import java.io.InputStream;
import java.nio.file.Files;

import org.apache.maven.toolchain.model.PersistedToolchains;
import org.apache.maven.toolchain.v4.MavenToolchainsXpp3Reader;
import org.codehaus.plexus.util.ReaderFactory;
import org.apache.maven.toolchain.v4.MavenToolchainsStaxReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -44,8 44,8 @@ public PersistedToolchains build(File userToolchainsFile) throws MisconfiguredTo
PersistedToolchains toolchains = null;

if (userToolchainsFile != null && userToolchainsFile.isFile()) {
try (Reader in = ReaderFactory.newXmlReader(userToolchainsFile)) {
toolchains = new PersistedToolchains(new MavenToolchainsXpp3Reader().read(in));
try (InputStream in = Files.newInputStream(userToolchainsFile.toPath())) {
toolchains = new PersistedToolchains(new MavenToolchainsStaxReader().read(in));
} catch (Exception e) {
throw new MisconfiguredToolchainException(
"Cannot read toolchains file at " userToolchainsFile.getAbsolutePath(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 20,7 @@

import javax.inject.Named;
import javax.inject.Singleton;
import javax.xml.stream.XMLStreamException;

import java.io.File;
import java.io.IOException;
Expand All @@ -29,9 30,7 @@
import java.util.Objects;

import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* Handles deserialization of metadata from some kind of textual format like XML.
Expand All @@ -52,19 51,27 @@ public Metadata read(Reader input, Map<String, ?> options) throws IOException {
Objects.requireNonNull(input, "input cannot be null");

try (Reader in = input) {
return new MetadataXpp3Reader().read(in, isStrict(options));
} catch (XmlPullParserException e) {
throw new MetadataParseException(e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e);
return new Metadata(new MetadataStaxReader().read(in, isStrict(options)));
} catch (XMLStreamException e) {
throw new MetadataParseException(
e.getMessage(),
e.getLocation().getLineNumber(),
e.getLocation().getColumnNumber(),
e);
}
}

public Metadata read(InputStream input, Map<String, ?> options) throws IOException {
Objects.requireNonNull(input, "input cannot be null");

try (InputStream in = input) {
return new MetadataXpp3Reader().read(in, isStrict(options));
} catch (XmlPullParserException e) {
throw new MetadataParseException(e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e);
return new Metadata(new MetadataStaxReader().read(in, isStrict(options)));
} catch (XMLStreamException e) {
throw new MetadataParseException(
e.getMessage(),
e.getLocation().getLineNumber(),
e.getLocation().getColumnNumber(),
e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 236,7 @@ public DefaultRepositorySystemSession newRepositorySession(MavenExecutionRequest
authSelector.add(server.getId(), authBuilder.build());

if (server.getConfiguration() != null) {
XmlNode dom = ((org.codehaus.plexus.util.xml.Xpp3Dom) server.getConfiguration()).getDom();
XmlNode dom = server.getDelegate().getConfiguration();
List<XmlNode> children = dom.getChildren().stream()
.filter(c -> !"wagonProvider".equals(c.getName()))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 37,8 @@
import org.apache.maven.api.services.xml.XmlReaderRequest;
import org.apache.maven.api.services.xml.XmlWriterException;
import org.apache.maven.api.services.xml.XmlWriterRequest;
import org.apache.maven.model.v4.MavenXpp3ReaderEx;
import org.apache.maven.model.v4.MavenXpp3WriterEx;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.apache.maven.model.v4.MavenStaxReader;
import org.apache.maven.model.v4.MavenStaxWriter;

import static org.apache.maven.internal.impl.Utils.nonNull;

Expand All @@ -61,16 60,21 @@ public Model read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
if (request.getModelId() != null || request.getLocation() != null) {
source = new InputSource(request.getModelId(), request.getLocation());
}
MavenXpp3ReaderEx xml = new MavenXpp3ReaderEx();
MavenStaxReader xml = new MavenStaxReader();
xml.setAddDefaultEntities(request.isAddDefaultEntities());
if (path != null) {
reader = new XmlStreamReader(path.toFile());
} else if (url != null) {
reader = new XmlStreamReader(url);
} else if (inputStream != null) {
reader = new XmlStreamReader(inputStream);
if (inputStream != null) {
return xml.read(inputStream, request.isStrict(), source);
} else if (reader != null) {
return xml.read(reader, request.isStrict(), source);
} else if (path != null) {
try (InputStream is = Files.newInputStream(path)) {
return xml.read(is, request.isStrict(), source);
}
} else {
try (InputStream is = url.openStream()) {
return xml.read(is, request.isStrict(), source);
}
}
return xml.read(reader, request.isStrict(), source);
} catch (Exception e) {
throw new XmlReaderException("Unable to read model", e);
}
Expand All @@ -88,12 92,12 @@ public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
}
try {
if (writer != null) {
new MavenXpp3WriterEx().write(writer, content);
new MavenStaxWriter().write(writer, content);
} else if (outputStream != null) {
new MavenXpp3WriterEx().write(outputStream, content);
new MavenStaxWriter().write(outputStream, content);
} else {
try (OutputStream os = Files.newOutputStream(path)) {
new MavenXpp3WriterEx().write(outputStream, content);
new MavenStaxWriter().write(outputStream, content);
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 35,8 @@
import org.apache.maven.api.services.xml.XmlWriterRequest;
import org.apache.maven.api.settings.InputSource;
import org.apache.maven.api.settings.Settings;
import org.apache.maven.settings.v4.SettingsXpp3ReaderEx;
import org.apache.maven.settings.v4.SettingsXpp3Writer;
import org.apache.maven.settings.v4.SettingsStaxReader;
import org.apache.maven.settings.v4.SettingsStaxWriter;

@Named
@Singleton
Expand All @@ -54,7 54,7 @@ public Settings read(@Nonnull XmlReaderRequest request) throws XmlReaderExceptio
if (request.getModelId() != null || request.getLocation() != null) {
source = new InputSource(request.getLocation());
}
SettingsXpp3ReaderEx xml = new SettingsXpp3ReaderEx();
SettingsStaxReader xml = new SettingsStaxReader();
xml.setAddDefaultEntities(request.isAddDefaultEntities());
if (reader != null) {
return xml.read(reader, request.isStrict(), source);
Expand All @@ -77,9 77,9 @@ public void write(XmlWriterRequest<Settings> request) throws XmlWriterException
}
try {
if (writer != null) {
new SettingsXpp3Writer().write(writer, content);
new SettingsStaxWriter().write(writer, content);
} else {
new SettingsXpp3Writer().write(outputStream, content);
new SettingsStaxWriter().write(outputStream, content);
}
} catch (Exception e) {
throw new XmlWriterException("Unable to write settings", e);
Expand Down
Loading