1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
40
41
42
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 }