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.routetemplate.xmlrouting;
18:
19: import javax.xml.xpath.XPath;
20: import javax.xml.xpath.XPathFactory;
21: import javax.xml.xpath.XPathFunctionResolver;
22:
23: import org.w3c.dom.Node;
24:
25: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
26:
27: /**
28: * Provides utilities for obtaining XPath instances which are "good-to-go" with access to the Workflow
29: * namespace and custom XPath functions.
30: *
31: * @author ewestfal
32: */
33: public class XPathHelper {
34:
35: /**
36: * Creates a new XPath instance and initializes it with the WorkflowNamespaceContext and the
37: * WorkflowFunctionResolver.
38: */
39: public static XPath newXPath() {
40: XPath xPath = XPathFactory.newInstance().newXPath();
41: xPath.setNamespaceContext(new WorkflowNamespaceContext());
42: WorkflowFunctionResolver resolver = new WorkflowFunctionResolver();
43: xPath.setXPathFunctionResolver(resolver);
44: return xPath;
45: }
46:
47: /**
48: * Creates a new XPath instances and initializes it with the WorkflowNamespaceContext and the
49: * WorkflowFunctionResolver. Also sets the root node on the WorkflowFunctionResolver to
50: * the given Node. This is required for some of the functions in the function resolver
51: * to perform properly.
52: */
53: public static XPath newXPath(Node rootNode) {
54: XPath xPath = newXPath();
55: WorkflowFunctionResolver resolver = extractFunctionResolver(xPath);
56: resolver.setRootNode(rootNode);
57: return xPath;
58: }
59:
60: /**
61: * A utility to extract the WorkflowFunctionResolver from the given XPath instances. If the XPath instance
62: * does not contain a WorkflowFunctionResolver, then this method will throw a WorkflowRuntimeException.
63: *
64: * @throws WorkflowRuntimeException if the given XPath instance does not contain a WorklflowFunctionResolver
65: */
66: public static WorkflowFunctionResolver extractFunctionResolver(
67: XPath xPath) {
68: XPathFunctionResolver resolver = xPath
69: .getXPathFunctionResolver();
70: if (!hasWorkflowFunctionResolver(xPath)) {
71: throw new WorkflowRuntimeException(
72: "The XPathFunctionResolver on the given XPath instance is not an instance of WorkflowFunctionResolver, was: "
73: + resolver);
74: }
75: return (WorkflowFunctionResolver) resolver;
76: }
77:
78: /**
79: * Returns true if the given XPath instance has a WorkflowFunctionResolver, false otherwise.
80: */
81: public static boolean hasWorkflowFunctionResolver(XPath xPath) {
82: return xPath.getXPathFunctionResolver() instanceof WorkflowFunctionResolver;
83: }
84:
85: }
|