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