Coverage Report - org.kuali.rice.kew.rule.xmlrouting.WorkflowFunctionResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
WorkflowFunctionResolver
0%
0/29
0%
0/18
3.083
WorkflowFunctionResolver$1
0%
0/10
0%
0/8
3.083
WorkflowFunctionResolver$2
0%
0/7
0%
0/2
3.083
WorkflowFunctionResolver$3
0%
0/2
N/A
3.083
 
 1  
 /*
 2  
  * Copyright 2005-2007 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 java.util.Iterator;
 20  
 import java.util.List;
 21  
 
 22  
 import javax.xml.namespace.QName;
 23  
 import javax.xml.xpath.XPath;
 24  
 import javax.xml.xpath.XPathFunction;
 25  
 import javax.xml.xpath.XPathFunctionResolver;
 26  
 
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 29  
 import org.kuali.rice.kew.rule.RuleExtension;
 30  
 import org.kuali.rice.kew.rule.RuleExtensionValue;
 31  
 import org.kuali.rice.kew.xml.xstream.XStreamSafeSearchFunction;
 32  
 import org.w3c.dom.Node;
 33  
 
 34  
 
 35  
 /**
 36  
  * A function resolver for XPath functions provided by KEW.
 37  
  *
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  */
 40  0
 public class WorkflowFunctionResolver implements XPathFunctionResolver {
 41  
         
 42  
         private List<RuleExtension> ruleExtensions;
 43  
         private Node rootNode;
 44  
         private XPath xpath;
 45  
 
 46  
 
 47  
         public XPathFunction resolveFunction(QName fname, int arity) {
 48  0
                 if (fname == null) {
 49  0
                         throw new NullPointerException("The function name cannot be null.");
 50  
                 }
 51  0
                 if (fname.equals(new QName("http://nothingfornowwf.com", "ruledata", "wf"))) {
 52  0
                         if (ruleExtensions == null) {
 53  0
                                 throw new IllegalArgumentException("There are no rule extensions.");
 54  
                         }
 55  0
                         return new XPathFunction() {
 56  
                                 public Object evaluate(List args) {
 57  0
                                         if (args.size() == 1) {
 58  0
                                                 String name = (String) args.get(0);
 59  0
                                                 for (RuleExtension ruleExtension : ruleExtensions) {
 60  0
                                                     for (Iterator iter = ruleExtension.getExtensionValues().iterator(); iter.hasNext();) {
 61  0
                                                         RuleExtensionValue value = (RuleExtensionValue) iter.next();
 62  0
                                                         if (value.getKey().equals(name)) {
 63  0
                                                             return value.getValue();
 64  
                                                         }
 65  0
                                                     }
 66  
                                                 }
 67  
                                         }
 68  0
                                         return "";
 69  
                                 }
 70  
                         };
 71  0
                 } else if (fname.equals(new QName("http://nothingfornowwf.com", "xstreamsafe", "wf"))) {
 72  0
                         return new XStreamSafeSearchFunction(rootNode, this.getXpath());
 73  0
                 } else if (fname.equals(new QName("http://nothingfornowwf.com", "upper-case", "wf"))) {
 74  0
                         return new UpperCaseFunction();
 75  0
                 } else if (fname.equals(new QName("http://nothingfornowwf.com", "field", "wf"))) {
 76  0
                         return new XPathFunction() {
 77  
                                 public Object evaluate(java.util.List args) {
 78  0
                                         if (args.size() == 1) {
 79  0
                                                 String name = (String) args.get(0);
 80  
                                                 try {
 81  0
                                                         return field(name);
 82  0
                                                 } catch (Exception e) {
 83  0
                                                         throw new WorkflowRuntimeException("Failed to find field to validate.", e);
 84  
                                                 }
 85  
                                         }
 86  0
                                         return "";
 87  
                                 }
 88  
                         };
 89  0
                 } else if (fname.equals(new QName("http://nothingfornowwf.com", "empty", "wf"))) {
 90  0
                         return new XPathFunction() {
 91  
                                 public Object evaluate(java.util.List args) {
 92  0
                                         return empty(args.get(0));
 93  
                                 }
 94  
                         };
 95  
                 } else {
 96  0
                         return null;
 97  
                 }
 98  
         }
 99  
 
 100  
         public String field(String fieldName) throws Exception {
 101  0
             return xpath.evaluate("//edlContent/data/version[@current='true']/field[@name='" + fieldName + "']/value", rootNode);
 102  
         }
 103  
             
 104  
             
 105  
         public boolean empty(Object object) {
 106  0
             if (object instanceof String) {
 107  0
                     return StringUtils.isBlank((String)object);
 108  
             }
 109  0
             return object == null;
 110  
         }
 111  
 
 112  
         public List<RuleExtension> getRuleExtensions() {
 113  0
         return this.ruleExtensions;
 114  
     }
 115  
 
 116  
     public void setRuleExtensions(List<RuleExtension> ruleExtensions) {
 117  0
         this.ruleExtensions = ruleExtensions;
 118  0
     }
 119  
 
 120  
     public Node getRootNode() {
 121  0
                 return rootNode;
 122  
         }
 123  
 
 124  
         public void setRootNode(Node rootNode) {
 125  0
                 this.rootNode = rootNode;
 126  0
         }
 127  
         
 128  
 
 129  
         public XPath getXpath() {
 130  0
                 return xpath;
 131  
         }
 132  
 
 133  
         public void setXpath(XPath xpath) {
 134  0
                 this.xpath = xpath;
 135  0
         }
 136  
 }