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.ArrayList;
20 import java.util.List;
21 import java.util.Map;
22 import javax.xml.namespace.QName;
23 import javax.xml.xpath.XPath;
24 import javax.xml.xpath.XPathConstants;
25 import javax.xml.xpath.XPathExpressionException;
26 import javax.xml.xpath.XPathFactory;
27
28 import org.springframework.xml.namespace.SimpleNamespaceContext;
29 import org.w3c.dom.DOMException;
30 import org.w3c.dom.Node;
31 import org.w3c.dom.NodeList;
32
33
34
35
36
37
38
39
40 abstract class Jaxp13XPathExpressionFactory {
41
42 private static XPathFactory xpathFactory;
43
44 static {
45 xpathFactory = XPathFactory.newInstance();
46 }
47
48
49
50
51
52
53
54
55 static XPathExpression createXPathExpression(String expression) {
56 try {
57 XPath xpath = xpathFactory.newXPath();
58 javax.xml.xpath.XPathExpression xpathExpression = xpath.compile(expression);
59 return new Jaxp13XPathExpression(xpathExpression);
60 }
61 catch (XPathExpressionException ex) {
62 throw new org.springframework.xml.xpath.XPathParseException(
63 "Could not compile [" + expression + "] to a XPathExpression: " + ex.getMessage(), ex);
64 }
65 }
66
67
68
69
70
71
72
73
74
75 public static XPathExpression createXPathExpression(String expression, Map namespaces) {
76 try {
77 XPath xpath = xpathFactory.newXPath();
78 SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
79 namespaceContext.setBindings(namespaces);
80 xpath.setNamespaceContext(namespaceContext);
81 javax.xml.xpath.XPathExpression xpathExpression = xpath.compile(expression);
82 return new Jaxp13XPathExpression(xpathExpression);
83 }
84 catch (XPathExpressionException ex) {
85 throw new org.springframework.xml.xpath.XPathParseException(
86 "Could not compile [" + expression + "] to a XPathExpression: " + ex.getMessage(), ex);
87 }
88 }
89
90
91 private static class Jaxp13XPathExpression implements XPathExpression {
92
93 private final javax.xml.xpath.XPathExpression xpathExpression;
94
95 private Jaxp13XPathExpression(javax.xml.xpath.XPathExpression xpathExpression) {
96 this.xpathExpression = xpathExpression;
97 }
98
99 public String evaluateAsString(Node node) {
100 return (String) evaluate(node, XPathConstants.STRING);
101 }
102
103 public List evaluateAsNodeList(Node node) {
104 NodeList nodeList = (NodeList) evaluate(node, XPathConstants.NODESET);
105 return toNodeList(nodeList);
106 }
107
108 private Object evaluate(Node node, QName returnType) {
109 try {
110
111 synchronized (xpathExpression) {
112 return xpathExpression.evaluate(node, returnType);
113 }
114 }
115 catch (XPathExpressionException ex) {
116 throw new XPathException("Could not evaluate XPath expression:" + ex.getMessage(), ex);
117 }
118 }
119
120 private List toNodeList(NodeList nodeList) {
121 List result = new ArrayList(nodeList.getLength());
122 for (int i = 0; i < nodeList.getLength(); i++) {
123 result.add(nodeList.item(i));
124 }
125 return result;
126 }
127
128 public double evaluateAsNumber(Node node) {
129 Double result = (Double) evaluate(node, XPathConstants.NUMBER);
130 return result.doubleValue();
131 }
132
133 public boolean evaluateAsBoolean(Node node) {
134 Boolean result = (Boolean) evaluate(node, XPathConstants.BOOLEAN);
135 return result.booleanValue();
136 }
137
138 public Node evaluateAsNode(Node node) {
139 return (Node) evaluate(node, XPathConstants.NODE);
140 }
141
142 public Object evaluateAsObject(Node node, NodeMapper nodeMapper) throws XPathException {
143 Node result = (Node) evaluate(node, XPathConstants.NODE);
144 if (result != null) {
145 try {
146 return nodeMapper.mapNode(result, 0);
147 }
148 catch (DOMException ex) {
149 throw new XPathException("Mapping resulted in DOMException", ex);
150 }
151 }
152 else {
153 return null;
154 }
155 }
156
157 public List evaluate(Node node, NodeMapper nodeMapper) throws XPathException {
158 NodeList nodes = (NodeList) evaluate(node, XPathConstants.NODESET);
159 List results = new ArrayList(nodes.getLength());
160 for (int i = 0; i < nodes.getLength(); i++) {
161 try {
162 results.add(nodeMapper.mapNode(nodes.item(i), i));
163 }
164 catch (DOMException ex) {
165 throw new XPathException("Mapping resulted in DOMException", ex);
166 }
167 }
168 return results;
169 }
170 }
171
172 }