1 /**
2 * Copyright 2005-2011 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.kew.rule.xmlrouting;
17
18 import javax.xml.xpath.XPath;
19 import javax.xml.xpath.XPathFactory;
20 import javax.xml.xpath.XPathFunctionResolver;
21
22 import org.kuali.rice.kew.api.WorkflowRuntimeException;
23 import org.w3c.dom.Node;
24
25
26 /**
27 * Provides utilities for obtaining XPath instances which are "good-to-go" with access to the Workflow
28 * namespace and custom XPath functions.
29 *
30 * @author Kuali Rice Team (rice.collab@kuali.org)
31 */
32 public class XPathHelper {
33
34 /**
35 * Creates a new XPath instance and initializes it with the WorkflowNamespaceContext and the
36 * WorkflowFunctionResolver.
37 */
38 public static XPath newXPath() {
39 XPath xPath = XPathFactory.newInstance().newXPath();
40 xPath.setNamespaceContext(new WorkflowNamespaceContext());
41 WorkflowFunctionResolver resolver = new WorkflowFunctionResolver();
42 xPath.setXPathFunctionResolver(resolver);
43 resolver.setXpath(xPath);
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(XPath xPath) {
67 XPathFunctionResolver resolver = xPath.getXPathFunctionResolver();
68 if (!hasWorkflowFunctionResolver(xPath)) {
69 throw new WorkflowRuntimeException("The XPathFunctionResolver on the given XPath instance is not an instance of WorkflowFunctionResolver, was: " + resolver);
70 }
71 return (WorkflowFunctionResolver)resolver;
72 }
73
74 /**
75 * Returns true if the given XPath instance has a WorkflowFunctionResolver, false otherwise.
76 */
77 public static boolean hasWorkflowFunctionResolver(XPath xPath) {
78 return xPath.getXPathFunctionResolver() instanceof WorkflowFunctionResolver;
79 }
80
81 }