1 57 58 package org.apache.soap.util.xml; 59 60 import java.io.*; 61 import org.w3c.dom.*; 62 import org.apache.soap.util.StringUtils; 63 64 75 public class DOMWriter 76 { 77 80 public static String nodeToString(Node node) 81 { 82 StringWriter sw = new StringWriter(); 83 84 serializeAsXML(node, sw); 85 86 return sw.toString(); 87 } 88 89 92 public static void serializeAsXML(Node node, Writer writer) 93 { 94 print(node, new PrintWriter(writer)); 95 } 96 97 private static void print(Node node, PrintWriter out) 98 { 99 if (node == null) 100 { 101 return; 102 } 103 104 boolean hasChildren = false; 105 int type = node.getNodeType(); 106 107 switch (type) 108 { 109 case Node.DOCUMENT_NODE : 110 { 111 out.println("<?xml version=\"1.0\"?>"); 112 113 NodeList children = node.getChildNodes(); 114 115 if (children != null) 116 { 117 int numChildren = children.getLength(); 118 119 for (int i = 0; i < numChildren; i++) 120 { 121 print(children.item(i), out); 122 } 123 } 124 break; 125 } 126 127 case Node.ELEMENT_NODE : 128 { 129 out.print('<' + node.getNodeName()); 130 131 NamedNodeMap attrs = node.getAttributes(); 132 int len = (attrs != null) ? attrs.getLength() : 0; 133 134 for (int i = 0; i < len; i++) 135 { 136 Attr attr = (Attr)attrs.item(i); 137 138 out.print(' ' + attr.getNodeName() +"=\"" + 139 normalize(attr.getValue()) + '\"'); 140 } 141 142 NodeList children = node.getChildNodes(); 143 144 if (children != null) 145 { 146 int numChildren = children.getLength(); 147 148 hasChildren = (numChildren > 0); 149 150 if (hasChildren) 151 { 152 out.print('>'); 153 } 154 155 for (int i = 0; i < numChildren; i++) 156 { 157 print(children.item(i), out); 158 } 159 } 160 else 161 { 162 hasChildren = false; 163 } 164 165 if (!hasChildren) 166 { 167 out.print("/>"); 168 } 169 break; 170 } 171 172 case Node.ENTITY_REFERENCE_NODE : 173 { 174 out.print('&'); 175 out.print(node.getNodeName()); 176 out.print(';'); 177 break; 178 } 179 180 case Node.CDATA_SECTION_NODE : 181 { 182 out.print("<![CDATA["); 183 out.print(node.getNodeValue()); 184 out.print("]]>"); 185 break; 186 } 187 188 case Node.TEXT_NODE : 189 { 190 out.print(normalize(node.getNodeValue())); 191 break; 192 } 193 194 case Node.COMMENT_NODE : 195 { 196 out.print("<!--"); 197 out.print(node.getNodeValue()); 198 out.print("-->"); 199 break; 200 } 201 202 case Node.PROCESSING_INSTRUCTION_NODE : 203 { 204 out.print("<?"); 205 out.print(node.getNodeName()); 206 207 String data = node.getNodeValue(); 208 209 if (data != null && data.length() > 0) 210 { 211 out.print(' '); 212 out.print(data); 213 } 214 215 out.println("?>"); 216 break; 217 } 218 } 219 220 if (type == Node.ELEMENT_NODE && hasChildren == true) 221 { 222 out.print("</"); 223 out.print(node.getNodeName()); 224 out.print('>'); 225 hasChildren = false; 226 } 227 } 228 229 private static String normalize(String s) 230 { 231 StringBuffer str = new StringBuffer (); 232 int len = (s != null) ? s.length() : 0; 233 234 for (int i = 0; i < len; i++) 235 { 236 char ch = s.charAt(i); 237 238 switch (ch) 239 { 240 case '<' : 241 { 242 str.append("<"); 243 break; 244 } 245 case '>' : 246 { 247 str.append(">"); 248 break; 249 } 250 case '&' : 251 { 252 str.append("&"); 253 break; 254 } 255 case '"' : 256 { 257 str.append("""); 258 break; 259 } 260 case '\n' : 261 { 262 if (i > 0) 263 { 264 char lastChar = str.charAt(str.length() - 1); 265 266 if (lastChar != '\r') 267 { 268 str.append(StringUtils.lineSeparator); 269 } 270 else 271 { 272 str.append('\n'); 273 } 274 } 275 else 276 { 277 str.append(StringUtils.lineSeparator); 278 } 279 break; 280 } 281 default : 282 { 283 str.append(ch); 284 } 285 } 286 } 287 288 return (str.toString()); 289 } 290 } 291 | Popular Tags |