1 23 24 package org.enhydra.xml.xmlc.driver; 25 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.OutputStream ; 30 import java.io.OutputStreamWriter ; 31 import java.io.PrintWriter ; 32 33 import org.enhydra.xml.dom.DOMInfo; 34 import org.enhydra.xml.dom.DOMStats; 35 import org.enhydra.xml.driver.TestException; 36 import org.enhydra.xml.driver.TestFileOps; 37 import org.enhydra.xml.io.DOMFormatter; 38 import org.enhydra.xml.io.OutputOptions; 39 import org.enhydra.xml.xmlc.XMLObject; 40 import org.w3c.dom.Document ; 41 42 45 public class OutputDocument { 46 47 private static DOMFormatter fDefaultFormatter = new DOMFormatter(); 48 49 50 public static void writeWithToDoc(XMLObject xmlObj, 51 File outFile) { 52 try { 53 PrintWriter out = new PrintWriter (new FileOutputStream (outFile)); 54 try { 55 out.println(xmlObj.toDocument()); 56 } finally { 57 out.close(); 58 } 59 } catch (Throwable err) { 60 throw new TestException(err); 61 } 62 } 63 64 67 public static void write(XMLObject xmlObj, 68 File outFile, 69 OutputOptions options) { 70 DOMFormatter formatter; 71 if (options == null) { 72 formatter = fDefaultFormatter; 73 } else { 74 formatter = new DOMFormatter(options); 75 } 76 77 try { 78 OutputStream out = new FileOutputStream (outFile); 79 try { 80 formatter.write(xmlObj, out); 81 } finally { 82 out.close(); 83 } 84 } catch (Throwable err) { 85 throw new TestException(err); 86 } 87 88 } 89 90 91 private static void doDump(Document doc, 92 File outFile) throws IOException { 93 TestFileOps.ensureFileDir(outFile); 94 95 PrintWriter out 97 = new PrintWriter (new OutputStreamWriter ( 98 new FileOutputStream (outFile), "UTF-8")); 99 try { 100 if (doc instanceof XMLObject) { 101 XMLObject xmlObj = (XMLObject)doc; 102 out.println("Document MIME type: " + xmlObj.getMIMEType()); 103 out.println("Document encoding: " + xmlObj.getEncoding()); 104 } 105 DOMInfo.printTree("DOM hierarchy", doc, 106 DOMInfo.PRINT_ATTR_DETAILS, out); 107 DOMStats.printStats("DOM statistics", doc, 108 DOMStats.SIMPLE_CLASS_NAMES, out); 109 } finally { 110 out.close(); 111 } 112 } 113 114 119 public static void dump(Document doc, 120 File outFile) { 121 try { 122 doDump(doc, outFile); 123 } catch (Throwable err) { 124 throw new TestException(err); 125 } 126 } 127 } 128 | Popular Tags |