<?xml version="1.0" encoding="utf-8"?>
<!--

  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER

  Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
  Copyright 2009 IBM. All rights reserved.

  Use is subject to license terms.

  Licensed 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. You can also
  obtain a copy of the License at http://odftoolkit.org/docs/license.txt

  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.

-->
<template class="java">

    <ref name="gen_dom_type_enumerations"/>
    <ref name="gen_dom_base_elements"/>
    <ref name="gen_manifest_dom_elements"/>
    <ref name="gen_OdfNamespace"/>
    <ref name="gen_manifest_pkg_elements"/>
    <ref name="gen_OdfElementFactory"/>

<!-- generate flat rng -->
<!--file name="elements" extension="xml">
<code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;elements&gt;</code>
      <foreach type="element">
<code>
&lt;element name="%{elementqname}"&gt;</code>
<foreach type="attribute">
<if test="valuetype='enum'">
<code>
&lt;attribute name="%{attributeqname}" type="enum"&gt;</code>
<foreach type="value" seperator=", ">
 <code>"%{value}"</code>
</foreach>
<code>&lt;/attribute&gt;</code>
<else>
<code>
&lt;attribute name="%{attributeqname}" type="%{valuetype}"/&gt;</code>
</else>
</if>
</foreach>
<code>
&lt;/element&gt;</code>
      </foreach>
<code>
&lt;/elements&gt;</code>
</file-->

<!-- copyright header for .java files -->
    <define name="header">
        <code>/************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * Licensed 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. You can also
 * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
 *
 * 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.
 *
 ************************************************************************/

/*
 * This file is automatically generated.
 * Don't edit manually.
 */
        </code>
    </define>

<!-- copyright header for .java files -->
    <define name="docheader">
        <code>/************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * Licensed 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. You can also
 * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
 *
 * 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.
 *
 ************************************************************************/
        </code>
    </define>
<!-- sets the name for the current attribute ============================== -->

    <define name="convertenumvaluetype">
        <if test="valuetype='enum'">
            <if test="not endswith(conversiontype,'type')">
                <set valuetype="org.odftoolkit.odfdom.dom.type.%{prefix(conversiontype)}:Odf%{identifier(conversiontype)}Type"/>
                <else>
                    <set valuetype="org.odftoolkit.odfdom.dom.type.%{prefix(conversiontype)}:Odf%{identifier(conversiontype)}"/>
                </else>
            </if>
            <set conversiontype="%{valuetype}"/>
            <set valueoffunc="enumValueOf"/>
            <else>
                <set valueoffunc="valueOf"/>
            </else>
        </if>
    </define>


<!-- create getter and setter for all attributes of the current element ==== -->

    <define name="setxlinkdefaults">
        <foreach type="attribute">
            <if test="(valuetype = 'String') and (attributeqname = 'xlink:type' or attributeqname = 'xlink:show' or attributeqname = 'xlink:actuate')">
                <foreach type="value">
                    <code>        setOdfAttribute( OdfName.get( OdfNamespace.XLINK, "%{local_name(attributeqname)}" ), "%{value}" );
                    </code>
                </foreach>
            </if>
        </foreach>
    </define>

    <define name="attributes">
        <foreach type="attribute">
            <ref name="convertenumvaluetype"/>
            <if test="not prefix(attributeqname)">
                <set namespace='""'/>
                <else>
                    <set namespace="OdfNamespace.%{toupper(prefix(attributeqname))}"/>
                </else>
            </if>
            <if test="(valuetype != 'String') or (attributeqname != 'xlink:type' and attributeqname != 'xlink:show' and attributeqname != 'xlink:actuate')">
                <code>
    /**
     * Get value of attribute {@attributeName %{attributeqname}}.
     *
     *@return return the attribute which type is %{local_name(valuetype)}
     */
    public %{local_name(valuetype)} get%{identifier(attributename)}()
    {</code>
                <if test="not defaultvalue and valuetype = 'String' and conversiontype = 'String'">
                    <code>
        return getOdfAttribute( OdfName.get( %{namespace}, "%{local_name(attributeqname)}" ) );
                    </code>
                    <else>
                        <code>
        String aStringVal = getOdfAttribute( OdfName.get( %{namespace}, "%{local_name(attributeqname)}" ) );</code>
                        <if test=" defaultvalue = '&quot;' ">
                            <code>
        if( aStringVal.length()==0 )
        {
            aStringVal = "\%{defaultvalue}";
        }
                            </code>
                            <else>
                                <if test="defaultvalue">
                                    <code>
        if( aStringVal.length()==0 )
        {
            aStringVal = "%{defaultvalue}";
        }
                                    </code>
                                </if>
                            </else>
                        </if>
                        <code>
        return %{local_name(conversiontype)}.%{valueoffunc}( aStringVal);
                        </code>
                    </else>
                </if>
                <code>    }

    /**
     * Set value of attribute {@attributeName %{attributeqname}}.
     *
     *@param  _a%{identifier(attributename)}    the attribute's type is %{local_name(valuetype)}
     */
    public void set%{identifier(attributename)}( %{local_name(valuetype)} _a%{identifier(attributename)} )
    {</code>
                <if test="valuetype = 'String' and conversiontype = 'String'">
                    <code>
        setOdfAttribute( OdfName.get( %{namespace}, "%{local_name(attributeqname)}" ), _a%{identifier(attributename)} );
                    </code>
                    <else>
                        <code>
        String aStringVal = %{local_name(conversiontype)}.toString( _a%{identifier(attributename)} );
        setOdfAttribute( OdfName.get( %{namespace}, "%{local_name(attributeqname)}" ), aStringVal );
                        </code>
                    </else>
                </if>

                <if test="attributeqname = 'xlink:href'">
                    <ref name="setxlinkdefaults"/>
                </if>
                <code>    }
                </code>
            </if>
        </foreach>
    </define>

<!-- imports section for the current element =============================== -->

    <define name="doImport">
        <if test="not contains(imports,import,';')">
            <code>import %{import};
            </code>
            <set imports="%{imports};%{import}"/>
        </if>
    </define>

    <define name="element-import-section">
        <code>import org.odftoolkit.odfdom.dom.OdfName;
import org.odftoolkit.odfdom.pkg.manifest.OdfNamespace;
import org.odftoolkit.odfdom.doc.OdfFileDom;
        </code>
        <if test="elementstylefamily!=''">
            <code>import org.odftoolkit.odfdom.dom.style.OdfStyleFamily;
            </code>
        </if>
<!-- import base class, but only if it is in another package -->
        <if test="prefix(baseclassname) != prefix(classname)">
            <code>import %{baseclassname};
            </code>
        </if>
  <!-- do imports for attribute values and conversion types -->
        <if test="elementbasename != 'styleproperties'">
            <set imports=""/>
            <foreach type="attribute">
                <set import=""/>
                <if test="valuetype = 'enum'">
                    <ref name="convertenumvaluetype"/>
                    <set import="%{prefix(valuetype)}.%{local_name(valuetype)}"/>
                    <set conversiontype="%{valuetype}"/>
                    <else>
                        <if test="prefix(valuetype) != ''">
                            <set import="%{substring-before(valuetype,'&lt;')}"/>
                        </if>
                    </else>
                </if>
                <if test="import">
                    <ref name="doImport"/>
                </if>
                <if test="valuetype != conversiontype">
                    <if test="prefix(conversiontype)">
                        <set import="%{substring-before(conversiontype,'&lt;')}"/>
                        <ref name="doImport"/>
                    </if>
                </if>
            </foreach>
            <else>
                <code>import org.odftoolkit.odfdom.dom.style.props.OdfStyleProperty;
import org.odftoolkit.odfdom.dom.style.props.OdfStylePropertiesSet;
                </code>
            </else>
        </if>
    </define>

<!-- sets 'ret' to 'true' if the current element has non optional attributes -->
    <define name="check_for_non_optional_attributes">
        <set ret="false"/>
        <foreach type="attribute">
            <if test="not optionalattribute">
                <set ret="true"/>
            </if>
        </foreach>
    </define>

<!-- create constructor parameter string for current non optional attributes -->
    <define name="element_ctor_params">
        <foreach type="attribute">
            <if test="not optionalattribute">
                <ref name="convertenumvaluetype"/>
                <code>%{sep}%{local_name(valuetype)} _a%{identifier(attributename)}</code>
                <set sep=", "/>
            </if>
        </foreach>
    </define>

<!-- create the parameter list for the init method of the current element == -->
    <define name="element_init_params">
        <foreach type="attribute">
            <if test="not optionalattribute">
                <ref name="convertenumvaluetype"/>
                <code>_a%{identifier(attributename)}</code>
                <set sep=", "/>
            </if>
        </foreach>
    </define>

<!-- create init method for current non optional attributes -->
    <define name="element_ctor_init">
        <foreach type="attribute">
            <if test="not optionalattribute">
                <ref name="convertenumvaluetype"/>
                <code>        set%{identifier(attributename)}( _a%{identifier(attributename)} );
                </code>
            </if>
        </foreach>
    </define>

<!-- create enumerations java files ===================================== -->
    <define name="gen_dom_type_enumerations">
        <foreach type="element">
            <foreach type="attribute">
                <if test="valuetype='enum'">
                    <ref name="convertenumvaluetype"/>
                    <set enumclassname="%{local_name(valuetype)}"/>
                    <file path="%{prefix(valuetype)}" name="%{enumclassname}" extensions="java">
                        <ref name="header"/>
                        <code>package %{prefix(valuetype)};

public enum %{enumclassname}
{
                        </code>
                        <foreach type="value" seperator=", ">
                            <code>%{toupper(identifier(replace("-","_",value)))}( "%{value}" )</code>
                        </foreach>
                        <code>;

    private String m_aValue;

    %{enumclassname}( String _aValue )
    {
        m_aValue = _aValue;
    }

    public String toString()
    {
        return m_aValue;
    }

    public static String toString( %{enumclassname} _aEnum )
    {
        return _aEnum.toString();
    }

    public static %{enumclassname} enumValueOf( String _aString )
    {
        for( %{enumclassname} aIter : values() )
        {
            if( _aString.equals( aIter.toString() ) )
            {
                return aIter;
            }
        }
        return null;
    }

}
                        </code>
                    </file>
                </if>
            </foreach>
        </foreach>
    </define>

<!-- create base element class files ===================================== -->
    <define name="gen_dom_base_elements">
        <foreach type="baseelement">
            <set classname="org.odftoolkit.odfdom.dom.element.%{prefix(elementqname)}.Odf%{identifier(elementname)}ElementBase"/>
            <if test="not elementstylefamily">
                <if test="elementbasename = 'container'">
                    <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfContainerElementBase"/>
                    <else>
                        <if test="elementbasename = 'style'">
                            <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfStyleBase"/>
                            <else>
                                <if test="elementbasename = 'styleproperties'">
                                    <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfStylePropertiesBase"/>
                                    <else>
                                        <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfElement"/>
                                    </else>
                                </if>
                            </else>
                        </if>
                    </else>
                </if>
                <else>
                    <if test="elementstylefamily!='draw:graphic,presentation:presentation'">
                        <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfStylableElement"/>
                        <else>
                            <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfStyleablePresentationElement"/>
                        </else>
                    </if>
                </else>
            </if>
            <file path="%{prefix(classname)}" name="%{local_name(classname)}" extensions="java">
                <ref name="header"/>
                <code>package org.odftoolkit.odfdom.dom.element.%{prefix(elementqname)};
                </code>
                <ref name="element-import-section"/>
                <code>

/**
 * ODF DOM base element implementation.
 */
public abstract class %{local_name(classname)} extends %{local_name(baseclassname)}
{</code>
                <if test="(elementstylefamily = '') or (elementstylefamily = 'draw:graphic,presentation:presentation')">
                    <code>
    public %{local_name(classname)}( OdfFileDom _aOwnerDoc, OdfName element_name )
    {
        super( _aOwnerDoc, element_name );
    }</code>
                    <else>
                        <code>
    public %{local_name(classname)}( OdfFileDom _aOwnerDoc ,OdfName _elementName ,OdfStyleFamily _styleFamily ,OdfName _styleAttrName )
    {
        super( _aOwnerDoc, _elementName, _styleFamily, _styleAttrName );
    }</code>
                    </else>
                </if>
                <code>
    /**
     * Initialize mandatory attributes.
     */
    public void init(</code>
                <set sep=""/>
                <ref name="element_ctor_params"/>
                <code>)
    {
                </code>
                <ref name="element_ctor_init"/>
                <code>    }
                </code>
                <if test="elementbasename != 'styleproperties'">
                    <ref name="attributes"/>
                    <else>

                    </else>
                </if>
                <code>
}
                </code>
            </file>
        </foreach>
    </define>

    <define name="get_baseclassname">
        <if test="not elementbasename">
            <if test="not elementstylefamily">
                <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfElement"/>
                <else>
                    <if test="elementstylefamily!='draw:graphic,presentation:presentation'">
                        <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfStylableElement"/>
                        <else>
                            <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfStyleablePresentationElement"/>
                        </else>
                    </if>
                </else>
            </if>
            <else>
                <if test="elementbasename = 'container'">
                    <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfContainerElementBase"/>
                    <else>
                        <if test="elementbasename = 'style'">
                            <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfStyleBase"/>
                            <else>
                                <if test="elementbasename = 'styleproperties'">
                                    <set baseclassname="org.odftoolkit.odfdom.dom.element.OdfStylePropertiesBase"/>
                                    <else>
                                        <set baseclassname="org.odftoolkit.odfdom.dom.element.%{prefix(elementbasename)}.Odf%{identifier(elementbasename)}ElementBase"/>
                                    </else>
                                </if>
                            </else>
                        </if>
                    </else>
                </if>
            </else>
        </if>
    </define>

<!-- create dom element class files ========================================== -->
    <define name="gen_manifest_dom_elements">
        <foreach type="element">
    <!-- test if this element has mandatory (non optional) attributes -->
            <ref name="check_for_non_optional_attributes"/>
            <set hasMandatoryAttr="%{ret}"/>
            <set packagename = "%{prefix(elementqname)}" />
            <set currentname = "%{elementname}" />

    <!-- now check if base element has mandatory (non optional) attributes -->
            <set baseHasMandatoryAttr="false"/>
            <if test="prefix(elementbasename) != ''">
                <select type="baseelement" name="%{elementbasename}">
                    <ref name="check_for_non_optional_attributes"/>
                    <set baseHasMandatoryAttr="%{ret}"/>
                </select>
            </if>

            <set classname="org.odftoolkit.odfdom.pkg.%{prefix(elementqname)}.element.Odf%{identifier(elementname)}Element"/>
            <ref name="get_baseclassname"/>
            <file path="%{prefix(classname)}" name="Odf%{identifier(elementname)}Element" extensions="java">
                <ref name="header"/>
                <code>
package %{prefix(classname)};

                </code>
                <ref name="element-import-section"/>

      <!-- now set the imports for child elements and attributes -->
                <set imports=""/>
                <set import="" />
                <children type="element">
                    <if test = "currentname != elementname">
                        <if test="prefix(elementqname) != packagename and elementname !='hyperlink' and elementname !='measure' and elementqname !='presentation:event-listener' ">
                            <set import="org.odftoolkit.odfdom.dom.element.%{prefix(elementqname)}.Odf%{identifier(elementname)}Element"/>
                        </if>
                    </if>
                    <if test="not contains(imports,import,';')">
                        <if test="import !=''">
                            <if test="imports =''">
                                <set imports="import %{import}"/>
                                <else>
                                    <set imports="%{imports};%{import}"/>
                                </else>
                            </if>
                        </if>
                    </if>
                    <set import="" />
                    <subattribute>
                        <group>
                            <set import="" />
                            <if test = "valuetype != conversiontype and attributeqname !='style:num-format'">" >
                                <if test=" valuetype = 'enum'">
                                    <if test="not endswith(conversiontype,'type')">
                                        <set import="org.odftoolkit.odfdom.dom.type.%{prefix(conversiontype)}.Odf%{identifier(conversiontype)}Type"/>
                                        <else>
                                            <set import="org.odftoolkit.odfdom.dom.type.%{prefix(conversiontype)}.Odf%{identifier(conversiontype)}"/>
                                        </else>
                                    </if>
                                </if>
                                <if test="local_name(valuetype) ='Duration' or local_name(valuetype) = 'XMLGregorianCalendar' ">
                                    <set import="%{valuetype}" />
                                </if>
                                <if test=" valuetype ='String' and (identifier(attributename)='ValueType' or identifier(attributename)='Type' or identifier(attributename)='Orientation' or identifier(attributename)='SortMode') ">
                                    <set import="org.odftoolkit.odfdom.dom.type.%{prefix(attributeqname)}.Odf%{identifier(conversiontype)}Type"/>
                                </if>

                                <else>
                                    <if test=" valuetype ='String' and identifier(attributename)='ValueType'">
                                        <set import="org.odftoolkit.odfdom.dom.type.%{prefix(attributeqname)}.Odf%{identifier(attributename)}"/>
                                    </if>
                                    <if test=" valuetype ='String' and identifier(attributename)='LegendExpansion'">
                                        <set import="org.odftoolkit.odfdom.dom.type.%{prefix(attributeqname)}.Odf%{identifier(attributename)}Type"/>
                                    </if>
                                    <if test="local_name(valuetype) ='OdfPresentationClasses'">
                                        <set import="%{valuetype}" />
                                    </if>
                                </else>
                            </if>
                            <if test="not contains(imports,import,';')">
                                <if test="import !=''">
                                    <if test="imports =''">
                                        <set imports="import %{import}"/>
                                        <else>
                                            <set imports="%{imports};%{import}"/>
                                        </else>
                                    </if>
                                </if>
                            </if>
                        </group>
                    </subattribute>
                </children>
                <if test ="imports =''">
                    <set allimport=""/>
                    <else>
                        <set allimport="%{imports}"/>
                        <set allimport="%{replace(';',';\nimport ',allimport)}" />
                    </else>
                </if><code>
%{allimport};</code>

                <code>

/**
 * ODF DOM Element implementation for element {@odfLink %{elementqname}}.
 *
 */
public abstract class %{local_name(classname)} extends %{local_name(baseclassname)}
{
    public static final OdfName ELEMENT_NAME = OdfName.get( OdfNamespace.%{toupper(prefix(elementqname))}, "%{local_name(elementqname)}" );

    public %{local_name(classname)}( OdfFileDom _aOwnerDoc )
    {
        super( _aOwnerDoc, ELEMENT_NAME</code>
                <if test="(elementstylefamily != '') and (elementstylefamily != 'draw:graphic,presentation:presentation')">
                    <code>, OdfStyleFamily.%{identifier(elementstylefamily)}, OdfName.get( OdfNamespace.%{toupper(prefix(elementstylefamily))}, "style-name" )</code>
                </if>
                <code> );
    }

    public OdfName getOdfName()
    {
        return ELEMENT_NAME;
    }

                </code>

                <if test="elementbasename != 'styleproperties'">

<!-- add init method if this element or the base element has mandatory attributes -->
                    <if test="hasMandatoryAttr or baseHasMandatoryAttr">
                        <code>    /**
     * Initialize mandatory attributes.
     */
    public void init(</code>
                        <set sep=""/>
                        <ref name="element_ctor_params"/>
                        <if test="baseHasMandatoryAttr">
                            <select type="baseelement" name="%{elementbasename}">
                                <ref name="element_ctor_params"/>
                            </select>
                        </if>
                        <code>)
    {
                        </code>
                        <ref name="element_ctor_init"/>
      <!-- if we have a base element, call its init function here -->
                        <if test="baseHasMandatoryAttr">
                            <select type="baseelement" name="%{elementbasename}">
                                <code>        super.init( </code>
                                <set sep=""/>
                                <ref name="element_init_params"/>
                                <code> );
                                </code>
                            </select>
                        </if>
                        <code>    }
                        </code>
                    </if>
                    <ref name="attributes"/>
                    <else>
                        <foreach type="attribute">
                            <code>    public final static OdfStyleProperty %{identifier(attributename)} =
        OdfStyleProperty.get(OdfStylePropertiesSet.%{identifier(local_name(elementqname))}, OdfName.get(OdfNamespace.%{toupper(prefix(attributeqname))}, "%{local_name(attributeqname)}"));
                            </code>
                        </foreach>
                    </else>
                </if>

<!-- add create child elements method  -->
                <set selements="" />
                <children type="element">
                    <set hasparameters="false" />
                    <subattribute>
                        <set hasparameters="true" />
                        <if test="elementname='hyperlink'or elementname='measure' or elementqname ='presentation:event-listener'">
                            <set childname="org.odftoolkit.odfdom.dom.element.%{prefix(elementqname)}.Odf%{identifier(elementname)}Element"/>
                            <code>
    /**
    * Create child element {@odfLink %{elementqname}}.
    *</code>
                            <set parameters = ""/>
                            <set sep="" />
                            <group>
                                <set hasparameters="true" />
                                <if test="valuetype != conversiontype " >
                                    <if test="valuetype ='enum' and attributeqname != 'style:num-format'">
                                        <if test="not endswith(conversiontype,'type') ">
                                            <set parameter="_a%{identifier(attributename)}     the attribute's type is Odf%{identifier(conversiontype)}Type, see {@attributeName  %{conversiontype}} at specification"/>
                                            <else>
                                                <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(conversiontype)}, see {@attributeName  %{conversiontype}} at specification"/>
                                            </else>
                                        </if>
                                        <else>
                                            <if test="attributeqname ='style:num-format'">
                                                <set parameter="_a%{identifier(attributename)}   the attribute's type is String"/>
                                            </if>
                                        </else>
                                    </if>
                                    <if test=" valuetype ='Integer' or valuetype ='Boolean' or valuetype='String'">
                                        <set parameter="_a%{identifier(attributename)}  the attribute's type is %{valuetype}"/>
                                    </if>
                                    <if test=" valuetype='javax.xml.datatype.Duration' or valuetype ='javax.xml.datatype.XMLGregorianCalendar' ">
                                        <set parameter="_a%{identifier(attributename)}    the attribute's type is %{local_name(valuetype)}"/>
                                    </if>
                                    <if test=" valuetype ='String' and (identifier(attributename)='ValueType' or identifier(attributename)='Display')">
                                        <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(conversiontype)}Type, see {@attributeName  %{conversiontype}} at specification"/>
                                    </if>
                                    <if test=" valuetype ='String' and identifier(attributename)='LegendExpansion' ">
                                        <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(attributename)}Type, see {@attributeName  %{conversiontype}} at specification"/>
                                    </if>
                                    <if test=" valuetype ='String' and attributename ='style:num-format' ">
                                        <set parameter="_a%{identifier(attributename)}   the attribute's type is String"/>
                                    </if>
                                    <if test=" valuetype ='String' and (identifier(attributename)='Type' or identifier(attributename)='Orientation' or identifier(attributename)='SortMode')">
                                        <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(conversiontype)}Type, see {@attributeName  %{conversiontype}} at specification"/>
                                    </if>
                                    <else>
                                        <set parameter="_a%{identifier(attributename)} %{local_name(valuetype)}   " />
                                        <if test=" valuetype ='String' and (identifier(attributename)='ValueType' or identifier(attributename)='MemberType') ">
                                            <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(attributename)}, see {@attributeName  %{attributename}} at specification"/>
                                        </if>
                                        <if test=" valuetype ='String' and (identifier(attributename)='LegendExpansion' or identifier(attributename)='SortMode')">
                                            <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(attributename)}Type, see {@attributeName  %{attributename}} at specification"/>
                                        </if>
                                        <if test=" valuetype ='String' and identifier(attributename)='Concave' ">
                                            <set parameter="_a%{identifier(attributename)}   the attribute's type is Boolean"/>
                                        </if>
                                    </else>
                                </if>
                                <set parameters="%{parameters}%{sep}%{parameter}" />
                                <set sep=";"/>
                            </group>
                            <if test="parameters != ''" ><code>*@param %{replace(';','\n    *@param ',parameters)}</code></if>
                            <code>*@return   return  the element {@odfLink %{elementqname}}
    */
    public %{childname} create%{prefix(elementqname)}%{identifier(elementname)}Element(</code>
                            <else>
                                <set childname = "Odf%{identifier(elementname)}Element" />
                                <code>
    /**
    * Create child element {@odfLink %{elementqname}}.
    *
                                </code>
                                <set parameters = ""/>
                                <set sep="" />
                                <group>
                                    <set hasparameters="true" />
                                    <if test="valuetype != conversiontype " >
                                        <if test="valuetype ='enum' and attributeqname != 'style:num-format'">
                                            <if test="not endswith(conversiontype,'type') ">
                                                <set parameter="_a%{identifier(attributename)}     the attribute's type is Odf%{identifier(conversiontype)}Type, see {@attributeName  %{conversiontype}} at specification"/>
                                                <else>
                                                    <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(conversiontype)}, see {@attributeName  %{conversiontype}} at specification"/>
                                                </else>
                                            </if>
                                            <else>
                                                <if test="attributeqname ='style:num-format'">
                                                    <set parameter="_a%{identifier(attributename)}   the attribute's type is String"/>
                                                </if>
                                            </else>
                                        </if>
                                        <if test=" valuetype ='Integer' or valuetype ='Boolean' or valuetype='String'">
                                            <set parameter="_a%{identifier(attributename)}  the attribute's type is %{valuetype}"/>
                                        </if>
                                        <if test=" valuetype='javax.xml.datatype.Duration' or valuetype ='javax.xml.datatype.XMLGregorianCalendar' ">
                                            <set parameter="_a%{identifier(attributename)}    the attribute's type is %{local_name(valuetype)}"/>
                                        </if>
                                        <if test=" valuetype ='String' and (identifier(attributename)='ValueType' or identifier(attributename)='Display')">
                                            <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(conversiontype)}Type, see {@attributeName  %{conversiontype}} at specification"/>
                                        </if>
                                        <if test=" valuetype ='String' and identifier(attributename)='LegendExpansion' ">
                                            <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(attributename)}Type"/>
                                        </if>
                                        <if test=" valuetype ='String' and attributename ='style:num-format' ">
                                            <set parameter="_a%{identifier(attributename)}   the attribute's type is String"/>
                                        </if>
                                        <if test=" valuetype ='String' and (identifier(attributename)='Type' or identifier(attributename)='Orientation' or identifier(attributename)='SortMode')">
                                            <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(conversiontype)}Type, see {@attributeName  %{conversiontype}} at specification"/>
                                        </if>
                                        <else>
                                            <set parameter="_a%{identifier(attributename)} %{local_name(valuetype)}   " />
                                            <if test=" valuetype ='String' and (identifier(attributename)='ValueType' or identifier(attributename)='MemberType') ">
                                                <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(attributename)}, see {@attributeName  %{attributename}} at specification"/>
                                            </if>
                                            <if test=" valuetype ='String' and (identifier(attributename)='LegendExpansion' or identifier(attributename)='SortMode')">
                                                <set parameter="_a%{identifier(attributename)}   the attribute's type is Odf%{identifier(attributename)}Type, see {@attributeName  %{attributename}} at specification"/>
                                            </if>
                                            <if test=" valuetype ='String' and identifier(attributename)='Concave' ">
                                                <set parameter="_a%{identifier(attributename)}   the attribute's type is Boolean"/>
                                            </if>
                                        </else>
                                    </if>
                                    <set parameters="%{parameters}%{sep}%{parameter}" />
                                    <set sep=";"/>
                                </group>
                                <if test="parameters != ''" ><code>*@param %{replace(';','\n    *@param ',parameters)}</code></if>
                                <code>*@return   return  the element {@odfLink %{elementqname}}
    */
    public %{childname} create%{identifier(elementname)}Element(</code>
                            </else>
                        </if>
                        <set parameters = ""/>
                        <set sep="" />
                        <group>
                            <set hasparameters="true" />
                            <if test="valuetype != conversiontype " >
                                <if test="valuetype ='enum' and attributeqname != 'style:num-format'">
                                    <if test="not endswith(conversiontype,'type') ">
                                        <set parameter="Odf%{identifier(conversiontype)}Type   _a%{identifier(attributename)}"/>
                                        <else>
                                            <set parameter="Odf%{identifier(conversiontype)}   _a%{identifier(attributename)}"/>
                                        </else>
                                    </if>
                                    <else>
                                        <if test="attributeqname ='style:num-format'">
                                            <set parameter="String   _a%{identifier(attributename)}"/>
                                        </if>
                                    </else>
                                </if>
                                <if test=" valuetype ='Integer' or valuetype ='Boolean' or valuetype='String'">
                                    <set parameter="%{valuetype}   _a%{identifier(attributename)}"/>
                                </if>
                                <if test=" valuetype='javax.xml.datatype.Duration' or valuetype ='javax.xml.datatype.XMLGregorianCalendar' ">
                                    <set parameter="%{local_name(valuetype)}   _a%{identifier(attributename)}"/>
                                </if>
                                <if test=" valuetype ='String' and (identifier(attributename)='ValueType' or identifier(attributename)='Display')">
                                    <set parameter="Odf%{identifier(conversiontype)}Type   _a%{identifier(attributename)}"/>
                                </if>
                                <if test=" valuetype ='String' and identifier(attributename)='LegendExpansion' ">
                                    <set parameter="Odf%{identifier(attributename)}Type   _a%{identifier(attributename)}"/>
                                </if>
                                <if test=" valuetype ='String' and attributename ='style:num-format' ">
                                    <set parameter="String   _a%{identifier(attributename)}"/>
                                </if>
                                <if test=" valuetype ='String' and (identifier(attributename)='Type' or identifier(attributename)='Orientation' or identifier(attributename)='SortMode')">
                                    <set parameter="Odf%{identifier(conversiontype)}Type   _a%{identifier(attributename)}"/>
                                </if>
                                <else>
                                    <set parameter="%{local_name(valuetype)}   _a%{identifier(attributename)}" />
                                    <if test=" valuetype ='String' and (identifier(attributename)='ValueType' or identifier(attributename)='MemberType') ">
                                        <set parameter="Odf%{identifier(attributename)}   _a%{identifier(attributename)}"/>
                                    </if>
                                    <if test=" valuetype ='String' and (identifier(attributename)='LegendExpansion' or identifier(attributename)='SortMode')">
                                        <set parameter="Odf%{identifier(attributename)}Type   _a%{identifier(attributename)}"/>
                                    </if>
                                    <if test=" valuetype ='String' and identifier(attributename)='Concave' ">
                                        <set parameter="Boolean   _a%{identifier(attributename)}"/>
                                    </if>
                                </else>
                            </if>
                            <set parameters="%{parameters}%{sep}%{parameter}" />
                            <set sep=", "/>
                        </group>
                        <code>%{parameters})
    {
        %{childname}  _n%{identifier(elementname)} = ((OdfFileDom)this.ownerDocument).createOdfElement(%{childname}.class);</code>
                        <group>
                            <ref name="convertenumvaluetype"/>
                            <if test="elementbasename = 'styleproperties' or attributeqname ='chart:class'" >
                                <else><code>
        _n%{identifier(elementname)}.set%{identifier(attributename)}( _a%{identifier(attributename)});</code>
                                </else>
                            </if>
                        </group><code>
        this.appendChild( _n%{identifier(elementname)});
        return  _n%{identifier(elementname)};
    }
                        </code>
                    </subattribute>
                    <if test="hasparameters='false'" >
                        <set selement="%{elementqname}"/>
                        <if test="not contains(selements,selement,';')">

                            <if test="elementname='hyperlink'or elementname='measure' or elementqname ='presentation:event-listener'">
                                <set childname="org.odftoolkit.odfdom.dom.element.%{prefix(elementqname)}.Odf%{identifier(elementname)}Element"/>
                                <else>
                                    <set childname = "Odf%{identifier(elementname)}Element" />
                                </else>
                            </if>

                            <code>
    /**
    * Create child element {@odfLink %{elementqname}}.
    *
    *@return   return  the element {@odfLink %{elementqname}}
    */
    public %{childname} create%{identifier(elementname)}Element()
    {
        %{childname}  _n%{identifier(elementname)} = ((OdfFileDom)this.ownerDocument).createOdfElement(%{childname}.class);
        this.appendChild( _n%{identifier(elementname)});
        return  _n%{identifier(elementname)};
    }
                            </code>
                            <set selements="%{selements};%{selement}" />

                        </if>
                    </if>
                </children>
                <code>
}
                </code>
            </file>
        </foreach>
    </define>

<!-- generate OdfNamespace java file -->
    <define name="gen_OdfNamespace">
        <set classname="org.odftoolkit.odfdom.pkg.manifest.OdfNamespace"/>
        <file path="%{prefix(classname)}" name="%{local_name(classname)}" extensions="java">
            <ref name="header"/>
            <code>
package %{prefix(classname)};

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.xml.namespace.NamespaceContext;
import org.odftoolkit.odfdom.dom.OdfName;

/**
 * class keeping some constants for OpenDocument namespaces
 */
public class %{local_name(classname)} implements Comparable, NamespaceContext {

    private static Map&lt;String, String&gt; namesspacesByPrefix;
    private static Map&lt;String, String&gt; namesspacesByUri;

    static private enum OOoNamespace {
            </code>
            <foreach type="namespace" seperator=","><code>
        %{toupper(namespaceprefix)}("%{namespaceprefix}", "%{namespaceuri}")</code>
            </foreach>
            <code>;
            </code>
            <code>
        private String mPrefix;
        private String mNamespaceUri;

        OOoNamespace(String prefix, String namespaceUri) {
            mPrefix = prefix;
            mNamespaceUri = namespaceUri;
        }
    }

    static {
        namesspacesByPrefix = new HashMap&lt;String, String&gt;();
        namesspacesByUri = new HashMap&lt;String, String&gt;();
        for (OOoNamespace ns : OOoNamespace.values()) {
            namesspacesByPrefix.put(ns.mPrefix, ns.mNamespaceUri);
            namesspacesByUri.put(ns.mNamespaceUri, ns.mPrefix);
        }
    }
    private String m_uri;
    private String m_prefix;

    public %{local_name(classname)}(){}

    private %{local_name(classname)}(String uri) {
        this(null, uri);
    }

    private %{local_name(classname)}(String prefix, String uri) {
        if (uri != null &amp;&amp; uri.length() > 0) {
            m_uri = uri;
        }
        if (prefix != null &amp;&amp; prefix.length() > 0) {
            m_prefix = prefix;
        }
    }

    public OdfName getOdfName(String name) {
        int i = name.indexOf(':');
        if (i >= 0) {
            return OdfName.get(this, name.substring(i + 1));
        } else {
            return OdfName.get(this, name);
        }
    }
    private static SortedSet m_namespaces = new TreeSet();

    public static OdfNamespace get(String uri) {
        OdfNamespace newns = new OdfNamespace(uri);
        SortedSet tail = m_namespaces.tailSet(newns);
        if (tail.size() > 0) {
            OdfNamespace result = (OdfNamespace) tail.first();
            if (result.equals(uri)) {
                return result;
            }
        }
        // no namespace found, create a new one
        m_namespaces.add(newns);
        return newns;
    }

    public static OdfNamespace get(String prefix, String uri) {
        OdfNamespace newns = new OdfNamespace(prefix, uri);
        SortedSet tail = m_namespaces.tailSet(newns);
        for (Object obj : tail) {
            OdfNamespace result = (OdfNamespace) obj;
            if (tail.first().equals(uri)) {
                if (result.getPrefix().equals(prefix)) {
                    return result;
                } else {
                    // try to find a matching prefix as long as the uri matches
                    continue;
                }
            } else {
                // uri doesn't match any longer. stop searching
                break;
            }
        }
        // no namespace found, create a new one
        m_namespaces.add(newns);
        return newns;

    }

    public boolean hasPrefix() {
        return m_prefix != null;
    }

    public String getPrefix() {
        return m_prefix;
    }

    public String getUri() {
        return m_uri;
    }

    @Override
    public boolean equals(Object obj) {
        if (m_uri != null) {
            return m_uri.equals(obj.toString());
        } else {
            return m_uri == obj;
        }
    }

    @Override
    public int hashCode() {
        if (m_uri != null) {
            return m_uri.hashCode();
        } else {
            return 0;
        }
    }

    @Override
    public String toString() {
        return m_uri;
    }

    public static String[] splitQName(String qname) {
        String localpart = qname;
        String prefix = null;
        int colon = qname.indexOf(':');
        if (colon > 0) {
            localpart = qname.substring(colon + 1);
            prefix = qname.substring(0, colon);
        }
        return new String[]{prefix, localpart};
    }

    public static String getPrefixPart(String qname) {
        return splitQName(qname)[0];
    }

    public static String getLocalPart(String qname) {
        return splitQName(qname)[1];
    }

    public int compareTo(Object o) {
        return toString().compareTo(o.toString());
    }

    // 2DO: use only constants from Enumeration
    // 2DO: move Namespaceconstants into OdfName.. (?)
            </code>
            <foreach type="namespace">
                <code>    public static final OdfNamespace %{toupper(namespaceprefix)} = get("%{namespaceprefix}", "%{namespaceuri}");
                </code>
            </foreach>
            <code>
    public String getNamespaceURI(String prefix) {
        return namesspacesByPrefix.get(prefix);
    }

    public static String getNamespaceURIByPrefix(String prefix) {
        return namesspacesByPrefix.get(prefix);
    }

    public String getPrefix(String namespaceUri) {
        return namesspacesByUri.get(namespaceUri);
    }

    public Iterator getPrefixes(String namespaceuri) {
        return namesspacesByPrefix.keySet().iterator();
    }
}
            </code>
        </file>
    </define>

<!-- generate doc element java classes ===================================== -->
    <define name="gen_manifest_pkg_elements">
        <foreach type="element">
            <set classname="org.odftoolkit.odfdom.pkg.%{prefix(elementqname)}.%{identifier(elementname)}"/>
            <set baseclassname="org.odftoolkit.odfdom.pkg.%{prefix(elementqname)}.element.Odf%{identifier(elementname)}Element"/>
            <file path="org/odftoolkit/odfdom/pkg/%{prefix(elementqname)}" name="%{identifier(elementname)}" extensions="java">
                <ref name="docheader"/>
                <code>
package %{prefix(classname)};

import org.odftoolkit.odfdom.doc.OdfFileDom;
import %{baseclassname};

/**
 *
 */
public class %{local_name(classname)} extends %{local_name(baseclassname)}
{
    public %{local_name(classname)}( OdfFileDom _aOwnerDoc )
    {
        super( _aOwnerDoc );
    }
}
                </code>
            </file>
        </foreach>
    </define>

<!-- create org.odftoolkit.odfdom.doc.element.OdfElementFactory.java ========== -->
    <define name="gen_OdfElementFactory">
        <file path="org/odftoolkit/odfdom/pkg/manifest" name="OdfElementFactory" extensions="java">
            <ref name="header"/>
            <code>
package org.odftoolkit.odfdom.pkg.manifest;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import org.odftoolkit.odfdom.doc.OdfFileDom;
import org.odftoolkit.odfdom.dom.element.OdfElement;
import org.odftoolkit.odfdom.dom.OdfName;
import org.w3c.dom.DOMException;
import org.odftoolkit.odfdom.doc.element.OdfDefault;

public class OdfElementFactory {

    private static Map&lt;OdfName, Class&gt; m_elementTypes;

    static {
        populateElementTypes();
    }

    private synchronized static void populateElementTypes() {
        if (m_elementTypes != null) {
            return;
        }
        m_elementTypes = new HashMap&lt;OdfName, Class&gt;();
            </code>
            <foreach type="element">
                <set classname="org.odftoolkit.odfdom.pkg.%{prefix(elementqname)}.%{identifier(elementname)}"/>
<!-- we must use the full qualified class name here as we have equal element names in different namespaces! -->
                <code>        m_elementTypes.put(%{classname}.ELEMENT_NAME, %{classname}.class);
                </code>
            </foreach>
            <code>    }

    public static void mapOdfNameToClass(OdfName odfName, Class className){
        m_elementTypes.put(odfName, className);
    }

    public static OdfElement createOdfElement(OdfFileDom ownerDocument, OdfName name) throws DOMException {
        OdfElement e = null;

        // lookup registered class for qname
        Class elementClass = m_elementTypes.get(name);

        // if a class was registered create an instance of that class
        if (elementClass != null) {
            try {
                Constructor ctor = elementClass.getConstructor(new Class[]{OdfFileDom.class});
                e = (OdfElement) ctor.newInstance(new Object[]{ownerDocument});
                return e;
            } catch (Exception cause) {
                // an exception at this point is a bug. Throw an Error
                throw new Error("OdfDOM error in element factory", cause);
            }
        }

        // otherwise create the default class for odf
        return (OdfElement) new OdfDefault(ownerDocument, name);
    }
}
            </code>
        </file>
    </define>
</template>
