com.init.octo.schema.XSDElementTypeComplex.java Source code

Java tutorial

Introduction

Here is the source code for com.init.octo.schema.XSDElementTypeComplex.java

Source

/**
 *  This class represents a complex type XSD type.
 * 
 *  Copyright (C) 2007  Stephen Harding
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $Revision: 1.4 $
 * 
 * $Log: XSDElementTypeComplex.java,v $
 * Revision 1.4  2008/09/03 15:33:34  stevewdh
 * The attributes defined in the base type are now returned by getAttrubuteList().
 *
 * Revision 1.3  2008/07/01 17:26:45  stevewdh
 * Changed logging so that user can specify the type they want (Java, Log4J or HTML).
 *
 * Revision 1.2  2008/03/05 10:47:27  stevewdh
 * *** empty log message ***
 *
 * Revision 1.1  2007/10/04 11:06:45  stevewdh
 * *** empty log message ***
 *
 * Revision 1.1  2007/09/15 16:09:05  stephen harding
 * Added header.
 *
 *
 */

package com.init.octo.schema;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.ws.commons.schema.XmlSchemaException;
import org.jdom2.Attribute;
import org.jdom2.Element;

import com.init.octo.util.Logger;

public class XSDElementTypeComplex extends XSDElementType {

    private static Logger log = Logger.getLogger(XSDElementTypeComplex.class.getName());
    private XSDGroupType elementGroup; // group of sub-elements
    private XSDElementTypeComplex baseType;
    private XMLType baseTypeGroup; // extension/restriction elements

    public XSDElementTypeComplex(int indent) {
        this.indent = indent;
    }

    /**
     * This method builds a complex type
     *
     * @param    root - the complexType element that defines this element
     * @param    cache - a list of pre-defined XML types
     */

    public boolean build(Element root, XSDCache cache, String parentName) {

        log.debug("" + indent + ": " + "Build representation of a ComplexType");

        /** get the complex types attributes **/

        id = root.getAttributeValue(XSDSchema.ID_ATT);
        name = root.getAttributeValue(XSDSchema.NAME_ATT);
        abstractAtt = root.getAttributeValue(XSDSchema.ABSTRACT_ATT);
        mixed = root.getAttributeValue(XSDSchema.MIXED_ATT);
        block = root.getAttributeValue(XSDSchema.BLOCK_ATT);
        finalAtt = root.getAttributeValue(XSDSchema.FINAL_ATT);

        log.debug("" + indent + ": ComplexType <" + name + ">");

        for (Element rootChild : root.getChildren()) {
            String elementName = rootChild.getName();

            if (elementName.equals(XSDSchema.ANNOTATION)) {
                annotation = rootChild.getTextTrim();

            } else if (elementName.equals(XSDSchema.SIMPLECONTENT)) {

            } else if (elementName.equals(XSDSchema.COMPLEXCONTENT)) {

                for (Element child : rootChild.getChildren()) {

                    Attribute baseTypeAtt = child.getAttribute(XSDSchema.BASE_ATT);
                    if (baseTypeAtt == null) {
                        throw new XmlSchemaException("No base attribute for element [" + elementName + "]");
                    }

                    XSDElementTypeComplex cachedObject = cache.getElementTypeComplex(baseTypeAtt.getValue());
                    if (cachedObject != null) {
                        baseType = cachedObject;
                    }

                    processComplexTypeChildren(child, cache);
                }

            } else if (elementName.equals(XSDSchema.GROUP)) {
                if (elementGroup != null) {
                    throw new XmlSchemaException("Cannot define a grouping element more than once");
                }
                elementGroup = new XSDElementGroup(indent);
                elementGroup.build(rootChild, cache, parentName);

            } else if (elementName.equals(XSDSchema.ALL)) {
                if (elementGroup != null) {
                    throw new XmlSchemaException("Cannot define a grouping element more than once");
                }
                elementGroup = new XsdAll(indent);
                elementGroup.build(rootChild, cache, parentName);
            } else if (elementName.equals(XSDSchema.CHOICE)) {
                if (elementGroup != null) {
                    throw new XmlSchemaException("Cannot define a grouping element more than once");
                }

                elementGroup = new XSDChoice(indent);
                elementGroup.build(rootChild, cache, parentName);
            } else if (elementName.equals(XSDSchema.SEQUENCE)) {

                if (elementGroup != null) {
                    log.error("Cannot define a grouping element more than once");
                    return (false);
                }

                elementGroup = new XSDSequence(indent);

                if (elementGroup.build(rootChild, cache, parentName) != true) {
                    log.error("Error building a sequence object");
                    return (false);
                }
            } else if (elementName.equals(XSDSchema.ATTRIBUTE)) {

                XSDAttribute attribute = new XSDAttribute();
                if (attribute.build(rootChild, cache, parentName) != true) {
                    log.error("Error building an attribute object");
                    return (false);
                }

                attributeList.add(attribute);
            } else if (elementName.equals(XSDSchema.ATTRIBUTEGROUP)) {

                XSDAttributeGroup group = new XSDAttributeGroup();

                if (group.build(rootChild, cache, parentName) != true) {
                    log.error("Error building an attribute group object");
                    return (false);
                }

                attributeList.addAll(group.getAllAttributes());

            } else {
                log.warn("" + indent + ": " + "Unexpected element <" + elementName + "> found and ignored");
            }

        } // end for all child elements of this <element> tag

        log.debug("" + indent + ": " + "ComplexType built");

        return (true);

    } // end build()

    private void processComplexTypeChildren(Element child, XSDCache cache) {
        for (Element grandchild : child.getChildren()) {
            String grandchildName = grandchild.getName();

            if (grandchildName.equals(XSDSchema.GROUP)) {
                baseTypeGroup = new XSDElementGroup(indent);
                if (baseTypeGroup.build(grandchild, cache, parentName) != true) {
                    throw new XmlSchemaException("Error building a group object");
                }

            } else if (grandchildName.equals(XSDSchema.ALL)) {
                baseTypeGroup = new XsdAll(indent);
                if (baseTypeGroup.build(grandchild, cache, parentName) != true) {
                    throw new XmlSchemaException("Error building a ALL object");
                }
            } else if (grandchildName.equals(XSDSchema.CHOICE)) {

                baseTypeGroup = new XSDChoice(indent);

                if (baseTypeGroup.build(grandchild, cache, parentName) != true) {
                    throw new XmlSchemaException("Error building a choice object");
                }

            } else if (grandchildName.equals(XSDSchema.SEQUENCE)) {
                baseTypeGroup = new XSDSequence(indent);
                if (baseTypeGroup.build(grandchild, cache, parentName) != true) {
                    throw new XmlSchemaException("Error building a sequence object");
                }
            }
        }
    }

    /**
      * This method gets the complex types name
      */

    public String getName() {
        return (name);
    }

    public String toString() {
        return (name);
    }

    @Override
    public void show(int preIndent, boolean subCachedType, boolean htmlForm) {

        if (baseType != null) {
            baseType.show(preIndent, subCachedType, htmlForm);
        }

        if (baseTypeGroup != null) {
            baseTypeGroup.show(preIndent, subCachedType, htmlForm);
        }

        if (elementGroup != null) {
            elementGroup.show(preIndent, subCachedType, htmlForm);
        }

        return;
    }

    @Override
    public boolean getListOfSchemaItems(int preIndent, boolean subCachedType, List<SchemaItem> list) {

        if (elementGroup != null) {
            elementGroup.getListOfSchemaItems(preIndent, subCachedType, list);
        }

        return (false);
    }

    public String attributesToString() {
        StringBuilder str = new StringBuilder();

        for (XSDAttributeType att : attributeList) {
            str.append(" " + att.getName() + "=\"\"");
        }

        if (baseType != null) {
            List<XSDAttributeType> baseTypeAttributes = baseType.getAttributeList();

            for (XSDAttributeType att : baseTypeAttributes) {
                str.append(" " + att.getName() + "=\"\"");
            }
        }

        return (str.toString());
    }

    public List<SchemaItem> attributesToList(int preIndent, boolean subCachedType, List<SchemaItem> list) {
        for (XSDAttributeType att : attributeList) {
            SchemaItem item = new SchemaItem();
            item.setItem(att);
            item.setIndent(preIndent + 1);
            list.add(item);
        }
        return list;
    }

    public XSDElement getChild(String childName) {

        if (baseType != null) {
            XSDElement child = baseType.getChild(childName);
            if (child != null) {
                return (child);
            }
        }

        if (baseTypeGroup != null) {
            List<XMLType> group = baseTypeGroup.getGroup();

            for (Iterator<?> it = group.iterator(); it.hasNext();) {
                Object obj = it.next();

                if (obj instanceof XSDElement) {
                    if (childName.equals(((XSDElement) obj).getName())) {
                        return ((XSDElement) obj);
                    }
                }
            }
        }

        if (elementGroup == null) {
            return (null);
        }

        List<?> group = elementGroup.getGroup();

        for (Iterator<?> it = group.iterator(); it.hasNext();) {
            Object obj = it.next();

            if (obj instanceof XSDElement) {
                if (childName.equals(((XSDElement) obj).getName())) {
                    return ((XSDElement) obj);
                }
            }

        }

        return (null);
    }

    public List<XSDElement> getChildren() {
        List<XSDElement> children = new ArrayList<XSDElement>();

        if (baseType != null) {
            children = baseType.getChildren();
        }

        List<XMLType> group;
        if (baseTypeGroup != null) {
            group = baseTypeGroup.getGroup();
            for (Object o : group) {
                if (o instanceof XSDElement) {
                    children.add((XSDElement) o);
                }
            }
        }

        if (elementGroup != null) {
            group = elementGroup.getGroup();
            for (Object o : group) {
                if (o instanceof XSDElement) {
                    children.add((XSDElement) o);
                }
            }
        }
        return (children);
    }

    public List<XSDAttributeType> getAttributeList() {

        List<XSDAttributeType> atts = new ArrayList<XSDAttributeType>(attributeList);

        if (baseType != null) {
            atts.addAll(baseType.getAttributeList());
        }

        return (atts);
    }

    public XSDGroupType getElementGroup() {
        return (elementGroup);
    }

    @Override
    public List<XMLType> getGroup() {
        // TODO Auto-generated method stub
        return null;
    }

} // end class XSDElementTypeComplex