Package org.mizartools.system.xml

Source Code of org.mizartools.system.xml.Constructors

/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   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

   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.
*/
/*  Contributors :
*  2011-02-05 Marco Riccardi - initial implementation
*  2011-03-25 Marco Riccardi - implementation of visitor pattern
*
*/
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLAttribute;
import org.mizartools.utility.xml.XMLElement;

public class Constructors implements IElement {


    public static final String NAME = "Constructors";

    private INode parentNode;
    private String aid = null;
    private String mizfiles = null;

    private SignatureWithCounts signatureWithCounts = null;
    private Signature signature = null;
    private ConstrCounts constrCounts = null;
    private LinkedList<Constructor> constructorList = new LinkedList<Constructor>();


    private Constructors(INode parentNode){
        this.parentNode = parentNode;
    }

    public INode getParentNode() {
        return parentNode;
    }

    public static Constructors newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        Constructors constructors = new Constructors(parentNode);
        XMLElement xmlElement = xmlElementList.poll();
        if (!xmlElement.getName().equals(NAME)){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a " + NAME, xmlElementList);
        }
        for (XMLAttribute xmlAttribute : xmlElement.getXMLAttributeList()){
            if (xmlAttribute.getName().equals("aid")){
                if (constructors.aid == null){
                    constructors.aid = xmlAttribute.getValue();
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                }
            } else if (xmlAttribute.getName().equals("mizfiles")){
                if (constructors.mizfiles == null){
                    constructors.mizfiles = xmlAttribute.getValue();
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                }
            } else
                throw new ElementParseException(ElementParseException.NOT_VALID_ATTRIBUTE_ELEMENT, "XMLElement has not a valid attribute " + xmlAttribute.getName(), xmlElementList);
        }

        if (!xmlElement.isEmpty()){
            while (true){
                xmlElement = xmlElementList.peek();
                if (xmlElement == null){
                    throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
                }
                if (xmlElement.getName().equals(NAME) && xmlElement.isEndElement()){
                    xmlElement = xmlElementList.poll();
                    break;
                }
                if (xmlElement.getName().equals("SignatureWithCounts")){
                    if (constructors.signatureWithCounts == null) {
                        constructors.signatureWithCounts = SignatureWithCounts.newInstance(constructors, xmlElementList);
                    } else {
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <SignatureWithCounts> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("Signature")){
                    if (constructors.signature == null) {
                        constructors.signature = Signature.newInstance(constructors, xmlElementList);
                    } else {
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Signature> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("ConstrCounts")){
                    if (constructors.constrCounts == null) {
                        constructors.constrCounts = ConstrCounts.newInstance(constructors, xmlElementList);
                    } else {
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <ConstrCounts> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("Constructor")){
                    constructors.constructorList.add(Constructor.newInstance(constructors, xmlElementList));
                } else
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
            }
        }

        return constructors;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        XMLElement xmlElement = new XMLElement(NAME);
        if (this.aid != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("aid", this.aid);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.mizfiles != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("mizfiles", this.mizfiles);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        boolean isEmpty = !(signatureWithCounts != null
         || signature != null || constrCounts != null
         || !constructorList.isEmpty());
        xmlElement.setEmpty(isEmpty);
        xmlElementList.add(xmlElement);
        if (this.signatureWithCounts != null){
            xmlElementList.addAll(this.signatureWithCounts.getXMLElementList());
        }
        if (this.signature != null){
            xmlElementList.addAll(this.signature.getXMLElementList());
        }
        if (this.constrCounts != null){
            xmlElementList.addAll(this.constrCounts.getXMLElementList());
        }
        for (Constructor constructor : this.constructorList){
            xmlElementList.addAll(constructor.getXMLElementList());
        }
        if (!isEmpty) {
            xmlElement = new XMLElement(NAME);
            xmlElement.setEndElement(true);
            xmlElementList.add(xmlElement);
        }
        return xmlElementList;
    }

    public String getAid(){
        return aid;
    }


    public SignatureWithCounts getSignatureWithCounts(){
        return signatureWithCounts;
    }

    public Signature getSignature(){
        return signature;
    }

    public ConstrCounts getConstrCounts(){
        return constrCounts;
    }

    public LinkedList<Constructor> getConstructorList(){
        return constructorList;
    }

  @Override
  public void accept(Visitor visitor) {
    if (visitor.visit(this)) {
      for(Constructor constructor: this.constructorList) {
        constructor.accept(visitor);
      }
    }
    visitor.endVisit(this);
  }

  @Override
  public String toString(){
    ToStringVisitor toStringVisitor = new ToStringVisitor();
    this.accept(toStringVisitor);
    return toStringVisitor.toString();
  }

}
TOP

Related Classes of org.mizartools.system.xml.Constructors

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.