001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.kernel.xml.handler.parser;
017
018 import java.util.ArrayList;
019 import java.util.List;
020
021 import org.qedeq.kernel.bo.parser.Operator;
022 import org.qedeq.kernel.xml.common.XmlSyntaxException;
023 import org.qedeq.kernel.xml.handler.common.AbstractSimpleHandler;
024 import org.qedeq.kernel.xml.handler.common.SaxDefaultHandler;
025 import org.qedeq.kernel.xml.handler.common.SimpleAttributes;
026
027
028 /**
029 * Parses list of operators. Result is a list of all parsed operators.
030 *
031 * @author Michael Meyling
032 */
033 public final class ParserHandler extends AbstractSimpleHandler {
034
035 /** List of all Operators. */
036 private List operators = new ArrayList();
037
038 /** Operator start symbol. */
039 private String startSymbol;
040
041 /** QEDEQ representation. E.g. "PREDCON". */
042 private String qedeq;
043
044 /** QEDEQ argument. E.g. "equal". */
045 private String qedeqArgument;
046
047 /** Operator priority. */
048 private Integer priority;
049
050 /** Minimum argument number. */
051 private Integer min;
052
053 /** Maximum argument number. */
054 private Integer max;
055
056
057 /**
058 * Handle a parser XML file.
059 *
060 * @param defaultHandler Startup handler.
061 */
062 public ParserHandler(final SaxDefaultHandler defaultHandler) {
063 super(defaultHandler, "parser");
064 }
065
066 public final void init() {
067 operators.clear();
068 }
069
070 /**
071 * Get list of operators.
072 *
073 * @return Operator list.
074 */
075 public final List getOperators() {
076 return operators;
077 }
078
079 public final void startElement(final String name, final SimpleAttributes attributes)
080 throws XmlSyntaxException {
081 if (getStartTag().equals(name)) {
082 // nothing todo
083 } else if ("prefixOperator".equals(name)) {
084 setBasisAttributes(name, attributes);
085 addOperator(Operator.SIMPLE_PREFIX);
086 } else if ("infixOperator".equals(name)) {
087 setBasisAttributes(name, attributes);
088 addOperator(Operator.INFIX);
089 } else if ("functionOperator".equals(name)) {
090 setBasisAttributes(name, attributes);
091 addOperator(Operator.FUNCTION);
092 } else if ("complexOperator".equals(name)) {
093 setBasisAttributes(name, attributes);
094 final String separatorSymbol = attributes.getString("separatorSymbol");
095 if (separatorSymbol == null) {
096 throw XmlSyntaxException.createEmptyAttributeException(name, "separatorSymbol");
097 }
098 if (separatorSymbol.length() == 0) {
099 throw XmlSyntaxException.createMissingAttributeException(name, "separatorSymbol");
100 }
101 final String endSymbol = attributes.getString("endSymbol");
102 if (endSymbol == null) {
103 throw XmlSyntaxException.createEmptyAttributeException(name, "endSymbol");
104 }
105 if (endSymbol.length() == 0) {
106 throw XmlSyntaxException.createMissingAttributeException(name, "endSymbol");
107 }
108 if (max == null) {
109 operators.add(new Operator(startSymbol, separatorSymbol, endSymbol, qedeq,
110 qedeqArgument, priority.intValue(), min.intValue()));
111 } else {
112 operators.add(new Operator(startSymbol, separatorSymbol, endSymbol, qedeq,
113 qedeqArgument, priority.intValue(), min.intValue(), max.intValue()));
114 }
115 } else {
116 throw XmlSyntaxException.createUnexpectedTagException(name);
117 }
118 }
119
120 private void addOperator(final int type) {
121 if (max == null) {
122 operators.add(new Operator(startSymbol, qedeq, qedeqArgument, priority.intValue(),
123 type, min.intValue()));
124 } else {
125 operators.add(new Operator(startSymbol, qedeq, qedeqArgument, priority.intValue(),
126 type, min.intValue(), max.intValue()));
127 }
128 }
129
130 private void setBasisAttributes(final String element, final SimpleAttributes attributes)
131 throws XmlSyntaxException {
132 startSymbol = attributes.getString("startSymbol");
133 if (startSymbol == null) {
134 throw XmlSyntaxException.createMissingAttributeException(element, "startSymbol");
135 }
136 if (startSymbol.length() == 0) {
137 throw XmlSyntaxException.createEmptyAttributeException(element, "startSymbol");
138 }
139 qedeq = attributes.getString("qedeq");
140 if (qedeq == null) {
141 throw XmlSyntaxException.createMissingAttributeException(element, "qedeq");
142 }
143 if (qedeq.length() == 0) {
144 throw XmlSyntaxException.createEmptyAttributeException(element, "qedeq");
145 }
146 qedeqArgument = attributes.getString("qedeqArgument");
147 priority = attributes.getInteger("priority");
148 if (priority == null || priority.intValue() < 0) {
149 throw XmlSyntaxException.createMissingAttributeException(element, "priority");
150 }
151 min = attributes.getInteger("min");
152 if (min == null) {
153 min = new Integer(0);
154 }
155 max = attributes.getInteger("max");
156 }
157
158 public final void endElement(final String name) throws XmlSyntaxException {
159 if (getStartTag().equals(name)) {
160 // nothing to do
161 } else if ("prefixOperator".equals(name)) {
162 // nothing to do
163 } else if ("infixOperator".equals(name)) {
164 // nothing to do
165 } else if ("functionOperator".equals(name)) {
166 // nothing to do
167 } else if ("complexOperator".equals(name)) {
168 // nothing to do
169 } else {
170 throw XmlSyntaxException.createUnexpectedTagException(name);
171 }
172 }
173
174 }
|