1 24 25 package com.bull.eclipse.jonas.utils.xml; 26 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 30 import org.apache.xml.serialize.DOMSerializer; 31 import org.apache.xml.serialize.LineSeparator; 32 import org.apache.xml.serialize.Method; 33 import org.apache.xml.serialize.OutputFormat; 34 import org.apache.xml.serialize.Serializer; 35 import org.apache.xml.serialize.SerializerFactory; 36 import org.w3c.dom.Document ; 37 38 39 44 public class XMLSerializer { 45 48 private static XMLSerializer instance = null; 49 50 53 private Document doc; 54 55 58 private OutputFormat format; 59 60 63 private SerializerFactory factory; 64 65 68 private XMLSerializer() { 69 format = new OutputFormat(); 71 format.setLineSeparator(LineSeparator.Unix); 72 format.setLineWidth(80); 73 format.setIndent(4); 74 75 factory = SerializerFactory.getSerializerFactory(Method.XML); 77 78 } 79 80 88 public static XMLSerializer getInstance(Document d) { 89 if(instance == null) { 90 instance = new XMLSerializer(); 91 } 92 93 instance.setDocument(d); 94 95 return instance; 96 } 97 98 103 private void setDocument(Document d) { 104 doc = d; 105 } 106 107 114 public void serialize(OutputStream os) throws IOException { 115 Serializer genericSerializer = factory.makeSerializer(os, format); 116 DOMSerializer domSerializer = genericSerializer.asDOMSerializer(); 117 118 domSerializer.serialize(doc); 119 } 120 } 121 | Popular Tags |