Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XStreamSafeSearchFunction |
|
| 2.4;2.4 |
1 | /* | |
2 | * Copyright 2005-2007 The Kuali Foundation | |
3 | * | |
4 | * | |
5 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.opensource.org/licenses/ecl2.php | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
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 | * An XPathFunction which will run XStream safe XPath queries. | |
30 | * | |
31 | * @see XStreamSafeEvaluator | |
32 | * | |
33 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
34 | */ | |
35 | public class XStreamSafeSearchFunction implements XPathFunction { | |
36 | ||
37 | private final Node rootNode; | |
38 | private XPath xpath; | |
39 | 1 | private static XStreamSafeEvaluator evaluator = new XStreamSafeEvaluator(); |
40 | ||
41 | 10 | public XStreamSafeSearchFunction(Node rootNode, XPath xpath) { |
42 | 10 | this.rootNode = rootNode; |
43 | 10 | this.xpath = xpath; |
44 | 10 | } |
45 | ||
46 | public Object evaluate(List parameters) throws XPathFunctionException { | |
47 | 10 | String xPathExpression = getXPathExpressionParameter(parameters); |
48 | 10 | evaluator.setXpath(xpath); |
49 | //Node rootSearchNode = getRootSearchNodeParameter(parameters); | |
50 | try { | |
51 | 10 | return evaluator.evaluate(xPathExpression, rootNode); |
52 | 0 | } catch (XPathExpressionException e) { |
53 | 0 | throw new XPathFunctionException(e); |
54 | } | |
55 | } | |
56 | ||
57 | private String getXPathExpressionParameter(List parameters) throws XPathFunctionException { | |
58 | 10 | if (parameters.size() < 1) { |
59 | 0 | throw new XPathFunctionException("First parameter must be an XPath expression."); |
60 | } | |
61 | 10 | if (!(parameters.get(0) instanceof String)) { |
62 | 0 | throw new XPathFunctionException("First parameter must be an XPath expression String"); |
63 | } | |
64 | 10 | return (String)parameters.get(0); |
65 | } | |
66 | ||
67 | public XPath getXpath() { | |
68 | 0 | return xpath; |
69 | } | |
70 | ||
71 | public void setXpath(XPath xpath) { | |
72 | 0 | this.xpath = xpath; |
73 | 0 | } |
74 | ||
75 | /*private Node getRootSearchNodeParameter(List parameters) throws XPathFunctionException { | |
76 | if (parameters.size() < 2) { | |
77 | throw new XPathFunctionException("Second parameter should be root node and is required"); | |
78 | } | |
79 | System.out.println(parameters.get(1)); | |
80 | if (!(parameters.get(1) instanceof Node)) { | |
81 | throw new XPathFunctionException("Second parameter should be an instance of Node (try using the root() XPath function)."); | |
82 | } | |
83 | return (Node)parameters.get(1); | |
84 | }*/ | |
85 | ||
86 | } |