| #* |
| 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}StaxWriter" ) |
| # |
| #set ( $root = $model.getClass( $model.getRoot($version), $version ) ) |
| #set ( $xmlModelMetadata = $model.getMetadata( "org.codehaus.modello.plugins.xml.metadata.XmlModelMetadata" ) ) |
| #if ( $forcedIOModelVersion ) |
| #set ( $Version = $model.class.classLoader.loadClass( "org.codehaus.modello.model.Version" ) ) |
| #set ( $ioVersion = $Version.getConstructor( $package.class ).newInstance( $forcedIOModelVersion ) ) |
| #else |
| #set ( $ioVersion = $version ) |
| #end |
| #set ( $namespace = $xmlModelMetadata.getNamespace($ioVersion) ) |
| #set ( $schemaLocation = $xmlModelMetadata.getSchemaLocation($ioVersion) ) |
| #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 javax.xml.stream.XMLOutputFactory; |
| import javax.xml.stream.XMLStreamException; |
| import javax.xml.stream.XMLStreamWriter; |
| import org.apache.maven.api.annotations.Generated; |
| #if ( $locationTracking ) |
| import ${packageModelV4}.InputLocation; |
| import ${packageModelV4}.InputLocationTracker; |
| #end |
| 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.stax2.util.StreamWriterDelegate; |
| |
| import static javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI; |
| |
| @Generated |
| public class ${className} { |
| |
| //--------------------------/ |
| //- Class/Member Variables -/ |
| //--------------------------/ |
| |
| /** |
| * Default namespace. |
| */ |
| private static final String NAMESPACE = "${namespace}"; |
| |
| /** |
| * Default schemaLocation. |
| */ |
| private static final String SCHEMA_LOCATION = "${schemaLocation}"; |
| |
| /** |
| * Field namespace. |
| */ |
| private String namespace = NAMESPACE; |
| |
| /** |
| * Field schemaLocation. |
| */ |
| private String schemaLocation = SCHEMA_LOCATION; |
| |
| /** |
| * Field fileComment. |
| */ |
| private String fileComment = null; |
| |
| #if ( $locationTracking ) |
| private boolean addLocationInformation = true; |
| |
| /** |
| * Field stringFormatter. |
| */ |
| protected InputLocation.StringFormatter stringFormatter; |
| |
| #end |
| //-----------/ |
| //- Methods -/ |
| //-----------/ |
| |
| /** |
| * Method setNamespace. |
| * |
| * @param namespace the namespace to use. |
| */ |
| public void setNamespace(String namespace) { |
| this.namespace = Objects.requireNonNull(namespace); |
| } //-- void setNamespace(String) |
| |
| /** |
| * Method setSchemaLocation. |
| * |
| * @param schemaLocation the schema location to use. |
| */ |
| public void setSchemaLocation(String schemaLocation) { |
| this.schemaLocation = Objects.requireNonNull(schemaLocation); |
| } //-- void setSchemaLocation(String) |
| |
| /** |
| * Method setFileComment. |
| * |
| * @param fileComment a fileComment object. |
| */ |
| public void setFileComment(String fileComment) { |
| this.fileComment = fileComment; |
| } //-- void setFileComment(String) |
| |
| #if ( $locationTracking ) |
| /** |
| * Method setAddLocationInformation. |
| */ |
| public void setAddLocationInformation(boolean addLocationInformation) { |
| this.addLocationInformation = addLocationInformation; |
| } //-- void setAddLocationInformation(String) |
| |
| /** |
| * Method setStringFormatter. |
| * |
| * @param stringFormatter |
| */ |
| public void setStringFormatter(InputLocation.StringFormatter stringFormatter) { |
| this.stringFormatter = stringFormatter; |
| } //-- void setStringFormatter(InputLocation.StringFormatter) |
| |
| #end |
| /** |
| * Method write. |
| * |
| * @param writer a writer object |
| * @param ${rootLcapName} a ${root.name} object |
| * @throws IOException IOException if any |
| */ |
| public void write(Writer writer, ${root.name} ${rootLcapName}) throws IOException, XMLStreamException { |
| XMLOutputFactory factory = new com.ctc.wstx.stax.WstxOutputFactory(); |
| factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false); |
| factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_USE_DOUBLE_QUOTES_IN_XML_DECL, true); |
| factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_ADD_SPACE_AFTER_EMPTY_ELEM, true); |
| XMLStreamWriter serializer = new IndentingXMLStreamWriter(factory.createXMLStreamWriter(writer)); |
| serializer.writeStartDocument(${rootLcapName}.getModelEncoding(), null); |
| write${root.name}("$rootTag", ${rootLcapName}, serializer); |
| serializer.writeEndDocument(); |
| } //-- void write(Writer, ${root.name}) |
| |
| /** |
| * Method write. |
| * |
| * @param stream a stream object |
| * @param ${rootLcapName} a ${root.name} object |
| * @throws IOException IOException if any |
| */ |
| public void write(OutputStream stream, ${root.name} ${rootLcapName}) throws IOException, XMLStreamException { |
| XMLOutputFactory factory = new com.ctc.wstx.stax.WstxOutputFactory(); |
| factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false); |
| factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_USE_DOUBLE_QUOTES_IN_XML_DECL, true); |
| factory.setProperty(com.ctc.wstx.api.WstxOutputProperties.P_ADD_SPACE_AFTER_EMPTY_ELEM, true); |
| XMLStreamWriter serializer = new IndentingXMLStreamWriter(factory.createXMLStreamWriter(stream, ${rootLcapName}.getModelEncoding())); |
| serializer.writeStartDocument(${rootLcapName}.getModelEncoding(), null); |
| write${root.name}("$rootTag", ${rootLcapName}, serializer); |
| serializer.writeEndDocument(); |
| } //-- 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}, XMLStreamWriter serializer) |
| throws IOException, XMLStreamException { |
| if (${classLcapName} != null) { |
| #if ( $class == $root ) |
| if (this.fileComment != null) { |
| serializer.writeCharacters("\n"); |
| serializer.writeComment(this.fileComment); |
| serializer.writeCharacters("\n"); |
| } |
| serializer.writeStartElement("", tagName, namespace); |
| serializer.writeNamespace("", namespace); |
| serializer.writeNamespace("xsi", W3C_XML_SCHEMA_INSTANCE_NS_URI); |
| serializer.writeAttribute(W3C_XML_SCHEMA_INSTANCE_NS_URI, "schemaLocation", namespace + " " + schemaLocation); |
| #else |
| serializer.writeStartElement(namespace, tagName); |
| #end |
| #foreach ( $field in $allFields ) |
| #if ( $Helper.xmlFieldMetadata( $field ).attribute && ! $Helper.xmlFieldMetadata( $field ).format ) |
| #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 |
| && ! $Helper.xmlFieldMetadata( $field ).format ) |
| #set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName ) |
| #if ( ! $fieldTagName ) |
| #set ( $fieldTagName = $field.name ) |
| #end |
| #set ( $fieldCapName = $Helper.capitalise( $field.name ) ) |
| #set ( $def = ${field.defaultValue} ) |
| #if ( $locationTracking ) |
| #set ( $loctrac = ", ${classLcapName}" ) |
| #set ( $loctracnull = ", null" ) |
| #else |
| #set ( $loctrac = "" ) |
| #set ( $loctracnull = "" ) |
| #end |
| #if ( $field.type == "String" ) |
| #if ( ! $def ) |
| writeTag("$fieldTagName", null, ${classLcapName}.get${fieldCapName}(), serializer${loctrac}); |
| #else |
| writeTag("$fieldTagName", "${def}", ${classLcapName}.get${fieldCapName}(), serializer${loctrac}); |
| #end |
| #elseif ( $field.type == "boolean" || $field.type == "Boolean" ) |
| #if ( ${def} == "true" ) |
| writeTag("$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer${loctrac}); |
| #else |
| writeTag("$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer${loctrac}); |
| #end |
| #elseif ( $field.type == "int" ) |
| writeTag("$fieldTagName", "${def}", Integer.toString(${classLcapName}.get${fieldCapName}()), serializer${loctrac}); |
| #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${loctrac}, |
| t -> writeTag("$singularField", null, t, serializer${loctracnull})); |
| #elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" ) |
| writeProperties("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer${loctrac}); |
| #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${loctrac}, |
| 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.writeEndElement(); |
| } |
| } |
| |
| #end |
| #end |
| @FunctionalInterface |
| private interface ElementWriter<T> { |
| public void write(T t) throws IOException, XMLStreamException; |
| } |
| |
| #if ( $locationTracking ) |
| private <T> void writeList(String tagName, List<T> list, XMLStreamWriter serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException, XMLStreamException { |
| writeList(tagName, false, list, serializer, locationTracker, writer); |
| #else |
| private <T> void writeList(String tagName, List<T> list, XMLStreamWriter serializer, ElementWriter<T> writer) throws IOException, XMLStreamException { |
| writeList(tagName, false, list, serializer, writer); |
| #end |
| } |
| |
| #if ( $locationTracking ) |
| private <T> void writeList(String tagName, boolean flat, List<T> list, XMLStreamWriter serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException, XMLStreamException { |
| #else |
| private <T> void writeList(String tagName, boolean flat, List<T> list, XMLStreamWriter serializer, ElementWriter<T> writer) throws IOException, XMLStreamException { |
| #end |
| if (list != null && !list.isEmpty()) { |
| if (!flat) { |
| serializer.writeStartElement(namespace, tagName); |
| } |
| int index = 0; |
| #if ( $locationTracking ) |
| InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null; |
| #end |
| for (T t : list) { |
| writer.write(t); |
| #if ( $locationTracking ) |
| writeLocationTracking(location, Integer.valueOf(index++), serializer); |
| #end |
| } |
| if (!flat) { |
| serializer.writeEndElement(); |
| } |
| } |
| } |
| |
| #if ( $locationTracking ) |
| private <T> void writeProperties(String tagName, Map<String, String> props, XMLStreamWriter serializer, InputLocationTracker locationTracker) throws IOException, XMLStreamException { |
| #else |
| private <T> void writeProperties(String tagName, Map<String, String> props, XMLStreamWriter serializer) throws IOException, XMLStreamException { |
| #end |
| if (props != null && !props.isEmpty()) { |
| serializer.writeStartElement(namespace, tagName); |
| #if ( $locationTracking ) |
| InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null; |
| #end |
| for (Map.Entry<String, String> entry : props.entrySet()) { |
| String key = entry.getKey(); |
| #if ( $locationTracking ) |
| writeTag(key, null, entry.getValue(), serializer, null); |
| writeLocationTracking(location, key, serializer); |
| #else |
| writeTag(key, null, entry.getValue(), serializer); |
| #end |
| } |
| serializer.writeEndElement(); |
| } |
| } |
| |
| private void writeDom(XmlNode dom, XMLStreamWriter serializer) throws IOException, XMLStreamException { |
| if (dom != null) { |
| serializer.writeStartElement(namespace, dom.getName()); |
| for (Map.Entry<String, String> attr : dom.getAttributes().entrySet()) { |
| if (attr.getKey().startsWith("xml:")) { |
| serializer.writeAttribute("http://www.w3.org/XML/1998/namespace", |
| attr.getKey().substring(4), attr.getValue()); |
| } else { |
| serializer.writeAttribute(attr.getKey(), attr.getValue()); |
| } |
| } |
| for (XmlNode child : dom.getChildren()) { |
| writeDom(child, serializer); |
| } |
| String value = dom.getValue(); |
| if (value != null) { |
| serializer.writeCharacters(value); |
| } |
| serializer.writeEndElement(); |
| #if ( $locationTracking ) |
| if (addLocationInformation && dom.getInputLocation() instanceof InputLocation && dom.getChildren().isEmpty()) { |
| serializer.writeComment(toString((InputLocation) dom.getInputLocation())); |
| } |
| #end |
| } |
| } |
| |
| #if ( $locationTracking ) |
| private void writeTag(String tagName, String defaultValue, String value, XMLStreamWriter serializer, InputLocationTracker locationTracker) throws IOException, XMLStreamException { |
| #else |
| private void writeTag(String tagName, String defaultValue, String value, XMLStreamWriter serializer) throws IOException, XMLStreamException { |
| #end |
| if (value != null && !Objects.equals(defaultValue, value)) { |
| serializer.writeStartElement(namespace, tagName); |
| serializer.writeCharacters(value); |
| serializer.writeEndElement(); |
| #if ( $locationTracking ) |
| writeLocationTracking(locationTracker, tagName, serializer); |
| #end |
| } |
| } |
| |
| private void writeAttr(String attrName, String value, XMLStreamWriter serializer) throws IOException, XMLStreamException { |
| if (value != null) { |
| serializer.writeAttribute(attrName, value); |
| } |
| } |
| #if ( $locationTracking ) |
| |
| /** |
| * Method writeLocationTracking. |
| * |
| * @param locationTracker |
| * @param serializer |
| * @param key |
| * @throws IOException |
| */ |
| protected void writeLocationTracking(InputLocationTracker locationTracker, Object key, XMLStreamWriter serializer) throws IOException, XMLStreamException { |
| if (addLocationInformation) { |
| InputLocation location = (locationTracker == null) ? null : locationTracker.getLocation(key); |
| if (location != null) { |
| serializer.writeComment(toString(location)); |
| } |
| } |
| } //-- void writeLocationTracking(InputLocationTracker, Object, XMLStreamWriter) |
| |
| /** |
| * Method toString. |
| * |
| * @param location |
| * @return String |
| */ |
| protected String toString(InputLocation location) { |
| 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 |
| |
| static class IndentingXMLStreamWriter extends StreamWriterDelegate { |
| |
| int depth = 0; |
| boolean hasChildren = false; |
| |
| public IndentingXMLStreamWriter(XMLStreamWriter parent) { |
| super(parent); |
| } |
| |
| @Override |
| public void writeEmptyElement(String localName) throws XMLStreamException { |
| indent(); |
| super.writeEmptyElement(localName); |
| hasChildren = true; |
| } |
| |
| @Override |
| public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException { |
| indent(); |
| super.writeEmptyElement(namespaceURI, localName); |
| hasChildren = true; |
| } |
| |
| @Override |
| public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { |
| indent(); |
| super.writeEmptyElement(prefix, localName, namespaceURI); |
| hasChildren = true; |
| } |
| |
| @Override |
| public void writeStartElement(String localName) throws XMLStreamException { |
| indent(); |
| super.writeStartElement(localName); |
| depth++; |
| hasChildren = false; |
| } |
| |
| @Override |
| public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException { |
| indent(); |
| super.writeStartElement(namespaceURI, localName); |
| depth++; |
| hasChildren = false; |
| } |
| |
| @Override |
| public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { |
| indent(); |
| super.writeStartElement(prefix, localName, namespaceURI); |
| depth++; |
| hasChildren = false; |
| } |
| |
| @Override |
| public void writeEndElement() throws XMLStreamException { |
| depth--; |
| if (hasChildren) { |
| indent(); |
| } |
| super.writeEndElement(); |
| hasChildren = true; |
| } |
| |
| private void indent() throws XMLStreamException { |
| super.writeCharacters("\n"); |
| for (int i = 0; i < depth; i++) { |
| super.writeCharacters(" "); |
| } |
| } |
| } |
| } |