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 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 public List evaluateAsNodeList(Node node) {
123 try {
124 return xpath.selectNodes(node);
125 }
126 catch (JaxenException ex) {
127 throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
128 }
129 }
130
131 public Object evaluateAsObject(Node context, NodeMapper nodeMapper) throws XPathException {
132 try {
133 Node result = (Node) xpath.selectSingleNode(context);
134 if (result != null) {
135 try {
136 return nodeMapper.mapNode(result, 0);
137 }
138 catch (DOMException ex) {
139 throw new XPathException("Mapping resulted in DOMException", ex);
140 }
141 }
142 else {
143 return null;
144 }
145 }
146 catch (JaxenException ex) {
147 throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
148 }
149 }
150
151 public List evaluate(Node context, NodeMapper nodeMapper) throws XPathException {
152 try {
153 List nodes = xpath.selectNodes(context);
154 List results = new ArrayList(nodes.size());
155 for (int i = 0; i < nodes.size(); i++) {
156 Node node = (Node) nodes.get(i);
157 try {
158 results.add(nodeMapper.mapNode(node, i));
159 }
160 catch (DOMException ex) {
161 throw new XPathException("Mapping resulted in DOMException", ex);
162 }
163 }
164 return results;
165 }
166 catch (JaxenException ex) {
167 throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
168 }
169 }
170 }
171 }