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.engine.node.var.schemes;
017
018import java.io.StringReader;
019
020import javax.xml.namespace.QName;
021import javax.xml.xpath.XPath;
022import javax.xml.xpath.XPathConstants;
023import javax.xml.xpath.XPathExpressionException;
024import javax.xml.xpath.XPathVariableResolver;
025
026import org.apache.log4j.Logger;
027import org.kuali.rice.kew.engine.RouteContext;
028import org.kuali.rice.kew.engine.node.BranchState;
029import org.kuali.rice.kew.engine.node.service.BranchService;
030import org.kuali.rice.kew.engine.node.var.Property;
031import org.kuali.rice.kew.engine.node.var.PropertyScheme;
032import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
033import org.kuali.rice.kew.service.KEWServiceLocator;
034import org.xml.sax.InputSource;
035
036
037/**
038 * A PropertyScheme that resolves the Property by evaluating it as an XPath expression.
039 * DocumentRouteHeaderValue variables are set on the XPath instance so they are accessible.
040 * 
041 * @author Kuali Rice Team (rice.collab@kuali.org)
042 */
043public class XPathScheme implements PropertyScheme {
044    private static final Logger LOG = Logger.getLogger(XPathScheme.class);
045
046    public String getName() {
047        return "xpath";
048    }
049
050    public String getShortName() {
051        return "xpath";
052    }
053
054    public Object load(Property property, final RouteContext context) {
055        XPath xpath = XPathHelper.newXPath();
056        final BranchService branchService = KEWServiceLocator.getBranchService();
057        xpath.setXPathVariableResolver(new XPathVariableResolver() {
058            public Object resolveVariable(QName name) {
059                LOG.debug("Resolving XPath variable: " + name);
060                String value = branchService.getScopedVariableValue(context.getNodeInstance().getBranch(), BranchState.VARIABLE_PREFIX + name.getLocalPart());
061                LOG.debug("Resolved XPath variable " + name + " to " + value);
062                return value;
063            }
064        });
065        try {
066            String docContent = context.getDocument().getDocContent();
067            LOG.debug("Executing xpath expression '" + property.locator + "' in doc '" + docContent + "'");
068            return xpath.evaluate(property.locator, new InputSource(new StringReader(docContent)), XPathConstants.STRING);
069        } catch (XPathExpressionException xpee) {
070            throw new RuntimeException("Error evaluating xpath expression '" + property.locator + "'", xpee);
071        }
072    }
073}