Coverage Report - org.kuali.rice.kew.rule.GenericAttributeContent
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericAttributeContent
0%
0/63
0%
0/20
4.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;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.HashMap;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import javax.xml.xpath.XPath;
 25  
 import javax.xml.xpath.XPathConstants;
 26  
 import javax.xml.xpath.XPathExpression;
 27  
 import javax.xml.xpath.XPathExpressionException;
 28  
 import javax.xml.xpath.XPathFactory;
 29  
 
 30  
 import org.apache.log4j.Logger;
 31  
 import org.kuali.rice.kew.util.XmlHelper;
 32  
 import org.w3c.dom.Element;
 33  
 import org.w3c.dom.Node;
 34  
 import org.w3c.dom.NodeList;
 35  
 
 36  
 
 37  
 /**
 38  
  * Helper class that can parse and generate generic attribute content
 39  
  * from Map<String,String> values.
 40  
  * 
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  
 public class GenericAttributeContent {
 44  
     private static final XPathExpression NAME_EXPR;
 45  
     private static final XPathExpression VALUE_EXPR;
 46  
     private static final XPathExpression FIELD_EXPR;
 47  
     static {
 48  0
         XPath xpath = XPathFactory.newInstance().newXPath();
 49  
         try {
 50  0
             NAME_EXPR = xpath.compile("name");
 51  0
             VALUE_EXPR = xpath.compile("value");
 52  0
             FIELD_EXPR = xpath.compile("field");
 53  0
         } catch (XPathExpressionException xpee) {
 54  0
             throw new RuntimeException(xpee);
 55  0
         }
 56  0
     }
 57  
 
 58  
     private final Logger log;
 59  
 
 60  
     private final String elementName;
 61  
     private final XPathExpression attr_expr;
 62  
 
 63  
     public GenericAttributeContent(Class clazz) {
 64  0
         this(clazz.getName());
 65  0
     }
 66  0
     public GenericAttributeContent(String elementName) {
 67  0
         this.elementName = elementName;
 68  0
         log = Logger.getLogger(GenericAttributeContent.class + "[" + elementName + "]");
 69  
         try {
 70  0
             attr_expr = XPathFactory.newInstance().newXPath().compile(elementName);
 71  0
         } catch (XPathExpressionException xpee) {
 72  0
             throw new RuntimeException(xpee);
 73  0
         }
 74  0
     }
 75  
 
 76  
     public String generateContent(Map<String, String> properties) {
 77  0
         if (properties.size() == 0) return "<" + elementName + "/>";
 78  
 
 79  0
         StringBuilder sb = new StringBuilder();
 80  0
         sb.append("<" + elementName + ">\r\n");
 81  0
         for (Map.Entry<String, String> entry: properties.entrySet()) {
 82  0
             String key = entry.getKey();
 83  0
             sb.append("  <field>\r\n");
 84  0
             if (key != null) {
 85  0
                 sb.append("    <name>" + key + "</name>\r\n");
 86  
             } else {
 87  0
                 log.warn("null key encountered");
 88  
             }
 89  0
             String value = entry.getValue();
 90  0
             if (value != null) {
 91  0
                 sb.append("    <value>" + entry.getValue() + "</value>\r\n");
 92  
             } else {
 93  0
                 log.warn("null value encountered for key: " + key);
 94  
             }
 95  0
             sb.append("  </field>\r\n");
 96  0
         }
 97  0
         sb.append("</" + elementName + ">\r\n");
 98  
 
 99  0
         return sb.toString();
 100  
     }
 101  
 
 102  
     public List<Map<String, String>> parseContent(Element attributeContent) throws XPathExpressionException {
 103  0
         List<Map<String, String>> attrs = new ArrayList<Map<String, String>>();
 104  0
         if (attributeContent == null) {
 105  0
             return attrs;
 106  
         }
 107  0
         log.info("Parsing content: "+ XmlHelper.jotNode(attributeContent));
 108  0
         NodeList attrNodes = (NodeList) attr_expr.evaluate(attributeContent, XPathConstants.NODESET);
 109  0
         if (attrNodes != null) {
 110  0
             for (int i = 0; i < attrNodes.getLength(); i++) {
 111  0
                 Map<String, String> props = new HashMap<String, String>();
 112  0
                 attrs.add(props);
 113  0
                 Node node = attrNodes.item(i);
 114  0
                 log.info("Found matching attribute: " + XmlHelper.jotNode(node));
 115  0
                 NodeList fieldNodes = (NodeList) FIELD_EXPR.evaluate(node, XPathConstants.NODESET);
 116  0
                 for (int j = 0; j < fieldNodes.getLength(); j++) {
 117  0
                     node = fieldNodes.item(j);
 118  0
                     log.info("Found matching attribute content field: " + XmlHelper.jotNode(node));
 119  0
                     Boolean b = (Boolean) NAME_EXPR.evaluate(node, XPathConstants.BOOLEAN);
 120  0
                     if (!b.booleanValue()) {
 121  0
                         log.error("Encountered field with no name, skipping!");
 122  0
                         continue;
 123  
                     }
 124  0
                     String name = NAME_EXPR.evaluate(node);
 125  0
                     b = (Boolean) VALUE_EXPR.evaluate(node, XPathConstants.BOOLEAN);
 126  0
                     String value = null;
 127  0
                     if (b.booleanValue()) {
 128  0
                         value = VALUE_EXPR.evaluate(node);
 129  
                     } else {
 130  0
                         log.warn("No value defined for transmitted field named: " + name);
 131  
                     }
 132  0
                     log.info("Matching attribute content field value: " + name + "=" + value);
 133  0
                     props.put(name, value);
 134  
                 }
 135  
             }
 136  
         }
 137  0
         return attrs;
 138  
     }
 139  
 }