001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.xml.xstream;
017
018import java.util.List;
019
020import javax.xml.xpath.XPath;
021import javax.xml.xpath.XPathExpressionException;
022import javax.xml.xpath.XPathFunction;
023import javax.xml.xpath.XPathFunctionException;
024
025import org.w3c.dom.Node;
026
027/**
028 * An XPathFunction which will run XStream safe XPath queries.
029 * 
030 * @see XStreamSafeEvaluator
031 * 
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class XStreamSafeSearchFunction implements XPathFunction {
035
036        private final Node rootNode;
037        private XPath xpath;
038        private static XStreamSafeEvaluator evaluator = new XStreamSafeEvaluator();
039        
040        public XStreamSafeSearchFunction(Node rootNode, XPath xpath) {
041                this.rootNode = rootNode;
042                this.xpath = xpath;
043        }
044        
045        public Object evaluate(List parameters) throws XPathFunctionException {
046                String xPathExpression = getXPathExpressionParameter(parameters);
047                evaluator.setXpath(xpath);
048                //Node rootSearchNode = getRootSearchNodeParameter(parameters);
049                try {
050                        return evaluator.evaluate(xPathExpression, rootNode);
051                } catch (XPathExpressionException e) {
052                        throw new XPathFunctionException(e);
053                }
054        }
055        
056        private String getXPathExpressionParameter(List parameters) throws XPathFunctionException {
057                if (parameters.size() < 1) {
058                        throw new XPathFunctionException("First parameter must be an XPath expression.");
059                }
060                if (!(parameters.get(0) instanceof String)) {
061                        throw new XPathFunctionException("First parameter must be an XPath expression String");
062                }
063                return (String)parameters.get(0);
064        }
065
066        public XPath getXpath() {
067                return xpath;
068        }
069
070        public void setXpath(XPath xpath) {
071                this.xpath = xpath;
072        }
073        
074        /*private Node getRootSearchNodeParameter(List parameters) throws XPathFunctionException {
075                if (parameters.size() < 2) {
076                        throw new XPathFunctionException("Second parameter should be root node and is required");
077                }
078                System.out.println(parameters.get(1));
079                if (!(parameters.get(1) instanceof Node)) {
080                        throw new XPathFunctionException("Second parameter should be an instance of Node (try using the root() XPath function).");
081                }
082                return (Node)parameters.get(1);
083        }*/
084
085}