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 | 0 | public class DefaultXmlWriter implements XmlWriter { |
16 | |
|
17 | |
public void write(Document doc, OutputStream outputStream) throws IOException { |
18 | |
try { |
19 | 0 | TransformerFactory factory = TransformerFactory.newInstance(); |
20 | |
try { |
21 | 0 | factory.setAttribute("indent-number", 4); |
22 | 0 | } catch (Exception e) { |
23 | |
; |
24 | 0 | } |
25 | |
|
26 | 0 | Transformer transformer = factory.newTransformer(); |
27 | 0 | transformer.setOutputProperty(OutputKeys.METHOD, "xml"); |
28 | 0 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
29 | 0 | transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); |
30 | |
|
31 | |
|
32 | |
|
33 | 0 | transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(outputStream, "utf-8"))); |
34 | 0 | } catch (TransformerException e) { |
35 | 0 | throw new IOException(e.getMessage()); |
36 | 0 | } |
37 | 0 | } |
38 | |
} |