public class SimpleXmlGenerator
{
- TransformerHandler handler_;
- Stack<String> openElements_ = new Stack<String>();
-
- public SimpleXmlGenerator(OutputStream os, String encoding)
- throws UnsupportedEncodingException
- {
- SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
- // factory.setAttribute("indent-number", new Integer(2));
- try {
- handler_ = factory.newTransformerHandler();
- }
- catch (TransformerConfigurationException e) {
- throw new RuntimeException("XML/SAX transformer configuration error");
- }
- catch (TransformerFactoryConfigurationError e) {
- throw new RuntimeException("XML/SAX transformer factory configuration error");
- }
- Transformer tf = handler_.getTransformer();
- tf.setOutputProperty(OutputKeys.ENCODING,encoding);
- // if (doctype != null)
- // tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,doctype);
- // tf.setOutputProperty(OutputKeys.INDENT,indent ? "yes" : "no");
- handler_.setResult(new StreamResult(new OutputStreamWriter(os, encoding)));
- }
-
- public void startDocument()
- throws SAXException
- {
- handler_.startDocument();
- }
-
- public void start(String name)
- throws SAXException
- {
- handler_.startElement("","",name,null);
- openElements_.add(name);
- }
-
- public static class Attributes
- {
- AttributesImpl attributes_ = new AttributesImpl();
-
- public Attributes attribute(String name, String value)
- {
- attributes_.addAttribute("","",name,"CDATA",value);
- return this;
- }
-
- AttributesImpl getAttributes()
- {
- return attributes_;
- }
-
- Attributes() {}
- }
-
- public Attributes attribute(String name, String value)
- {
- Attributes a = new Attributes();
- return a.attribute(name, value);
- }
-
- public void start(String name, Attributes attrs)
- throws SAXException
- {
- handler_.startElement("","",name, attrs.getAttributes());
- openElements_.push(name);
- }
-
- public void end()
- throws SAXException
- {
- handler_.endElement("","",openElements_.pop());
- }
-
- public void empty(String name)
- throws SAXException
- {
- start(name);
- end();
- }
-
- public void empty(String name, Attributes attrs)
- throws SAXException
- {
- start(name, attrs);
- end();
- }
-
- public void text(String text)
- throws SAXException
- {
- handler_.characters(text.toCharArray(), 0, text.length());
- }
-
- public void nl()
- throws SAXException
- {
- text("\n");
- }
-
- public void endDocument()
- throws SAXException
- {
- handler_.endDocument();
- }
+ TransformerHandler handler_;
+ Stack<String> openElements_ = new Stack<String>();
+
+ public SimpleXmlGenerator(OutputStream os, String encoding)
+ throws UnsupportedEncodingException
+ {
+ SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory
+ .newInstance();
+ // factory.setAttribute("indent-number", new Integer(2));
+ try {
+ handler_ = factory.newTransformerHandler();
+ }
+ catch (TransformerConfigurationException e) {
+ throw new AssertionError("XML/SAX transformer configuration error");
+ }
+ catch (TransformerFactoryConfigurationError e) {
+ throw new AssertionError(
+ "XML/SAX transformer factory configuration error");
+ }
+ Transformer tf = handler_.getTransformer();
+ tf.setOutputProperty(OutputKeys.ENCODING, encoding);
+ // if (doctype != null)
+ // tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,doctype);
+ // tf.setOutputProperty(OutputKeys.INDENT,indent ? "yes" : "no");
+ handler_.setResult(new StreamResult(
+ new OutputStreamWriter(os, encoding)));
+ }
+
+ public void startDocument() throws SAXException
+ {
+ handler_.startDocument();
+ }
+
+ public void start(String name) throws SAXException
+ {
+ handler_.startElement("", "", name, null);
+ openElements_.add(name);
+ }
+
+ public static class Attributes
+ {
+ AttributesImpl attributes_ = new AttributesImpl();
+
+ public Attributes attribute(String name, String value)
+ {
+ attributes_.addAttribute("", "", name, "CDATA", value);
+ return this;
+ }
+
+ AttributesImpl getAttributes()
+ {
+ return attributes_;
+ }
+
+ Attributes()
+ {}
+ }
+
+ public Attributes attribute(String name, String value)
+ {
+ Attributes a = new Attributes();
+ return a.attribute(name, value);
+ }
+
+ public void start(String name, Attributes attrs) throws SAXException
+ {
+ handler_.startElement("", "", name, attrs.getAttributes());
+ openElements_.push(name);
+ }
+
+ public void end() throws SAXException
+ {
+ handler_.endElement("", "", openElements_.pop());
+ }
+
+ public void empty(String name) throws SAXException
+ {
+ start(name);
+ end();
+ }
+
+ public void empty(String name, Attributes attrs) throws SAXException
+ {
+ start(name, attrs);
+ end();
+ }
+
+ public void text(String text) throws SAXException
+ {
+ handler_.characters(text.toCharArray(), 0, text.length());
+ }
+
+ public void nl() throws SAXException
+ {
+ text("\n");
+ }
+
+ public void endDocument() throws SAXException
+ {
+ handler_.endDocument();
+ }
}