01: package org.geotools.xml.impl.jxpath;
02:
03: import java.util.Iterator;
04:
05: import org.apache.commons.jxpath.JXPathContext;
06: import org.apache.commons.jxpath.JXPathContextFactory;
07: import org.apache.commons.jxpath.JXPathIntrospector;
08: import org.geotools.xml.Configuration;
09: import org.geotools.xml.Node;
10: import org.geotools.xml.impl.DocumentHandler;
11: import org.geotools.xml.impl.ElementHandler;
12: import org.geotools.xml.impl.ElementHandlerImpl;
13: import org.geotools.xml.impl.NodeImpl;
14: import org.geotools.xml.impl.StreamingParserHandler;
15:
16: public class JXPathStreamingParserHandler extends
17: StreamingParserHandler {
18:
19: /** xpath to stream **/
20: String xpath;
21:
22: public JXPathStreamingParserHandler(Configuration config,
23: String xpath) {
24: super (config);
25:
26: this .xpath = xpath;
27: }
28:
29: protected boolean stream(ElementHandler handler) {
30: //create an xpath context from the root element
31: // TODO: cache the context, should work just the same
32: // JXPathIntrospector.registerDynamicClass(ElementHandlerImpl.class,
33: // ElementHandlerPropertyHandler.class);
34: JXPathIntrospector.registerDynamicClass(NodeImpl.class,
35: NodePropertyHandler.class);
36:
37: // ElementHandler rootHandler =
38: // ((DocumentHandler) handlers.firstElement()).getDocumentElementHandler();
39:
40: Node root = ((DocumentHandler) handlers.firstElement())
41: .getParseNode();
42: JXPathContext jxpContext = JXPathContextFactory.newInstance()
43: .newContext(null, root);
44:
45: jxpContext.setLenient(true);
46:
47: Iterator itr = jxpContext.iterate(xpath);
48:
49: for (; itr.hasNext();) {
50: Object obj = itr.next();
51: if (handler.getParseNode().equals(obj)) {
52: return true;
53: }
54: }
55:
56: return false;
57: }
58:
59: }
|