1 29 30 package nextapp.echo2.webrender.output; 31 32 import java.io.IOException ; 33 import java.io.PrintWriter ; 34 import java.util.Properties ; 35 36 import javax.xml.transform.Transformer ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.TransformerFactory ; 39 import javax.xml.transform.dom.DOMSource ; 40 import javax.xml.transform.stream.StreamResult ; 41 42 import nextapp.echo2.webrender.util.DomUtil; 43 44 import org.w3c.dom.DOMImplementation ; 45 import org.w3c.dom.Document ; 46 import org.w3c.dom.DocumentType ; 47 48 52 public class XmlDocument { 53 54 private Document document; 55 private Properties outputProperties; 56 57 66 public XmlDocument(String qualifiedName, String publicId, String systemId, String namespaceUri) { 67 super(); 68 DOMImplementation dom = DomUtil.getDocumentBuilder().getDOMImplementation(); 69 DocumentType docType = dom.createDocumentType(qualifiedName, publicId, systemId); 70 document = dom.createDocument(namespaceUri, qualifiedName, docType); 71 if (namespaceUri != null) { 72 document.getDocumentElement().setAttribute("xmlns", namespaceUri); 73 } 74 } 75 76 81 public Document getDocument() { 82 return document; 83 } 84 85 90 public void render(PrintWriter pw) 91 throws IOException { 92 try { 93 TransformerFactory tFactory = DomUtil.getTransformerFactory(); 94 Transformer transformer = tFactory.newTransformer(); 95 if (outputProperties != null) { 96 transformer.setOutputProperties(outputProperties); 97 } 98 DOMSource source = new DOMSource (document); 99 StreamResult result = new StreamResult (pw); 100 transformer.transform(source, result); 101 } catch (TransformerException ex) { 102 throw new IOException ("Unable to write document to OutputStream: " + ex.toString()); 103 } 104 } 105 106 112 public void setOutputProperties(Properties newValue) { 113 outputProperties = newValue; 114 } 115 } 116 | Popular Tags |