001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.kew.engine.node.var.schemes;
017    
018    import java.io.StringReader;
019    
020    import javax.xml.namespace.QName;
021    import javax.xml.xpath.XPath;
022    import javax.xml.xpath.XPathConstants;
023    import javax.xml.xpath.XPathExpressionException;
024    import javax.xml.xpath.XPathVariableResolver;
025    
026    import org.apache.log4j.Logger;
027    import org.kuali.rice.kew.engine.RouteContext;
028    import org.kuali.rice.kew.engine.node.BranchState;
029    import org.kuali.rice.kew.engine.node.service.BranchService;
030    import org.kuali.rice.kew.engine.node.var.Property;
031    import org.kuali.rice.kew.engine.node.var.PropertyScheme;
032    import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
033    import org.kuali.rice.kew.service.KEWServiceLocator;
034    import 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     */
043    public 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    }