X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fj32%2Futil%2FSimpleXmlGenerator.java;fp=src%2Fmain%2Fjava%2Fde%2Fj32%2Futil%2FSimpleXmlGenerator.java;h=440683bdbe39d4c54e79412f2644237e5b56a7c1;hb=4c31953ffe274db62393de67740de5df70b06d33;hp=0000000000000000000000000000000000000000;hpb=5ac05364dc652686046f01849b810da6ffef1192;p=jpim.git diff --git a/src/main/java/de/j32/util/SimpleXmlGenerator.java b/src/main/java/de/j32/util/SimpleXmlGenerator.java new file mode 100644 index 0000000..440683b --- /dev/null +++ b/src/main/java/de/j32/util/SimpleXmlGenerator.java @@ -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 openElements_ = new Stack(); + + 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(); + } +}