1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
2 | * |
3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
4 | * |
5 | * "Hilbert II" is free software; you can redistribute |
6 | * it and/or modify it under the terms of the GNU General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | */ |
15 | |
16 | package org.qedeq.kernel.xml.handler.parser; |
17 | |
18 | import java.util.ArrayList; |
19 | import java.util.List; |
20 | |
21 | import org.qedeq.kernel.bo.parser.Operator; |
22 | import org.qedeq.kernel.xml.common.XmlSyntaxException; |
23 | import org.qedeq.kernel.xml.handler.common.AbstractSimpleHandler; |
24 | import org.qedeq.kernel.xml.handler.common.SaxDefaultHandler; |
25 | import org.qedeq.kernel.xml.handler.common.SimpleAttributes; |
26 | |
27 | |
28 | /** |
29 | * Parses list of operators. Result is a list of all parsed operators. |
30 | * |
31 | * @author Michael Meyling |
32 | */ |
33 | public final class ParserHandler extends AbstractSimpleHandler { |
34 | |
35 | /** List of all Operators. */ |
36 | private List operators = new ArrayList(); |
37 | |
38 | /** Operator start symbol. */ |
39 | private String startSymbol; |
40 | |
41 | /** QEDEQ representation. E.g. "PREDCON". */ |
42 | private String qedeq; |
43 | |
44 | /** QEDEQ argument. E.g. "equal". */ |
45 | private String qedeqArgument; |
46 | |
47 | /** Operator priority. */ |
48 | private Integer priority; |
49 | |
50 | /** Minimum argument number. */ |
51 | private Integer min; |
52 | |
53 | /** Maximum argument number. */ |
54 | private Integer max; |
55 | |
56 | |
57 | /** |
58 | * Handle a parser XML file. |
59 | * |
60 | * @param defaultHandler Startup handler. |
61 | */ |
62 | public ParserHandler(final SaxDefaultHandler defaultHandler) { |
63 | super(defaultHandler, "parser"); |
64 | } |
65 | |
66 | public final void init() { |
67 | operators.clear(); |
68 | } |
69 | |
70 | /** |
71 | * Get list of operators. |
72 | * |
73 | * @return Operator list. |
74 | */ |
75 | public final List getOperators() { |
76 | return operators; |
77 | } |
78 | |
79 | public final void startElement(final String name, final SimpleAttributes attributes) |
80 | throws XmlSyntaxException { |
81 | if (getStartTag().equals(name)) { |
82 | // nothing todo |
83 | } else if ("prefixOperator".equals(name)) { |
84 | setBasisAttributes(name, attributes); |
85 | addOperator(Operator.SIMPLE_PREFIX); |
86 | } else if ("infixOperator".equals(name)) { |
87 | setBasisAttributes(name, attributes); |
88 | addOperator(Operator.INFIX); |
89 | } else if ("functionOperator".equals(name)) { |
90 | setBasisAttributes(name, attributes); |
91 | addOperator(Operator.FUNCTION); |
92 | } else if ("complexOperator".equals(name)) { |
93 | setBasisAttributes(name, attributes); |
94 | final String separatorSymbol = attributes.getString("separatorSymbol"); |
95 | if (separatorSymbol == null) { |
96 | throw XmlSyntaxException.createEmptyAttributeException(name, "separatorSymbol"); |
97 | } |
98 | if (separatorSymbol.length() == 0) { |
99 | 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 | } |