01: package liquibase.xml;
02:
03: import org.w3c.dom.Document;
04:
05: import javax.xml.transform.OutputKeys;
06: import javax.xml.transform.Transformer;
07: import javax.xml.transform.TransformerException;
08: import javax.xml.transform.TransformerFactory;
09: import javax.xml.transform.dom.DOMSource;
10: import javax.xml.transform.stream.StreamResult;
11: import java.io.IOException;
12: import java.io.OutputStream;
13: import java.io.OutputStreamWriter;
14:
15: public class DefaultXmlWriter implements XmlWriter {
16:
17: public void write(Document doc, OutputStream outputStream)
18: throws IOException {
19: // OutputFormat format = new OutputFormat(doc);
20: // format.setIndenting(true);
21: // format.setEncoding("UTF-8");
22: // XMLSerializer serializer = new XMLSerializer(outputStream, format);
23: // serializer.asDOMSerializer();
24: // serializer.serialize(doc);
25:
26: try {
27: TransformerFactory factory = TransformerFactory
28: .newInstance();
29: try {
30: factory.setAttribute("indent-number", 4);
31: } catch (Exception e) {
32: ; //guess we can't set it, that's ok
33: }
34:
35: Transformer transformer = factory.newTransformer();
36: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
37: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
38: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
39:
40: //need to nest outputStreamWriter to get around JDK 5 bug. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446
41: transformer.transform(new DOMSource(doc), new StreamResult(
42: new OutputStreamWriter(outputStream, "utf-8")));
43: } catch (TransformerException e) {
44: throw new IOException(e.getMessage());
45: }
46: }
47: }
|