package com.quantum.util.xml; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Writer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import org.apache.xalan.serialize.DOMSerializer; import org.apache.xalan.serialize.SerializerToXML; import org.w3c.dom.Document; import org.xml.sax.SAXException; /** *
This class constructs a pretty-printing XML Serializer * * @author BC */ public class XMLHelper { private XMLHelper() {} public static DOMSerializer createDOMSerializer(Writer writer) throws IOException { // XMLSerializer serializer = new XMLSerializer(writer, createOutputFormat()); // return serializer.asDOMSerializer(); SerializerToXML serializer = new SerializerToXML(); serializer.setWriter(writer); return serializer.asDOMSerializer(); } public static DOMSerializer createDOMSerializer(OutputStream stream) throws IOException { // XMLSerializer serializer = new XMLSerializer(stream, createOutputFormat()); // return serializer.asDOMSerializer(); SerializerToXML serializer = new SerializerToXML(); serializer.setOutputStream(stream); return serializer.asDOMSerializer(); } // private static OutputFormat createOutputFormat() { // OutputFormat format = new OutputFormat(); // format.setIndenting(true); // format.setLineWidth(80); // return format; // } public static Document createEmptyDocument() throws ParserConfigurationException { DocumentBuilder builder = createDocumentBuilder(); return builder.newDocument(); } public static Document createFromInputStream(InputStream stream) throws IOException, SAXException, ParserConfigurationException { DocumentBuilder builder = createDocumentBuilder(); return builder.parse(stream); } private static DocumentBuilder createDocumentBuilder() throws FactoryConfigurationError, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); return factory.newDocumentBuilder(); } public static void write(Writer writer, Document document) throws IOException { createDOMSerializer(writer).serialize(document); } public static void write(OutputStream stream, Document document) throws IOException { createDOMSerializer(stream).serialize(document); } }