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