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

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

The 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.InputStream;
import java.io.Reader;
import java.lang.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.annotation.processing.Generated;
import org.w3c.dom.Element;

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

@Generated("org.apache.camel.maven.packaging.ModelXmlParserGeneratorMojo")
@SuppressWarnings("unused")
public class ModelParser extends BaseParser {

    public ModelParser(Resource input) throws IOException, XmlPullParserException {
        super(input);
    }
    public ModelParser(Resource input, String namespace) throws IOException, XmlPullParserException {
        super(input, namespace);
    }
    public ModelParser(InputStream input) throws IOException, XmlPullParserException {
        super(input);
    }
    public ModelParser(Reader reader) throws IOException, XmlPullParserException {
        super(reader);
    }
    public ModelParser(InputStream input, String namespace) throws IOException, XmlPullParserException {
        super(input, namespace);
    }
    public ModelParser(Reader reader, String namespace) throws IOException, XmlPullParserException {
        super(reader, namespace);
    }

#set( $elementRefs = [] )
#set( $expressionDefinitionClass = false )
#foreach( $clazz in $model )
    #if( $clazz.simpleName == "ProcessorDefinition" || $clazz.simpleName == "ExpressionDefinition"
            || $clazz.simpleName == "DataFormatDefinition" )
        #set( $foo = $elementRefs.add($clazz) )
    #end
    #if( $clazz.simpleName == "ExpressionDefinition" )
        #set( $expressionDefinitionClass = $clazz )
    #end
#end
#foreach( $clazz in $model )
    #if( !$clazz.xmlEnum && !$clazz.interface )
        #set( $hasDerived = false )
        #foreach( $c in $model )
            #if( $c.superclass == $clazz )
                #set( $hasDerived = true )
                #break
            #end
        #end
        #set( $name = ${clazz.simpleName} )
        #set( $thisAttributes = $clazz.attributes )
        #set( $superWithAttributes = false )
        #foreach( $cl in $clazz.superclass.classAndSuper )
            #if( !$cl.attributes.empty )
                #set( $superWithAttributes = $cl )
                #break
            #end
        #end
        #macro( attributeHandler $first $indent )
            #if( $thisAttributes.isEmpty() )
                #if( $superWithAttributes )#*
                    *#${first}${mojo.lowercase($superWithAttributes.getSimpleName())}AttributeHandler()##
                #else#*
                    *#${first}noAttributeHandler()##
                #end
            #else#*
                *#${first}(def, key, val) -> switch (key) {
                #foreach( $member in $thisAttributes )
                    #set( $type = $member.type )
                    #set( $rawClass = $type.rawClass )
                    #if( $rawClass.name == "java.lang.String" )
                        #if( ${member.name} == "uri" )
                            #set( $convert = "sanitizeUri(val)" )
                        #else
                            #set( $convert = "val" )
                        #end
                    #elseif( $rawClass.enum || $rawClass.name == "java.lang.Integer"
                            || $rawClass.name == "java.lang.Long" || $rawClass.name == "java.lang.Boolean"
                            || $rawClass.name == "java.lang.Float" )
                        #set( $convert = "${rawClass.simpleName}.valueOf(val)" )
                    #elseif( $rawClass.name == "java.util.List" && $type.getActualTypeArgument(0).rawClass.name == "java.lang.String" )
                        #set( $convert = "asStringList(val)" )
                    #elseif( $rawClass.name == "java.util.Set" && $type.getActualTypeArgument(0).rawClass.name == "java.lang.String" )
                        #set( $convert = "asStringSet(val)" )
                    #elseif( $rawClass.name == "java.lang.Class" )
                        #set( $convert = "asClass(val)" )
                    #elseif( $rawClass.name == "java.lang.Class[]" )
                        #set( $convert = "asClassArray(val)" )
                    #elseif( $rawClass.simpleName == "byte[]" )
                        #set( $convert = "asByteArray(val)" )
                    #else
                        #set( $convert = $mojo.failure("Unsupported type ${rawClass.simpleName} in class ${clazz.name}") )
                    #end
${indent}            case "${member.name}": def.${member.setter}(${convert}); yield true;
                #end
                #if( $superWithAttributes )
${indent}            default: yield ${mojo.lowercase($superWithAttributes.getSimpleName())}AttributeHandler().accept(def, key, val);
                #else
${indent}            default: yield false;
                #end
${indent}        }##
            #end
        #end
        #set( $thisElements = $clazz.elements )
        #set( $superWithElements = false )
        #foreach( $cl in $clazz.superclass.classAndSuper )
            #if( !$cl.elements.empty )
                #set( $superWithElements = $cl )
                #break
            #end
        #end
        #macro( elementHandler $first $indent )
            #set( $expressionHandlersDef = false )
            #set( $externals = [] )
            #set( $others = [] )
            #foreach( $member in $thisElements )
                #if( $member.xmlAnyElement && $member.externalSchemaElement )
                    #set( $foo = $externals.add($member) )
                #else
                    #set( $type = $member.type )
                    #set( $rawClass = $type.rawClass )
                    #if( $rawClass.name == "java.util.List" )
                        #set( $root = $type.getActualTypeArgument(0).rawClass )
                        #set( $list = true )
                    #else
                        #set( $root = $rawClass )
                        #set( $list = false )
                    #end
                    #set( $root = $mojo.wrap($root) )
                    #if( $elementRefs.contains($root) )
                        #if( $expressionHandlersDef )
                            #set( $foo = $mojo.failure("Only one XmlElementRef is supported on a given class") )
                        #else
                            #set( $expressionHandlersDef = $member )
                        #end
                    #else
                        #set( $foo = $others.add($member) )
                    #end
                #end
            #end
            #macro( elementCase $retInst $idnt )
                #macro( doWriteElement )
                    #if( $list )
${idnt}case "${en}": ${adder}(${convert}, def.${member.getter}(), def::${member.setter}); ${retInst};
                    #else
${idnt}case "${en}": def.${member.setter}(${convert}); ${retInst};
                    #end
                #end
                #set( $type = $member.type )
                #set( $rawClass = $type.rawClass )
                #set( $adder = "doAdd" )
                #if( $rawClass.name == "java.util.List" )
                    #set( $root = $type.getActualTypeArgument(0).rawClass )
                    #set( $list = true )
                #elseif( $rawClass.array )
                    #set( $root = $rawClass.componentType )
                    #set( $list = true )
                #else
                    #set( $root = $rawClass )
                    #set( $list = false )
                #end
                #set( $root = $mojo.wrap($root) )
                #if( $member.xmlElementRef || $clazz.simpleName == "OutputDefinition" && $member.name == "outputs" )
                    #if( !$elementRefs.contains($root) )
                        #foreach( $cl in $model )
                            #if( $root.isAssignableFrom($cl) && $cl.xmlRootElement )
                                #set( $tn = $cl.simpleName )
                                #set( $en = $cl.xmlRootElement.name() )
                                #if( $en == $default )
                                    #set( $en = $mojo.lowercase($tn) )
                                #end
                                #set( $convert = "doParse${tn}()" )
                                #doWriteElement
                            #end
                        #end
                    #end
                #elseif( $member.xmlElements )
                    #foreach( $xe in $member.xmlElements.value() )
                        #set( $tn = $xe.type().getSimpleName() )
                        #set( $en = $xe.name() )
                        #set( $convert = "doParse${tn}()" )
                        #doWriteElement
                    #end
                #elseif( !$member.xmlAnyElement || !$member.externalSchemaElement )
                    #set( $en = $default )
                    #if( $member.xmlElement )
                        #set( $en = $member.xmlElement.name() )
                        #if ( $en == "value" && $member.xmlElementWrapper )
                            #set( $en = $member.xmlElementWrapper.name() )
                        #end
                    #end
                    #if( $en == $default )
                        #set( $en = $member.name )
                    #end
                    #set( $tn = $root.simpleName )
                    #if( $member.xmlJavaTypeAdapter )
                        #set( $adapter = $mojo.wrap($member.xmlJavaTypeAdapter.value()) )
                        #set( $gsct = false )
                        #foreach( $c in $adapter.classAndSuper )
                            #if( $c.superclass.simpleName == "XmlAdapter" )
                                #set( $gsct = $c.superclassType )
                            #end
                        #end
                        #if( $adapter.declaringClass )
                            #set( $n = $adapter.declaringClass.simpleName + "." + $adapter.simpleName )
                        #else
                            #set( $n = $adapter.simpleName )
                        #end
                        #set( $c = $gsct.getActualTypeArgument(0).rawClass )
                        #if( $c.name == "java.lang.String" )
                            #set( $convert = "new ${n}().unmarshal(doParseText())" )
                        #elseif( $model.contains($mojo.wrap($c)) )
                            #set( $convert = "new ${n}().unmarshal(doParse${c.simpleName}())" )
                        #else
                            #set( $convert = $mojo.failure("Unsupported type ${rawClass.simpleName} for member ${member.name} in class ${clazz.name}") )
                        #end
                    #elseif( $root.name == "java.lang.String" )
                        #set( $convert = "doParseText()" )
                    #elseif( $root.enum || $root.name == "java.lang.Integer" || $root.name == "java.lang.Long"
                            || $root.name == "java.lang.Boolean" || $root.name == "java.lang.Float" )
                        #set( $convert = "${root.simpleName}.valueOf(doParseText())" )
                    #elseif( $model.contains($root) )
                        #set( $convert = "doParse${tn}()" )
                    #else
                        #set( $convert = $mojo.failure("Unsupported type ${rawClass.simpleName} for member ${member.name} in class ${clazz.name}") )
                    #end
                    #if( $en == "allowableValues" )
                        #set( $adder = "doAddValues" )
                    #else
                        #set( $adder = "doAdd" )
                    #end
                    #doWriteElement
                #end
            #end
            #if( $thisElements.empty )
                #if( $superWithElements )#*
                    *#${first}${mojo.lowercase($superWithElements.simpleName)}ElementHandler()##
                #else#*
                    *#${first}noElementHandler()##
                #end
            #elseif( $externals.empty && !$expressionHandlersDef )#*
                *#${first}(def, key) -> switch (key) {
                #foreach( $member in $others )
                    #elementCase( "yield true" "${indent}            " )
                #end
                #if( $superWithElements )
${indent}            default: yield ${mojo.lowercase($superWithElements.simpleName)}ElementHandler().accept(def, key);
                #else
${indent}            default: yield false;
                #end
${indent}        }##
            #else#*
                *#${first}(def, key) -> {
                #foreach( $member in $externals )
                    #set( $external = $member.externalSchemaElement )
${indent}            if ("${external.namespace()}".equals(parser.getNamespace())) {
${indent}                Element el = doParseDOMElement("${external.documentElement()}", "${external.namespace()}", def.${member.getter}());
${indent}                if (el != null) {
${indent}                    doAddElement(el, def.${member.getter}(), def::${member.setter});
${indent}                    return true;
${indent}                }
${indent}                return false;
${indent}            }
                #end
                #if( $others.empty )
                    #if( $expressionHandlersDef )
                        #set( $type = $expressionHandlersDef.type )
                        #set( $rawClass = $type.rawClass )
                        #if( $rawClass.name == "java.util.List" )
                            #set( $root = $type.getActualTypeArgument(0).rawClass )
                            #set( $list = true )
                        #else
                            #set( $root = $rawClass )
                            #set( $list = false )
                        #end
${indent}            ${root.simpleName} v = doParse${root.simpleName}Ref(key);
${indent}            if (v != null) {
                        #if( $list )
${indent}                doAdd(v, def.${expressionHandlersDef.getter}(), def::${expressionHandlersDef.setter});
                        #else
${indent}                def.${expressionHandlersDef.setter}(v);
                        #end
${indent}                return true;
${indent}            }
                        #if( $superWithElements )
${indent}            return ${mojo.lowercase($superWithElements.getSimpleName())}ElementHandler().accept(def, key);
                        #else
${indent}            return false;
                        #end
                    #else
                    TODO
                    #end
                #else
${indent}            switch (key) {
                    #foreach( $member in $others )
                        #elementCase( "break" "${indent}                " )
                    #end
                    #if( $expressionHandlersDef )
${indent}                default:
                        #set( $type = $expressionHandlersDef.type )
                        #set( $rawClass = $type.rawClass )
                        #if( $rawClass.name == "java.util.List" )
                            #set( $root = $type.getActualTypeArgument(0).rawClass )
                            #set( $list = true )
                        #else
                            #set( $root = $rawClass )
                            #set( $list = false )
                        #end
${indent}                    ${root.simpleName} v = doParse${root.simpleName}Ref(key);
${indent}                    if (v != null) {
                        #if( $list )
${indent}                        doAdd(v, def.${expressionHandlersDef.getter}(), def::${expressionHandlersDef.setter});
                        #else
${indent}                        def.${expressionHandlersDef.setter}(v);
                        #end
${indent}                        return true;
${indent}                    }
                        #if( $superWithElements )
${indent}                    return ${mojo.lowercase($superWithElements.simpleName)}ElementHandler().accept(def, key);
                        #else
${indent}                    return false;
                        #end
                    #elseif( $superWithElements )
${indent}                default: return ${mojo.lowercase($superWithElements.simpleName)}ElementHandler().accept(def, key);
                    #else
${indent}                default: return false;
                    #end
${indent}            }
${indent}            return true;
                #end
${indent}        }##
            #end
        #end
        #if( $hasDerived && !$thisAttributes.empty )
    protected  AttributeHandler ${mojo.lowercase(${name})}AttributeHandler() {
        return #attributeHandler( "", "" );
    }
        #end
        #if( $hasDerived && !$thisElements.empty )
    protected  ElementHandler ${mojo.lowercase(${name})}ElementHandler() {
        return #elementHandler( "", "" );
    }
        #end
        #if( $name == "ValueDefinition" )
    protected List doParseValueDefinition() throws IOException, XmlPullParserException {
        return doParseValue(() -> new ValueDefinition(), (def, val) -> def.setValue(val));
    }
        #elseif( !$clazz.abstract )
    protected ${name} doParse${name}() throws IOException, XmlPullParserException {
        return doParse(new ${name}(), ##
            #if( $hasDerived && !$thisAttributes.empty )#*
                *#${mojo.lowercase(${name})}AttributeHandler(), ##
            #else#*
                *##attributeHandler( "" "    " ), ##
            #end
            #if( $hasDerived && !$thisElements.empty )#*
                *#${mojo.lowercase(${name})}ElementHandler(), ##
            #else#*
                *##elementHandler( "" "    " ), ##
            #end
            #set( $thisValue = $clazz.value.orElse(null) )
            #set( $superWithValue = false )
            #foreach( $cl in $clazz.superclass.classAndSuper )
                #if( $cl.value.isPresent() )
                    #set( $superWithValue = $cl )
                    #break
                #end
            #end
            #if( $thisValue )
                #if( $expressionDefinitionClass.isAssignableFrom($thisValue.declaringClass) )#*
                    *#expressionDefinitionValueHandler()##
                #else#*
                    *#(def, val) -> def.${thisValue.setter}(val)##
                #end
            #else
                #if( $superWithValue )#*
                    *#${mojo.lowercase($superWithValue.simpleName)}ValueHandler()##
                #else#*
                    *#noValueHandler()##
                #end
            #end
            #if( !$externals.empty )#*
                *#, true##
            #end#*
            *#);
    }
        #end
    #end
    #if( $name == "BeansDefinition" || $name == "ApplicationDefinition" )
    public Optional<${name}> parse${name}() throws IOException, XmlPullParserException {
        String tag = getNextTag("beans", "blueprint", "camel");
        if (tag != null) {
            return Optional.of(doParse${name}());
        }
        return Optional.empty();
    }
    #elseif( $name == "RoutesDefinition" || $name == "RouteTemplatesDefinition" || $name == "TemplatedRoutesDefinition"
            || $name == "RestsDefinition" || $name == "RouteConfigurationsDefinition" || $name == "DataFormatsDefinition" )
        #set( $element = $clazz.xmlRootElement.name() )
        #set( $capitalElement = $mojo.uppercase($element) )
        #if( $element.endsWith("s") )
            #set( $singleElement = $element.substring(0, $element.length() - 1) )
        #else
            #set( $singleElement = $element )
        #end
        #set( $singleName = $name.replace("sDefinition", "Definition") )
    public Optional<${name}> parse${name}() throws IOException, XmlPullParserException {
        String tag = getNextTag("${element}", "${singleElement}");
        if (tag != null) {
            switch (tag) {
                case "${element}" : return Optional.of(doParse${name}());
                case "${singleElement}" : return parseSingle${name}();
            }
        }
        return Optional.empty();
    }
    private Optional<${name}> parseSingle${name}() throws IOException, XmlPullParserException {
        Optional<${singleName}> single = Optional.of(doParse${singleName}());
        if (single.isPresent()) {
            List<${singleName}> list = new ArrayList<>();
            list.add(single.get());
            ${name} def = new ${name}();
            def.set${capitalElement}(list);
            return Optional.of(def);
        }
        return Optional.empty();
    }
    #end
#end
#foreach( $root in $elementRefs )
    #set( $name = ${root.simpleName} )
    protected ${name} doParse${name}Ref(String key) throws IOException, XmlPullParserException {
        switch (key) {
    #foreach( $cl in $model )
        #if( $root.isAssignableFrom($cl) && $cl.xmlRootElement )
            #set( $en = $cl.xmlRootElement.name() )
            #if( $en == $default )
                #set( $en = $mojo.lowercase($cl.simpleName) )
            #end
            case "${en}": return doParse${cl.simpleName}();
        #end
    #end
            default: return null;
        }
    }
#end
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy