1 package eu.ehri.project.exporters.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
14
15
16
17
18 public class DocumentWriter {
19 private final Document document;
20 private final int indent;
21
22 public DocumentWriter(Document document, int indent) {
23 this.document = document;
24 this.indent = indent;
25 }
26
27 public DocumentWriter(Document document) {
28 this(document, 4);
29 }
30
31 public void write(OutputStream outputStream) throws IOException, TransformerException {
32 TransformerFactory transformerFactory = TransformerFactory.newInstance();
33 Transformer transformer = transformerFactory.newTransformer();
34 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
35 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
36 String.valueOf(indent));
37
38 DOMSource source = new DOMSource(document);
39 StreamResult result = new StreamResult(outputStream);
40 transformer.transform(source, result);
41 }
42 }