Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XPathHelper |
|
| 1.5;1.5 |
1 | /* | |
2 | * Copyright 2005-2008 The Kuali Foundation | |
3 | * | |
4 | * | |
5 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.opensource.org/licenses/ecl2.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 org.kuali.rice.kew.rule.xmlrouting; | |
18 | ||
19 | import javax.xml.xpath.XPath; | |
20 | import javax.xml.xpath.XPathFactory; | |
21 | import javax.xml.xpath.XPathFunctionResolver; | |
22 | ||
23 | import org.kuali.rice.kew.exception.WorkflowRuntimeException; | |
24 | import org.w3c.dom.Node; | |
25 | ||
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 Kuali Rice Team (rice.collab@kuali.org) | |
32 | */ | |
33 | 0 | 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 | 0 | XPath xPath = XPathFactory.newInstance().newXPath(); |
41 | 0 | xPath.setNamespaceContext(new WorkflowNamespaceContext()); |
42 | 0 | WorkflowFunctionResolver resolver = new WorkflowFunctionResolver(); |
43 | 0 | xPath.setXPathFunctionResolver(resolver); |
44 | 0 | resolver.setXpath(xPath); |
45 | 0 | return xPath; |
46 | } | |
47 | ||
48 | /** | |
49 | * Creates a new XPath instances and initializes it with the WorkflowNamespaceContext and the | |
50 | * WorkflowFunctionResolver. Also sets the root node on the WorkflowFunctionResolver to | |
51 | * the given Node. This is required for some of the functions in the function resolver | |
52 | * to perform properly. | |
53 | */ | |
54 | public static XPath newXPath(Node rootNode) { | |
55 | 0 | XPath xPath = newXPath(); |
56 | 0 | WorkflowFunctionResolver resolver = extractFunctionResolver(xPath); |
57 | 0 | resolver.setRootNode(rootNode); |
58 | 0 | return xPath; |
59 | } | |
60 | ||
61 | /** | |
62 | * A utility to extract the WorkflowFunctionResolver from the given XPath instances. If the XPath instance | |
63 | * does not contain a WorkflowFunctionResolver, then this method will throw a WorkflowRuntimeException. | |
64 | * | |
65 | * @throws WorkflowRuntimeException if the given XPath instance does not contain a WorklflowFunctionResolver | |
66 | */ | |
67 | public static WorkflowFunctionResolver extractFunctionResolver(XPath xPath) { | |
68 | 0 | XPathFunctionResolver resolver = xPath.getXPathFunctionResolver(); |
69 | 0 | if (!hasWorkflowFunctionResolver(xPath)) { |
70 | 0 | throw new WorkflowRuntimeException("The XPathFunctionResolver on the given XPath instance is not an instance of WorkflowFunctionResolver, was: " + resolver); |
71 | } | |
72 | 0 | return (WorkflowFunctionResolver)resolver; |
73 | } | |
74 | ||
75 | /** | |
76 | * Returns true if the given XPath instance has a WorkflowFunctionResolver, false otherwise. | |
77 | */ | |
78 | public static boolean hasWorkflowFunctionResolver(XPath xPath) { | |
79 | 0 | return xPath.getXPathFunctionResolver() instanceof WorkflowFunctionResolver; |
80 | } | |
81 | ||
82 | } |