KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > driver > OutputDocument


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: OutputDocument.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.driver;
25
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.OutputStreamWriter JavaDoc;
31 import java.io.PrintWriter JavaDoc;
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 JavaDoc;
41
42 /**
43  * Output or dump information about a compiled document.
44  */

45 public class OutputDocument {
46     /** Default formatter to use when no output options are specified */
47     private static DOMFormatter fDefaultFormatter = new DOMFormatter();
48
49     /** Write with toDocuemnt() */
50     public static void writeWithToDoc(XMLObject xmlObj,
51                                       File JavaDoc outFile) {
52         try {
53             PrintWriter JavaDoc out = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(outFile));
54             try {
55                 out.println(xmlObj.toDocument());
56             } finally {
57                 out.close();
58             }
59         } catch (Throwable JavaDoc err) {
60             throw new TestException(err);
61         }
62     }
63     
64     /*
65      * Write a document using a DOMFormatter
66      */

67     public static void write(XMLObject xmlObj,
68                              File JavaDoc 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 JavaDoc out = new FileOutputStream JavaDoc(outFile);
79             try {
80                 formatter.write(xmlObj, out);
81             } finally {
82                 out.close();
83             }
84         } catch (Throwable JavaDoc err) {
85             throw new TestException(err);
86         }
87
88     }
89
90     /** Do DOM dump */
91     private static void doDump(Document JavaDoc doc,
92                               File JavaDoc outFile) throws IOException JavaDoc {
93         TestFileOps.ensureFileDir(outFile);
94
95         // Force consistent encoding
96
PrintWriter JavaDoc out
97             = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(
98                                 new FileOutputStream JavaDoc(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     /**
115      * Dump DOM and other docuement information to a file.
116      * Additional information is provided if the document is a
117      * XMLObject.
118      */

119     public static void dump(Document JavaDoc doc,
120                             File JavaDoc outFile) {
121         try {
122             doDump(doc, outFile);
123         } catch (Throwable JavaDoc err) {
124             throw new TestException(err);
125         }
126     }
127 }
128
Popular Tags