01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.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 edu.iu.uis.eden.support.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 ewestfal
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)
47: throws XPathFunctionException {
48: String xPathExpression = getXPathExpressionParameter(parameters);
49: evaluator.setXpath(xpath);
50: //Node rootSearchNode = getRootSearchNodeParameter(parameters);
51: try {
52: return evaluator.evaluate(xPathExpression, rootNode);
53: } catch (XPathExpressionException e) {
54: throw new XPathFunctionException(e);
55: }
56: }
57:
58: private String getXPathExpressionParameter(List parameters)
59: throws XPathFunctionException {
60: if (parameters.size() < 1) {
61: throw new XPathFunctionException(
62: "First parameter must be an XPath expression.");
63: }
64: if (!(parameters.get(0) instanceof String)) {
65: throw new XPathFunctionException(
66: "First parameter must be an XPath expression String");
67: }
68: return (String) parameters.get(0);
69: }
70:
71: public XPath getXpath() {
72: return xpath;
73: }
74:
75: public void setXpath(XPath xpath) {
76: this .xpath = xpath;
77: }
78:
79: /*private Node getRootSearchNodeParameter(List parameters) throws XPathFunctionException {
80: if (parameters.size() < 2) {
81: throw new XPathFunctionException("Second parameter should be root node and is required");
82: }
83: System.out.println(parameters.get(1));
84: if (!(parameters.get(1) instanceof Node)) {
85: throw new XPathFunctionException("Second parameter should be an instance of Node (try using the root() XPath function).");
86: }
87: return (Node)parameters.get(1);
88: }*/
89:
90: }
|