Coverage Report - org.kuali.rice.kew.engine.node.var.schemes.XPathScheme
 
Classes in this File Line Coverage Branch Coverage Complexity
XPathScheme
0%
0/12
N/A
1.75
XPathScheme$1
0%
0/5
N/A
1.75
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.engine.node.var.schemes;
 17  
 
 18  
 import java.io.StringReader;
 19  
 
 20  
 import javax.xml.namespace.QName;
 21  
 import javax.xml.xpath.XPath;
 22  
 import javax.xml.xpath.XPathConstants;
 23  
 import javax.xml.xpath.XPathExpressionException;
 24  
 import javax.xml.xpath.XPathVariableResolver;
 25  
 
 26  
 import org.apache.log4j.Logger;
 27  
 import org.kuali.rice.kew.engine.RouteContext;
 28  
 import org.kuali.rice.kew.engine.node.BranchState;
 29  
 import org.kuali.rice.kew.engine.node.service.BranchService;
 30  
 import org.kuali.rice.kew.engine.node.var.Property;
 31  
 import org.kuali.rice.kew.engine.node.var.PropertyScheme;
 32  
 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
 33  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 34  
 import org.xml.sax.InputSource;
 35  
 
 36  
 
 37  
 /**
 38  
  * A PropertyScheme that resolves the Property by evaluating it as an XPath expression.
 39  
  * DocumentRouteHeaderValue variables are set on the XPath instance so they are accessible.
 40  
  * 
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  0
 public class XPathScheme implements PropertyScheme {
 44  0
     private static final Logger LOG = Logger.getLogger(XPathScheme.class);
 45  
 
 46  
     public String getName() {
 47  0
         return "xpath";
 48  
     }
 49  
 
 50  
     public String getShortName() {
 51  0
         return "xpath";
 52  
     }
 53  
 
 54  
     public Object load(Property property, final RouteContext context) {
 55  0
         XPath xpath = XPathHelper.newXPath();
 56  0
         final BranchService branchService = KEWServiceLocator.getBranchService();
 57  0
         xpath.setXPathVariableResolver(new XPathVariableResolver() {
 58  
             public Object resolveVariable(QName name) {
 59  0
                 LOG.debug("Resolving XPath variable: " + name);
 60  0
                 String value = branchService.getScopedVariableValue(context.getNodeInstance().getBranch(), BranchState.VARIABLE_PREFIX + name.getLocalPart());
 61  0
                 LOG.debug("Resolved XPath variable " + name + " to " + value);
 62  0
                 return value;
 63  
             }
 64  
         });
 65  
         try {
 66  0
             String docContent = context.getDocument().getDocContent();
 67  0
             LOG.debug("Executing xpath expression '" + property.locator + "' in doc '" + docContent + "'");
 68  0
             return xpath.evaluate(property.locator, new InputSource(new StringReader(docContent)), XPathConstants.STRING);
 69  0
         } catch (XPathExpressionException xpee) {
 70  0
             throw new RuntimeException("Error evaluating xpath expression '" + property.locator + "'", xpee);
 71  
         }
 72  
     }
 73  
 }