/*
Copyright (c) 2012 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 :
* 2012-05-05 Marco Riccardi - initial implementation
*
*/
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 Iff extends Formula implements IElement {
public static final String NAME = "Iff";
private INode parentNode;
private Integer pid = null;
private Formula formula1 = null;
private Formula formula2 = null;
private Iff(INode parentNode){
this.parentNode = parentNode;
}
public INode getParentNode() {
return parentNode;
}
public static Iff newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
Iff iff = new Iff(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("pid")){
if (iff.pid == null){
try {iff.pid = Integer.parseInt(xmlAttribute.getValue());}
catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
} 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 (Formula.isFormula(xmlElement.getName())){
if (iff.formula1 == null) {
iff.formula1 = Formula.newInstance(iff, xmlElementList);
} else if (iff.formula2 == null) {
iff.formula2 = Formula.newInstance(iff, xmlElementList);
} else {
throw new ElementParseException(ElementParseException.TOO_MANY_ELEMENTS, "XMLElement list contains too many elements <Formula> in element " + NAME, xmlElementList);
}
} else
throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
}
}
return iff;
}
public LinkedList<XMLElement> getXMLElementList() {
LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
XMLElement xmlElement = new XMLElement(NAME);
if (this.pid != null) {
XMLAttribute xmlAttribute = new XMLAttribute("pid", this.pid);
xmlElement.getXMLAttributeList().add(xmlAttribute);
}
boolean isEmpty = !(formula1 != null || formula2 != null);
xmlElement.setEmpty(isEmpty);
xmlElementList.add(xmlElement);
if (this.formula1 != null){
xmlElementList.addAll(this.formula1.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 Integer getPid(){
return pid;
}
public Formula getFormula1(){
return formula1;
}
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();
}
}