1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.xml.xstream;
17
18 import java.util.List;
19
20 import javax.xml.xpath.XPath;
21 import javax.xml.xpath.XPathExpressionException;
22 import javax.xml.xpath.XPathFunction;
23 import javax.xml.xpath.XPathFunctionException;
24
25 import org.w3c.dom.Node;
26
27
28
29
30
31
32
33
34 public class XStreamSafeSearchFunction implements XPathFunction {
35
36 private final Node rootNode;
37 private XPath xpath;
38 private static XStreamSafeEvaluator evaluator = new XStreamSafeEvaluator();
39
40 public XStreamSafeSearchFunction(Node rootNode, XPath xpath) {
41 this.rootNode = rootNode;
42 this.xpath = xpath;
43 }
44
45 public Object evaluate(List parameters) throws XPathFunctionException {
46 String xPathExpression = getXPathExpressionParameter(parameters);
47 evaluator.setXpath(xpath);
48
49 try {
50 return evaluator.evaluate(xPathExpression, rootNode);
51 } catch (XPathExpressionException e) {
52 throw new XPathFunctionException(e);
53 }
54 }
55
56 private String getXPathExpressionParameter(List parameters) throws XPathFunctionException {
57 if (parameters.size() < 1) {
58 throw new XPathFunctionException("First parameter must be an XPath expression.");
59 }
60 if (!(parameters.get(0) instanceof String)) {
61 throw new XPathFunctionException("First parameter must be an XPath expression String");
62 }
63 return (String)parameters.get(0);
64 }
65
66 public XPath getXpath() {
67 return xpath;
68 }
69
70 public void setXpath(XPath xpath) {
71 this.xpath = xpath;
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85 }