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-7904] Use SettingsStaxReader/Writer for SettingsXpp3Reader/Writer #1285

Merged
merged 3 commits into from
Oct 20, 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
2 changes: 0 additions & 2 deletions maven-settings/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 82,6 @@ under the License.
<templates>
<template>merger.vm</template>
<template>transformer.vm</template>
<template>reader.vm</template>
<template>writer.vm</template>
<template>reader-stax.vm</template>
<template>writer-stax.vm</template>
</templates>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 18,32 @@
*/
package org.apache.maven.settings.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.settings.Settings;
import org.apache.maven.settings.v4.SettingsStaxReader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* @deprecated Maven 3 compatibility - please use {@code org.apache.maven.api.services.xml.SettingsXmlFactory} from {@code maven-api-core}
* or {@link SettingsStaxReader}
*/
@Deprecated
public class SettingsXpp3Reader {
private boolean addDefaultEntities = true;

private final ContentTransformer contentTransformer;
private final SettingsStaxReader delegate;

public SettingsXpp3Reader() {
this((source, fieldName) -> source);
delegate = new SettingsStaxReader();
}

public SettingsXpp3Reader(ContentTransformer contentTransformer) {
this.contentTransformer = contentTransformer;
delegate = new SettingsStaxReader(contentTransformer::transform);
}

/**
Expand All @@ -49,98 52,101 @@ public SettingsXpp3Reader(ContentTransformer contentTransformer) {
* @return boolean
*/
public boolean getAddDefaultEntities() {
return addDefaultEntities;
} // -- boolean getAddDefaultEntities()
return delegate.getAddDefaultEntities();
}

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

/**
* @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 Settings
*/
public Settings 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;
public Settings read(Reader reader, boolean strict) throws IOException, XmlPullParserException {
try {
parser = factory.createXMLStreamReader(reader);
return new Settings(delegate.read(reader, strict, null));
} catch (XMLStreamException e) {
throw new RuntimeException(e);
throw new XmlPullParserException(e.getMessage(), null, e);
}
return read(parser, strict);
} // -- 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 Settings read(Reader reader) throws IOException, XMLStreamException {
return read(reader, true);
} // -- Model read( Reader )
public Settings read(Reader reader) throws IOException, XmlPullParserException {
try {
return new Settings(delegate.read(reader));
} catch (XMLStreamException e) {
throw new XmlPullParserException(e.getMessage(), null, e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just pass the exception?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just pass the exception?

Pass how / where ? We can't pass through, that would change the throws clause and break the API. Which would be problematic because that's the whole purpose of that class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not what I meant. Single arg ctor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ww have two ctor

  • XmlPullParserException(String s) - only message
  • XmlPullParserException(String msg, XmlPullParser parser, Throwable chain)

so I choose second with cause exception

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, another one is missing for this case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the xpp3 api may even predate chained exceptions that were introduced in JDK 1.4 ;-)

}
}

/**
* 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 Settings
*/
public Settings 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);
} // -- Model read( InputStream, boolean )
public Settings read(InputStream in, boolean strict) throws IOException, XmlPullParserException {
try {
return new Settings(delegate.read(in, strict, null));
} catch (XMLStreamException e) {
throw new XmlPullParserException(e.getMessage(), null, e);
}
}

/**
* Method read.
*
* @param in a in object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* @throws XmlPullParserException XmlPullParserException if
* any.
* @return Settings
*/
public Settings read(InputStream in) throws IOException, XMLStreamException {
return read(in, true);
} // -- Model read( InputStream )
public Settings read(InputStream in) throws IOException, XmlPullParserException {
try {
return new Settings(delegate.read(in));
} catch (XMLStreamException e) {
throw new XmlPullParserException(e.getMessage(), null, e);
}
}

/**
* Method read.
*
* @param parser a parser object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* @throws XmlPullParserException XmlPullParserException if
* any.
* @return Settings
*/
public Settings read(XMLStreamReader parser, boolean strict) throws IOException, XMLStreamException {
org.apache.maven.settings.v4.SettingsXpp3Reader reader = contentTransformer != null
? new org.apache.maven.settings.v4.SettingsXpp3Reader(contentTransformer::transform)
: new org.apache.maven.settings.v4.SettingsXpp3Reader();
reader.setAddDefaultEntities(addDefaultEntities);
org.apache.maven.api.settings.Settings settings = reader.read(parser, strict);
return new Settings(settings);
} // -- Model read( XMLStreamReader, 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 Settings read(XMLStreamReader parser, boolean strict) throws IOException, XmlPullParserException {
try {
return new Settings(delegate.read(parser, strict, null));
} catch (XMLStreamException e) {
throw new XmlPullParserException(e.getMessage(), null, e);
}
}

public interface ContentTransformer {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 18,36 @@
*/
package org.apache.maven.settings.io.xpp3;

import javax.xml.stream.XMLStreamException;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;

import org.apache.maven.settings.Settings;
import org.apache.maven.settings.v4.SettingsStaxWriter;

/**
* @deprecated Maven 3 compatibility - please use {@code org.apache.maven.api.services.xml.SettingsXmlFactory} from {@code maven-api-core}
* or {@link SettingsStaxWriter}
*/
@Deprecated
public class SettingsXpp3Writer {
// --------------------------/
// - Class/Member Variables -/
// --------------------------/

/**
* Field fileComment.
*/
private String fileComment = null;

// -----------/
// - Methods -/
// -----------/
private final SettingsStaxWriter delegate;

public SettingsXpp3Writer() {
delegate = new SettingsStaxWriter();
delegate.setAddLocationInformation(false);
}
/**
* Method setFileComment.
*
* @param fileComment a fileComment object.
*/
public void setFileComment(String fileComment) {
this.fileComment = fileComment;
} // -- void setFileComment( String )
delegate.setFileComment(fileComment);
}

/**
* Method write.
Expand All @@ -56,10 57,12 @@ public void setFileComment(String fileComment) {
* @throws IOException java.io.IOException if any.
*/
public void write(Writer writer, Settings settings) throws IOException {
org.apache.maven.settings.v4.SettingsXpp3Writer xw = new org.apache.maven.settings.v4.SettingsXpp3Writer();
xw.setFileComment(fileComment);
xw.write(writer, settings.getDelegate());
} // -- void write( Writer, Model )
try {
delegate.write(writer, settings.getDelegate());
} catch (XMLStreamException e) {
throw new IOException(e);
}
}

/**
* Method write.
Expand All @@ -69,8 72,10 @@ public void write(Writer writer, Settings settings) throws IOException {
* @throws IOException java.io.IOException if any.
*/
public void write(OutputStream stream, Settings settings) throws IOException {
org.apache.maven.settings.v4.SettingsXpp3Writer xw = new org.apache.maven.settings.v4.SettingsXpp3Writer();
xw.setFileComment(fileComment);
xw.write(stream, settings.getDelegate());
} // -- void write( OutputStream, Model )
try {
delegate.write(stream, settings.getDelegate());
} catch (XMLStreamException e) {
throw new IOException(e);
}
}
}