1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.parse; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.HashMap; |
19 |
| import java.util.List; |
20 |
| import java.util.Map; |
21 |
| |
22 |
| import org.apache.hivemind.ApplicationRuntimeException; |
23 |
| import org.apache.hivemind.HiveMind; |
24 |
| import org.apache.hivemind.Location; |
25 |
| import org.apache.hivemind.Resource; |
26 |
| import org.apache.hivemind.impl.LocationImpl; |
27 |
| import org.xml.sax.Attributes; |
28 |
| import org.xml.sax.Locator; |
29 |
| import org.xml.sax.SAXException; |
30 |
| import org.xml.sax.SAXParseException; |
31 |
| import org.xml.sax.helpers.DefaultHandler; |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| public abstract class AbstractParser extends DefaultHandler |
46 |
| { |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| private static class Item |
53 |
| { |
54 |
| StringBuffer _buffer; |
55 |
| |
56 |
| String _elementName; |
57 |
| |
58 |
| boolean _ignoreCharacterData; |
59 |
| |
60 |
| Object _object; |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| int _priorState; |
66 |
| |
67 |
42085
| Item(String elementName, Object object, int priorState, boolean ignoreCharacterData)
|
68 |
| { |
69 |
42085
| _elementName = elementName;
|
70 |
42085
| _object = object;
|
71 |
42085
| _priorState = priorState;
|
72 |
42085
| _ignoreCharacterData = ignoreCharacterData;
|
73 |
| } |
74 |
| |
75 |
98061
| void addContent(char[] buffer, int start, int length)
|
76 |
| { |
77 |
98061
| if (_ignoreCharacterData)
|
78 |
4277
| return;
|
79 |
| |
80 |
93784
| if (_buffer == null)
|
81 |
19166
| _buffer = new StringBuffer(length);
|
82 |
| |
83 |
93784
| _buffer.append(buffer, start, length);
|
84 |
| } |
85 |
| |
86 |
41556
| String getContent()
|
87 |
| { |
88 |
41556
| if (_buffer != null)
|
89 |
19157
| return _buffer.toString().trim();
|
90 |
| |
91 |
22399
| return null;
|
92 |
| } |
93 |
| } |
94 |
| |
95 |
| private int _currentColumn; |
96 |
| |
97 |
| private int _currentLine; |
98 |
| |
99 |
| private Location _location; |
100 |
| |
101 |
| private Locator _locator; |
102 |
| |
103 |
| private Resource _resource; |
104 |
| |
105 |
| private List _stack; |
106 |
| |
107 |
| private int _state; |
108 |
| |
109 |
| private Item _top; |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
98061
| public void characters(char[] ch, int start, int length) throws SAXException
|
117 |
| { |
118 |
98061
| _top.addContent(ch, start, length);
|
119 |
| } |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
0
| public void error(SAXParseException ex) throws SAXException
|
125 |
| { |
126 |
0
| fatalError(ex);
|
127 |
| } |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
0
| public void fatalError(SAXParseException ex) throws SAXException
|
135 |
| { |
136 |
0
| throw ex;
|
137 |
| } |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
6
| protected String getElementPath()
|
144 |
| { |
145 |
6
| StringBuffer buffer = new StringBuffer();
|
146 |
| |
147 |
6
| int count = _stack.size();
|
148 |
6
| for (int i = 0; i < count; i++)
|
149 |
| { |
150 |
12
| if (i > 0)
|
151 |
6
| buffer.append('/');
|
152 |
| |
153 |
12
| Item item = (Item) _stack.get(i);
|
154 |
| |
155 |
12
| buffer.append(item._elementName);
|
156 |
| } |
157 |
| |
158 |
6
| return buffer.toString();
|
159 |
| } |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
42093
| protected Location getLocation()
|
165 |
| { |
166 |
42093
| int line = _locator.getLineNumber();
|
167 |
42093
| int column = _locator.getColumnNumber();
|
168 |
| |
169 |
42093
| if (line != _currentLine || column != _currentColumn)
|
170 |
42086
| _location = null;
|
171 |
| |
172 |
42093
| if (_location == null)
|
173 |
42086
| _location = new LocationImpl(_resource, line, column);
|
174 |
| |
175 |
42093
| _currentLine = line;
|
176 |
42093
| _currentColumn = column;
|
177 |
| |
178 |
42093
| return _location;
|
179 |
| } |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
3
| protected Resource getResource()
|
186 |
| { |
187 |
3
| return _resource;
|
188 |
| } |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
84160
| protected int getState()
|
196 |
| { |
197 |
84160
| return _state;
|
198 |
| } |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
285
| protected void initializeParser(Resource resource, int startState)
|
210 |
| { |
211 |
285
| _resource = resource;
|
212 |
285
| _stack = new ArrayList();
|
213 |
| |
214 |
285
| _location = null;
|
215 |
285
| _state = startState;
|
216 |
| } |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
41556
| protected String peekContent()
|
223 |
| { |
224 |
41556
| return _top.getContent();
|
225 |
| } |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
32466
| protected String peekElementName()
|
231 |
| { |
232 |
32466
| return _top._elementName;
|
233 |
| } |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
82324
| protected Object peekObject()
|
240 |
| { |
241 |
82324
| return _top._object;
|
242 |
| } |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
42074
| protected void pop()
|
250 |
| { |
251 |
42074
| int count = _stack.size();
|
252 |
| |
253 |
42074
| _state = _top._priorState;
|
254 |
| |
255 |
42074
| _stack.remove(count - 1);
|
256 |
| |
257 |
42074
| if (count == 1)
|
258 |
280
| _top = null;
|
259 |
| else |
260 |
41794
| _top = (Item) _stack.get(count - 2);
|
261 |
| } |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
0
| protected void push(String elementName, Object object, int state)
|
275 |
| { |
276 |
0
| push(elementName, object, state, true);
|
277 |
| } |
278 |
| |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
| |
286 |
| |
287 |
| |
288 |
| |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
42085
| protected void push(String elementName, Object object, int state, boolean ignoreCharacterData)
|
295 |
| { |
296 |
42085
| HiveMind.setLocation(object, getLocation());
|
297 |
| |
298 |
42085
| Item item = new Item(elementName, object, _state, ignoreCharacterData);
|
299 |
| |
300 |
42085
| _stack.add(item);
|
301 |
| |
302 |
42085
| _top = item;
|
303 |
42085
| _state = state;
|
304 |
| } |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
285
| protected void resetParser()
|
310 |
| { |
311 |
285
| _resource = null;
|
312 |
285
| _locator = null;
|
313 |
285
| _stack = null;
|
314 |
285
| _location = null;
|
315 |
| } |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
283
| public void setDocumentLocator(Locator locator)
|
321 |
| { |
322 |
283
| _locator = locator;
|
323 |
| } |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
0
| protected void setState(int state)
|
329 |
| { |
330 |
0
| _state = state;
|
331 |
| } |
332 |
| |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
1
| protected void unexpectedElement(String elementName)
|
341 |
| { |
342 |
1
| throw new ApplicationRuntimeException(ParseMessages.unexpectedElement(
|
343 |
| elementName, |
344 |
| getElementPath()), getLocation(), null); |
345 |
| } |
346 |
| |
347 |
| |
348 |
| |
349 |
| |
350 |
| |
351 |
| |
352 |
| |
353 |
0
| protected void updateObject(Object object)
|
354 |
| { |
355 |
0
| _top._object = object;
|
356 |
| } |
357 |
| |
358 |
| |
359 |
| |
360 |
| |
361 |
0
| public void warning(SAXParseException ex) throws SAXException
|
362 |
| { |
363 |
0
| fatalError(ex);
|
364 |
| } |
365 |
| |
366 |
42086
| private Map constructAttributesMap(Attributes attributes)
|
367 |
| { |
368 |
42086
| Map result = new HashMap();
|
369 |
42086
| int count = attributes.getLength();
|
370 |
| |
371 |
42086
| for (int i = 0; i < count; i++)
|
372 |
| { |
373 |
59880
| String key = attributes.getLocalName(i);
|
374 |
| |
375 |
59880
| if (HiveMind.isBlank(key))
|
376 |
59880
| key = attributes.getQName(i);
|
377 |
| |
378 |
59880
| String value = attributes.getValue(i);
|
379 |
| |
380 |
59880
| result.put(key, value);
|
381 |
| } |
382 |
| |
383 |
42086
| return result;
|
384 |
| } |
385 |
| |
386 |
| |
387 |
| |
388 |
| |
389 |
| |
390 |
| protected abstract void begin(String elementName, Map attributes); |
391 |
| |
392 |
| |
393 |
| |
394 |
| |
395 |
| |
396 |
| |
397 |
| |
398 |
| protected abstract void end(String elementName); |
399 |
| |
400 |
42074
| public void endElement(String uri, String localName, String qName) throws SAXException
|
401 |
| { |
402 |
42074
| end(getElementName(localName, qName));
|
403 |
| } |
404 |
| |
405 |
42086
| public void startElement(String uri, String localName, String qName, Attributes attributes)
|
406 |
| throws SAXException |
407 |
| { |
408 |
42086
| String elementName = getElementName(localName, qName);
|
409 |
| |
410 |
42086
| begin(elementName, constructAttributesMap(attributes));
|
411 |
| } |
412 |
| |
413 |
84160
| private String getElementName(String localName, String qName)
|
414 |
| { |
415 |
84160
| return qName != null ? qName : localName;
|
416 |
| } |
417 |
| } |