Added constants from David Foerster to phpsyntax.xml file
[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.xalan.serialize.DOMSerializer;
14 import org.apache.xalan.serialize.SerializerToXML;
15 import org.w3c.dom.Document;
16 import org.xml.sax.SAXException;
17
18
19 /**
20  * <p>This class constructs a pretty-printing XML Serializer
21  * 
22  * @author BC
23  */
24 public class XMLHelper {
25     
26     private XMLHelper() {}
27     
28     public static DOMSerializer createDOMSerializer(Writer writer) 
29         throws IOException {
30 //      XMLSerializer serializer = new XMLSerializer(writer, createOutputFormat());
31 //      return serializer.asDOMSerializer();
32         SerializerToXML serializer = new SerializerToXML();
33         serializer.setWriter(writer);
34         return serializer.asDOMSerializer();
35     }
36
37     public static DOMSerializer createDOMSerializer(OutputStream stream) 
38         throws IOException {
39 //      XMLSerializer serializer = new XMLSerializer(stream, createOutputFormat());
40 //      return serializer.asDOMSerializer();
41         SerializerToXML serializer = new SerializerToXML();
42         serializer.setOutputStream(stream);
43         return serializer.asDOMSerializer();
44     }
45 //    private static OutputFormat createOutputFormat() {
46 //        OutputFormat format = new OutputFormat();
47 //        format.setIndenting(true);
48 //        format.setLineWidth(80);
49 //        return format;
50 //    }
51     
52     public static Document createEmptyDocument() throws ParserConfigurationException {
53         DocumentBuilder builder = createDocumentBuilder();
54         return builder.newDocument();
55     }
56     
57     public static Document createFromInputStream(InputStream stream) 
58         throws IOException, SAXException, ParserConfigurationException {
59         DocumentBuilder builder = createDocumentBuilder();
60         return builder.parse(stream);
61     }
62
63     private static DocumentBuilder createDocumentBuilder()
64         throws FactoryConfigurationError, ParserConfigurationException {
65         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
66         return factory.newDocumentBuilder();
67     }
68     
69     public static void write(Writer writer, Document document) 
70         throws IOException {
71         createDOMSerializer(writer).serialize(document);
72     }
73
74     public static void write(OutputStream stream, Document document) 
75         throws IOException {
76         createDOMSerializer(stream).serialize(document);
77     }
78 }