View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * An XPathFunction which will run XStream safe XPath queries.
29   * 
30   * @see XStreamSafeEvaluator
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
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  		//Node rootSearchNode = getRootSearchNodeParameter(parameters);
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  	/*private Node getRootSearchNodeParameter(List parameters) throws XPathFunctionException {
75  		if (parameters.size() < 2) {
76  			throw new XPathFunctionException("Second parameter should be root node and is required");
77  		}
78  		System.out.println(parameters.get(1));
79  		if (!(parameters.get(1) instanceof Node)) {
80  			throw new XPathFunctionException("Second parameter should be an instance of Node (try using the root() XPath function).");
81  		}
82  		return (Node)parameters.get(1);
83  	}*/
84  
85  }