1 18 19 package sync4j.syncclient.common; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.InputStream ; 23 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 import javax.xml.parsers.DocumentBuilder ; 29 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.NodeList ; 34 35 42 public class SourceUtils { 43 44 46 public static final String ROOT_NAME = "__root__name__" ; 47 48 public static final String TAG_XML_VERSION = "<?xml version=\"1.0\" encoding=\"" 49 + System.getProperty("file.encoding") 50 + "\"?>" ; 51 52 53 55 57 67 public static HashMap xmlToHashMap(String content) throws Exception { 68 content = dropTagCData(content); 69 return xmlToHashMap(new ByteArrayInputStream (content.getBytes())); 70 } 71 72 82 public static HashMap xmlToHashMap (InputStream is) 83 throws Exception { 84 85 DocumentBuilderFactory docBuilderFactory = null ; 86 DocumentBuilder docBuilder = null ; 87 Document docXml = null ; 88 NodeList lstChildren = null ; 89 Element el = null ; 90 Node node = null ; 91 92 HashMap fields = null ; 93 94 String value = null ; 95 String nodeValue = null ; 96 String rootName = null ; 97 98 try { 99 100 docBuilderFactory = DocumentBuilderFactory.newInstance ( ) ; 101 docBuilder = docBuilderFactory.newDocumentBuilder ( ) ; 102 docXml = docBuilder.parse ( is ) ; 103 el = docXml.getDocumentElement ( ) ; 104 rootName = el.getTagName ( ) ; 105 lstChildren = el.getChildNodes ( ) ; 106 107 fields = new HashMap (); 108 109 for (int i=0, l = lstChildren.getLength(); i < l; i++) { 110 111 node = lstChildren.item(i); 112 113 if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 114 115 if (node.getChildNodes().item(0) != null) { 116 nodeValue = node.getChildNodes().item(0).getNodeValue(); 117 } else { 118 nodeValue = ""; 119 } 120 121 fields.put(node.getNodeName(), nodeValue); 122 123 } 124 125 } 126 127 if (rootName != null) 130 fields.put(ROOT_NAME, rootName); 131 132 } catch (Exception e) { 133 throw new Exception (e.toString()); 134 } 135 136 return fields; 137 138 } 139 140 149 150 public static String hashMapToXml (HashMap fields) 151 throws Exception { 152 153 String fieldName = null ; 154 String fieldValue = null ; 155 String message = ""; 156 String rootName = null ; 157 158 Iterator i = null ; 159 int j = 0; 160 161 i = fields.keySet().iterator(); 162 163 while(i.hasNext()) { 164 165 fieldName = (String ) i.next() ; 166 fieldValue = (String ) fields.get((String )fieldName) ; 167 168 fieldValue = StringTools.escapeXml(fieldValue); 169 170 if (fieldName.equals (ROOT_NAME)) { 171 rootName = fieldValue; 172 } else { 173 message = message + 174 "<" + 175 fieldName + 176 ">" + 177 fieldValue + 178 "</" + 179 fieldName + 180 ">" ; 181 } 182 } 183 184 message = "<" + 185 rootName + 186 ">" + 187 message ; 188 189 message = message + 190 "</" + 191 rootName + 192 ">" ; 193 194 return TAG_XML_VERSION + message; 195 196 } 197 198 199 201 205 private static String dropTagCData(String content) { 206 207 String tmp = null ; 208 209 int startData = 0 ; 210 int endData = 0 ; 211 212 if (content.indexOf("<![CDATA[") != - 1) { 213 214 startData = content.indexOf("<![CDATA[") + "<![CDATA[".length(); 215 216 endData = content.lastIndexOf("]]]]>"); 220 221 if (endData == -1) { 222 endData = content.lastIndexOf("]]>"); 223 } 224 225 tmp = content.substring(startData, endData); 226 227 return content.substring(startData, endData); 228 229 } else { 230 return content; 231 } 232 233 } 234 235 243 public static String handleLineDelimiting(String content) { 244 return content.replaceAll("=\r\n","\r\n "); 245 } 246 } | Popular Tags |