added button to start tests.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / util / xml / XMLHelper.java
1 package com.quantum.util.xml;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.io.Writer;
7
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.FactoryConfigurationError;
11 import javax.xml.parsers.ParserConfigurationException;
12
13 import org.apache.xml.serialize.DOMSerializer;
14 import org.apache.xml.serialize.OutputFormat;
15 import org.apache.xml.serialize.XMLSerializer;
16 import org.w3c.dom.Document;
17 import org.xml.sax.SAXException;
18
19
20 /**
21  * <p>This class constructs a pretty-printing XML Serializer
22  * 
23  * @author BC
24  */
25 public class XMLHelper {
26     
27     private XMLHelper() {}
28     
29     public static DOMSerializer createDOMSerializer(Writer writer) 
30         throws IOException {
31         XMLSerializer serializer = new XMLSerializer(writer, createOutputFormat());
32         return serializer.asDOMSerializer();
33     }
34
35     public static DOMSerializer createDOMSerializer(OutputStream stream) 
36         throws IOException {
37         XMLSerializer serializer = new XMLSerializer(stream, createOutputFormat());
38         return serializer.asDOMSerializer();
39     }
40     private static OutputFormat createOutputFormat() {
41         OutputFormat format = new OutputFormat();
42         format.setIndenting(true);
43         format.setLineWidth(80);
44         return format;
45     }
46     
47     public static Document createEmptyDocument() throws ParserConfigurationException {
48         DocumentBuilder builder = createDocumentBuilder();
49         return builder.newDocument();
50     }
51     
52     public static Document createFromInputStream(InputStream stream) 
53         throws IOException, SAXException, ParserConfigurationException {
54         DocumentBuilder builder = createDocumentBuilder();
55         return builder.parse(stream);
56     }
57
58     private static DocumentBuilder createDocumentBuilder()
59         throws FactoryConfigurationError, ParserConfigurationException {
60         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
61         return factory.newDocumentBuilder();
62     }
63     
64     public static void write(Writer writer, Document document) 
65         throws IOException {
66         createDOMSerializer(writer).serialize(document);
67     }
68
69     public static void write(OutputStream stream, Document document) 
70         throws IOException {
71         createDOMSerializer(stream).serialize(document);
72     }
73 }