[MNG-7915] Use MavenStaxReader/Writer in MavenXpp3Reader/Writer (#1293)

This removes the generated v4 model reader/writer based on Xpp3 api and deprecates the v3 Xpp3 reader/writer.
This commit is contained in:
Guillaume Nodet 2023-10-20 09:14:01 +02:00 committed by GitHub
parent a734e2a336
commit 4251e3f9c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 212 additions and 2227 deletions

View File

@ -110,12 +110,7 @@ public class DefaultModelReader implements ModelReader {
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();
@ -139,10 +134,7 @@ public class DefaultModelReader implements ModelReader {
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();

View File

@ -20,6 +20,7 @@ package org.apache.maven.model.io;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;
@ -31,7 +32,7 @@ import java.util.Map;
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;
/**
@ -58,7 +59,9 @@ public class DefaultModelWriter implements ModelWriter {
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);
}
}

View File

@ -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;
@ -93,8 +94,8 @@ class DefaultModelBuilderFactoryTest {
}
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);
}
}
}

View File

@ -26,7 +26,7 @@ import org.apache.maven.model.building.DefaultModelBuildingRequest;
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;
@ -43,9 +43,10 @@ class DefaultModelValidatorTest {
private Model read(String pom) throws Exception {
String resource = "/poms/validation/" + pom;
InputStream is = getClass().getResourceAsStream(resource);
try (InputStream is = getClass().getResourceAsStream(resource)) {
assertNotNull(is, "missing resource: " + resource);
return new Model(new MavenXpp3Reader().read(is));
return new Model(new MavenStaxReader().read(is));
}
}
private SimpleProblemCollector validate(String pom) throws Exception {

View File

@ -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>
@ -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>

View File

@ -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);
}
/**
@ -48,105 +56,91 @@ public class MavenXpp3Reader {
* @return boolean
*/
public boolean getAddDefaultEntities() {
return addDefaultEntities;
return delegate.getAddDefaultEntities();
} // -- boolean getAddDefaultEntities()
/**
*
* @param reader a reader object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException 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);
} // -- Model read( Reader, boolean )
/**
*
* @param reader a reader object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return Model
*/
public Model read(Reader reader) throws IOException, XMLStreamException {
return read(reader, true);
} // -- Model read( Reader )
/**
* Method read.
*
* @param in a in object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException 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);
} // -- Model read( InputStream, boolean )
/**
* Method read.
*
* @param in a in object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException 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);
} // -- 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;
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 XmlPullParserException XmlPullParserException if
* any.
* @return Model
*/
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 XmlPullParserException XmlPullParserException if
* any.
* @return Model
*/
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 XmlPullParserException XmlPullParserException if
* any.
* @return Model
*/
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 XmlPullParserException XmlPullParserException if
* any.
* @return Model
*/
public Model read(InputStream in) throws IOException, XmlPullParserException {
return read(in, true);
} // -- Model read( InputStream )
public interface ContentTransformer {
/**
* Interpolate the value read from the xpp3 document

View File

@ -18,125 +18,57 @@
*/
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.codehaus.plexus.util.xml.pull.XmlPullParserException;
public class MavenXpp3ReaderEx {
private boolean addDefaultEntities = true;
private final ContentTransformer contentTransformer;
/**
* @deprecated Use MavenStaxReader instead
*/
@Deprecated
public class MavenXpp3ReaderEx extends MavenXpp3Reader {
public MavenXpp3ReaderEx() {
this((source, fieldName) -> source);
this(null);
}
public MavenXpp3ReaderEx(ContentTransformer contentTransformer) {
this.contentTransformer = contentTransformer;
super(contentTransformer != null ? contentTransformer::transform : null, true);
}
/**
* Returns the state of the "add default entities" flag.
*
* @return boolean
*/
public boolean getAddDefaultEntities() {
return addDefaultEntities;
} // -- boolean getAddDefaultEntities()
/**
* @param reader a reader object
* @param strict a strict object
* @return Model
* @throws IOException IOException if an I/O error occurs while reading from the underlying source
* @throws XMLStreamException XMLStreamException if an error occurs while parser xml
*/
public Model read(Reader reader, boolean strict, InputSource source) throws IOException, XMLStreamException {
XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory();
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
try {
XMLStreamReader parser = factory.createXMLStreamReader(reader);
return read(parser, strict, source);
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
} // -- Model read( Reader, boolean )
/**
* @param reader a reader object
* @return Model
* @throws IOException IOException if an I/O error occurs while reading from the underlying source
* @throws XMLStreamException XMLStreamException if an error occurs while parser xml
*/
public Model read(Reader reader, InputSource source) throws IOException, XMLStreamException {
return read(reader, true, source);
} // -- Model read( Reader )
/**
* Method read.
*
* @param in a in object
* @param strict a strict object
* @return Model
* @throws IOException IOException if an I/O error occurs while reading from the underlying source
* @throws XMLStreamException XMLStreamException if an error occurs while parser xml
*/
public Model read(InputStream in, boolean strict, InputSource source) 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, source);
} // -- Model read( InputStream, boolean )
/**
* Method read.
*
* @param in a in object
* @return Model
* @throws IOException IOException if an I/O error occurs while reading from the underlying source
* @throws XMLStreamException XMLStreamException if an error occurs while parser xml
*/
public Model read(InputStream in, InputSource source) throws IOException, XMLStreamException {
return read(in, true, source);
} // -- Model read( InputStream )
/**
* Method read.
*
* @param parser a parser object
* @param strict a strict object
* @return Model
* @throws IOException IOException if an I/O error occurs while reading from the underlying source
* @throws XMLStreamException XMLStreamException if an error occurs while parser xml
*/
public Model read(XMLStreamReader parser, boolean strict, InputSource source)
throws IOException, XMLStreamException {
org.apache.maven.model.v4.MavenXpp3ReaderEx reader = contentTransformer != null
? new org.apache.maven.model.v4.MavenXpp3ReaderEx(contentTransformer::transform)
: new org.apache.maven.model.v4.MavenXpp3ReaderEx();
reader.setAddDefaultEntities(addDefaultEntities);
org.apache.maven.api.model.Model model = reader.read(
parser, strict, new org.apache.maven.api.model.InputSource(source.getModelId(), source.getLocation()));
return new Model(model);
@Override
public Model read(Reader reader, boolean strict, InputSource source) throws IOException, XmlPullParserException {
return super.read(reader, strict, source);
}
/**
* 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 )
@Override
public Model read(Reader reader, boolean strict) throws IOException, XmlPullParserException {
return super.read(reader, strict);
}
@Override
public Model read(Reader reader) throws IOException, XmlPullParserException {
return super.read(reader);
}
@Override
public Model read(InputStream in, boolean strict, InputSource source) throws IOException, XmlPullParserException {
return super.read(in, strict, source);
}
@Override
public Model read(InputStream in, boolean strict) throws IOException, XmlPullParserException {
return super.read(in, strict);
}
@Override
public Model read(InputStream in) throws IOException, XmlPullParserException {
return super.read(in);
}
public interface ContentTransformer {
/**

View File

@ -18,35 +18,65 @@
*/
package org.apache.maven.model.io.xpp3;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import org.apache.maven.model.InputLocation;
import org.apache.maven.model.Model;
import org.apache.maven.model.v4.MavenStaxWriter;
/**
* @deprecated Use MavenStaxWriter instead
*/
@Deprecated
public class MavenXpp3Writer {
// --------------------------/
// - Class/Member Variables -/
// --------------------------/
/**
* Field fileComment.
*/
private String fileComment = null;
private final MavenStaxWriter delegate = new MavenStaxWriter();
// -----------/
// - Methods -/
// -----------/
public MavenXpp3Writer() {
this(false);
}
protected MavenXpp3Writer(boolean addLocationInformation) {
delegate.setAddLocationInformation(addLocationInformation);
}
/**
* Method setFileComment.
*
* @param fileComment a fileComment object.
*/
public void setFileComment(String fileComment) {
this.fileComment = fileComment;
delegate.setFileComment(fileComment);
} // -- void setFileComment( String )
/**
* Method setStringFormatter.
*
* @param stringFormatter
*/
public void setStringFormatter(InputLocation.StringFormatter stringFormatter) {
delegate.setStringFormatter(
stringFormatter != null
? new org.apache.maven.api.model.InputLocation.StringFormatter() {
@Override
public String toString(org.apache.maven.api.model.InputLocation location) {
return stringFormatter.toString(new InputLocation(location));
}
}
: null);
} // -- void setStringFormatter( InputLocation.StringFormatter )
/**
* Method write.
*
@ -55,9 +85,11 @@ public class MavenXpp3Writer {
* @throws IOException java.io.IOException if any.
*/
public void write(Writer writer, Model model) throws IOException {
org.apache.maven.model.v4.MavenXpp3Writer xw = new org.apache.maven.model.v4.MavenXpp3Writer();
xw.setFileComment(fileComment);
xw.write(writer, model.getDelegate());
try {
delegate.write(writer, model.getDelegate());
} catch (XMLStreamException e) {
throw new IOException(e);
}
} // -- void write( Writer, Model )
/**
@ -68,8 +100,10 @@ public class MavenXpp3Writer {
* @throws IOException java.io.IOException if any.
*/
public void write(OutputStream stream, Model model) throws IOException {
org.apache.maven.model.v4.MavenXpp3Writer xw = new org.apache.maven.model.v4.MavenXpp3Writer();
xw.setFileComment(fileComment);
xw.write(stream, model.getDelegate());
try {
delegate.write(stream, model.getDelegate());
} catch (XMLStreamException e) {
throw new IOException(e);
}
} // -- void write( OutputStream, Model )
}

View File

@ -25,32 +25,27 @@ import java.io.Writer;
import org.apache.maven.model.InputLocation;
import org.apache.maven.model.Model;
public class MavenXpp3WriterEx {
// --------------------------/
// - Class/Member Variables -/
// --------------------------/
/**
* Field fileComment.
* @deprecated Use MavenStaxWriter instead
*/
private String fileComment = null;
/**
* Field stringFormatter.
*/
protected InputLocation.StringFormatter stringFormatter;
@Deprecated
public class MavenXpp3WriterEx extends MavenXpp3Writer {
// -----------/
// - Methods -/
// -----------/
public MavenXpp3WriterEx() {
super(true);
}
/**
* Method setFileComment.
*
* @param fileComment a fileComment object.
*/
public void setFileComment(String fileComment) {
this.fileComment = fileComment;
super.setFileComment(fileComment);
} // -- void setFileComment( String )
/**
@ -59,7 +54,7 @@ public class MavenXpp3WriterEx {
* @param stringFormatter
*/
public void setStringFormatter(InputLocation.StringFormatter stringFormatter) {
this.stringFormatter = stringFormatter;
super.setStringFormatter(stringFormatter);
} // -- void setStringFormatter( InputLocation.StringFormatter )
/**
@ -70,18 +65,7 @@ public class MavenXpp3WriterEx {
* @throws IOException java.io.IOException if any.
*/
public void write(Writer writer, Model model) throws IOException {
org.apache.maven.model.v4.MavenXpp3WriterEx xw = new org.apache.maven.model.v4.MavenXpp3WriterEx();
xw.setFileComment(fileComment);
xw.setStringFormatter(
stringFormatter != null
? new org.apache.maven.api.model.InputLocation.StringFormatter() {
@Override
public String toString(org.apache.maven.api.model.InputLocation location) {
return stringFormatter.toString(new InputLocation(location));
}
}
: null);
xw.write(writer, model.getDelegate());
super.write(writer, model);
} // -- void write( Writer, Model )
/**
@ -92,8 +76,6 @@ public class MavenXpp3WriterEx {
* @throws IOException java.io.IOException if any.
*/
public void write(OutputStream stream, Model model) throws IOException {
org.apache.maven.model.v4.MavenXpp3WriterEx xw = new org.apache.maven.model.v4.MavenXpp3WriterEx();
xw.setFileComment(fileComment);
xw.write(stream, model.getDelegate());
super.write(stream, model);
} // -- void write( OutputStream, Model )
}

View File

@ -50,9 +50,11 @@ class ModelXmlTest {
}
}
String toXml(Model model) throws IOException {
String toXml(Model model) throws IOException, XMLStreamException {
StringWriter sw = new StringWriter();
new MavenXpp3Writer().write(sw, model);
MavenStaxWriter writer = new MavenStaxWriter();
writer.setAddLocationInformation(false);
writer.write(sw, model);
return sw.toString();
}

View File

@ -57,20 +57,6 @@ public class Xpp3DomPerfTest {
}
}
@Benchmark
public int readWithXpp3(AdditionState state) throws IOException {
int i = 0;
for (Path pom : state.poms) {
try (InputStream is = Files.newInputStream(pom)) {
new MavenXpp3ReaderEx().read(is, true, new InputSource("id", pom.toString()));
i++;
} catch (XMLStreamException e) {
throw new RuntimeException("Error parsing: " + pom, e);
}
}
return i;
}
@Benchmark
public int readWithStax(AdditionState state) throws IOException, XMLStreamException {
int i = 0;

View File

@ -1,785 +0,0 @@
#*
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.
*#
#parse ( "common.vm" )
#
#if( ${packageToolV4Xpp3} )
#set ( $package = "${packageToolV4Xpp3}" )
#else
#set ( $package = "${packageToolV4}" )
#end
#set ( $className = "${model.name}Xpp3ReaderEx" )
#
#set ( $root = $model.getClass( $model.getRoot($version), $version ) )
#set ( $rootXml = $Helper.xmlClassMetadata( $root ) )
#set ( $rootTag = $rootXml.tagName )
#set ( $rootUcapName = $Helper.capitalise( $root.name ) )
#set ( $rootLcapName = $Helper.uncapitalise( $root.name ) )
#
#MODELLO-VELOCITY#SAVE-OUTPUT-TO ${package.replace('.','/')}/${className}.java
// =================== DO NOT EDIT THIS FILE ====================
// Generated by Modello Velocity from ${template}
// template, any modifications will be overwritten.
// ==============================================================
package ${package};
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Collections;
import org.apache.maven.api.annotations.Generated;
import org.apache.maven.internal.xml.XmlNodeBuilder;
import ${packageModelV4}.InputSource;
import ${packageModelV4}.InputLocation;
#foreach ( $class in $model.allClasses )
import ${packageModelV4}.${class.name};
#end
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;
@Deprecated
@Generated
public class ${className} {
private boolean addDefaultEntities = true;
private final ContentTransformer contentTransformer;
/**
* XSI namespace
*/
private static final String XSI_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance";
private static final Map<String, String> DEFAULT_ENTITIES;
static {
Map<String, String> entities = new HashMap<>();
entities.put("nbsp", "\u00a0");
entities.put("iexcl", "\u00a1");
entities.put("cent", "\u00a2");
entities.put("pound", "\u00a3");
entities.put("curren", "\u00a4");
entities.put("yen", "\u00a5");
entities.put("brvbar", "\u00a6");
entities.put("sect", "\u00a7");
entities.put("uml", "\u00a8");
entities.put("copy", "\u00a9");
entities.put("ordf", "\u00aa");
entities.put("laquo", "\u00ab");
entities.put("not", "\u00ac");
entities.put("shy", "\u00ad");
entities.put("reg", "\u00ae");
entities.put("macr", "\u00af");
entities.put("deg", "\u00b0");
entities.put("plusmn", "\u00b1");
entities.put("sup2", "\u00b2");
entities.put("sup3", "\u00b3");
entities.put("acute", "\u00b4");
entities.put("micro", "\u00b5");
entities.put("para", "\u00b6");
entities.put("middot", "\u00b7");
entities.put("cedil", "\u00b8");
entities.put("sup1", "\u00b9");
entities.put("ordm", "\u00ba");
entities.put("raquo", "\u00bb");
entities.put("frac14", "\u00bc");
entities.put("frac12", "\u00bd");
entities.put("frac34", "\u00be");
entities.put("iquest", "\u00bf");
entities.put("Agrave", "\u00c0");
entities.put("Aacute", "\u00c1");
entities.put("Acirc", "\u00c2");
entities.put("Atilde", "\u00c3");
entities.put("Auml", "\u00c4");
entities.put("Aring", "\u00c5");
entities.put("AElig", "\u00c6");
entities.put("Ccedil", "\u00c7");
entities.put("Egrave", "\u00c8");
entities.put("Eacute", "\u00c9");
entities.put("Ecirc", "\u00ca");
entities.put("Euml", "\u00cb");
entities.put("Igrave", "\u00cc");
entities.put("Iacute", "\u00cd");
entities.put("Icirc", "\u00ce");
entities.put("Iuml", "\u00cf");
entities.put("ETH", "\u00d0");
entities.put("Ntilde", "\u00d1");
entities.put("Ograve", "\u00d2");
entities.put("Oacute", "\u00d3");
entities.put("Ocirc", "\u00d4");
entities.put("Otilde", "\u00d5");
entities.put("Ouml", "\u00d6");
entities.put("times", "\u00d7");
entities.put("Oslash", "\u00d8");
entities.put("Ugrave", "\u00d9");
entities.put("Uacute", "\u00da");
entities.put("Ucirc", "\u00db");
entities.put("Uuml", "\u00dc");
entities.put("Yacute", "\u00dd");
entities.put("THORN", "\u00de");
entities.put("szlig", "\u00df");
entities.put("agrave", "\u00e0");
entities.put("aacute", "\u00e1");
entities.put("acirc", "\u00e2");
entities.put("atilde", "\u00e3");
entities.put("auml", "\u00e4");
entities.put("aring", "\u00e5");
entities.put("aelig", "\u00e6");
entities.put("ccedil", "\u00e7");
entities.put("egrave", "\u00e8");
entities.put("eacute", "\u00e9");
entities.put("ecirc", "\u00ea");
entities.put("euml", "\u00eb");
entities.put("igrave", "\u00ec");
entities.put("iacute", "\u00ed");
entities.put("icirc", "\u00ee");
entities.put("iuml", "\u00ef");
entities.put("eth", "\u00f0");
entities.put("ntilde", "\u00f1");
entities.put("ograve", "\u00f2");
entities.put("oacute", "\u00f3");
entities.put("ocirc", "\u00f4");
entities.put("otilde", "\u00f5");
entities.put("ouml", "\u00f6");
entities.put("divide", "\u00f7");
entities.put("oslash", "\u00f8");
entities.put("ugrave", "\u00f9");
entities.put("uacute", "\u00fa");
entities.put("ucirc", "\u00fb");
entities.put("uuml", "\u00fc");
entities.put("yacute", "\u00fd");
entities.put("thorn", "\u00fe");
entities.put("yuml", "\u00ff");
// ----------------------------------------------------------------------
// Special entities
// ----------------------------------------------------------------------
entities.put("OElig", "\u0152");
entities.put("oelig", "\u0153");
entities.put("Scaron", "\u0160");
entities.put("scaron", "\u0161");
entities.put("Yuml", "\u0178");
entities.put("circ", "\u02c6");
entities.put("tilde", "\u02dc");
entities.put("ensp", "\u2002");
entities.put("emsp", "\u2003");
entities.put("thinsp", "\u2009");
entities.put("zwnj", "\u200c");
entities.put("zwj", "\u200d");
entities.put("lrm", "\u200e");
entities.put("rlm", "\u200f");
entities.put("ndash", "\u2013");
entities.put("mdash", "\u2014");
entities.put("lsquo", "\u2018");
entities.put("rsquo", "\u2019");
entities.put("sbquo", "\u201a");
entities.put("ldquo", "\u201c");
entities.put("rdquo", "\u201d");
entities.put("bdquo", "\u201e");
entities.put("dagger", "\u2020");
entities.put("Dagger", "\u2021");
entities.put("permil", "\u2030");
entities.put("lsaquo", "\u2039");
entities.put("rsaquo", "\u203a");
entities.put("euro", "\u20ac");
// ----------------------------------------------------------------------
// Symbol entities
// ----------------------------------------------------------------------
entities.put("fnof", "\u0192");
entities.put("Alpha", "\u0391");
entities.put("Beta", "\u0392");
entities.put("Gamma", "\u0393");
entities.put("Delta", "\u0394");
entities.put("Epsilon", "\u0395");
entities.put("Zeta", "\u0396");
entities.put("Eta", "\u0397");
entities.put("Theta", "\u0398");
entities.put("Iota", "\u0399");
entities.put("Kappa", "\u039a");
entities.put("Lambda", "\u039b");
entities.put("Mu", "\u039c");
entities.put("Nu", "\u039d");
entities.put("Xi", "\u039e");
entities.put("Omicron", "\u039f");
entities.put("Pi", "\u03a0");
entities.put("Rho", "\u03a1");
entities.put("Sigma", "\u03a3");
entities.put("Tau", "\u03a4");
entities.put("Upsilon", "\u03a5");
entities.put("Phi", "\u03a6");
entities.put("Chi", "\u03a7");
entities.put("Psi", "\u03a8");
entities.put("Omega", "\u03a9");
entities.put("alpha", "\u03b1");
entities.put("beta", "\u03b2");
entities.put("gamma", "\u03b3");
entities.put("delta", "\u03b4");
entities.put("epsilon", "\u03b5");
entities.put("zeta", "\u03b6");
entities.put("eta", "\u03b7");
entities.put("theta", "\u03b8");
entities.put("iota", "\u03b9");
entities.put("kappa", "\u03ba");
entities.put("lambda", "\u03bb");
entities.put("mu", "\u03bc");
entities.put("nu", "\u03bd");
entities.put("xi", "\u03be");
entities.put("omicron", "\u03bf");
entities.put("pi", "\u03c0");
entities.put("rho", "\u03c1");
entities.put("sigmaf", "\u03c2");
entities.put("sigma", "\u03c3");
entities.put("tau", "\u03c4");
entities.put("upsilon", "\u03c5");
entities.put("phi", "\u03c6");
entities.put("chi", "\u03c7");
entities.put("psi", "\u03c8");
entities.put("omega", "\u03c9");
entities.put("thetasym", "\u03d1");
entities.put("upsih", "\u03d2");
entities.put("piv", "\u03d6");
entities.put("bull", "\u2022");
entities.put("hellip", "\u2026");
entities.put("prime", "\u2032");
entities.put("Prime", "\u2033");
entities.put("oline", "\u203e");
entities.put("frasl", "\u2044");
entities.put("weierp", "\u2118");
entities.put("image", "\u2111");
entities.put("real", "\u211c");
entities.put("trade", "\u2122");
entities.put("alefsym", "\u2135");
entities.put("larr", "\u2190");
entities.put("uarr", "\u2191");
entities.put("rarr", "\u2192");
entities.put("darr", "\u2193");
entities.put("harr", "\u2194");
entities.put("crarr", "\u21b5");
entities.put("lArr", "\u21d0");
entities.put("uArr", "\u21d1");
entities.put("rArr", "\u21d2");
entities.put("dArr", "\u21d3");
entities.put("hArr", "\u21d4");
entities.put("forall", "\u2200");
entities.put("part", "\u2202");
entities.put("exist", "\u2203");
entities.put("empty", "\u2205");
entities.put("nabla", "\u2207");
entities.put("isin", "\u2208");
entities.put("notin", "\u2209");
entities.put("ni", "\u220b");
entities.put("prod", "\u220f");
entities.put("sum", "\u2211");
entities.put("minus", "\u2212");
entities.put("lowast", "\u2217");
entities.put("radic", "\u221a");
entities.put("prop", "\u221d");
entities.put("infin", "\u221e");
entities.put("ang", "\u2220");
entities.put("and", "\u2227");
entities.put("or", "\u2228");
entities.put("cap", "\u2229");
entities.put("cup", "\u222a");
entities.put("int", "\u222b");
entities.put("there4", "\u2234");
entities.put("sim", "\u223c");
entities.put("cong", "\u2245");
entities.put("asymp", "\u2248");
entities.put("ne", "\u2260");
entities.put("equiv", "\u2261");
entities.put("le", "\u2264");
entities.put("ge", "\u2265");
entities.put("sub", "\u2282");
entities.put("sup", "\u2283");
entities.put("nsub", "\u2284");
entities.put("sube", "\u2286");
entities.put("supe", "\u2287");
entities.put("oplus", "\u2295");
entities.put("otimes", "\u2297");
entities.put("perp", "\u22a5");
entities.put("sdot", "\u22c5");
entities.put("lceil", "\u2308");
entities.put("rceil", "\u2309");
entities.put("lfloor", "\u230a");
entities.put("rfloor", "\u230b");
entities.put("lang", "\u2329");
entities.put("rang", "\u232a");
entities.put("loz", "\u25ca");
entities.put("spades", "\u2660");
entities.put("clubs", "\u2663");
entities.put("hearts", "\u2665");
entities.put("diams", "\u2666");
DEFAULT_ENTITIES = Collections.unmodifiableMap(entities);
}
public ${className}() {
this((s, f) -> s);
}
public ${className}(ContentTransformer contentTransformer) {
this.contentTransformer = contentTransformer;
}
/**
* Returns the state of the "add default entities" flag.
*
* @return boolean
*/
public boolean getAddDefaultEntities() {
return addDefaultEntities;
} //-- boolean getAddDefaultEntities()
/**
* 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)
/**
*
* @param reader a reader object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return ${root.name}
*/
public ${root.name} read(Reader reader, boolean strict, InputSource source) 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, source);
} //-- ${root.name} read(Reader, boolean)
/**
* Method read.
*
* @param in a in object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return ${root.name}
*/
public ${root.name} read(InputStream in, boolean strict, InputSource source) 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, source);
} //-- ${root.name} read(InputStream, boolean)
/**
* Method read.
*
* @param parser a parser object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return ${root.name}
*/
public ${root.name} read(XMLStreamReader parser, boolean strict, InputSource source) throws IOException, XMLStreamException {
$rootUcapName $rootLcapName = null;
int eventType = parser.getEventType();
boolean parsed = false;
while (eventType != XMLStreamReader.END_DOCUMENT) {
if (eventType == XMLStreamReader.START_ELEMENT) {
if (strict && ! "${rootTag}".equals(parser.getName())) {
throw new XMLStreamException("Expected root element '${rootTag}' but found '" + parser.getLocalName() + "'", parser.getLocation(), null);
} else if (parsed) {
// fallback, already expected a XMLStreamException due to invalid XML
throw new XMLStreamException("Duplicated tag: '${rootTag}'", parser.getLocation(), null);
}
$rootLcapName = parse${rootUcapName}(parser, strict, source);
parsed = true;
}
eventType = parser.next();
}
if (parsed) {
return $rootLcapName;
}
throw new XMLStreamException("Expected root element '${rootTag}' but found no element at all: invalid XML document", parser.getLocation(), null);
} //-- ${root.name} read(XMLStreamReader, boolean)
#foreach ( $class in $model.allClasses )
#if ( $class.name != "InputSource" && $class.name != "InputLocation" )
#set ( $classUcapName = $Helper.capitalise( $class.name ) )
#set ( $classLcapName = $Helper.uncapitalise( $class.name ) )
#set ( $ancestors = $Helper.ancestors( $class ) )
#set ( $allFields = $Helper.xmlFields( $class ) )
private ${classUcapName} parse${classUcapName}(XMLStreamReader parser, boolean strict, InputSource source) throws IOException, XMLStreamException {
String tagName = parser.getLocalName();
${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder(true);
${classLcapName}.location("", new InputLocation(parser.getLocation().getLineNumber(), parser.getLocation().getColumnNumber(), source));
for (int i = parser.getAttributeCount() - 1; i >= 0; i--) {
String name = parser.getAttributeLocalName(i);
String value = parser.getAttributeNamespace(i);
if (XSI_NAMESPACE.equals(value)) {
// just ignore attributes with non-default namespace (for example: xmlns:xsi)
#if ( $class == $root )
} else if ("xmlns".equals(name)) {
// ignore xmlns attribute in root class, which is a reserved attribute name
#end
#foreach ( $field in $allFields )
#if ( $Helper.xmlFieldMetadata( $field ).attribute )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#set ( $fieldCapName = $Helper.capitalise( $field.name ) )
} else if ("$fieldTagName".equals(name)) {
${classLcapName}.location(name, new InputLocation(parser.getLocation().getLineNumber(), parser.getLocation().getColumnNumber(), source));
#if ( $field.type == "String" )
${classLcapName}.${field.name}(interpolatedTrimmed(value, "$fieldTagName"));
#elseif ( $field.type == "boolean" || $field.type == "Boolean" )
${classLcapName}.${field.name}(getBooleanValue(interpolatedTrimmed(value, "$fieldTagName"), "$fieldTagName", parser, ${field.defaultValue}));
#else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
#end
#end
#end
} else {
checkUnknownAttribute(parser, name, tagName, strict);
}
}
Set<String> parsed = new HashSet<>();
while ((strict ? parser.nextTag() : nextTag(parser)) == XMLStreamReader.START_ELEMENT) {
String childName = unalias(parser.getLocalName());
if (!parsed.add(childName)) {
throw new XMLStreamException("Duplicated tag: '" + childName + "'", parser.getLocation(), null);
}
int line = parser.getLocation().getLineNumber();
int column = parser.getLocation().getColumnNumber();
Map<Object, InputLocation> locations = null;
switch (childName) {
#set( $ift = "if" )
#foreach ( $field in $allFields )
#if ( ! $Helper.xmlFieldMetadata( $field ).attribute && ! $Helper.xmlFieldMetadata( $field ).transient )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#if ( ! $fieldTagName )
#set ( $fieldTagName = $field.name )
#end
#set ( $fieldCapName = $Helper.capitalise($field.name))
case "${fieldTagName}": {
#if ( $field.type == "String" )
${classLcapName}.${field.name}(interpolatedTrimmed(nextText(parser, strict), "${fieldTagName}"));
break;
#elseif ( $field.type == "boolean" || $field.type == "Boolean" )
${classLcapName}.${field.name}(getBooleanValue(interpolatedTrimmed(nextText(parser, strict), "${fieldTagName}"), "${fieldTagName}", parser, ${field.defaultValue}));
break;
#elseif ( $field.type == "int" )
${classLcapName}.${field.name}(getIntegerValue(interpolatedTrimmed(nextText(parser, strict), "${fieldTagName}"), "${fieldTagName}", parser, strict, ${field.defaultValue}));
break;
#elseif ( $field.type == "DOM" )
${classLcapName}.${field.name}(XmlNodeBuilder.build(parser));
break;
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
List<String> ${field.name} = new ArrayList<>();
locations = new HashMap<>();
while (parser.nextTag() == XMLStreamReader.START_ELEMENT) {
if ("${Helper.singular($fieldTagName)}".equals(parser.getName())) {
locations.put(Integer.valueOf(locations.size()), new InputLocation(parser.getLocation().getLineNumber(), parser.getLocation().getColumnNumber(), source));
${field.name}.add(interpolatedTrimmed(nextText(parser, strict), "${fieldTagName}"));
} else {
checkUnknownElement(parser, strict);
}
}
${classLcapName}.${field.name}(${field.name});
break;
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> ${field.name} = new LinkedHashMap<>();
locations = new HashMap<>();
while (parser.nextTag() == XMLStreamReader.START_ELEMENT) {
String key = parser.getLocalName();
String value = nextText(parser, strict).trim();
locations.put(key, new InputLocation(parser.getLocation().getLineNumber(), parser.getLocation().getColumnNumber(), source));
${field.name}.put(key, value);
}
${classLcapName}.${field.name}(${field.name});
break;
#elseif ( $field.to && $field.multiplicity == "1" )
${classLcapName}.${field.name}(parse${field.toClass.name}(parser, strict, source ));
break;
#elseif ( $field.to && $field.multiplicity == "*" )
List<$field.to> ${field.name} = new ArrayList<>();
while (parser.nextTag() == XMLStreamReader.START_ELEMENT) {
if ("${Helper.singular($fieldTagName)}".equals(parser.getName())) {
${field.name}.add(parse${field.toClass.name}(parser, strict, source));
} else {
checkUnknownElement(parser, strict);
}
}
${classLcapName}.${field.name}(${field.name});
break;
#else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
break;
#end
}
#set( $ift = "else if" )
#end
#end
default: {
checkUnknownElement(parser, strict);
break;
}
}
${classLcapName}.location(childName, new InputLocation(line, column, source, locations));
}
#if ( $class == $root )
${classLcapName}.modelEncoding(parser.getEncoding());
#end
return ${classLcapName}.build();
}
#end
#end
private String unalias(String tagName) {
switch (tagName) {
#set( $aliases = { } )
#foreach( $class in $model.allClasses )
#foreach ( $field in $class.allFields )
#if ( $field.alias )
#set ( $dummy = $aliases.put( $field.alias, $field.name ) )
#end
#end
#end
#foreach( $entry in $aliases.entrySet() )
case "${entry.key}":
return "${entry.value}";
#end
default:
return tagName;
}
}
/**
* Method checkUnknownAttribute.
*
* @param parser a parser object.
* @param strict a strict object.
* @param tagName a tagName object.
* @param attribute a attribute object.
* @throws XMLStreamException XMLStreamException if
* any.
* @throws IOException IOException if any.
*/
private void checkUnknownAttribute(XMLStreamReader parser, String attribute, String tagName, boolean strict) throws XMLStreamException, IOException {
// strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
if (strict) {
throw new XMLStreamException("Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser.getLocation(), null);
}
} //-- void checkUnknownAttribute(XMLStreamReader, String, String, boolean)
/**
* Method checkUnknownElement.
*
* @param parser a parser object.
* @param strict a strict object.
* @throws XMLStreamException XMLStreamException if
* any.
* @throws IOException IOException if any.
*/
private void checkUnknownElement(XMLStreamReader parser, boolean strict) throws XMLStreamException, IOException {
if (strict) {
throw new XMLStreamException("Unrecognised tag: '" + parser.getName() + "'", parser.getLocation(), null);
}
for (int unrecognizedTagCount = 1; unrecognizedTagCount > 0;) {
int eventType = parser.next();
if (eventType == XMLStreamReader.START_ELEMENT) {
unrecognizedTagCount++;
} else if (eventType == XMLStreamReader.END_ELEMENT) {
unrecognizedTagCount--;
}
}
} //-- void checkUnknownElement(XMLStreamReader, boolean)
/**
* Method getTrimmedValue.
*
* @param s a s object.
* @return String
*/
private String getTrimmedValue(String s) {
if (s != null) {
s = s.trim();
}
return s;
} //-- String getTrimmedValue(String)
/**
* Method interpolatedTrimmed.
*
* @param value a value object.
* @param context a context object.
* @return String
*/
private String interpolatedTrimmed(String value, String context) {
return getTrimmedValue(contentTransformer.transform(value, context));
} //-- String interpolatedTrimmed(String, String)
/**
* Method nextTag.
*
* @param parser a parser object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return int
*/
private int nextTag(XMLStreamReader parser) throws IOException, XMLStreamException {
while (true) {
int next = parser.next();
switch (next) {
case XMLStreamReader.SPACE:
case XMLStreamReader.COMMENT:
case XMLStreamReader.PROCESSING_INSTRUCTION:
case XMLStreamReader.CDATA:
case XMLStreamReader.CHARACTERS:
continue;
case XMLStreamReader.START_ELEMENT:
case XMLStreamReader.END_ELEMENT:
return next;
}
}
} //-- int nextTag(XMLStreamReader)
#foreach ( $class in $model.allClasses )
#foreach ( $field in $class.getFields($version) )
#if ( $field.type == "boolean" || $field.type == "Boolean" )
#set ( $hasBooleanField = true )
#elseif ( $field.type == "int" || $field.type == "Integer" )
#set ( $hasIntegerField = true )
#end
#end
#end
#if ( $hasBooleanField )
/**
* Method getBooleanValue.
*
* @param s a s object.
* @param defaultValue a defaultValue object.
* @param parser a parser object.
* @param attribute a attribute object.
* @throws XMLStreamException XMLStreamException if
* any.
* @return boolean
*/
private boolean getBooleanValue(String s, String attribute, XMLStreamReader parser, boolean defaultValue) throws XMLStreamException {
if (s != null && s.length() != 0) {
return Boolean.valueOf(s).booleanValue();
}
return defaultValue;
} //-- boolean getBooleanValue(String, String, XMLStreamReader, String)
#end
#if ( $hasIntegerField )
/**
* Method getIntegerValue.
*
* @param s a s object.
* @param strict a strict object.
* @param parser a parser object.
* @param attribute a attribute object.
* @throws XMLStreamException XMLStreamException if
* any.
* @return int
*/
private int getIntegerValue(String s, String attribute, XMLStreamReader parser, boolean strict, int defaultValue) throws XMLStreamException {
if (s != null) {
try {
return Integer.valueOf(s).intValue();
} catch (NumberFormatException nfe) {
if (strict) {
throw new XMLStreamException("Unable to parse element '" + attribute + "', must be an integer", parser.getLocation(), nfe);
}
}
}
return defaultValue;
} //-- int getIntegerValue(String, String, XMLStreamReader, boolean)
#end
public static interface ContentTransformer {
/**
* Interpolate the value read from the xpp3 document
* @param source The source value
* @param fieldName A description of the field being interpolated. The implementation may use this to
* log stuff.
* @return The interpolated value.
*/
String transform(String source, String fieldName);
}
private String nextText(XMLStreamReader parser, boolean strict) throws XMLStreamException {
int eventType = parser.getEventType();
if (eventType != XMLStreamReader.START_ELEMENT) {
throw new XMLStreamException("parser must be on START_ELEMENT to read next text", parser.getLocation(), null);
}
eventType = parser.next();
StringBuilder result = new StringBuilder();
while (true) {
if (eventType == XMLStreamReader.CHARACTERS || eventType == XMLStreamReader.CDATA) {
result.append(parser.getText());
} else if (eventType == XMLStreamReader.ENTITY_REFERENCE) {
String val = null;
if (strict) {
throw new XMLStreamException("Entities are not supported in strict mode", parser.getLocation(), null);
} else if (addDefaultEntities) {
val = DEFAULT_ENTITIES.get(parser.getLocalName());
}
if (val != null) {
result.append(val);
} else {
result.append("&").append(parser.getLocalName()).append(";");
}
} else if (eventType != XMLStreamReader.COMMENT) {
break;
}
eventType = parser.next();
}
if (eventType != XMLStreamReader.END_ELEMENT) {
throw new XMLStreamException(
"TEXT must be immediately followed by END_ELEMENT and not " + eventType /*TODO: TYPES[eventType]*/, parser.getLocation(), null);
}
return result.toString();
}
}

View File

@ -1,818 +0,0 @@
#*
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.
*#
#parse ( "common.vm" )
#
#if( ${packageToolV4Xpp3} )
#set ( $package = "${packageToolV4Xpp3}" )
#else
#set ( $package = "${packageToolV4}" )
#end
#set ( $className = "${model.name}Xpp3Reader" )
#
#set ( $root = $model.getClass( $model.getRoot($version), $version ) )
#set ( $rootXml = $Helper.xmlClassMetadata( $root ) )
#set ( $rootTag = $rootXml.tagName )
#set ( $rootUcapName = $Helper.capitalise( $root.name ) )
#set ( $rootLcapName = $Helper.uncapitalise( $root.name ) )
#
#MODELLO-VELOCITY#SAVE-OUTPUT-TO ${package.replace('.','/')}/${className}.java
// =================== DO NOT EDIT THIS FILE ====================
// Generated by Modello Velocity from ${template}
// template, any modifications will be overwritten.
// ==============================================================
package ${package};
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Collections;
import org.apache.maven.api.annotations.Generated;
import org.apache.maven.internal.xml.XmlNodeBuilder;
#foreach ( $class in $model.allClasses )
import ${packageModelV4}.${class.name};
#end
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;
@Deprecated
@Generated
public class ${className} {
private boolean addDefaultEntities = true;
private final ContentTransformer contentTransformer;
/**
* XSI namespace
*/
private static final String XSI_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance";
private static final Map<String, String> DEFAULT_ENTITIES;
static {
Map<String, String> entities = new HashMap<>();
entities.put("nbsp", "\u00a0");
entities.put("iexcl", "\u00a1");
entities.put("cent", "\u00a2");
entities.put("pound", "\u00a3");
entities.put("curren", "\u00a4");
entities.put("yen", "\u00a5");
entities.put("brvbar", "\u00a6");
entities.put("sect", "\u00a7");
entities.put("uml", "\u00a8");
entities.put("copy", "\u00a9");
entities.put("ordf", "\u00aa");
entities.put("laquo", "\u00ab");
entities.put("not", "\u00ac");
entities.put("shy", "\u00ad");
entities.put("reg", "\u00ae");
entities.put("macr", "\u00af");
entities.put("deg", "\u00b0");
entities.put("plusmn", "\u00b1");
entities.put("sup2", "\u00b2");
entities.put("sup3", "\u00b3");
entities.put("acute", "\u00b4");
entities.put("micro", "\u00b5");
entities.put("para", "\u00b6");
entities.put("middot", "\u00b7");
entities.put("cedil", "\u00b8");
entities.put("sup1", "\u00b9");
entities.put("ordm", "\u00ba");
entities.put("raquo", "\u00bb");
entities.put("frac14", "\u00bc");
entities.put("frac12", "\u00bd");
entities.put("frac34", "\u00be");
entities.put("iquest", "\u00bf");
entities.put("Agrave", "\u00c0");
entities.put("Aacute", "\u00c1");
entities.put("Acirc", "\u00c2");
entities.put("Atilde", "\u00c3");
entities.put("Auml", "\u00c4");
entities.put("Aring", "\u00c5");
entities.put("AElig", "\u00c6");
entities.put("Ccedil", "\u00c7");
entities.put("Egrave", "\u00c8");
entities.put("Eacute", "\u00c9");
entities.put("Ecirc", "\u00ca");
entities.put("Euml", "\u00cb");
entities.put("Igrave", "\u00cc");
entities.put("Iacute", "\u00cd");
entities.put("Icirc", "\u00ce");
entities.put("Iuml", "\u00cf");
entities.put("ETH", "\u00d0");
entities.put("Ntilde", "\u00d1");
entities.put("Ograve", "\u00d2");
entities.put("Oacute", "\u00d3");
entities.put("Ocirc", "\u00d4");
entities.put("Otilde", "\u00d5");
entities.put("Ouml", "\u00d6");
entities.put("times", "\u00d7");
entities.put("Oslash", "\u00d8");
entities.put("Ugrave", "\u00d9");
entities.put("Uacute", "\u00da");
entities.put("Ucirc", "\u00db");
entities.put("Uuml", "\u00dc");
entities.put("Yacute", "\u00dd");
entities.put("THORN", "\u00de");
entities.put("szlig", "\u00df");
entities.put("agrave", "\u00e0");
entities.put("aacute", "\u00e1");
entities.put("acirc", "\u00e2");
entities.put("atilde", "\u00e3");
entities.put("auml", "\u00e4");
entities.put("aring", "\u00e5");
entities.put("aelig", "\u00e6");
entities.put("ccedil", "\u00e7");
entities.put("egrave", "\u00e8");
entities.put("eacute", "\u00e9");
entities.put("ecirc", "\u00ea");
entities.put("euml", "\u00eb");
entities.put("igrave", "\u00ec");
entities.put("iacute", "\u00ed");
entities.put("icirc", "\u00ee");
entities.put("iuml", "\u00ef");
entities.put("eth", "\u00f0");
entities.put("ntilde", "\u00f1");
entities.put("ograve", "\u00f2");
entities.put("oacute", "\u00f3");
entities.put("ocirc", "\u00f4");
entities.put("otilde", "\u00f5");
entities.put("ouml", "\u00f6");
entities.put("divide", "\u00f7");
entities.put("oslash", "\u00f8");
entities.put("ugrave", "\u00f9");
entities.put("uacute", "\u00fa");
entities.put("ucirc", "\u00fb");
entities.put("uuml", "\u00fc");
entities.put("yacute", "\u00fd");
entities.put("thorn", "\u00fe");
entities.put("yuml", "\u00ff");
// ----------------------------------------------------------------------
// Special entities
// ----------------------------------------------------------------------
entities.put("OElig", "\u0152");
entities.put("oelig", "\u0153");
entities.put("Scaron", "\u0160");
entities.put("scaron", "\u0161");
entities.put("Yuml", "\u0178");
entities.put("circ", "\u02c6");
entities.put("tilde", "\u02dc");
entities.put("ensp", "\u2002");
entities.put("emsp", "\u2003");
entities.put("thinsp", "\u2009");
entities.put("zwnj", "\u200c");
entities.put("zwj", "\u200d");
entities.put("lrm", "\u200e");
entities.put("rlm", "\u200f");
entities.put("ndash", "\u2013");
entities.put("mdash", "\u2014");
entities.put("lsquo", "\u2018");
entities.put("rsquo", "\u2019");
entities.put("sbquo", "\u201a");
entities.put("ldquo", "\u201c");
entities.put("rdquo", "\u201d");
entities.put("bdquo", "\u201e");
entities.put("dagger", "\u2020");
entities.put("Dagger", "\u2021");
entities.put("permil", "\u2030");
entities.put("lsaquo", "\u2039");
entities.put("rsaquo", "\u203a");
entities.put("euro", "\u20ac");
// ----------------------------------------------------------------------
// Symbol entities
// ----------------------------------------------------------------------
entities.put("fnof", "\u0192");
entities.put("Alpha", "\u0391");
entities.put("Beta", "\u0392");
entities.put("Gamma", "\u0393");
entities.put("Delta", "\u0394");
entities.put("Epsilon", "\u0395");
entities.put("Zeta", "\u0396");
entities.put("Eta", "\u0397");
entities.put("Theta", "\u0398");
entities.put("Iota", "\u0399");
entities.put("Kappa", "\u039a");
entities.put("Lambda", "\u039b");
entities.put("Mu", "\u039c");
entities.put("Nu", "\u039d");
entities.put("Xi", "\u039e");
entities.put("Omicron", "\u039f");
entities.put("Pi", "\u03a0");
entities.put("Rho", "\u03a1");
entities.put("Sigma", "\u03a3");
entities.put("Tau", "\u03a4");
entities.put("Upsilon", "\u03a5");
entities.put("Phi", "\u03a6");
entities.put("Chi", "\u03a7");
entities.put("Psi", "\u03a8");
entities.put("Omega", "\u03a9");
entities.put("alpha", "\u03b1");
entities.put("beta", "\u03b2");
entities.put("gamma", "\u03b3");
entities.put("delta", "\u03b4");
entities.put("epsilon", "\u03b5");
entities.put("zeta", "\u03b6");
entities.put("eta", "\u03b7");
entities.put("theta", "\u03b8");
entities.put("iota", "\u03b9");
entities.put("kappa", "\u03ba");
entities.put("lambda", "\u03bb");
entities.put("mu", "\u03bc");
entities.put("nu", "\u03bd");
entities.put("xi", "\u03be");
entities.put("omicron", "\u03bf");
entities.put("pi", "\u03c0");
entities.put("rho", "\u03c1");
entities.put("sigmaf", "\u03c2");
entities.put("sigma", "\u03c3");
entities.put("tau", "\u03c4");
entities.put("upsilon", "\u03c5");
entities.put("phi", "\u03c6");
entities.put("chi", "\u03c7");
entities.put("psi", "\u03c8");
entities.put("omega", "\u03c9");
entities.put("thetasym", "\u03d1");
entities.put("upsih", "\u03d2");
entities.put("piv", "\u03d6");
entities.put("bull", "\u2022");
entities.put("hellip", "\u2026");
entities.put("prime", "\u2032");
entities.put("Prime", "\u2033");
entities.put("oline", "\u203e");
entities.put("frasl", "\u2044");
entities.put("weierp", "\u2118");
entities.put("image", "\u2111");
entities.put("real", "\u211c");
entities.put("trade", "\u2122");
entities.put("alefsym", "\u2135");
entities.put("larr", "\u2190");
entities.put("uarr", "\u2191");
entities.put("rarr", "\u2192");
entities.put("darr", "\u2193");
entities.put("harr", "\u2194");
entities.put("crarr", "\u21b5");
entities.put("lArr", "\u21d0");
entities.put("uArr", "\u21d1");
entities.put("rArr", "\u21d2");
entities.put("dArr", "\u21d3");
entities.put("hArr", "\u21d4");
entities.put("forall", "\u2200");
entities.put("part", "\u2202");
entities.put("exist", "\u2203");
entities.put("empty", "\u2205");
entities.put("nabla", "\u2207");
entities.put("isin", "\u2208");
entities.put("notin", "\u2209");
entities.put("ni", "\u220b");
entities.put("prod", "\u220f");
entities.put("sum", "\u2211");
entities.put("minus", "\u2212");
entities.put("lowast", "\u2217");
entities.put("radic", "\u221a");
entities.put("prop", "\u221d");
entities.put("infin", "\u221e");
entities.put("ang", "\u2220");
entities.put("and", "\u2227");
entities.put("or", "\u2228");
entities.put("cap", "\u2229");
entities.put("cup", "\u222a");
entities.put("int", "\u222b");
entities.put("there4", "\u2234");
entities.put("sim", "\u223c");
entities.put("cong", "\u2245");
entities.put("asymp", "\u2248");
entities.put("ne", "\u2260");
entities.put("equiv", "\u2261");
entities.put("le", "\u2264");
entities.put("ge", "\u2265");
entities.put("sub", "\u2282");
entities.put("sup", "\u2283");
entities.put("nsub", "\u2284");
entities.put("sube", "\u2286");
entities.put("supe", "\u2287");
entities.put("oplus", "\u2295");
entities.put("otimes", "\u2297");
entities.put("perp", "\u22a5");
entities.put("sdot", "\u22c5");
entities.put("lceil", "\u2308");
entities.put("rceil", "\u2309");
entities.put("lfloor", "\u230a");
entities.put("rfloor", "\u230b");
entities.put("lang", "\u2329");
entities.put("rang", "\u232a");
entities.put("loz", "\u25ca");
entities.put("spades", "\u2660");
entities.put("clubs", "\u2663");
entities.put("hearts", "\u2665");
entities.put("diams", "\u2666");
DEFAULT_ENTITIES = Collections.unmodifiableMap(entities);
}
public ${className}() {
this((s, f) -> s);
}
public ${className}(ContentTransformer contentTransformer) {
this.contentTransformer = contentTransformer;
}
/**
* Returns the state of the "add default entities" flag.
*
* @return boolean
*/
public boolean getAddDefaultEntities() {
return addDefaultEntities;
} //-- boolean getAddDefaultEntities()
/**
* 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)
/**
*
* @param reader a reader object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return ${root.name}
*/
public ${root.name} 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);
} //-- ${root.name} read(Reader, boolean)
/**
*
* @param reader a reader object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return ${root.name}
*/
public ${root.name} read(Reader reader) 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,true);
} //-- ${root.name} read(Reader)
/**
* Method read.
*
* @param in a in object.
* @param strict a strict object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return ${root.name}
*/
public ${root.name} 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);
} //-- ${root.name} read(InputStream, boolean)
/**
* Method read.
*
* @param in a in object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return ${root.name}
*/
public ${root.name} 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);
} //-- ${root.name} 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 ${root.name}
*/
public ${root.name} read(XMLStreamReader parser, boolean strict) throws IOException, XMLStreamException {
$rootUcapName $rootLcapName = null;
int eventType = parser.getEventType();
boolean parsed = false;
while (eventType != XMLStreamReader.END_DOCUMENT) {
if (eventType == XMLStreamReader.START_ELEMENT) {
if (strict && ! "${rootTag}".equals(parser.getLocalName())) {
throw new XMLStreamException("Expected root element '${rootTag}' but found '" + parser.getLocalName() + "'", parser.getLocation(), null);
}
else if (parsed) {
// fallback, already expected a XMLStreamException due to invalid XML
throw new XMLStreamException("Duplicated tag: '${rootTag}'", parser.getLocation(), null);
}
$rootLcapName = parse${rootUcapName}(parser, strict);
parsed = true;
}
eventType = parser.next();
}
if (parsed) {
return $rootLcapName;
}
throw new XMLStreamException("Expected root element '${rootTag}' but found no element at all: invalid XML document", parser.getLocation(), null);
} //-- ${root.name} read(XMLStreamReader, boolean)
#foreach ( $class in $model.allClasses )
#if ( $class.name != "InputSource" && $class.name != "InputLocation" )
#set ( $classUcapName = $Helper.capitalise( $class.name ) )
#set ( $classLcapName = $Helper.uncapitalise( $class.name ) )
#set ( $ancestors = $Helper.ancestors( $class ) )
#set ( $allFields = $Helper.xmlFields( $class ) )
private ${classUcapName} parse${classUcapName}(XMLStreamReader parser, boolean strict) throws IOException, XMLStreamException {
String tagName = parser.getLocalName();
${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder(true);
for (int i = parser.getAttributeCount() - 1; i >= 0; i--) {
String name = parser.getAttributeLocalName(i);
String value = parser.getAttributeNamespace(i);
if (XSI_NAMESPACE.equals(value)) {
// just ignore attributes with non-default namespace (for example: xmlns:xsi)
}
#if ( $class == $root )
else if ("xmlns".equals(name)) {
// ignore xmlns attribute in root class, which is a reserved attribute name
}
#end
#foreach ( $field in $allFields )
#if ( $Helper.xmlFieldMetadata( $field ).attribute )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#set ( $fieldCapName = $Helper.capitalise( $field.name ) )
else if ("$fieldTagName".equals(name)) {
#if ( $field.type == "String" )
${classLcapName}.${field.name}(interpolatedTrimmed(value, "$fieldTagName"));
#elseif ( $field.type == "boolean" || $field.type == "Boolean" )
${classLcapName}.${field.name}(getBooleanValue(interpolatedTrimmed(value, "$fieldTagName"), "$fieldTagName", parser, ${field.defaultValue}));
#else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
#end
}
#end
#end
else {
checkUnknownAttribute(parser, name, tagName, strict);
}
}
Set<String> parsed = new HashSet<>();
while ((strict ? parser.nextTag() : nextTag(parser)) == XMLStreamReader.START_ELEMENT) {
String childName = unalias(parser.getLocalName());
if (!parsed.add(childName)) {
throw new XMLStreamException("Duplicated tag: '" + childName + "'", parser.getLocation(), null);
}
switch (childName) {
#set( $ift = "if" )
#foreach ( $field in $allFields )
#if ( ! $Helper.xmlFieldMetadata( $field ).attribute && ! $Helper.xmlFieldMetadata( $field ).transient )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#if ( ! $fieldTagName )
#set ( $fieldTagName = $field.name )
#end
#set ( $fieldCapName = $Helper.capitalise( $field.name ) )
case "${fieldTagName}": {
#if ( $field.type == "String" )
${classLcapName}.${field.name}(interpolatedTrimmed(nextText(parser, strict), "${fieldTagName}"));
break;
#elseif ( $field.type == "boolean" || $field.type == "Boolean" )
${classLcapName}.${field.name}(getBooleanValue(interpolatedTrimmed(nextText(parser, strict), "${fieldTagName}"), "${fieldTagName}", parser, ${field.defaultValue}));
break;
#elseif ( $field.type == "int" )
${classLcapName}.${field.name}(getIntegerValue(interpolatedTrimmed(nextText(parser, strict), "${fieldTagName}"), "${fieldTagName}", parser, strict, ${field.defaultValue}));
break;
#elseif ( $field.type == "DOM" )
${classLcapName}.${field.name}(XmlNodeBuilder.build(parser));
break;
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
List<String> ${field.name} = new ArrayList<>();
while (parser.nextTag() == XMLStreamReader.START_ELEMENT) {
if ("${Helper.singular($fieldTagName)}".equals(parser.getLocalName())) {
${field.name}.add(interpolatedTrimmed(nextText(parser, strict), "${fieldTagName}"));
}
else {
checkUnknownElement(parser, strict);
}
}
${classLcapName}.${field.name}(${field.name});
break;
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> ${field.name} = new LinkedHashMap<>();
while (parser.nextTag() == XMLStreamReader.START_ELEMENT) {
String key = parser.getLocalName();
String value = nextText(parser, strict).trim();
${field.name}.put(key, value);
}
${classLcapName}.${field.name}(${field.name});
break;
#elseif ( $field.to && $field.multiplicity == "1" )
${classLcapName}.${field.name}(parse${field.toClass.name}(parser, strict));
break;
#elseif ( $field.to && $field.multiplicity == "*" )
List<$field.to> ${field.name} = new ArrayList<>();
while (parser.nextTag() == XMLStreamReader.START_ELEMENT) {
if ("${Helper.singular($fieldTagName)}".equals(parser.getLocalName())) {
${field.name}.add(parse${field.toClass.name}(parser, strict));
}
else {
checkUnknownElement(parser, strict);
}
}
${classLcapName}.${field.name}(${field.name});
break;
#else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
break;
#end
}
#set( $ift = "else if" )
#end
#end
default: {
checkUnknownElement(parser, strict);
break;
}
}
}
#if ( $class == $root )
${classLcapName}.modelEncoding(parser.getEncoding());
#end
return ${classLcapName}.build();
}
#end
#end
private String unalias(String tagName) {
switch (tagName) {
#set( $aliases = { } )
#foreach( $class in $model.allClasses )
#foreach ( $field in $class.allFields )
#if ( $field.alias )
#set ( $dummy = $aliases.put( $field.alias, $field.name ) )
#end
#end
#end
#foreach( $entry in $aliases.entrySet() )
case "${entry.key}":
return "${entry.value}";
#end
default:
return tagName;
}
}
/**
* Method checkUnknownAttribute.
*
* @param parser a parser object.
* @param strict a strict object.
* @param tagName a tagName object.
* @param attribute a attribute object.
* @throws XMLStreamException XMLStreamException if
* any.
* @throws IOException IOException if any.
*/
private void checkUnknownAttribute(XMLStreamReader parser, String attribute, String tagName, boolean strict) throws XMLStreamException, IOException {
// strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
if (strict) {
throw new XMLStreamException("Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser.getLocation(), null);
}
} //-- void checkUnknownAttribute(XMLStreamReader, String, String, boolean)
/**
* Method checkUnknownElement.
*
* @param parser a parser object.
* @param strict a strict object.
* @throws XMLStreamException XMLStreamException if
* any.
* @throws IOException IOException if any.
*/
private void checkUnknownElement(XMLStreamReader parser, boolean strict) throws XMLStreamException, IOException {
if (strict) {
throw new XMLStreamException("Unrecognised tag: '" + parser.getLocalName() + "'", parser.getLocation(), null);
}
for (int unrecognizedTagCount = 1; unrecognizedTagCount > 0;) {
int eventType = parser.next();
if (eventType == XMLStreamReader.START_ELEMENT) {
unrecognizedTagCount++;
}
else if (eventType == XMLStreamReader.END_ELEMENT) {
unrecognizedTagCount--;
}
}
} //-- void checkUnknownElement(XMLStreamReader, boolean)
/**
* Method getTrimmedValue.
*
* @param s a s object.
* @return String
*/
private String getTrimmedValue(String s) {
if (s != null) {
s = s.trim();
}
return s;
} //-- String getTrimmedValue(String)
/**
* Method interpolatedTrimmed.
*
* @param value a value object.
* @param context a context object.
* @return String
*/
private String interpolatedTrimmed(String value, String context) {
return getTrimmedValue(contentTransformer.transform(value, context));
} //-- String interpolatedTrimmed(String, String)
/**
* Method nextTag.
*
* @param parser a parser object.
* @throws IOException IOException if any.
* @throws XMLStreamException XMLStreamException if
* any.
* @return int
*/
private int nextTag(XMLStreamReader parser) throws IOException, XMLStreamException {
while (true) {
int next = parser.next();
switch (next) {
case XMLStreamReader.SPACE:
case XMLStreamReader.COMMENT:
case XMLStreamReader.PROCESSING_INSTRUCTION:
case XMLStreamReader.CDATA:
case XMLStreamReader.CHARACTERS:
continue;
case XMLStreamReader.START_ELEMENT:
case XMLStreamReader.END_ELEMENT:
return next;
}
}
} //-- int nextTag(XMLStreamReader)
#foreach ( $class in $model.allClasses )
#foreach ( $field in $class.getFields($version) )
#if ( $field.type == "boolean" || $field.type == "Boolean" )
#set ( $hasBooleanField = true )
#elseif ( $field.type == "int" || $field.type == "Integer" )
#set ( $hasIntegerField = true )
#end
#end
#end
#if ( $hasBooleanField )
/**
* Method getBooleanValue.
*
* @param s a s object.
* @param defaultValue a defaultValue object.
* @param parser a parser object.
* @param attribute a attribute object.
* @throws XMLStreamException XMLStreamException if
* any.
* @return boolean
*/
private boolean getBooleanValue(String s, String attribute, XMLStreamReader parser, boolean defaultValue) throws XMLStreamException {
if (s != null && s.length() != 0) {
return Boolean.valueOf(s).booleanValue();
}
return defaultValue;
} //-- boolean getBooleanValue(String, String, XMLStreamReader, String)
#end
#if ( $hasIntegerField )
/**
* Method getIntegerValue.
*
* @param s a s object.
* @param strict a strict object.
* @param parser a parser object.
* @param attribute a attribute object.
* @throws XMLStreamException XMLStreamException if
* any.
* @return int
*/
private int getIntegerValue(String s, String attribute, XMLStreamReader parser, boolean strict, int defaultValue) throws XMLStreamException {
if (s != null) {
try {
return Integer.valueOf(s).intValue();
}
catch (NumberFormatException nfe) {
if (strict) {
throw new XMLStreamException("Unable to parse element '" + attribute + "', must be an integer", parser.getLocation(), nfe);
}
}
}
return defaultValue;
} //-- int getIntegerValue(String, String, XMLStreamReader, boolean)
#end
public static interface ContentTransformer {
/**
* Interpolate the value read from the xpp3 document
* @param source The source value
* @param fieldName A description of the field being interpolated. The implementation may use this to
* log stuff.
* @return The interpolated value.
*/
String transform(String source, String fieldName);
}
private String nextText(XMLStreamReader parser, boolean strict) throws XMLStreamException {
int eventType = parser.getEventType();
if (eventType != XMLStreamReader.START_ELEMENT) {
throw new XMLStreamException("parser must be on START_ELEMENT to read next text", parser.getLocation(), null);
}
eventType = parser.next();
StringBuilder result = new StringBuilder();
while (true) {
if (eventType == XMLStreamReader.CHARACTERS || eventType == XMLStreamReader.CDATA) {
result.append(parser.getText());
} else if (eventType == XMLStreamReader.ENTITY_REFERENCE) {
String val = null;
if (strict) {
throw new XMLStreamException("Entities are not supported in strict mode", parser.getLocation(), null);
} else if (addDefaultEntities) {
val = DEFAULT_ENTITIES.get(parser.getLocalName());
}
if (val != null) {
result.append(val);
} else {
result.append("&").append(parser.getLocalName()).append(";");
}
} else if (eventType != XMLStreamReader.COMMENT) {
break;
}
eventType = parser.next();
}
if (eventType != XMLStreamReader.END_ELEMENT) {
throw new XMLStreamException(
"TEXT must be immediately followed by END_ELEMENT and not " + eventType /*TODO: TYPES[eventType]*/, parser.getLocation(), null);
}
return result.toString();
}
}

View File

@ -1,340 +0,0 @@
#*
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.
*#
#parse ( "common.vm" )
#
#if( ${packageToolV4Xpp3} )
#set ( $package = "${packageToolV4Xpp3}" )
#else
#set ( $package = "${packageToolV4}" )
#end
#set ( $className = "${model.name}Xpp3WriterEx" )
#
#set ( $root = $model.getClass( $model.getRoot($version), $version ) )
#set ( $rootXml = $Helper.xmlClassMetadata( $root ) )
#set ( $rootTag = $rootXml.tagName )
#set ( $rootUcapName = $Helper.capitalise( $root.name ) )
#set ( $rootLcapName = $Helper.uncapitalise( $root.name ) )
#
#MODELLO-VELOCITY#SAVE-OUTPUT-TO ${package.replace('.','/')}/${className}.java
// =================== DO NOT EDIT THIS FILE ====================
// Generated by Modello Velocity from ${template}
// template, any modifications will be overwritten.
// ==============================================================
package ${package};
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import org.apache.maven.api.annotations.Generated;
import ${packageModelV4}.InputLocation;
import ${packageModelV4}.InputLocationTracker;
import org.apache.maven.api.xml.XmlNode;
import org.apache.maven.internal.xml.XmlNodeBuilder;
#foreach ( $class in $model.allClasses )
#if ( $class.name != "InputLocation" )
import ${packageModelV4}.${class.name};
#end
#end
import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
import org.codehaus.plexus.util.xml.pull.MXParser;
import org.codehaus.plexus.util.xml.pull.MXSerializer;
import org.codehaus.plexus.util.xml.pull.XmlPullParser;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.codehaus.plexus.util.xml.pull.XmlSerializer;
@Deprecated
@Generated
public class ${className} {
//--------------------------/
//- Class/Member Variables -/
//--------------------------/
/**
* Field NAMESPACE.
*/
private static final String NAMESPACE = null;
/**
* Field fileComment.
*/
private String fileComment = null;
/**
* Field stringFormatter.
*/
protected InputLocation.StringFormatter stringFormatter;
//-----------/
//- Methods -/
//-----------/
/**
* Method setFileComment.
*
* @param fileComment a fileComment object.
*/
public void setFileComment(String fileComment) {
this.fileComment = fileComment;
} //-- void setFileComment(String)
/**
* Method setStringFormatter.
*
* @param stringFormatter
*/
public void setStringFormatter(InputLocation.StringFormatter stringFormatter) {
this.stringFormatter = stringFormatter;
} //-- void setStringFormatter(InputLocation.StringFormatter)
/**
* Method write.
*
* @param writer a writer object
* @param ${rootLcapName} a ${root.name} object
* @throws java.io.IOException java.io.IOException if any
*/
public void write(Writer writer, ${root.name} ${rootLcapName}) throws java.io.IOException {
XmlSerializer serializer = new MXSerializer();
serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " ");
serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n");
serializer.setOutput(writer);
serializer.startDocument(${rootLcapName}.getModelEncoding(), null);
write${root.name}("$rootTag", ${rootLcapName}, serializer);
serializer.endDocument();
} //-- void write(Writer, ${root.name})
/**
* Method write.
*
* @param stream a stream object
* @param ${rootLcapName} a ${root.name} object
* @throws java.io.IOException java.io.IOException if any
*/
public void write(OutputStream stream, ${root.name} ${rootLcapName}) throws java.io.IOException {
XmlSerializer serializer = new MXSerializer();
serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " ");
serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n");
serializer.setOutput(stream, ${rootLcapName}.getModelEncoding());
serializer.startDocument(${rootLcapName}.getModelEncoding(), null);
write${root.name}("$rootTag", ${rootLcapName}, serializer);
serializer.endDocument();
} //-- void write(OutputStream, ${root.name})
#foreach ( $class in $model.allClasses )
#if ( $class.name != "InputSource" && $class.name != "InputLocation" )
#set ( $classUcapName = $Helper.capitalise( $class.name ) )
#set ( $classLcapName = $Helper.uncapitalise( $class.name ) )
#set ( $allFields = $Helper.xmlFields( $class ) )
private void write${classUcapName}(String tagName, ${classUcapName} ${classLcapName}, XmlSerializer serializer)
throws IOException {
if (${classLcapName} != null) {
#if ( $class == $root )
if (this.fileComment != null) {
serializer.comment(this.fileComment);
}
serializer.setPrefix("", "http://maven.apache.org/POM/4.0.0");
serializer.setPrefix("xsi", "http://www.w3.org/2001/XMLSchema-instance");
serializer.startTag(NAMESPACE, tagName);
serializer.attribute("", "xsi:schemaLocation", "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd");
#else
serializer.startTag(NAMESPACE, tagName);
#end
#foreach ( $field in $allFields )
#if ( $Helper.xmlFieldMetadata( $field ).attribute )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#set ( $fieldCapName = $Helper.capitalise( $field.name ) )
#if ( $field.type == "String" )
writeAttr("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer);
#elseif ( $field.type == "boolean" )
#set ( $def = ${field.defaultValue} )
#if ( ${def} == "true" )
writeAttr("$fieldTagName", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer);
#else
writeAttr("$fieldTagName", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer);
#end
#else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
#end
#end
#end
#foreach ( $field in $allFields )
#if ( ! $Helper.xmlFieldMetadata( $field ).attribute && ! $Helper.xmlFieldMetadata( $field ).transient )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#if ( ! $fieldTagName )
#set ( $fieldTagName = $field.name )
#end
#set ( $fieldCapName = $Helper.capitalise( $field.name ) )
#set ( $def = ${field.defaultValue} )
#if ( $field.type == "String" )
#if ( ! $def )
writeTag("$fieldTagName", null, ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName});
#else
writeTag("$fieldTagName", "${def}", ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName});
#end
#elseif ( $field.type == "boolean" || $field.type == "Boolean" )
#if ( ${def} == "true" )
writeTag("$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer, ${classLcapName});
#else
writeTag("$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer, ${classLcapName});
#end
#elseif ( $field.type == "int" )
writeTag("$fieldTagName", "${def}", Integer.toString(${classLcapName}.get${fieldCapName}()), serializer, ${classLcapName});
#elseif ( $field.type == "DOM" )
writeDom(${classLcapName}.get${fieldCapName}(), serializer);
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
#set( $singularField = ${Helper.singular($fieldTagName)} )
writeList("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName},
t -> writeTag("$singularField", null, t, serializer, null));
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
writeProperties("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName});
#elseif ( $field.to && $field.multiplicity == "1" )
write${field.to}("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer);
#elseif ( $field.to && $field.multiplicity == "*" )
#set( $singularField = ${Helper.singular($fieldTagName)} )
writeList("$fieldTagName", $Helper.isFlatItems($field), ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName},
t -> write${field.to}("$singularField", t, serializer));
#else
// TODO: name=${field.name} type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
#end
#end
#end
serializer.endTag(NAMESPACE, tagName);
#if ( $field.to && $field.multiplicity == "1" )
writeLocationTracking(${classLcapName}, "", serializer);
#end
}
}
#end
#end
@FunctionalInterface
private interface ElementWriter<T> {
public void write(T t) throws IOException;
}
private <T> void writeList(String tagName, List<T> list, XmlSerializer serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException {
writeList(tagName, false, list, serializer, locationTracker, writer);
}
private <T> void writeList(String tagName, boolean flat, List<T> list, XmlSerializer serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException {
if (list != null && !list.isEmpty()) {
if (!flat) {
serializer.startTag(NAMESPACE, tagName);
}
int index = 0;
InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null;
for (T t : list) {
writer.write(t);
writeLocationTracking(location, Integer.valueOf(index++), serializer);
}
if (!flat) {
serializer.endTag(NAMESPACE, tagName);
}
}
}
private <T> void writeProperties(String tagName, Map<String, String> props, XmlSerializer serializer, InputLocationTracker locationTracker) throws IOException {
if (props != null && !props.isEmpty()) {
serializer.startTag(NAMESPACE, tagName);
InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null;
for (Map.Entry<String, String> entry : props.entrySet()) {
String key = entry.getKey();
writeTag(key, null, entry.getValue(), serializer, null);
writeLocationTracking(location, key, serializer);
}
serializer.endTag(NAMESPACE, tagName);
writeLocationTracking(locationTracker, tagName, serializer);
}
}
private void writeDom(XmlNode dom, XmlSerializer serializer) throws IOException {
if (dom != null) {
serializer.startTag(NAMESPACE, dom.getName());
for (Map.Entry<String, String> attr : dom.getAttributes().entrySet()) {
serializer.attribute(NAMESPACE, attr.getKey(), attr.getValue());
}
for (XmlNode child : dom.getChildren()) {
writeDom(child, serializer);
}
String value = dom.getValue();
if (value != null) {
serializer.text(value);
}
serializer.endTag(NAMESPACE, dom.getName());
#if ( $locationTracking )
if (dom.getInputLocation() instanceof InputLocation && dom.getChildren().isEmpty()) {
serializer.comment(toString((InputLocation) dom.getInputLocation()));
}
#end
}
}
private void writeTag(String tagName, String defaultValue, String value, XmlSerializer serializer, InputLocationTracker locationTracker) throws IOException {
if (value != null && !Objects.equals(defaultValue, value)) {
serializer.startTag(NAMESPACE, tagName).text(value).endTag(NAMESPACE, tagName);
writeLocationTracking(locationTracker, tagName, serializer);
}
}
private void writeAttr(String attrName, String value, XmlSerializer serializer) throws IOException {
if (value != null) {
serializer.attribute(NAMESPACE, attrName, value);
}
}
/**
* Method writeLocationTracking.
*
* @param locationTracker
* @param serializer
* @param key
* @throws java.io.IOException
*/
protected void writeLocationTracking(InputLocationTracker locationTracker, Object key, XmlSerializer serializer) throws java.io.IOException {
InputLocation location = (locationTracker == null) ? null : locationTracker.getLocation(key);
if (location != null) {
serializer.comment(toString(location));
}
} //-- void writeLocationTracking(InputLocationTracker, Object, XmlSerializer)
/**
* Method toString.
*
* @param location
* @return String
*/
protected String toString(InputLocation location) {
if (stringFormatter != null) {
return stringFormatter.toString(location);
}
return ' ' + location.getSource().toString() + ':' + location.getLineNumber() + ' ';
} //-- String toString(InputLocation)
}

View File

@ -433,7 +433,11 @@ public class ${className} {
if (stringFormatter != null) {
return stringFormatter.toString(location);
}
if (location.getSource() != null) {
return ' ' + location.getSource().toString() + ':' + location.getLineNumber() + ' ';
} else {
return " " + location.getLineNumber() + " ";
}
} //-- String toString(InputLocation)
#end