01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.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 edu.iu.uis.eden.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.xml.sax.InputSource;
29:
30: import edu.iu.uis.eden.KEWServiceLocator;
31: import edu.iu.uis.eden.engine.RouteContext;
32: import edu.iu.uis.eden.engine.node.BranchService;
33: import edu.iu.uis.eden.engine.node.BranchState;
34: import edu.iu.uis.eden.engine.node.var.Property;
35: import edu.iu.uis.eden.engine.node.var.PropertyScheme;
36: import edu.iu.uis.eden.routetemplate.xmlrouting.XPathHelper;
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 Aaron Hamid (arh14 at cornell dot edu)
43: */
44: public class XPathScheme implements PropertyScheme {
45: private static final Logger LOG = Logger
46: .getLogger(XPathScheme.class);
47:
48: public String getName() {
49: return "xpath";
50: }
51:
52: public String getShortName() {
53: return "xpath";
54: }
55:
56: public Object load(Property property, final RouteContext context) {
57: XPath xpath = XPathHelper.newXPath();
58: final BranchService branchService = KEWServiceLocator
59: .getBranchService();
60: xpath.setXPathVariableResolver(new XPathVariableResolver() {
61: public Object resolveVariable(QName name) {
62: LOG.debug("Resolving XPath variable: " + name);
63: String value = branchService.getScopedVariableValue(
64: context.getNodeInstance().getBranch(),
65: BranchState.VARIABLE_PREFIX
66: + name.getLocalPart());
67: LOG.debug("Resolved XPath variable " + name + " to "
68: + value);
69: return value;
70: }
71: });
72: try {
73: String docContent = context.getDocument().getDocContent();
74: LOG.debug("Executing xpath expression '" + property.locator
75: + "' in doc '" + docContent + "'");
76: return xpath.evaluate(property.locator, new InputSource(
77: new StringReader(docContent)),
78: XPathConstants.STRING);
79: } catch (XPathExpressionException xpee) {
80: throw new RuntimeException(
81: "Error evaluating xpath expression '"
82: + property.locator + "'", xpee);
83: }
84: }
85: }
|