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