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