View Javadoc
1 /*-- 2 3 Copyright (C) 2001 Brett McLaughlin. 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 10 1. Redistributions of source code must retain the above copyright 11 notice, this list of conditions, and the following disclaimer. 12 13 2. Redistributions in binary form must reproduce the above copyright 14 notice, this list of conditions, and the disclaimer that follows 15 these conditions in the documentation and/or other materials 16 provided with the distribution. 17 18 3. The name "Java and XML" must not be used to endorse or promote products 19 derived from this software without prior written permission. For 20 written permission, please contact brett@newInstance.com. 21 22 In addition, we request (but do not require) that you include in the 23 end-user documentation provided with the redistribution and/or in the 24 software itself an acknowledgement equivalent to the following: 25 "This product includes software developed for the 26 'Java and XML' book, by Brett McLaughlin (O'Reilly & Associates)." 27 28 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 29 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT 32 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 35 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 36 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 37 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 38 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 SUCH DAMAGE. 40 41 */ 42 package javaxml2; 43 44 import java.io.File; 45 import java.io.FileWriter; 46 import java.io.IOException; 47 import java.io.OutputStream; 48 import java.io.OutputStreamWriter; 49 import java.io.Writer; 50 import org.w3c.dom.Document; 51 import org.w3c.dom.DocumentType; 52 import org.w3c.dom.NamedNodeMap; 53 import org.w3c.dom.Node; 54 import org.w3c.dom.NodeList; 55 56 /*** 57 * <b><code>DOMSerializer</code></b> will take a DOM 58 * tree and serialize that tree. 59 */ 60 public class DOMSerializer { 61 62 /*** Indentation to use */ 63 private String indent; 64 65 /*** Line separator to use */ 66 private String lineSeparator; 67 68 /*** 69 * <p> This initializes the needed settings. </p> 70 */ 71 public DOMSerializer() { 72 indent = ""; 73 lineSeparator = "\n"; 74 } 75 76 /*** 77 * <p> This sets the indentation to use. </p> 78 * 79 * @param indent the indentation <code>String</code> to use. 80 */ 81 public void setIndent(String indent) { 82 this.indent = indent; 83 } 84 85 /*** 86 * <p> This sets the line separator to use. </p> 87 * 88 * @param lineSeparator line separator to use. 89 */ 90 public void setLineSeparator(String lineSeparator) { 91 this.lineSeparator = lineSeparator; 92 } 93 94 /*** 95 * <p> This serializes a DOM tree to the supplied 96 * <code>OutputStream</code>.</p> 97 * 98 * @param doc DOM tree to serialize. 99 * @param out <code>OutputStream</code> to write to. 100 */ 101 public void serialize(Document doc, OutputStream out) 102 throws IOException { 103 104 Writer writer = new OutputStreamWriter(out); 105 serialize(doc, writer); 106 } 107 108 /*** 109 * <p> This serializes a DOM tree to the supplied 110 * <code>OutputStream</code>.</p> 111 * 112 * @param doc DOM tree to serialize. 113 * @param file <code>File</code> to write to. 114 */ 115 public void serialize(Document doc, File file) 116 throws IOException { 117 118 Writer writer = new FileWriter(file); 119 serialize(doc, writer); 120 } 121 122 /*** 123 * <p> This serializes a DOM tree to the supplied 124 * <code>OutputStream</code>.</p> 125 * 126 * @param doc DOM tree to serialize. 127 * @param writer <code>Writer</code> to write to. 128 */ 129 public void serialize(Document doc, Writer writer) 130 throws IOException { 131 132 // Start serialization recursion with no indenting 133 serializeNode(doc, writer, ""); 134 writer.flush(); 135 } 136 137 /*** 138 * <p> This will serialize a DOM <code>Node</code> to 139 * the supplied <code>Writer</code>. </p> 140 * 141 * @param node DOM <code>Node</code> to serialize. 142 * @param writer <code>Writer</code> to write to. 143 * @param indentLevel current indentation. 144 */ 145 public void serializeNode(Node node, Writer writer, 146 String indentLevel) 147 throws IOException { 148 149 // Determine action based on node type 150 switch (node.getNodeType()) { 151 case Node.DOCUMENT_NODE: 152 writer.write("<?xml version=\"1.0\"?>"); 153 writer.write(lineSeparator); 154 155 // recurse on each child 156 NodeList nodes = node.getChildNodes(); 157 if (nodes != null) { 158 for (int i=0; i<nodes.getLength(); i++) { 159 serializeNode(nodes.item(i), writer, ""); 160 } 161 } 162 /* 163 Document doc = (Document)node; 164 serializeNode(doc.getDocumentElement(), writer, ""); 165 */ 166 break; 167 168 case Node.ELEMENT_NODE: 169 String name = node.getNodeName(); 170 writer.write(indentLevel + "<" + name); 171 NamedNodeMap attributes = node.getAttributes(); 172 for (int i=0; i<attributes.getLength(); i++) { 173 Node current = attributes.item(i); 174 writer.write(" " + current.getNodeName() + 175 "=\"" + current.getNodeValue() + 176 "\""); 177 } 178 writer.write(">"); 179 180 // recurse on each child 181 NodeList children = node.getChildNodes(); 182 if (children != null) { 183 if ((children.item(0) != null) && 184 (children.item(0).getNodeType() == 185 Node.ELEMENT_NODE)) { 186 187 writer.write(lineSeparator); 188 } 189 for (int i=0; i<children.getLength(); i++) { 190 serializeNode(children.item(i), writer, 191 indentLevel + indent); 192 } 193 if ((children.item(0) != null) && 194 (children.item(children.getLength()-1) 195 .getNodeType() == 196 Node.ELEMENT_NODE)) { 197 198 writer.write(indentLevel); 199 } 200 } 201 202 writer.write("</" + name + ">"); 203 writer.write(lineSeparator); 204 break; 205 206 case Node.TEXT_NODE: 207 writer.write(node.getNodeValue()); 208 break; 209 210 case Node.CDATA_SECTION_NODE: 211 writer.write("<![CDATA[" + 212 node.getNodeValue() + "]]>"); 213 break; 214 215 case Node.COMMENT_NODE: 216 writer.write(indentLevel + "<!-- " + 217 node.getNodeValue() + " -->"); 218 writer.write(lineSeparator); 219 break; 220 221 case Node.PROCESSING_INSTRUCTION_NODE: 222 writer.write("<?" + node.getNodeName() + 223 " " + node.getNodeValue() + 224 "?>"); 225 writer.write(lineSeparator); 226 break; 227 228 case Node.ENTITY_REFERENCE_NODE: 229 writer.write("&" + node.getNodeName() + ";"); 230 break; 231 232 case Node.DOCUMENT_TYPE_NODE: 233 DocumentType docType = (DocumentType)node; 234 writer.write("<!DOCTYPE " + docType.getName()); 235 if (docType.getPublicId() != null) { 236 System.out.print(" PUBLIC \"" + 237 docType.getPublicId() + "\" "); 238 } else { 239 writer.write(" SYSTEM "); 240 } 241 writer.write("\"" + docType.getSystemId() + "\">"); 242 writer.write(lineSeparator); 243 break; 244 } 245 } 246 247 /*** 248 * <p>Added by Kate Rhodes (masukomi at masukomi dot org)</p> 249 * <p> This will serialize a DOM <code>Node</code> to 250 * the supplied <code>Writer</code>. </p> 251 * 252 * @param node DOM <code>Node</code> to serialize. 253 * @param writer <code>Writer</code> to write to. 254 * @param indentLevel current indentation. 255 */ 256 public String serializeNodeToString(Node node, 257 String indentLevel) 258 { 259 260 StringBuffer buffer = new StringBuffer(); 261 262 // Determine action based on node type 263 switch (node.getNodeType()) { 264 case Node.DOCUMENT_NODE: 265 buffer.append("<?xml version=\"1.0\"?>"); 266 buffer.append(lineSeparator); 267 268 // recurse on each child 269 NodeList nodes = node.getChildNodes(); 270 if (nodes != null) { 271 for (int i=0; i<nodes.getLength(); i++) { 272 buffer.append(serializeNodeToString(nodes.item(i), "")); 273 } 274 } 275 /* 276 Document doc = (Document)node; 277 serializeNode(doc.getDocumentElement(), writer, ""); 278 */ 279 break; 280 281 case Node.ELEMENT_NODE: 282 String name = node.getNodeName(); 283 buffer.append(indentLevel + "<" + name); 284 NamedNodeMap attributes = node.getAttributes(); 285 for (int i=0; i<attributes.getLength(); i++) { 286 Node current = attributes.item(i); 287 buffer.append(" " + current.getNodeName() + 288 "=\"" + current.getNodeValue() + 289 "\""); 290 } 291 buffer.append(">"); 292 293 // recurse on each child 294 NodeList children = node.getChildNodes(); 295 if (children != null) { 296 if ((children.item(0) != null) && 297 (children.item(0).getNodeType() == 298 Node.ELEMENT_NODE)) { 299 300 buffer.append(lineSeparator); 301 } 302 for (int i=0; i<children.getLength(); i++) { 303 304 buffer.append(serializeNodeToString(children.item(i), 305 indentLevel + " ")); // was + indent 306 } 307 if ((children.item(0) != null) && 308 (children.item(children.getLength()-1) 309 .getNodeType() == 310 Node.ELEMENT_NODE)) { 311 312 buffer.append(indentLevel); 313 } 314 } 315 316 buffer.append(indentLevel+"</" + name + ">"); 317 buffer.append(lineSeparator); 318 break; 319 320 case Node.TEXT_NODE: 321 buffer.append(node.getNodeValue()); 322 break; 323 324 case Node.CDATA_SECTION_NODE: 325 buffer.append("<![CDATA[" + 326 node.getNodeValue() + "]]>"); 327 break; 328 329 case Node.COMMENT_NODE: 330 buffer.append(indentLevel + "<!-- " + 331 node.getNodeValue() + " -->"); 332 buffer.append(lineSeparator); 333 break; 334 335 case Node.PROCESSING_INSTRUCTION_NODE: 336 buffer.append("<?" + node.getNodeName() + 337 " " + node.getNodeValue() + 338 "?>"); 339 buffer.append(lineSeparator); 340 break; 341 342 case Node.ENTITY_REFERENCE_NODE: 343 buffer.append("&" + node.getNodeName() + ";"); 344 break; 345 346 case Node.DOCUMENT_TYPE_NODE: 347 DocumentType docType = (DocumentType)node; 348 buffer.append("<!DOCTYPE " + docType.getName()); 349 if (docType.getPublicId() != null) { 350 System.out.print(" PUBLIC \"" + 351 docType.getPublicId() + "\" "); 352 } else { 353 buffer.append(" SYSTEM "); 354 } 355 buffer.append("\"" + docType.getSystemId() + "\">"); 356 buffer.append(lineSeparator); 357 break; 358 } 359 return buffer.toString(); 360 } 361 }

This page was automatically generated by Maven