1 package liquibase.util.xml;
2
3 import org.w3c.dom.Document;
4
5 import javax.xml.transform.OutputKeys;
6 import javax.xml.transform.Transformer;
7 import javax.xml.transform.TransformerException;
8 import javax.xml.transform.TransformerFactory;
9 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) throws IOException {
18 try {
19 TransformerFactory factory = TransformerFactory.newInstance();
20 try {
21 factory.setAttribute("indent-number", 4);
22 } catch (Exception e) {
23 ;
24 }
25
26 Transformer transformer = factory.newTransformer();
27 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
28 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
29 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
30
31
32
33 transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(outputStream, "utf-8")));
34 } catch (TransformerException e) {
35 throw new IOException(e.getMessage());
36 }
37 }
38 }