001: package org.manentia.kasai.util;
002:
003: import java.io.IOException;
004: import java.io.StringReader;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.Map;
008:
009: import javax.xml.parsers.DocumentBuilder;
010: import javax.xml.parsers.DocumentBuilderFactory;
011: import javax.xml.parsers.FactoryConfigurationError;
012: import javax.xml.parsers.ParserConfigurationException;
013:
014: import org.apache.commons.lang.StringUtils;
015: import org.w3c.dom.Document;
016: import org.w3c.dom.Element;
017: import org.w3c.dom.Node;
018: import org.w3c.dom.NodeList;
019: import org.w3c.dom.Text;
020: import org.xml.sax.InputSource;
021: import org.xml.sax.SAXException;
022:
023: import com.manentia.commons.xml.XMLException;
024: import com.manentia.commons.xml.XMLUtil;
025:
026: public class MiscUtils {
027:
028: public static Map parseXMLMap(String xml) throws SAXException,
029: IOException, ParserConfigurationException,
030: FactoryConfigurationError {
031: Map result = new HashMap();
032:
033: if (StringUtils.isNotEmpty(xml)) {
034: DocumentBuilder docBuilder = DocumentBuilderFactory
035: .newInstance().newDocumentBuilder();
036:
037: Document doc = docBuilder.parse(new InputSource(
038: new StringReader(xml)));
039:
040: Element root = doc.getDocumentElement();
041: NodeList rootChilds = root.getChildNodes();
042: Element attribute = null;
043: Element key = null;
044: Element value = null;
045:
046: for (int i = 0; i < rootChilds.getLength(); i++) {
047: if (rootChilds.item(i).getNodeType() == Node.ELEMENT_NODE) {
048: attribute = (Element) rootChilds.item(i);
049: if (attribute.getNodeName().equals("Attribute")) {
050: key = (Element) attribute.getElementsByTagName(
051: "Key").item(0);
052: value = (Element) attribute
053: .getElementsByTagName("Value").item(0);
054:
055: result.put(XMLUtil.getElementTextContents(key),
056: XMLUtil.getElementTextContents(value));
057: }
058: }
059: }
060: }
061:
062: return result;
063: }
064:
065: public static String serializeMapToXML(Map map)
066: throws ParserConfigurationException,
067: FactoryConfigurationError {
068: String result = "";
069:
070: if (map != null) {
071: DocumentBuilder docBuilder = DocumentBuilderFactory
072: .newInstance().newDocumentBuilder();
073:
074: Document doc = docBuilder.newDocument();
075: Element root = doc.createElement("Attributes");
076: Element attribute = null;
077: Element key = null;
078: Element value = null;
079: Text elementText = null;
080:
081: doc.appendChild(root);
082:
083: String keyStr = null;
084: String valueStr = null;
085:
086: for (Iterator iter = map.keySet().iterator(); iter
087: .hasNext();) {
088: keyStr = (String) iter.next();
089: valueStr = (String) map.get(keyStr);
090:
091: attribute = doc.createElement("Attribute");
092: root.appendChild(attribute);
093:
094: key = doc.createElement("Key");
095: attribute.appendChild(key);
096: elementText = doc.createTextNode("keyText");
097: elementText.setData(keyStr);
098: key.appendChild(elementText);
099:
100: value = doc.createElement("Value");
101: attribute.appendChild(value);
102: elementText = doc.createTextNode("valueText");
103: elementText.setData(valueStr);
104: value.appendChild(elementText);
105: }
106:
107: result = XMLUtil.documentToString(doc);
108: }
109:
110: return result;
111: }
112:
113: }
|