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