|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| package org.qedeq.kernel.xml.parser; |
|
18 |
| |
|
19 |
| import java.net.URL; |
|
20 |
| import java.util.Stack; |
|
21 |
| |
|
22 |
| import org.qedeq.kernel.log.Trace; |
|
23 |
| import org.xml.sax.Attributes; |
|
24 |
| import org.xml.sax.Locator; |
|
25 |
| import org.xml.sax.SAXException; |
|
26 |
| import org.xml.sax.SAXParseException; |
|
27 |
| import org.xml.sax.helpers.DefaultHandler; |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| public final class SaxDefaultHandler extends DefaultHandler { |
|
43 |
| |
|
44 |
| |
|
45 |
| private AbstractSimpleHandler currentHandler; |
|
46 |
| |
|
47 |
| |
|
48 |
| private Stack handlerStack = new Stack(); |
|
49 |
| |
|
50 |
| |
|
51 |
| private AbstractSimpleHandler basisHandler; |
|
52 |
| |
|
53 |
| |
|
54 |
| private ExceptionList errorList; |
|
55 |
| |
|
56 |
| |
|
57 |
| private StringBuffer buffer = new StringBuffer(2000); |
|
58 |
| |
|
59 |
| |
|
60 |
| private Locator locator; |
|
61 |
| |
|
62 |
| |
|
63 |
| private int level; |
|
64 |
| |
|
65 |
| |
|
66 |
| private Stack levelStack = new Stack(); |
|
67 |
| |
|
68 |
| |
|
69 |
| private String currentElementName; |
|
70 |
| |
|
71 |
| |
|
72 |
| private URL url; |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
250
| public SaxDefaultHandler() {
|
|
79 |
250
| super();
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
248
| public final void setExceptionList(final ExceptionList errorList) {
|
|
88 |
248
| this.errorList = errorList;
|
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
248
| public final void setDocumentLocator(final Locator locator) {
|
|
100 |
248
| this.locator = locator;
|
|
101 |
| } |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
250
| public final void setBasisDocumentHandler(final AbstractSimpleHandler handler) {
|
|
112 |
250
| basisHandler = handler;
|
|
113 |
250
| currentHandler = handler;
|
|
114 |
250
| handlerStack.clear();
|
|
115 |
250
| level = 0;
|
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
248
| public final void startDocument() throws SAXException {
|
|
122 |
248
| sendCharacters();
|
|
123 |
248
| currentHandler = basisHandler;
|
|
124 |
248
| handlerStack.clear();
|
|
125 |
248
| level = 0;
|
|
126 |
248
| currentElementName = null;
|
|
127 |
| } |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
248
| public final void endDocument() throws SAXException {
|
|
133 |
248
| sendCharacters();
|
|
134 |
248
| currentElementName = null;
|
|
135 |
| } |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
13352
| public final void startElement(final String uri, final String localName, final String qName,
|
|
142 |
| final Attributes amap) throws SAXException { |
|
143 |
13352
| final String method = "startElement";
|
|
144 |
13352
| try {
|
|
145 |
13352
| Trace.param(this, method, "currentHandler", currentHandler.getClass().getName());
|
|
146 |
13352
| Trace.param(this, method, "localName", localName);
|
|
147 |
13352
| Trace.param(this, method, "qName", qName);
|
|
148 |
13352
| if (handlerStack.empty() && level == 0) {
|
|
149 |
248
| currentHandler.init();
|
|
150 |
| } |
|
151 |
13352
| level++;
|
|
152 |
13352
| sendCharacters();
|
|
153 |
13352
| currentElementName = localName;
|
|
154 |
13352
| final SimpleAttributes attributes = new SimpleAttributes();
|
|
155 |
13352
| for (int i = 0; i < amap.getLength(); i++) {
|
|
156 |
14493
| attributes.add(amap.getQName(i), amap.getValue(i));
|
|
157 |
| } |
|
158 |
13352
| Trace.param(this, method, "attributes", attributes);
|
|
159 |
13352
| currentHandler.startElement(qName, attributes);
|
|
160 |
| } catch (SyntaxException e) { |
|
161 |
0
| Trace.trace(this, method, e);
|
|
162 |
0
| errorList.add(e);
|
|
163 |
| } catch (RuntimeException e) { |
|
164 |
0
| Trace.trace(this, method, e);
|
|
165 |
0
| errorList.add(new SAXParseException(qName, locator, e));
|
|
166 |
| } |
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
13352
| public final void endElement(final String uri, final String localName, final String qName)
|
|
174 |
| throws SAXException { |
|
175 |
13352
| sendCharacters();
|
|
176 |
13352
| final String method = "endElement";
|
|
177 |
13352
| try {
|
|
178 |
13352
| Trace.param(this, method, "currentHandler", currentHandler.getClass().getName());
|
|
179 |
13352
| Trace.param(this, method, "localName", localName);
|
|
180 |
13352
| currentHandler.endElement(localName);
|
|
181 |
| } catch (SyntaxException e) { |
|
182 |
0
| Trace.trace(this, method, e);
|
|
183 |
0
| setLocationInformation(e);
|
|
184 |
0
| errorList.add(e);
|
|
185 |
| } catch (RuntimeException e) { |
|
186 |
0
| Trace.trace(this, method, e);
|
|
187 |
0
| errorList.add(new SAXParseException(qName, locator, e));
|
|
188 |
| } |
|
189 |
13352
| try {
|
|
190 |
13352
| currentElementName = null;
|
|
191 |
13352
| level--;
|
|
192 |
13352
| if (level <= 0) {
|
|
193 |
3861
| restoreHandler(localName);
|
|
194 |
| } |
|
195 |
| } catch (SyntaxException e) { |
|
196 |
0
| Trace.trace(this, method, e);
|
|
197 |
0
| errorList.add(e);
|
|
198 |
| } catch (RuntimeException e) { |
|
199 |
0
| Trace.trace(this, method, e);
|
|
200 |
0
| errorList.add(new SAXParseException(qName, locator, e));
|
|
201 |
| } |
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
31595
| public final void characters(final char[] ch, final int start, final int length) {
|
|
208 |
31595
| buffer.append(ch, start, length);
|
|
209 |
| } |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
27200
| private void sendCharacters() throws SAXException {
|
|
217 |
27200
| try {
|
|
218 |
27200
| if (buffer.length() > 0) {
|
|
219 |
21847
| final String str = buffer.toString().trim();
|
|
220 |
21847
| buffer.setLength(0);
|
|
221 |
21847
| if (str.length() > 0) {
|
|
222 |
2748
| currentHandler.characters(currentElementName, str);
|
|
223 |
| } |
|
224 |
| } |
|
225 |
| } catch (SyntaxException e) { |
|
226 |
0
| setLocationInformation(e);
|
|
227 |
0
| Trace.trace(this, "startElement", e);
|
|
228 |
0
| errorList.add(e);
|
|
229 |
| } catch (RuntimeException e) { |
|
230 |
0
| Trace.trace(this, "startElement", e);
|
|
231 |
0
| errorList.add(new SAXParseException(null, locator, e));
|
|
232 |
| } |
|
233 |
| } |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
3666
| public final void changeHandler(final AbstractSimpleHandler newHandler,
|
|
263 |
| final String elementName, final SimpleAttributes attributes) |
|
264 |
| throws SyntaxException { |
|
265 |
3666
| handlerStack.push(currentHandler);
|
|
266 |
3666
| levelStack.push(new Integer(level));
|
|
267 |
3666
| currentHandler = newHandler;
|
|
268 |
3666
| level = 0;
|
|
269 |
3666
| level++;
|
|
270 |
3666
| currentHandler.init();
|
|
271 |
3666
| currentHandler.startElement(elementName, attributes);
|
|
272 |
| } |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
3861
| private final void restoreHandler(final String elementName) throws SyntaxException {
|
|
282 |
3861
| while (level <= 0 && !handlerStack.empty()) {
|
|
283 |
3666
| currentHandler = (AbstractSimpleHandler) handlerStack.pop();
|
|
284 |
3666
| level = ((Integer) levelStack.pop()).intValue();
|
|
285 |
3666
| currentHandler.endElement(elementName);
|
|
286 |
3666
| level--;
|
|
287 |
| } |
|
288 |
3861
| if (handlerStack.empty()) {
|
|
289 |
| |
|
290 |
365
| Trace.trace(this, "restoreHandler", "no handler to restore");
|
|
291 |
| } |
|
292 |
| } |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
942
| public final int getLevel() {
|
|
300 |
942
| return level;
|
|
301 |
| } |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
0
| public final SAXParseException createSAXParseException(final Exception e) {
|
|
310 |
0
| return new SAXParseException(null, locator, e);
|
|
311 |
| } |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
0
| public final SAXParseException createSAXParseException(final String message) {
|
|
320 |
0
| return new SAXParseException(message, locator);
|
|
321 |
| } |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
0
| private final void setLocationInformation(final SyntaxException e) {
|
|
329 |
0
| if (locator != null && url != null) {
|
|
330 |
0
| e.setErrorPosition(new SourcePosition(url, locator.getLineNumber(),
|
|
331 |
| locator.getColumnNumber())); |
|
332 |
| } |
|
333 |
| } |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
248
| public final void setUrl(final URL url) {
|
|
341 |
248
| this.url = url;
|
|
342 |
| } |
|
343 |
| } |