1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.xml.xpath;
18
19 import java.util.Map;
20 import javax.xml.transform.Source;
21 import javax.xml.transform.TransformerException;
22 import javax.xml.transform.dom.DOMResult;
23
24 import org.springframework.xml.transform.TransformerObjectSupport;
25
26 import org.w3c.dom.DOMException;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.Node;
30
31
32
33
34
35
36
37 public abstract class AbstractXPathTemplate extends TransformerObjectSupport implements XPathOperations {
38
39 private Map<String, String> namespaces;
40
41
42 public Map<String, String> getNamespaces() {
43 return namespaces;
44 }
45
46
47 public void setNamespaces(Map<String, String> namespaces) {
48 this.namespaces = namespaces;
49 }
50
51 public final void evaluate(String expression, Source context, NodeCallbackHandler callbackHandler)
52 throws XPathException {
53 evaluate(expression, context, new NodeCallbackHandlerNodeMapper(callbackHandler));
54 }
55
56
57 private static class NodeCallbackHandlerNodeMapper implements NodeMapper<Object> {
58
59 private final NodeCallbackHandler callbackHandler;
60
61 public NodeCallbackHandlerNodeMapper(NodeCallbackHandler callbackHandler) {
62 this.callbackHandler = callbackHandler;
63 }
64
65 public Object mapNode(Node node, int nodeNum) throws DOMException {
66 callbackHandler.processNode(node);
67 return null;
68 }
69 }
70
71
72
73
74
75
76
77 protected Element getRootElement(Source source) throws TransformerException {
78 DOMResult domResult = new DOMResult();
79 transform(source, domResult);
80 Document document = (Document) domResult.getNode();
81 return document.getDocumentElement();
82 }
83
84 }