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