1 25 package org.objectweb.jonas_lib.xml; 26 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.io.OutputStreamWriter ; 30 import java.io.Writer ; 31 32 import org.w3c.dom.Document ; 33 34 import org.apache.xml.serialize.DOMSerializer; 35 import org.apache.xml.serialize.Method; 36 import org.apache.xml.serialize.OutputFormat; 37 import org.apache.xml.serialize.Serializer; 38 import org.apache.xml.serialize.SerializerFactory; 39 40 41 47 public class XMLSerializer { 48 49 52 private static final int DEF_INDENT_SIZE = 4; 53 54 57 private static final int DEF_LINE_WIDTH = 80; 58 59 62 private static final String DEF_LINE_SEP = "\n"; 63 64 67 private Document doc; 68 69 72 private OutputFormat format; 73 74 77 private SerializerFactory factory; 78 79 83 public XMLSerializer(Document doc) { 84 this.doc = doc; 85 format = new OutputFormat(); 87 setIndent(DEF_INDENT_SIZE); 88 setLineSeparator(DEF_LINE_SEP); 89 setLineWidth(DEF_LINE_WIDTH); 90 91 factory = SerializerFactory.getSerializerFactory(Method.XML); 93 } 94 95 99 public void setLineSeparator(String sep) { 100 format.setLineSeparator(sep); 101 } 102 103 107 public void setLineWidth(int width) { 108 format.setLineWidth(width); 109 } 110 111 115 public void setIndent(int indent) { 116 format.setIndent(indent); 117 } 118 119 126 public void serialize(OutputStream os) throws IOException { 127 serialize(new OutputStreamWriter (os)); 128 } 129 130 137 public void serialize(Writer writer) throws IOException { 138 Serializer genericSerializer = factory.makeSerializer(writer, format); 139 DOMSerializer domSerializer = genericSerializer.asDOMSerializer(); 140 141 domSerializer.serialize(doc); 142 } 143 } 144 | Popular Tags |