Skip to content

Commit

Permalink
[MNG-7915] Use MavenStaxReader/Writer in MavenXpp3Reader/Writer (#1293)
Browse files Browse the repository at this point in the history
This removes the generated v4 model reader/writer based on Xpp3 api and deprecates the v3 Xpp3 reader/writer.
  • Loading branch information
gnodet committed Oct 20, 2023
1 parent a734e2a commit 4251e3f
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 2,190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,7 @@ private Model read(InputStream input, Path pomFile, Map<String, ?> options) thro
boolean strict = isStrict(options);
MavenStaxReader mr = new MavenStaxReader();
mr.setAddLocationInformation(source != null);
Model model = new Model(mr.read(
parser,
strict,
source != null
? new org.apache.maven.api.model.InputSource(source.getModelId(), source.getLocation())
: null));
Model model = new Model(mr.read(parser, strict, source != null ? source.toApiSource() : null));
return model;
} catch (XMLStreamException e) {
Location location = e.getLocation();
Expand All @@ -139,10 +134,7 @@ private Model read(Reader reader, Path pomFile, Map<String, ?> options) throws I
boolean strict = isStrict(options);
MavenStaxReader mr = new MavenStaxReader();
mr.setAddLocationInformation(source != null);
Model model = new Model(mr.read(
parser,
strict,
new org.apache.maven.api.model.InputSource(source.getModelId(), source.getLocation())));
Model model = new Model(mr.read(parser, strict, source != null ? source.toApiSource() : null));
return model;
} catch (XMLStreamException e) {
Location location = e.getLocation();
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 @@ -31,7 +32,7 @@
import java.util.Objects;

import org.apache.maven.api.model.Model;
import org.apache.maven.model.v4.MavenXpp3Writer;
import org.apache.maven.model.v4.MavenStaxWriter;
import org.codehaus.plexus.util.xml.XmlStreamWriter;

/**
Expand All @@ -58,7 +59,9 @@ public void write(Writer output, Map<String, Object> options, Model model) throw
Objects.requireNonNull(model, "model cannot be null");

try (Writer out = output) {
new MavenXpp3Writer().write(out, model);
new MavenStaxWriter().write(out, model);
} catch (XMLStreamException e) {
throw new IOException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
package org.apache.maven.model.building;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.maven.api.model.Model;
import org.apache.maven.model.v4.MavenXpp3Reader;
import org.apache.maven.model.v4.MavenStaxReader;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -93,8 +94,8 @@ void testPomChanges() throws Exception {
}

private static Model readPom(File file) throws Exception {
MavenXpp3Reader reader = new MavenXpp3Reader();

return reader.read(new FileInputStream(file));
try (InputStream is = Files.newInputStream(file.toPath())) {
return new MavenStaxReader().read(is);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.SimpleProblemCollector;
import org.apache.maven.model.interpolation.DefaultModelVersionProcessor;
import org.apache.maven.model.v4.MavenXpp3Reader;
import org.apache.maven.model.v4.MavenStaxReader;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -43,9 +43,10 @@ class DefaultModelValidatorTest {

private Model read(String pom) throws Exception {
String resource = "/poms/validation/" + pom;
InputStream is = getClass().getResourceAsStream(resource);
assertNotNull(is, "missing resource: " + resource);
return new Model(new MavenXpp3Reader().read(is));
try (InputStream is = getClass().getResourceAsStream(resource)) {
assertNotNull(is, "missing resource: " + resource);
return new Model(new MavenStaxReader().read(is));
}
}

private SimpleProblemCollector validate(String pom) throws Exception {
Expand Down
5 changes: 1 addition & 4 deletions maven-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ under the License.
<templates>
<template>merger.vm</template>
<template>transformer.vm</template>
<template>reader-modified.vm</template>
<template>reader-ex.vm</template>
<template>writer.vm</template>
<template>writer-ex.vm</template>
<template>reader-stax.vm</template>
<template>writer-stax.vm</template>
<template>model-version.vm</template>
Expand Down Expand Up @@ -172,6 +168,7 @@ under the License.
<exclude>org.apache.maven.model.Site#setChildSiteUrlInheritAppendPath(boolean):METHOD_REMOVED</exclude>
<exclude>org.apache.maven.model.io.xpp3.MavenXpp3Reader#contentTransformer</exclude>
<exclude>org.apache.maven.model.io.xpp3.MavenXpp3ReaderEx#contentTransformer</exclude>
<exclude>org.apache.maven.model.io.xpp3.MavenXpp3WriterEx#stringFormatter</exclude>
<exclude>org.apache.maven.model.io.xpp3.MavenXpp3WriterEx#toString(org.apache.maven.model.InputLocation):METHOD_REMOVED</exclude>
<exclude>org.apache.maven.model.io.xpp3.MavenXpp3WriterEx#writeXpp3DomToSerializer(org.codehaus.plexus.util.xml.Xpp3Dom,org.codehaus.plexus.util.xml.pull.XmlSerializer):METHOD_REMOVED</exclude>
<exclude>org.apache.maven.model.merge.ModelMerger</exclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,36 @@
*/
package org.apache.maven.model.io.xpp3;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import org.apache.maven.model.InputSource;
import org.apache.maven.model.Model;
import org.apache.maven.model.v4.MavenStaxReader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* @deprecated Use MavenStaxReader instead
*/
@Deprecated
public class MavenXpp3Reader {
private boolean addDefaultEntities = true;

private final ContentTransformer contentTransformer;
private MavenStaxReader delegate;

public MavenXpp3Reader() {
this((source, fieldName) -> source);
this(null, false);
}

public MavenXpp3Reader(ContentTransformer contentTransformer) {
this.contentTransformer = contentTransformer;
this(contentTransformer, false);
}

protected MavenXpp3Reader(ContentTransformer contentTransformer, boolean addLocationInformation) {
delegate =
contentTransformer != null ? new MavenStaxReader(contentTransformer::transform) : new MavenStaxReader();
delegate.setAddLocationInformation(addLocationInformation);
}

/**
Expand All @@ -48,105 +56,91 @@ public MavenXpp3Reader(ContentTransformer contentTransformer) {
* @return boolean
*/
public boolean getAddDefaultEntities() {
return addDefaultEntities;
return delegate.getAddDefaultEntities();
} // -- boolean getAddDefaultEntities()

/**
* Sets the state of the "add default entities" flag.
*
* @param addDefaultEntities a addDefaultEntities object.
*/
public void setAddDefaultEntities(boolean addDefaultEntities) {
delegate.setAddLocationInformation(addDefaultEntities);
} // -- void setAddDefaultEntities( boolean )

protected Model read(Reader reader, boolean strict, InputSource source) throws IOException, XmlPullParserException {
try {
org.apache.maven.api.model.Model model =
delegate.read(reader, strict, source != null ? source.toApiSource() : null);
return new Model(model);
} catch (XMLStreamException e) {
throw new XmlPullParserException(e.getMessage(), null, e);
}
}

/**
*
* @param reader a reader object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* @throws XmlPullParserException XmlPullParserException if
* any.
* @return Model
*/
public Model read(Reader reader, boolean strict) throws IOException, XMLStreamException {
XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory();
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
XMLStreamReader parser = null;
try {
parser = factory.createXMLStreamReader(reader);
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
return read(parser, strict);
public Model read(Reader reader, boolean strict) throws IOException, XmlPullParserException {
return read(reader, strict, null);
} // -- Model read( Reader, boolean )

/**
*
* @param reader a reader object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* @throws XmlPullParserException XmlPullParserException if
* any.
* @return Model
*/
public Model read(Reader reader) throws IOException, XMLStreamException {
public Model read(Reader reader) throws IOException, XmlPullParserException {
return read(reader, true);
} // -- Model read( Reader )

protected Model read(InputStream is, boolean strict, InputSource source)
throws IOException, XmlPullParserException {
try {
org.apache.maven.api.model.Model model =
delegate.read(is, strict, source != null ? source.toApiSource() : null);
return new Model(model);
} catch (XMLStreamException e) {
throw new XmlPullParserException(e.getMessage(), null, e);
}
}

/**
* Method read.
*
* @param in a in object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* @throws XmlPullParserException XmlPullParserException if
* any.
* @return Model
*/
public Model read(InputStream in, boolean strict) throws IOException, XMLStreamException {
XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory();
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
StreamSource streamSource = new StreamSource(in, null);
XMLStreamReader parser = factory.createXMLStreamReader(streamSource);
return read(parser, strict);
public Model read(InputStream in, boolean strict) throws IOException, XmlPullParserException {
return read(in, strict, null);
} // -- Model read( InputStream, boolean )

/**
* Method read.
*
* @param in a in object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* @throws XmlPullParserException XmlPullParserException if
* any.
* @return Model
*/
public Model read(InputStream in) throws IOException, XMLStreamException {
XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory();
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
StreamSource streamSource = new StreamSource(in, null);
XMLStreamReader parser = factory.createXMLStreamReader(streamSource);
return read(parser, true);
public Model read(InputStream in) throws IOException, XmlPullParserException {
return read(in, true);
} // -- Model read( InputStream )

/**
* Method read.
*
* @param parser a parser object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return Model
*/
public Model read(XMLStreamReader parser, boolean strict) throws IOException, XMLStreamException {
org.apache.maven.model.v4.MavenXpp3Reader reader = contentTransformer != null
? new org.apache.maven.model.v4.MavenXpp3Reader(contentTransformer::transform)
: new org.apache.maven.model.v4.MavenXpp3Reader();
reader.setAddDefaultEntities(addDefaultEntities);
org.apache.maven.api.model.Model model = reader.read(parser, strict);
return new Model(model);
} // -- Model read( XmlPullParser, boolean )

/**
* Sets the state of the "add default entities" flag.
*
* @param addDefaultEntities a addDefaultEntities object.
*/
public void setAddDefaultEntities(boolean addDefaultEntities) {
this.addDefaultEntities = addDefaultEntities;
} // -- void setAddDefaultEntities( boolean )

public interface ContentTransformer {
/**
* Interpolate the value read from the xpp3 document
Expand Down
Loading

0 comments on commit 4251e3f

Please sign in to comment.