All Downloads are FREE. Search and download functionalities are using the official Maven repository.

velocity.model-writer.vm Maven / Gradle / Ivy

There is a newer version: 4.9.0
Show newest version
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------
#set( $default = "#[[##default]]#" )
/*
 * 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.
 */

/**
 * Generated by camel build tools - do NOT edit this file!
 */
package ${package};

import java.io.IOException;
import java.io.Writer;
import java.util.Base64;
import java.util.List;
import javax.annotation.processing.Generated;

#set( $pkgs = $mojo.newTreeSet() )
#foreach( $clazz in $model )
    #set( $foo = $pkgs.add($clazz.getPackageName()) )
#end
#foreach( $pkg in $pkgs )
import ${pkg}.*;
#end

@Generated("org.apache.camel.maven.packaging.XmlModelWriterGeneratorMojo")
public class ModelWriter extends BaseWriter {

    public ModelWriter(Writer writer, String namespace) throws IOException {
        super(writer, namespace);
    }

    public ModelWriter(Writer writer) throws IOException {
        super(writer, null);
    }

#foreach( $clazz in $model )
    #if( $mojo.getXmlRootElement( $clazz ) )
        #set( $name = ${clazz.getSimpleName()} )
        #set( $element = $mojo.getXmlRootElement($clazz).name() )
    public void write${name}(${name} def) throws IOException {
        doWrite${name}("${element}", def);
    }
    #end
#end

    public void writeOptionalIdentifiedDefinitionRef(OptionalIdentifiedDefinition def) throws IOException {
        doWriteOptionalIdentifiedDefinitionRef(null, def);
    }

#set( $elementRefs = $mojo.newClassTreeSet() )
#foreach( $clazz in $model )
    #if( $clazz.simpleName == "OptionalIdentifiedDefinition" )
        #set( $foo = $elementRefs.add($clazz) )
    #end
    #if( !$mojo.getXmlEnum($clazz) && !$clazz.isInterface() )
        #set( $hasDerived = false )
        #foreach( $c in $model )
            #if( $c.getSuperclass() == $clazz )
                #set( $hasDerived = true )
                #break
            #end
        #end
        #set( $name = $clazz.simpleName )
        #set( $gname = $mojo.getGenericSimpleName($clazz) )
        #set( $members = $mojo.getProperties($clazz).toList() )
        #set( $thisAttributes = $mojo.getAttributes($clazz).toList() )
        #set( $superWithAttributes = false )
        #foreach( $cl in $mojo.getClassAndSuper($clazz.getSuperclass()).toList() )
            #if( $mojo.getAttributes($cl).findAny().isPresent() )
                #set( $superWithAttributes = $cl )
                #break
            #end
        #end
        #set( $thisElements = $mojo.getElements($clazz).toList() )
        #set( $superWithElements = false )
        #foreach( $cl in $mojo.getClassAndSuper($clazz.getSuperclass()).toList() )
            #if( $mojo.getElements($cl).findAny().isPresent() )
                #set( $superWithElements = $cl )
                #break
            #end
        #end
        #if( !$thisAttributes.isEmpty() || !$thisElements.isEmpty() || $mojo.getXmlRootElement($clazz) || $mojo.isReferenced($clazz, $model) )
            #macro( writeAttributes )
                #if( $superWithAttributes )
        doWrite${superWithAttributes.getSimpleName()}Attributes(def);
                #end
                #foreach( $member in $thisAttributes )
                    #if( $member.type.getName() == "java.lang.String" )
        doWriteAttribute("${member.attributeName}", def.${member.getter}());
                    #else
        doWriteAttribute("${member.attributeName}", toString(def.${member.getter}()));
                    #end
                #end
            #end
            #macro( writeElements )
                #if( $superWithElements )
        doWrite${superWithElements.getSimpleName()}Elements(def);
                #end
                #foreach( $member in $thisElements )
                    #set( $list = $member.genericType.rawClass.name == "java.util.List" )
                    #if( $list )
                        #set( $root = $member.genericType.getActualTypeArgument(0).rawClass )
                    #else
                        #set( $root = $member.genericType.rawClass )
                    #end
                    #if( $member.xmlElementRefs )
        // TODO: @XmlElementRefs: ${member}
                    #elseif( $member.xmlElementRef )
                        #set( $foo = $elementRefs.add($root) )
                        #if( $list )
        doWriteList(null, null, def.${member.getter}(), this::doWrite${root.simpleName}Ref);
                        #else
        doWriteElement(null, def.${member.getter}(), this::doWrite${root.simpleName}Ref);
                        #end
                    #elseif( $member.xmlElements )
                        #if( $list )
        doWriteList(null, null, def.${member.getter}(), (n, v) -> {
            switch (v.getClass().getSimpleName()) {
                            #foreach( $elem in $member.xmlElements.value() )
                                #set( $t = $elem.type().simpleName )
                case "${t}" -> doWrite${t}("${elem.name()}", (${t}) v);
                            #end
            }
        });
                        #else
        doWriteElement(null, def.${member.getter}(), (n, v) -> {
            switch (v.getClass().getSimpleName()) {
                            #foreach( $elem in $member.xmlElements.value() )
                                #set( $t = $elem.type().simpleName )
                case "${t}" -> doWrite${t}("${elem.name()}", (${t}) v);
                            #end
            }
        });
                        #end
                    #elseif( $member.xmlElement )
                        #set( $t = $root.simpleName )
                        #set( $n = $member.xmlElement.name() )
                        #if( $n == $default )
                            #set( $n = ${member.name} )
                        #end
                        #if( $list )
                            #set( $w = false )
                            #if( $member.xmlElementWrapper )
                                #set( $w = $member.xmlElementWrapper.name() )
                            #end
        doWriteList(#if($w)"${w}"#{else}null#end, "${n}", def.${member.getter}(), this::doWrite${t});
                        #else
                            #set( $adapter = $member.xmlJavaTypeAdapter )
                            #if( $adapter )
                                #set( $cl = $adapter.value() )
                                #set( $actualType = false )
                                #foreach( $m in $cl.declaredMethods )
                                    #if( $m.name == "marshal" && $m.returnType.simpleName != "Object" )
                                        #set( $actualType = $m.returnType )
                                        #break
                                    #end
                                #end
                                #if( !$actualType )
                  ERROR: Unable to determine property name for JAXB adapted member: ${member}
                                #end
        doWriteElement("${n}", new ${cl.simpleName}().marshal(def.${member.getter}()), this::doWrite${actualType.simpleName});
                            #else
        doWriteElement("${n}", def.${member.getter}(), this::doWrite${t});
                            #end
                        #end
                    #elseif( $member.xmlAnyElement )
        domElements(def.${member.getter}());
                    #else
                        #set( $t = $root.simpleName )
                        #set( $n = $member.xmlRootElement.name() )
                        #if( $n == $default )
                            #set( $n = ${member.name} )
                        #end
                        #if( $list )
                doWriteList(null, "${n}", def.${member.getter}(), this::doWrite${t});
                        #else
                doWriteElement("${n}", def.${member.getter}(), this::doWrite${t});
                        #end
                    #end
                #end
            #end
            #if( $hasDerived && !$thisAttributes.isEmpty() )
    protected void doWrite${name}Attributes(${gname} def) throws IOException {
                #writeAttributes
    }
            #end
            #if( $hasDerived && !$thisElements.isEmpty() )
    protected void doWrite${name}Elements(${gname} def) throws IOException {
                #writeElements
    }
            #end
    protected void doWrite${name}(String name, ${gname} def) throws IOException {
            #if( $name == "ExpressionSubElementDefinition" )
        startExpressionElement(name);
            #else
        startElement(name);
            #end
            #if( $hasDerived && !$thisAttributes.isEmpty() )
        doWrite${name}Attributes(def);
            #else
                #writeAttributes
            #end
            #foreach( $cl in $mojo.getClassAndSuper($clazz.getSuperclass()) )
                #if( $cl.getSimpleName() == "NamespaceAwareExpression" )
        doWriteNamespaces(def);
                    #break
                #end
            #end
            #set( $value = $mojo.getValues($mojo.getClassAndSuper($clazz)).findFirst() )
            #if( $value.isPresent() )
        doWriteValue(def.${value.get().getter}());
            #end
            #if( $hasDerived && !$thisElements.isEmpty() )
        doWrite${name}Elements(def);
            #else
                #writeElements
            #end
            #if( $name == "ExpressionSubElementDefinition" )
        endExpressionElement(name);
            #else
        endElement(name);
            #end
    }
        #end
    #end
#end

#foreach( $clazz in $elementRefs )
    #set( $qname = $clazz.simpleName )
    protected void doWrite${qname}Ref(String n, ${qname} v) throws IOException {
        if (v != null) {
            switch (v.getClass().getSimpleName()) {
    #foreach( $cl in $model )
        #if( $mojo.getXmlRootElement($cl) && $mojo.getClassAndSuper($cl).toList().contains($clazz) )
            #set( $t = $cl.simpleName )
            #set( $n = $mojo.getXmlRootElement($cl).name() )
            #if( $n == $default )
                #set( $n = $mojo.lowercase($t) )
            #end
                case "${t}" -> doWrite${t}("${n}", (${t}) v);
        #end
    #end
            }
        }
    }
#end

    protected void doWriteAttribute(
            String attribute,
            Object value)
            throws IOException {
        if (value != null) {
            attribute(attribute, value);
        }
    }
    protected void doWriteValue(String value) throws IOException {
        if (value != null) {
            value(value);
        }
    }
    protected  void doWriteList(String wrapperName, String name, List list, ElementSerializer elementSerializer) throws IOException {
        if (list != null) {
            if (wrapperName != null) {
                startElement(wrapperName);
            }
            for (T v : list) {
                elementSerializer.doWriteElement(name, v);
            }
            if (wrapperName != null) {
                endElement(wrapperName);
            }
        }
    }
    protected  void doWriteElement(String name, T v, ElementSerializer elementSerializer) throws IOException {
        if (v != null) {
            elementSerializer.doWriteElement(name, v);
        }
    }
    protected String toString(Boolean b) {
        return b != null ? b.toString() : null;
    }
    protected String toString(Enum e) {
        return e != null ? e.name() : null;
    }
    protected String toString(Number n) {
        return n != null ? n.toString() : null;
    }
    protected String toString(byte[] b) {
        return b != null ? Base64.getEncoder().encodeToString(b) : null;
    }
    protected void doWriteString(String name, String value) throws IOException {
        if (value != null) {
            startElement(name);
            text(name, value);
            endElement(name);
        }
    }
    protected void doWriteNamespaces(
            NamespaceAwareExpression def)
            throws IOException {
        if (def.getNamespaceAsMap() != null) {
            for (var e : def.getNamespaceAsMap().entrySet()) {
                doWriteAttribute("xmlns:" + e.getKey(), e.getValue());
            }
        }
    }

    public interface ElementSerializer {
        void doWriteElement(String name, T value) throws IOException;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy