Coverage Report - org.kuali.rice.kew.rule.xmlrouting.XPathHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
XPathHelper
87%
14/16
50%
1/2
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.api.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  7
                 XPath xPath = XPathFactory.newInstance().newXPath();
 41  7
                 xPath.setNamespaceContext(new WorkflowNamespaceContext());
 42  7
                 WorkflowFunctionResolver resolver = new WorkflowFunctionResolver();
 43  7
                 xPath.setXPathFunctionResolver(resolver); 
 44  7
                 resolver.setXpath(xPath);
 45  7
                 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  5
                 XPath xPath = newXPath();
 56  5
                 WorkflowFunctionResolver resolver = extractFunctionResolver(xPath);
 57  5
                 resolver.setRootNode(rootNode);
 58  5
                 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  5
                 XPathFunctionResolver resolver = xPath.getXPathFunctionResolver();
 69  5
                 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  5
                 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  5
                 return xPath.getXPathFunctionResolver() instanceof WorkflowFunctionResolver;
 80  
         }
 81  
         
 82  
 }