blob: fd66eed8a3279fb02542df805120ec019f7aff5f [file] [log] [blame]
#*
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" )
#
#set ( $package = "${packageToolV4}" )
#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.ReaderFactory;
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;
@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})
/**
* Method writeDomToSerializer.
*
* @param dom a dom object.
* @param serializer a serializer object.
* @throws java.io.IOException java.io.IOException if any.
*/
protected void writeDomToSerializer(org.apache.maven.api.xml.XmlNode dom, XmlSerializer serializer) throws java.io.IOException {
serializer.startTag(NAMESPACE, dom.getName());
for (Map.Entry<String, String> attribute : dom.getAttributes().entrySet()) {
serializer.attribute(NAMESPACE, attribute.getKey(), attribute.getValue());
}
for (XmlNode aChild : dom.getChildren()) {
writeDomToSerializer(aChild, serializer);
}
String value = dom.getValue();
if (value != null) {
serializer.text(value);
}
serializer.endTag(NAMESPACE, dom.getName());
} //-- void writeDomToSerializer(org.apache.maven.api.xml.XmlNode, XmlSerializer)
#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);
writeLocationTracking(${classLcapName}, "", serializer);
}
}
#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);
writeLocationTracking(locationTracker, tagName, serializer);
}
}
}
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());
}
}
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)
}