Rename pimstuff -> jpim and move to Maven
[jpim.git] / src / main / java / de / j32 / util / SimpleXmlGenerator.java
diff --git a/src/main/java/de/j32/util/SimpleXmlGenerator.java b/src/main/java/de/j32/util/SimpleXmlGenerator.java
new file mode 100644 (file)
index 0000000..440683b
--- /dev/null
@@ -0,0 +1,122 @@
+package de.j32.util;
+
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.Stack;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamResult;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+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 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();
+    }
+}