Code formating and whitespace cleanup
[jpim.git] / src / de / j32 / util / SimpleXmlGenerator.java
1 package de.j32.util;
2
3 import java.io.OutputStream;
4 import java.io.OutputStreamWriter;
5 import java.io.UnsupportedEncodingException;
6 import java.util.Stack;
7
8 import javax.xml.transform.OutputKeys;
9 import javax.xml.transform.Transformer;
10 import javax.xml.transform.TransformerConfigurationException;
11 import javax.xml.transform.TransformerFactoryConfigurationError;
12 import javax.xml.transform.sax.SAXTransformerFactory;
13 import javax.xml.transform.sax.TransformerHandler;
14 import javax.xml.transform.stream.StreamResult;
15
16 import org.xml.sax.SAXException;
17 import org.xml.sax.helpers.AttributesImpl;
18
19 public class SimpleXmlGenerator
20 {
21     TransformerHandler handler_;
22     Stack<String> openElements_ = new Stack<String>();
23
24     public SimpleXmlGenerator(OutputStream os, String encoding)
25             throws UnsupportedEncodingException
26     {
27         SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory
28                 .newInstance();
29         // factory.setAttribute("indent-number", new Integer(2));
30         try {
31             handler_ = factory.newTransformerHandler();
32         }
33         catch (TransformerConfigurationException e) {
34             throw new AssertionError("XML/SAX transformer configuration error");
35         }
36         catch (TransformerFactoryConfigurationError e) {
37             throw new AssertionError(
38                     "XML/SAX transformer factory configuration error");
39         }
40         Transformer tf = handler_.getTransformer();
41         tf.setOutputProperty(OutputKeys.ENCODING, encoding);
42         // if (doctype != null)
43         // tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,doctype);
44         // tf.setOutputProperty(OutputKeys.INDENT,indent ? "yes" : "no");
45         handler_.setResult(new StreamResult(
46                 new OutputStreamWriter(os, encoding)));
47     }
48
49     public void startDocument() throws SAXException
50     {
51         handler_.startDocument();
52     }
53
54     public void start(String name) throws SAXException
55     {
56         handler_.startElement("", "", name, null);
57         openElements_.add(name);
58     }
59
60     public static class Attributes
61     {
62         AttributesImpl attributes_ = new AttributesImpl();
63
64         public Attributes attribute(String name, String value)
65         {
66             attributes_.addAttribute("", "", name, "CDATA", value);
67             return this;
68         }
69
70         AttributesImpl getAttributes()
71         {
72             return attributes_;
73         }
74
75         Attributes()
76         {}
77     }
78
79     public Attributes attribute(String name, String value)
80     {
81         Attributes a = new Attributes();
82         return a.attribute(name, value);
83     }
84
85     public void start(String name, Attributes attrs) throws SAXException
86     {
87         handler_.startElement("", "", name, attrs.getAttributes());
88         openElements_.push(name);
89     }
90
91     public void end() throws SAXException
92     {
93         handler_.endElement("", "", openElements_.pop());
94     }
95
96     public void empty(String name) throws SAXException
97     {
98         start(name);
99         end();
100     }
101
102     public void empty(String name, Attributes attrs) throws SAXException
103     {
104         start(name, attrs);
105         end();
106     }
107
108     public void text(String text) throws SAXException
109     {
110         handler_.characters(text.toCharArray(), 0, text.length());
111     }
112
113     public void nl() throws SAXException
114     {
115         text("\n");
116     }
117
118     public void endDocument() throws SAXException
119     {
120         handler_.endDocument();
121     }
122 }