1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.builder.xml; |
19 |
|
|
20 |
|
import static org.apache.camel.converter.ObjectConverter.toBoolean; |
21 |
|
import org.apache.camel.Exchange; |
22 |
|
import org.apache.camel.Expression; |
23 |
|
import org.apache.camel.Predicate; |
24 |
|
import org.apache.camel.RuntimeExpressionException; |
25 |
|
import org.apache.camel.Message; |
26 |
|
import org.w3c.dom.Document; |
27 |
|
import org.w3c.dom.Element; |
28 |
|
import org.xml.sax.InputSource; |
29 |
|
|
30 |
|
import javax.xml.namespace.QName; |
31 |
|
import javax.xml.xpath.XPath; |
32 |
|
import javax.xml.xpath.XPathConstants; |
33 |
|
import javax.xml.xpath.XPathExpression; |
34 |
|
import javax.xml.xpath.XPathExpressionException; |
35 |
|
import javax.xml.xpath.XPathFactory; |
36 |
|
import javax.xml.xpath.XPathFactoryConfigurationException; |
37 |
|
import javax.xml.xpath.XPathFunctionResolver; |
38 |
|
import java.io.StringReader; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
16 |
public class XPathBuilder<E extends Exchange> implements Expression<E>, Predicate<E> { |
46 |
|
private final String text; |
47 |
|
private XPathFactory xpathFactory; |
48 |
16 |
private Class documentType = Document.class; |
49 |
16 |
private QName resultType = null; |
50 |
16 |
private String objectModelUri = null; |
51 |
|
private DefaultNamespaceContext namespaceContext; |
52 |
|
private XPathFunctionResolver functionResolver; |
53 |
|
private XPathExpression expression; |
54 |
16 |
private MessageVariableResolver variableResolver = new MessageVariableResolver(); |
55 |
|
|
56 |
|
public static XPathBuilder xpath(String text) { |
57 |
16 |
return new XPathBuilder(text); |
58 |
|
} |
59 |
|
|
60 |
16 |
public XPathBuilder(String text) { |
61 |
16 |
this.text = text; |
62 |
16 |
} |
63 |
|
|
64 |
|
@Override |
65 |
|
public String toString() { |
66 |
29 |
return "XPath: " + text; |
67 |
|
} |
68 |
|
|
69 |
|
public boolean matches(E exchange) { |
70 |
12 |
Object booleanResult = evaluateAs(exchange, XPathConstants.BOOLEAN); |
71 |
12 |
return toBoolean(booleanResult); |
72 |
|
} |
73 |
|
|
74 |
|
public void assertMatches(String text, E exchange) throws AssertionError { |
75 |
4 |
Object booleanResult = evaluateAs(exchange, XPathConstants.BOOLEAN); |
76 |
4 |
if (!toBoolean(booleanResult)) { |
77 |
1 |
throw new AssertionError(this + " failed on " + exchange + " as returned <" + booleanResult + ">"); |
78 |
|
} |
79 |
3 |
} |
80 |
|
|
81 |
|
public Object evaluate(E exchange) { |
82 |
5 |
return evaluateAs(exchange, resultType); |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
public XPathBuilder<E> booleanResult() { |
95 |
0 |
resultType = XPathConstants.BOOLEAN; |
96 |
0 |
return this; |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
public XPathBuilder<E> nodeResult() { |
105 |
0 |
resultType = XPathConstants.NODE; |
106 |
0 |
return this; |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
public XPathBuilder<E> nodeSetResult() { |
115 |
0 |
resultType = XPathConstants.NODESET; |
116 |
0 |
return this; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
public XPathBuilder<E> numberResult() { |
125 |
0 |
resultType = XPathConstants.NUMBER; |
126 |
0 |
return this; |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
public XPathBuilder<E> stringResult() { |
135 |
0 |
resultType = XPathConstants.STRING; |
136 |
0 |
return this; |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
public XPathBuilder<E> objectModel(String uri) { |
145 |
0 |
this.objectModelUri = uri; |
146 |
0 |
return this; |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
public XPathBuilder<E> functionResolver(XPathFunctionResolver functionResolver) { |
155 |
0 |
this.functionResolver = functionResolver; |
156 |
0 |
return this; |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
public XPathBuilder<E> namespace(String prefix, String uri) { |
167 |
6 |
getNamespaceContext().add(prefix, uri); |
168 |
6 |
return this; |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
public XPathBuilder<E> variable(String name, Object value) { |
175 |
1 |
variableResolver.addVariable(name, value); |
176 |
1 |
return this; |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
public XPathFactory getXPathFactory() throws XPathFactoryConfigurationException { |
183 |
32 |
if (xpathFactory == null) { |
184 |
27 |
if (objectModelUri != null) { |
185 |
0 |
xpathFactory = XPathFactory.newInstance(objectModelUri); |
186 |
|
} |
187 |
27 |
xpathFactory = XPathFactory.newInstance(); |
188 |
|
} |
189 |
32 |
return xpathFactory; |
190 |
|
} |
191 |
|
|
192 |
|
public void setXPathFactory(XPathFactory xpathFactory) { |
193 |
0 |
this.xpathFactory = xpathFactory; |
194 |
0 |
} |
195 |
|
|
196 |
|
public Class getDocumentType() { |
197 |
21 |
return documentType; |
198 |
|
} |
199 |
|
|
200 |
|
public void setDocumentType(Class documentType) { |
201 |
0 |
this.documentType = documentType; |
202 |
0 |
} |
203 |
|
|
204 |
|
public String getText() { |
205 |
0 |
return text; |
206 |
|
} |
207 |
|
|
208 |
|
public QName getResultType() { |
209 |
0 |
return resultType; |
210 |
|
} |
211 |
|
|
212 |
|
public DefaultNamespaceContext getNamespaceContext() { |
213 |
24 |
if (namespaceContext == null) { |
214 |
|
try { |
215 |
16 |
namespaceContext = new DefaultNamespaceContext(getXPathFactory()); |
216 |
|
} |
217 |
0 |
catch (XPathFactoryConfigurationException e) { |
218 |
0 |
throw new RuntimeExpressionException(e); |
219 |
16 |
} |
220 |
|
} |
221 |
24 |
return namespaceContext; |
222 |
|
} |
223 |
|
|
224 |
|
public void setNamespaceContext(DefaultNamespaceContext namespaceContext) { |
225 |
0 |
this.namespaceContext = namespaceContext; |
226 |
0 |
} |
227 |
|
|
228 |
|
public XPathFunctionResolver getFunctionResolver() { |
229 |
0 |
return functionResolver; |
230 |
|
} |
231 |
|
|
232 |
|
public void setFunctionResolver(XPathFunctionResolver functionResolver) { |
233 |
0 |
this.functionResolver = functionResolver; |
234 |
0 |
} |
235 |
|
|
236 |
|
public XPathExpression getExpression() throws XPathFactoryConfigurationException, XPathExpressionException { |
237 |
21 |
if (expression == null) { |
238 |
16 |
expression = createXPathExpression(); |
239 |
|
} |
240 |
21 |
return expression; |
241 |
|
} |
242 |
|
|
243 |
|
public void setNamespacesFromDom(Element node) { |
244 |
1 |
getNamespaceContext().setNamespacesFromDom(node); |
245 |
1 |
} |
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
protected synchronized Object evaluateAs(E exchange, QName resultType) { |
255 |
21 |
variableResolver.setExchange(exchange); |
256 |
|
try { |
257 |
21 |
Object document = getDocument(exchange); |
258 |
21 |
if (resultType != null) { |
259 |
16 |
if (document instanceof InputSource) { |
260 |
0 |
InputSource inputSource = (InputSource) document; |
261 |
0 |
return getExpression().evaluate(inputSource, resultType); |
262 |
|
} |
263 |
|
else { |
264 |
16 |
return getExpression().evaluate(document, resultType); |
265 |
|
} |
266 |
|
} |
267 |
|
else { |
268 |
5 |
if (document instanceof InputSource) { |
269 |
0 |
InputSource inputSource = (InputSource) document; |
270 |
0 |
return getExpression().evaluate(inputSource); |
271 |
|
} |
272 |
|
else { |
273 |
5 |
return getExpression().evaluate(document); |
274 |
|
} |
275 |
|
} |
276 |
|
} |
277 |
0 |
catch (XPathExpressionException e) { |
278 |
0 |
throw new InvalidXPathExpression(getText(), e); |
279 |
|
} |
280 |
0 |
catch (XPathFactoryConfigurationException e) { |
281 |
0 |
throw new InvalidXPathExpression(getText(), e); |
282 |
|
} |
283 |
|
} |
284 |
|
|
285 |
|
protected XPathExpression createXPathExpression() throws XPathExpressionException, XPathFactoryConfigurationException { |
286 |
16 |
XPath xPath = getXPathFactory().newXPath(); |
287 |
|
|
288 |
|
|
289 |
16 |
xpathFactory = null; |
290 |
|
|
291 |
16 |
xPath.setNamespaceContext(getNamespaceContext()); |
292 |
16 |
xPath.setXPathVariableResolver(variableResolver); |
293 |
16 |
if (functionResolver != null) { |
294 |
0 |
xPath.setXPathFunctionResolver(functionResolver); |
295 |
|
} |
296 |
16 |
return xPath.compile(text); |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
protected Object getDocument(E exchange) { |
303 |
21 |
Message in = exchange.getIn(); |
304 |
21 |
Class type = getDocumentType(); |
305 |
21 |
Object answer = null; |
306 |
21 |
if (type != null) { |
307 |
21 |
answer = in.getBody(type); |
308 |
|
} |
309 |
21 |
if (answer == null) { |
310 |
0 |
answer = in.getBody(); |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
21 |
if (answer instanceof String) { |
315 |
0 |
answer = new InputSource(new StringReader(answer.toString())); |
316 |
|
} |
317 |
21 |
return answer; |
318 |
|
} |
319 |
|
|
320 |
|
|
321 |
|
} |