View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.engine.node.var.schemes;
18  
19  import java.io.StringReader;
20  
21  import javax.xml.namespace.QName;
22  import javax.xml.xpath.XPath;
23  import javax.xml.xpath.XPathConstants;
24  import javax.xml.xpath.XPathExpressionException;
25  import javax.xml.xpath.XPathVariableResolver;
26  
27  import org.apache.log4j.Logger;
28  import org.kuali.rice.kew.engine.RouteContext;
29  import org.kuali.rice.kew.engine.node.BranchState;
30  import org.kuali.rice.kew.engine.node.service.BranchService;
31  import org.kuali.rice.kew.engine.node.var.Property;
32  import org.kuali.rice.kew.engine.node.var.PropertyScheme;
33  import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
34  import org.kuali.rice.kew.service.KEWServiceLocator;
35  import org.xml.sax.InputSource;
36  
37  
38  /**
39   * A PropertyScheme that resolves the Property by evaluating it as an XPath expression.
40   * DocumentRouteHeaderValue variables are set on the XPath instance so they are accessible.
41   * 
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class XPathScheme implements PropertyScheme {
45      private static final Logger LOG = Logger.getLogger(XPathScheme.class);
46  
47      public String getName() {
48          return "xpath";
49      }
50  
51      public String getShortName() {
52          return "xpath";
53      }
54  
55      public Object load(Property property, final RouteContext context) {
56          XPath xpath = XPathHelper.newXPath();
57          final BranchService branchService = KEWServiceLocator.getBranchService();
58          xpath.setXPathVariableResolver(new XPathVariableResolver() {
59              public Object resolveVariable(QName name) {
60                  LOG.debug("Resolving XPath variable: " + name);
61                  String value = branchService.getScopedVariableValue(context.getNodeInstance().getBranch(), BranchState.VARIABLE_PREFIX + name.getLocalPart());
62                  LOG.debug("Resolved XPath variable " + name + " to " + value);
63                  return value;
64              }
65          });
66          try {
67              String docContent = context.getDocument().getDocContent();
68              LOG.debug("Executing xpath expression '" + property.locator + "' in doc '" + docContent + "'");
69              return xpath.evaluate(property.locator, new InputSource(new StringReader(docContent)), XPathConstants.STRING);
70          } catch (XPathExpressionException xpee) {
71              throw new RuntimeException("Error evaluating xpath expression '" + property.locator + "'", xpee);
72          }
73      }
74  }