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