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

Java tutorial

Introduction

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

Source

/**
 *  This class represents an XSD choice element.
 * 
 *  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/>.
 *  
 *  Please send inquiries to; steve@inverse2.com
 *
 * $Revision: 1.3 $
 * 
 * $Log: XSDChoice.java,v $
 * Revision 1.3  2008/07/01 17:26:46  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:41  stevewdh
 * *** empty log message ***
 *
 * Revision 1.2  2007/09/30 13:09:05  stephen harding
 * Added contact details to license header.
 *
 * Revision 1.1  2007/09/15 16:09:05  stephen harding
 * Added header.
 *
 *
 */

package com.init.octo.schema;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.jdom2.Element;

import com.init.octo.util.Logger;

public class XSDChoice extends XSDGroupType {

    private static Logger log = Logger.getLogger(XSDChoice.class.getName());
    private List<XMLType> group;

    public XSDChoice(int indent) {
        this.indent = indent;
        groupType = "XSDChoice";
    }

    /**
     * This method builds a choice element
     * @param root
     *            - the choice 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 choice");

        id = root.getAttributeValue(XSDSchema.ID_ATT);
        maxOccurs = root.getAttributeValue(XSDSchema.MAXOCCURS_ATT);
        minOccurs = root.getAttributeValue(XSDSchema.MINOCCURS_ATT);

        group = new LinkedList<XMLType>();

        String elementName;

        for (Element element : root.getChildren()) {
            elementName = element.getName();
            log.debug("" + indent + ": " + "Child element <" + elementName + "> found");

            if (elementName.equals(XSDSchema.ANNOTATION)) {
                annotation = element.getTextTrim();
            } else if (elementName.equals(XSDSchema.ELEMENT)) {
                log.debug("" + indent + ": " + "Adding element to the list of child elements");
                XSDElement e = new XSDElement(indent + 1);
                if (e.build(element, cache, parentName) != true) {
                    log.error("Error building the element");
                    return (false);
                }

                group.add(e);
            } else if (elementName.equals(XSDSchema.GROUP)) {

                log.debug("" + indent + ": " + "Adding group to the list of child elements");
                XSDGroupType g = new XSDElementGroup(indent);
                if (g.build(element, cache, parentName) != true) {
                    log.error("Error building a group");
                    return (false);
                }

                group.add(g);

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

                log.debug("" + indent + ": " + "Adding choice to the list of child elements");

                XSDChoice c = new XSDChoice(indent);
                if (c.build(element, cache, parentName) != true) {
                    log.error("Error building a choice");
                    return (false);
                }

                group.add(c);
            } else if (elementName.equals(XSDSchema.SEQUENCE)) {

                log.debug("" + indent + ": " + "Adding sequence to the list of child elements");
                XSDSequence s = new XSDSequence(indent); // child elements all at the same level

                if (s.build(element, cache, parentName) != true) {
                    log.debug("Error building a sequence");
                    return (false);
                }

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

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

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

        return (true);

    } // end build()

    public void show(int preIndent, boolean subCachedType, boolean htmlForm) {
        if (group == null) {
            return;
        }

        int num = 1;
        log.debug("+++ Start GROUP: " + groupType);

        for (Iterator<XMLType> i = group.iterator(); i.hasNext();) {
            log.debug("*** START CHOICE: " + num);
            XSDElementGroup e = (XSDElementGroup) i.next();
            e.show(preIndent, subCachedType, htmlForm);
            log.debug("*** END   CHOICE: " + num);
            num++;
        }

        log.debug("+++ End   GROUP: " + groupType);
    }

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

} // end class XSDChoice