|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.util.xml; |
|
16 |
| |
|
17 |
| import java.io.IOException; |
|
18 |
| import java.io.InputStream; |
|
19 |
| import java.net.URL; |
|
20 |
| import java.util.ArrayList; |
|
21 |
| import java.util.HashMap; |
|
22 |
| import java.util.List; |
|
23 |
| import java.util.Map; |
|
24 |
| |
|
25 |
| import javax.xml.parsers.ParserConfigurationException; |
|
26 |
| import javax.xml.parsers.SAXParser; |
|
27 |
| import javax.xml.parsers.SAXParserFactory; |
|
28 |
| |
|
29 |
| import org.apache.commons.logging.Log; |
|
30 |
| import org.apache.commons.logging.LogFactory; |
|
31 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
32 |
| import org.apache.hivemind.HiveMind; |
|
33 |
| import org.apache.hivemind.Location; |
|
34 |
| import org.apache.hivemind.Resource; |
|
35 |
| import org.apache.hivemind.impl.LocationImpl; |
|
36 |
| import org.apache.tapestry.Tapestry; |
|
37 |
| import org.apache.tapestry.util.RegexpMatcher; |
|
38 |
| import org.xml.sax.Attributes; |
|
39 |
| import org.xml.sax.InputSource; |
|
40 |
| import org.xml.sax.Locator; |
|
41 |
| import org.xml.sax.SAXException; |
|
42 |
| import org.xml.sax.SAXParseException; |
|
43 |
| import org.xml.sax.helpers.DefaultHandler; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| public class RuleDirectedParser extends DefaultHandler |
|
65 |
| { |
|
66 |
| private static final Log LOG = LogFactory.getLog(RuleDirectedParser.class); |
|
67 |
| |
|
68 |
| private Resource _documentLocation; |
|
69 |
| |
|
70 |
| private List _ruleStack = new ArrayList(); |
|
71 |
| |
|
72 |
| private List _objectStack = new ArrayList(); |
|
73 |
| |
|
74 |
| private Object _documentObject; |
|
75 |
| |
|
76 |
| private Locator _locator; |
|
77 |
| |
|
78 |
| private int _line = -1; |
|
79 |
| |
|
80 |
| private int _column = -1; |
|
81 |
| |
|
82 |
| private Location _location; |
|
83 |
| |
|
84 |
| private static SAXParserFactory _parserFactory; |
|
85 |
| |
|
86 |
| private SAXParser _parser; |
|
87 |
| |
|
88 |
| private RegexpMatcher _matcher; |
|
89 |
| |
|
90 |
| private String _uri; |
|
91 |
| |
|
92 |
| private String _localName; |
|
93 |
| |
|
94 |
| private String _qName; |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| private Map _ruleMap = new HashMap(); |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| private StringBuffer _contentBuffer = new StringBuffer(); |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| private Map _entities = new HashMap(); |
|
113 |
| |
|
114 |
48
| public Object parse(Resource documentLocation)
|
|
115 |
| { |
|
116 |
48
| if (LOG.isDebugEnabled())
|
|
117 |
0
| LOG.debug("Parsing: " + documentLocation);
|
|
118 |
| |
|
119 |
48
| try
|
|
120 |
| { |
|
121 |
48
| _documentLocation = documentLocation;
|
|
122 |
| |
|
123 |
48
| URL url = documentLocation.getResourceURL();
|
|
124 |
| |
|
125 |
48
| if (url == null)
|
|
126 |
0
| throw new DocumentParseException(Tapestry.format(
|
|
127 |
| "RuleDrivenParser.resource-missing", |
|
128 |
| documentLocation), documentLocation); |
|
129 |
| |
|
130 |
48
| return parse(url);
|
|
131 |
| } |
|
132 |
| finally |
|
133 |
| { |
|
134 |
48
| _documentLocation = null;
|
|
135 |
48
| _ruleStack.clear();
|
|
136 |
48
| _objectStack.clear();
|
|
137 |
48
| _documentObject = null;
|
|
138 |
| |
|
139 |
48
| _uri = null;
|
|
140 |
48
| _localName = null;
|
|
141 |
48
| _qName = null;
|
|
142 |
| |
|
143 |
48
| _line = -1;
|
|
144 |
48
| _column = -1;
|
|
145 |
48
| _location = null;
|
|
146 |
48
| _locator = null;
|
|
147 |
| |
|
148 |
48
| _contentBuffer.setLength(0);
|
|
149 |
| } |
|
150 |
| } |
|
151 |
| |
|
152 |
48
| protected Object parse(URL url)
|
|
153 |
| { |
|
154 |
48
| if (_parser == null)
|
|
155 |
48
| _parser = constructParser();
|
|
156 |
| |
|
157 |
48
| InputStream stream = null;
|
|
158 |
| |
|
159 |
48
| try
|
|
160 |
| { |
|
161 |
48
| stream = url.openStream();
|
|
162 |
| } |
|
163 |
| catch (IOException ex) |
|
164 |
| { |
|
165 |
0
| throw new DocumentParseException(Tapestry.format(
|
|
166 |
| "RuleDrivenParser.unable-to-open-resource", |
|
167 |
| url), _documentLocation, ex); |
|
168 |
| } |
|
169 |
| |
|
170 |
48
| InputSource source = new InputSource(stream);
|
|
171 |
| |
|
172 |
48
| try
|
|
173 |
| { |
|
174 |
48
| _parser.parse(source, this);
|
|
175 |
| |
|
176 |
39
| stream.close();
|
|
177 |
| } |
|
178 |
| catch (Exception ex) |
|
179 |
| { |
|
180 |
9
| throw new DocumentParseException(Tapestry.format(
|
|
181 |
| "RuleDrivenParser.parse-error", |
|
182 |
| url, |
|
183 |
| ex.getMessage()), getLocation(), ex); |
|
184 |
| } |
|
185 |
| |
|
186 |
39
| if (LOG.isDebugEnabled())
|
|
187 |
0
| LOG.debug("Document parsed as: " + _documentObject);
|
|
188 |
| |
|
189 |
39
| return _documentObject;
|
|
190 |
| } |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
207
| public Location getLocation()
|
|
198 |
| { |
|
199 |
207
| if (_locator == null)
|
|
200 |
0
| return null;
|
|
201 |
| |
|
202 |
207
| int line = _locator.getLineNumber();
|
|
203 |
207
| int column = _locator.getColumnNumber();
|
|
204 |
| |
|
205 |
207
| if (_line != line || _column != column)
|
|
206 |
| { |
|
207 |
189
| _location = null;
|
|
208 |
189
| _line = line;
|
|
209 |
189
| _column = column;
|
|
210 |
| } |
|
211 |
| |
|
212 |
207
| if (_location == null)
|
|
213 |
189
| _location = new LocationImpl(_documentLocation, _line, _column);
|
|
214 |
| |
|
215 |
207
| return _location;
|
|
216 |
| } |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
105
| public void push(Object object)
|
|
223 |
| { |
|
224 |
105
| if (_documentObject == null)
|
|
225 |
45
| _documentObject = object;
|
|
226 |
| |
|
227 |
105
| push(_objectStack, object, "object stack");
|
|
228 |
| } |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
147
| public Object peek()
|
|
234 |
| { |
|
235 |
147
| return peek(_objectStack, 0);
|
|
236 |
| } |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
0
| public Object peek(int depth)
|
|
244 |
| { |
|
245 |
0
| return peek(_objectStack, depth);
|
|
246 |
| } |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
99
| public Object pop()
|
|
252 |
| { |
|
253 |
99
| return pop(_objectStack, "object stack");
|
|
254 |
| } |
|
255 |
| |
|
256 |
219
| private Object pop(List list, String name)
|
|
257 |
| { |
|
258 |
219
| Object result = list.remove(list.size() - 1);
|
|
259 |
| |
|
260 |
219
| if (LOG.isDebugEnabled())
|
|
261 |
0
| LOG.debug("Popped " + result + " off " + name + " (at " + getLocation() + ")");
|
|
262 |
| |
|
263 |
219
| return result;
|
|
264 |
| } |
|
265 |
| |
|
266 |
354
| private Object peek(List list, int depth)
|
|
267 |
| { |
|
268 |
354
| return list.get(list.size() - 1 - depth);
|
|
269 |
| } |
|
270 |
| |
|
271 |
237
| private void push(List list, Object object, String name)
|
|
272 |
| { |
|
273 |
237
| if (LOG.isDebugEnabled())
|
|
274 |
0
| LOG.debug("Pushing " + object + " onto " + name + " (at " + getLocation() + ")");
|
|
275 |
| |
|
276 |
237
| list.add(object);
|
|
277 |
| } |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
132
| protected void pushRule(IRule rule)
|
|
284 |
| { |
|
285 |
132
| push(_ruleStack, rule, "rule stack");
|
|
286 |
| } |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
207
| protected IRule peekRule()
|
|
293 |
| { |
|
294 |
207
| return (IRule) peek(_ruleStack, 0);
|
|
295 |
| } |
|
296 |
| |
|
297 |
120
| protected IRule popRule()
|
|
298 |
| { |
|
299 |
120
| return (IRule) pop(_ruleStack, "rule stack");
|
|
300 |
| } |
|
301 |
| |
|
302 |
576
| public void addRule(String localElementName, IRule rule)
|
|
303 |
| { |
|
304 |
576
| _ruleMap.put(localElementName, rule);
|
|
305 |
| } |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
192
| public void registerEntity(String publicId, String entityPath)
|
|
321 |
| { |
|
322 |
192
| if (LOG.isDebugEnabled())
|
|
323 |
0
| LOG.debug("Registering " + publicId + " as " + entityPath);
|
|
324 |
| |
|
325 |
192
| if (_entities == null)
|
|
326 |
0
| _entities = new HashMap();
|
|
327 |
| |
|
328 |
192
| _entities.put(publicId, entityPath);
|
|
329 |
| } |
|
330 |
| |
|
331 |
132
| protected IRule selectRule(String localName, Attributes attributes)
|
|
332 |
| { |
|
333 |
132
| IRule rule = (IRule) _ruleMap.get(localName);
|
|
334 |
| |
|
335 |
132
| if (rule == null)
|
|
336 |
0
| throw new DocumentParseException(Tapestry.format(
|
|
337 |
| "RuleDrivenParser.no-rule-for-element", |
|
338 |
| localName), getLocation()); |
|
339 |
| |
|
340 |
132
| return rule;
|
|
341 |
| } |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
48
| public void setDocumentLocator(Locator locator)
|
|
351 |
| { |
|
352 |
48
| _locator = locator;
|
|
353 |
| } |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
117
| public void characters(char[] ch, int start, int length) throws SAXException
|
|
360 |
| { |
|
361 |
117
| _contentBuffer.append(ch, start, length);
|
|
362 |
| } |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
120
| public void endElement(String uri, String localName, String qName) throws SAXException
|
|
368 |
| { |
|
369 |
120
| fireContentRule();
|
|
370 |
| |
|
371 |
120
| _uri = uri;
|
|
372 |
120
| _localName = localName;
|
|
373 |
120
| _qName = qName;
|
|
374 |
| |
|
375 |
120
| popRule().endElement(this);
|
|
376 |
| } |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
126
| public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
|
|
382 |
| { |
|
383 |
| } |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
132
| public void startElement(String uri, String localName, String qName, Attributes attributes)
|
|
390 |
| throws SAXException |
|
391 |
| { |
|
392 |
132
| fireContentRule();
|
|
393 |
| |
|
394 |
132
| _uri = uri;
|
|
395 |
132
| _localName = localName;
|
|
396 |
132
| _qName = qName;
|
|
397 |
| |
|
398 |
132
| String name = extractName(uri, localName, qName);
|
|
399 |
| |
|
400 |
132
| IRule newRule = selectRule(name, attributes);
|
|
401 |
| |
|
402 |
132
| pushRule(newRule);
|
|
403 |
| |
|
404 |
132
| newRule.startElement(this, attributes);
|
|
405 |
| } |
|
406 |
| |
|
407 |
132
| private String extractName(String uri, String localName, String qName)
|
|
408 |
| { |
|
409 |
132
| return HiveMind.isBlank(localName) ? qName : localName;
|
|
410 |
| } |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
48
| protected synchronized SAXParser constructParser()
|
|
417 |
| { |
|
418 |
48
| if (_parserFactory == null)
|
|
419 |
| { |
|
420 |
3
| _parserFactory = SAXParserFactory.newInstance();
|
|
421 |
3
| configureParserFactory(_parserFactory);
|
|
422 |
| } |
|
423 |
| |
|
424 |
48
| try
|
|
425 |
| { |
|
426 |
48
| return _parserFactory.newSAXParser();
|
|
427 |
| } |
|
428 |
| catch (SAXException ex) |
|
429 |
| { |
|
430 |
0
| throw new ApplicationRuntimeException(ex);
|
|
431 |
| } |
|
432 |
| catch (ParserConfigurationException ex) |
|
433 |
| { |
|
434 |
0
| throw new ApplicationRuntimeException(ex);
|
|
435 |
| } |
|
436 |
| |
|
437 |
| } |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
3
| protected void configureParserFactory(SAXParserFactory factory)
|
|
445 |
| { |
|
446 |
3
| factory.setValidating(true);
|
|
447 |
3
| factory.setNamespaceAware(false);
|
|
448 |
| } |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
3
| public void error(SAXParseException ex) throws SAXException
|
|
454 |
| { |
|
455 |
3
| fatalError(ex);
|
|
456 |
| } |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
3
| public void fatalError(SAXParseException ex) throws SAXException
|
|
462 |
| { |
|
463 |
| |
|
464 |
| |
|
465 |
| |
|
466 |
| |
|
467 |
3
| _parser = null;
|
|
468 |
| |
|
469 |
3
| throw ex;
|
|
470 |
| } |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
| |
|
475 |
0
| public void warning(SAXParseException ex) throws SAXException
|
|
476 |
| { |
|
477 |
0
| fatalError(ex);
|
|
478 |
| } |
|
479 |
| |
|
480 |
45
| public InputSource resolveEntity(String publicId, String systemId) throws SAXException
|
|
481 |
| { |
|
482 |
45
| String entityPath = null;
|
|
483 |
| |
|
484 |
45
| if (LOG.isDebugEnabled())
|
|
485 |
0
| LOG.debug("Attempting to resolve entity; publicId = " + publicId + " systemId = "
|
|
486 |
| + systemId); |
|
487 |
| |
|
488 |
45
| if (_entities != null)
|
|
489 |
45
| entityPath = (String) _entities.get(publicId);
|
|
490 |
| |
|
491 |
45
| if (entityPath == null)
|
|
492 |
| { |
|
493 |
0
| if (LOG.isDebugEnabled())
|
|
494 |
0
| LOG.debug("Entity not found, using " + systemId);
|
|
495 |
| |
|
496 |
0
| return null;
|
|
497 |
| } |
|
498 |
| |
|
499 |
45
| InputStream stream = getClass().getResourceAsStream(entityPath);
|
|
500 |
| |
|
501 |
45
| InputSource result = new InputSource(stream);
|
|
502 |
| |
|
503 |
45
| if (result != null && LOG.isDebugEnabled())
|
|
504 |
0
| LOG.debug("Resolved " + publicId + " as " + result + " (for " + entityPath + ")");
|
|
505 |
| |
|
506 |
45
| return result;
|
|
507 |
| } |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
| |
|
512 |
| |
|
513 |
| |
|
514 |
| |
|
515 |
54
| public void validate(String value, String pattern, String errorKey)
|
|
516 |
| throws DocumentParseException |
|
517 |
| { |
|
518 |
54
| if (_matcher == null)
|
|
519 |
30
| _matcher = new RegexpMatcher();
|
|
520 |
| |
|
521 |
54
| if (_matcher.matches(pattern, value))
|
|
522 |
48
| return;
|
|
523 |
| |
|
524 |
6
| throw new InvalidStringException(Tapestry.format(errorKey, value), value, getLocation());
|
|
525 |
| } |
|
526 |
| |
|
527 |
0
| public Resource getDocumentLocation()
|
|
528 |
| { |
|
529 |
0
| return _documentLocation;
|
|
530 |
| } |
|
531 |
| |
|
532 |
| |
|
533 |
| |
|
534 |
| |
|
535 |
| |
|
536 |
| |
|
537 |
| |
|
538 |
0
| public String getLocalName()
|
|
539 |
| { |
|
540 |
0
| return _localName;
|
|
541 |
| } |
|
542 |
| |
|
543 |
| |
|
544 |
| |
|
545 |
| |
|
546 |
| |
|
547 |
| |
|
548 |
| |
|
549 |
0
| public String getQName()
|
|
550 |
| { |
|
551 |
0
| return _qName;
|
|
552 |
| } |
|
553 |
| |
|
554 |
| |
|
555 |
| |
|
556 |
| |
|
557 |
| |
|
558 |
| |
|
559 |
| |
|
560 |
0
| public String getUri()
|
|
561 |
| { |
|
562 |
0
| return _uri;
|
|
563 |
| } |
|
564 |
| |
|
565 |
252
| private void fireContentRule()
|
|
566 |
| { |
|
567 |
252
| String content = _contentBuffer.toString();
|
|
568 |
252
| _contentBuffer.setLength(0);
|
|
569 |
| |
|
570 |
252
| if (!_ruleStack.isEmpty())
|
|
571 |
207
| peekRule().content(this, content);
|
|
572 |
| } |
|
573 |
| |
|
574 |
| } |