1
2
3
4
5
6
7
8
9
10
11
12
13
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
30
31
32
33 public final class ParserHandler extends AbstractSimpleHandler {
34
35
36 private List operators = new ArrayList();
37
38
39 private String startSymbol;
40
41
42 private String qedeq;
43
44
45 private String qedeqArgument;
46
47
48 private Integer priority;
49
50
51 private Integer min;
52
53
54 private Integer max;
55
56
57
58
59
60
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
72
73
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
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
161 } else if ("prefixOperator".equals(name)) {
162
163 } else if ("infixOperator".equals(name)) {
164
165 } else if ("functionOperator".equals(name)) {
166
167 } else if ("complexOperator".equals(name)) {
168
169 } else {
170 throw XmlSyntaxException.createUnexpectedTagException(name);
171 }
172 }
173
174 }