Coverage Report - liquibase.util.xml.DefaultXmlWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultXmlWriter
0%
0/14
N/A
4
 
 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  
                 ; // guess we can't set it, that's ok
 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  
             // need to nest outputStreamWriter to get around JDK 5 bug. See
 32  
             // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446
 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  
 }