1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.qedeq.kernel.xml.handler.parser; |
19 |
| |
20 |
| import java.util.ArrayList; |
21 |
| import java.util.List; |
22 |
| |
23 |
| import org.qedeq.kernel.parser.Operator; |
24 |
| import org.qedeq.kernel.xml.parser.AbstractSimpleHandler; |
25 |
| import org.qedeq.kernel.xml.parser.SaxDefaultHandler; |
26 |
| import org.qedeq.kernel.xml.parser.SimpleAttributes; |
27 |
| import org.qedeq.kernel.xml.parser.SyntaxException; |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| public final class ParserHandler extends AbstractSimpleHandler { |
37 |
| |
38 |
| |
39 |
| private List operators = new ArrayList(); |
40 |
| |
41 |
| |
42 |
| private String startSymbol; |
43 |
| |
44 |
| |
45 |
| private String qedeq; |
46 |
| |
47 |
| |
48 |
| private String qedeqArgument; |
49 |
| |
50 |
| |
51 |
| private Integer priority; |
52 |
| |
53 |
| |
54 |
| private Integer min; |
55 |
| |
56 |
| |
57 |
| private Integer max; |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
81
| public ParserHandler(final SaxDefaultHandler defaultHandler) {
|
66 |
81
| super(defaultHandler, "parser");
|
67 |
| } |
68 |
| |
69 |
81
| public final void init() {
|
70 |
81
| operators.clear();
|
71 |
| } |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
81
| public final List getOperators() {
|
79 |
81
| return operators;
|
80 |
| } |
81 |
| |
82 |
1317
| public final void startElement(final String name, final SimpleAttributes attributes)
|
83 |
| throws SyntaxException { |
84 |
1317
| if (getStartTag().equals(name)) {
|
85 |
| |
86 |
1236
| } else if ("prefixOperator".equals(name)) {
|
87 |
426
| setBasisAttributes(attributes);
|
88 |
426
| addOperator(Operator.SIMPLE_PREFIX);
|
89 |
810
| } else if ("infixOperator".equals(name)) {
|
90 |
648
| setBasisAttributes(attributes);
|
91 |
648
| addOperator(Operator.INFIX);
|
92 |
162
| } else if ("functionOperator".equals(name)) {
|
93 |
0
| setBasisAttributes(attributes);
|
94 |
0
| addOperator(Operator.FUNCTION);
|
95 |
162
| } else if ("complexOperator".equals(name)) {
|
96 |
162
| setBasisAttributes(attributes);
|
97 |
162
| final String separatorSymbol = attributes.getString("separatorSymbol");
|
98 |
162
| if (separatorSymbol == null || separatorSymbol.length() == 0) {
|
99 |
0
| throw new SyntaxException(100, "Attribute \"separatorSymbol\" is empty");
|
100 |
| } |
101 |
162
| final String endSymbol = attributes.getString("endSymbol");
|
102 |
162
| if (endSymbol == null || endSymbol.length() == 0) {
|
103 |
0
| throw new SyntaxException(100, "Attribute \"separatorSymbol\" is empty");
|
104 |
| } |
105 |
162
| if (max == null) {
|
106 |
81
| operators.add(new Operator(startSymbol, separatorSymbol, endSymbol, qedeq,
|
107 |
| qedeqArgument, priority.intValue(), min.intValue())); |
108 |
| } else { |
109 |
81
| operators.add(new Operator(startSymbol, separatorSymbol, endSymbol, qedeq,
|
110 |
| qedeqArgument, priority.intValue(), min.intValue(), max.intValue())); |
111 |
| } |
112 |
| } else { |
113 |
0
| throw SyntaxException.createUnexpectedTagException(name);
|
114 |
| } |
115 |
| } |
116 |
| |
117 |
1074
| private void addOperator(final int type) {
|
118 |
1074
| if (max == null) {
|
119 |
456
| operators.add(new Operator(startSymbol, qedeq, qedeqArgument, priority.intValue(),
|
120 |
| type, min.intValue())); |
121 |
| } else { |
122 |
618
| operators.add(new Operator(startSymbol, qedeq, qedeqArgument, priority.intValue(),
|
123 |
| type, min.intValue(), max.intValue())); |
124 |
| } |
125 |
| } |
126 |
| |
127 |
1236
| private void setBasisAttributes(final SimpleAttributes attributes) throws SyntaxException {
|
128 |
1236
| startSymbol = attributes.getString("startSymbol");
|
129 |
1236
| if (startSymbol == null || startSymbol.length() == 0) {
|
130 |
0
| throw new SyntaxException(100, "Attribute \"symbol\" is empty");
|
131 |
| } |
132 |
1236
| qedeq = attributes.getString("qedeq");
|
133 |
1236
| if (qedeq == null || qedeq.length() == 0) {
|
134 |
0
| throw new SyntaxException(100, "Attribute \"qedeq\" is empty");
|
135 |
| } |
136 |
1236
| qedeqArgument = attributes.getString("qedeqArgument");
|
137 |
1236
| priority = attributes.getInteger("priority");
|
138 |
1236
| if (priority == null || priority.intValue() < 0) {
|
139 |
0
| throw new SyntaxException(100, "Attribute \"priority\" is empty");
|
140 |
| } |
141 |
1236
| min = attributes.getInteger("min");
|
142 |
1236
| if (min == null) {
|
143 |
0
| min = new Integer(0);
|
144 |
| } |
145 |
1236
| max = attributes.getInteger("max");
|
146 |
| } |
147 |
| |
148 |
1317
| public final void endElement(final String name) throws SyntaxException {
|
149 |
1317
| if (getStartTag().equals(name)) {
|
150 |
| |
151 |
1236
| } else if ("prefixOperator".equals(name)) {
|
152 |
| |
153 |
810
| } else if ("infixOperator".equals(name)) {
|
154 |
| |
155 |
162
| } else if ("functionOperator".equals(name)) {
|
156 |
| |
157 |
162
| } else if ("complexOperator".equals(name)) {
|
158 |
| |
159 |
| } else { |
160 |
0
| throw SyntaxException.createUnexpectedTagException(name);
|
161 |
| } |
162 |
| } |
163 |
| |
164 |
| } |