View Javadoc

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          XPath xpath = XPathFactory.newInstance().newXPath();
49          try {
50              NAME_EXPR = xpath.compile("name");
51              VALUE_EXPR = xpath.compile("value");
52              FIELD_EXPR = xpath.compile("field");
53          } catch (XPathExpressionException xpee) {
54              throw new RuntimeException(xpee);
55          }
56      }
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          this(clazz.getName());
65      }
66      public GenericAttributeContent(String elementName) {
67          this.elementName = elementName;
68          log = Logger.getLogger(GenericAttributeContent.class + "[" + elementName + "]");
69          try {
70              attr_expr = XPathFactory.newInstance().newXPath().compile(elementName);
71          } catch (XPathExpressionException xpee) {
72              throw new RuntimeException(xpee);
73          }
74      }
75  
76      public String generateContent(Map<String, String> properties) {
77          if (properties.size() == 0) return "<" + elementName + "/>";
78  
79          StringBuilder sb = new StringBuilder();
80          sb.append("<" + elementName + ">\r\n");
81          for (Map.Entry<String, String> entry: properties.entrySet()) {
82              String key = entry.getKey();
83              sb.append("  <field>\r\n");
84              if (key != null) {
85                  sb.append("    <name>" + key + "</name>\r\n");
86              } else {
87                  log.warn("null key encountered");
88              }
89              String value = entry.getValue();
90              if (value != null) {
91                  sb.append("    <value>" + entry.getValue() + "</value>\r\n");
92              } else {
93                  log.warn("null value encountered for key: " + key);
94              }
95              sb.append("  </field>\r\n");
96          }
97          sb.append("</" + elementName + ">\r\n");
98  
99          return sb.toString();
100     }
101 
102     public List<Map<String, String>> parseContent(Element attributeContent) throws XPathExpressionException {
103         List<Map<String, String>> attrs = new ArrayList<Map<String, String>>();
104         if (attributeContent == null) {
105             return attrs;
106         }
107         log.info("Parsing content: "+ XmlHelper.jotNode(attributeContent));
108         NodeList attrNodes = (NodeList) attr_expr.evaluate(attributeContent, XPathConstants.NODESET);
109         if (attrNodes != null) {
110             for (int i = 0; i < attrNodes.getLength(); i++) {
111                 Map<String, String> props = new HashMap<String, String>();
112                 attrs.add(props);
113                 Node node = attrNodes.item(i);
114                 log.info("Found matching attribute: " + XmlHelper.jotNode(node));
115                 NodeList fieldNodes = (NodeList) FIELD_EXPR.evaluate(node, XPathConstants.NODESET);
116                 for (int j = 0; j < fieldNodes.getLength(); j++) {
117                     node = fieldNodes.item(j);
118                     log.info("Found matching attribute content field: " + XmlHelper.jotNode(node));
119                     Boolean b = (Boolean) NAME_EXPR.evaluate(node, XPathConstants.BOOLEAN);
120                     if (!b.booleanValue()) {
121                         log.error("Encountered field with no name, skipping!");
122                         continue;
123                     }
124                     String name = NAME_EXPR.evaluate(node);
125                     b = (Boolean) VALUE_EXPR.evaluate(node, XPathConstants.BOOLEAN);
126                     String value = null;
127                     if (b.booleanValue()) {
128                         value = VALUE_EXPR.evaluate(node);
129                     } else {
130                         log.warn("No value defined for transmitted field named: " + name);
131                     }
132                     log.info("Matching attribute content field value: " + name + "=" + value);
133                     props.put(name, value);
134                 }
135             }
136         }
137         return attrs;
138     }
139 }