Package org.mizartools.system.xml

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

/*
   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.XMLElement;

public class PartialDef implements IElement {

    public static final String NAME = "PartialDef";

    private INode parentNode;

    private Formula formula1 = null;
   
  private Term term = null;
    private Formula formula2 = null;

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

    public INode getParentNode() {
        return parentNode;
    }

    public static PartialDef newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        PartialDef partialDef = new PartialDef(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);
        }

        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 (Formula.isFormula(xmlElement.getName()) && partialDef.formula1 == null && partialDef.term == null) {
                    partialDef.formula1 = Formula.newInstance(partialDef, xmlElementList);
                } else if (Term.isTerm(xmlElement.getName())){
                    if (partialDef.term == null) {
                        partialDef.term = Term.newInstance(partialDef, xmlElementList);
                    } else {
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Term> in element " + NAME, xmlElementList);
                    }
                } else if (Formula.isFormula(xmlElement.getName())){
                    if (partialDef.formula2 == null) {
                        partialDef.formula2 = Formula.newInstance(partialDef, xmlElementList);
                    } else {
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Formula> in element " + NAME, xmlElementList);
                    }
                } else
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
            }
        }

        return partialDef;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        XMLElement xmlElement = new XMLElement(NAME);
        boolean isEmpty = !(formula1 != null
         || term != null || formula2 != null);
        xmlElement.setEmpty(isEmpty);
        xmlElementList.add(xmlElement);
        if (this.formula1 != null){
            xmlElementList.addAll(this.formula1.getXMLElementList());
        }
        if (this.term != null){
            xmlElementList.addAll(this.term.getXMLElementList());
        }
        if (this.formula2 != null){
            xmlElementList.addAll(this.formula2.getXMLElementList());
        }
        if (!isEmpty) {
            xmlElement = new XMLElement(NAME);
            xmlElement.setEndElement(true);
            xmlElementList.add(xmlElement);
        }
        return xmlElementList;
    }

    public Formula getFormula1() {
    return formula1;
  }

  public Term getTerm() {
    return term;
  }

  public Formula getFormula2() {
    return formula2;
  }

  @Override
  public void accept(Visitor visitor) {
    if (visitor.visit(this)) {}
    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.PartialDef

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.