001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.routetemplate;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.xml.xpath.XPath;
025: import javax.xml.xpath.XPathConstants;
026: import javax.xml.xpath.XPathExpression;
027: import javax.xml.xpath.XPathExpressionException;
028: import javax.xml.xpath.XPathFactory;
029:
030: import org.apache.log4j.Logger;
031: import org.w3c.dom.Element;
032: import org.w3c.dom.Node;
033: import org.w3c.dom.NodeList;
034:
035: import edu.iu.uis.eden.util.XmlHelper;
036:
037: /**
038: * Helper class that can parse and generate generic attribute content
039: * from Map<String,String> values.
040: *
041: * @author Aaron Hamid (arh14 at cornell dot edu)
042: */
043: public class GenericAttributeContent {
044: private static final XPathExpression NAME_EXPR;
045: private static final XPathExpression VALUE_EXPR;
046: private static final XPathExpression FIELD_EXPR;
047: static {
048: XPath xpath = XPathFactory.newInstance().newXPath();
049: try {
050: NAME_EXPR = xpath.compile("name");
051: VALUE_EXPR = xpath.compile("value");
052: FIELD_EXPR = xpath.compile("field");
053: } catch (XPathExpressionException xpee) {
054: throw new RuntimeException(xpee);
055: }
056: }
057:
058: private final Logger log;
059:
060: private final String elementName;
061: private final XPathExpression attr_expr;
062:
063: public GenericAttributeContent(Class clazz) {
064: this (clazz.getName());
065: }
066:
067: public GenericAttributeContent(String elementName) {
068: this .elementName = elementName;
069: log = Logger.getLogger(GenericAttributeContent.class + "["
070: + elementName + "]");
071: try {
072: attr_expr = XPathFactory.newInstance().newXPath().compile(
073: elementName);
074: } catch (XPathExpressionException xpee) {
075: throw new RuntimeException(xpee);
076: }
077: }
078:
079: public String generateContent(Map<String, String> properties) {
080: if (properties.size() == 0)
081: return "<" + elementName + "/>";
082:
083: StringBuilder sb = new StringBuilder();
084: sb.append("<" + elementName + ">\r\n");
085: for (Map.Entry<String, String> entry : properties.entrySet()) {
086: String key = entry.getKey();
087: sb.append(" <field>\r\n");
088: if (key != null) {
089: sb.append(" <name>" + key + "</name>\r\n");
090: } else {
091: log.warn("null key encountered");
092: }
093: String value = entry.getValue();
094: if (value != null) {
095: sb.append(" <value>" + entry.getValue()
096: + "</value>\r\n");
097: } else {
098: log.warn("null value encountered for key: " + key);
099: }
100: sb.append(" </field>\r\n");
101: }
102: sb.append("</" + elementName + ">\r\n");
103:
104: return sb.toString();
105: }
106:
107: public List<Map<String, String>> parseContent(
108: Element attributeContent) throws XPathExpressionException {
109: List<Map<String, String>> attrs = new ArrayList<Map<String, String>>();
110: if (attributeContent == null) {
111: return attrs;
112: }
113: log.info("Parsing content: "
114: + XmlHelper.jotNode(attributeContent));
115: NodeList attrNodes = (NodeList) attr_expr.evaluate(
116: attributeContent, XPathConstants.NODESET);
117: if (attrNodes != null) {
118: for (int i = 0; i < attrNodes.getLength(); i++) {
119: Map<String, String> props = new HashMap<String, String>();
120: attrs.add(props);
121: Node node = attrNodes.item(i);
122: log.info("Found matching attribute: "
123: + XmlHelper.jotNode(node));
124: NodeList fieldNodes = (NodeList) FIELD_EXPR.evaluate(
125: node, XPathConstants.NODESET);
126: for (int j = 0; j < fieldNodes.getLength(); j++) {
127: node = fieldNodes.item(j);
128: log.info("Found matching attribute content field: "
129: + XmlHelper.jotNode(node));
130: Boolean b = (Boolean) NAME_EXPR.evaluate(node,
131: XPathConstants.BOOLEAN);
132: if (!b.booleanValue()) {
133: log
134: .error("Encountered field with no name, skipping!");
135: continue;
136: }
137: String name = NAME_EXPR.evaluate(node);
138: b = (Boolean) VALUE_EXPR.evaluate(node,
139: XPathConstants.BOOLEAN);
140: String value = null;
141: if (b.booleanValue()) {
142: value = VALUE_EXPR.evaluate(node);
143: } else {
144: log
145: .warn("No value defined for transmitted field named: "
146: + name);
147: }
148: log.info("Matching attribute content field value: "
149: + name + "=" + value);
150: props.put(name, value);
151: }
152: }
153: }
154: return attrs;
155: }
156: }
|