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