View Javadoc

1   /**
2    * Copyright 2005-2014 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  public class XPathScheme implements PropertyScheme {
44      private static final Logger LOG = Logger.getLogger(XPathScheme.class);
45  
46      public String getName() {
47          return "xpath";
48      }
49  
50      public String getShortName() {
51          return "xpath";
52      }
53  
54      public Object load(Property property, final RouteContext context) {
55          XPath xpath = XPathHelper.newXPath();
56          final BranchService branchService = KEWServiceLocator.getBranchService();
57          xpath.setXPathVariableResolver(new XPathVariableResolver() {
58              public Object resolveVariable(QName name) {
59                  LOG.debug("Resolving XPath variable: " + name);
60                  String value = branchService.getScopedVariableValue(context.getNodeInstance().getBranch(), BranchState.VARIABLE_PREFIX + name.getLocalPart());
61                  LOG.debug("Resolved XPath variable " + name + " to " + value);
62                  return value;
63              }
64          });
65          try {
66              String docContent = context.getDocument().getDocContent();
67              LOG.debug("Executing xpath expression '" + property.locator + "' in doc '" + docContent + "'");
68              return xpath.evaluate(property.locator, new InputSource(new StringReader(docContent)), XPathConstants.STRING);
69          } catch (XPathExpressionException xpee) {
70              throw new RuntimeException("Error evaluating xpath expression '" + property.locator + "'", xpee);
71          }
72      }
73  }