fbddb0a8ba3ab2bea9b855db047e2409da154a0f
[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.newInstance();
28                 // factory.setAttribute("indent-number", new Integer(2));
29                 try {
30                         handler_ = factory.newTransformerHandler();
31                 }
32                 catch (TransformerConfigurationException e) {
33                         throw new AssertionError("XML/SAX transformer configuration error");
34                 }
35                 catch (TransformerFactoryConfigurationError e) {
36                         throw new AssertionError("XML/SAX transformer factory configuration error");
37                 }
38                 Transformer tf = handler_.getTransformer();
39                 tf.setOutputProperty(OutputKeys.ENCODING,encoding);
40                 // if (doctype != null)
41                 //        tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,doctype);
42                 // tf.setOutputProperty(OutputKeys.INDENT,indent ? "yes" : "no");
43                 handler_.setResult(new StreamResult(new OutputStreamWriter(os, encoding)));
44         }
45         
46         public void startDocument()
47                 throws SAXException
48         {
49                 handler_.startDocument();
50         }
51
52         public void start(String name)
53                 throws SAXException
54         {
55                 handler_.startElement("","",name,null);
56                 openElements_.add(name);
57         }
58         
59         public static class Attributes
60         {
61                 AttributesImpl attributes_ = new AttributesImpl();
62                 
63                 public Attributes attribute(String name, String value)
64                 {
65                         attributes_.addAttribute("","",name,"CDATA",value);
66                         return this;
67                 }
68                 
69                 AttributesImpl getAttributes()
70                 {
71                         return attributes_;
72                 }
73                 
74                 Attributes() {}
75         }
76
77         public Attributes attribute(String name, String value)
78         {
79                 Attributes a = new Attributes();
80                 return a.attribute(name, value);
81         }
82         
83         public void start(String name, Attributes attrs)
84                 throws SAXException
85         {
86                 handler_.startElement("","",name, attrs.getAttributes());
87                 openElements_.push(name);
88         }
89         
90         public void end()
91                 throws SAXException
92         {
93                 handler_.endElement("","",openElements_.pop());
94         }
95         
96         public void empty(String name)
97                 throws SAXException
98         {
99                 start(name);
100                 end();
101         }
102         
103         public void empty(String name, Attributes attrs)
104                 throws SAXException
105         {
106                 start(name, attrs);
107                 end();
108         }
109         
110         public void text(String text)
111                 throws SAXException
112         {
113                 handler_.characters(text.toCharArray(), 0, text.length());
114         }
115         
116         public void nl()
117                 throws SAXException
118         {
119                 text("\n");
120         }
121         
122         public void endDocument()
123                 throws SAXException
124         {
125                 handler_.endDocument();
126         }
127 }