KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > io > DOMFormatter


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: DOMFormatter.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.io;
25
26 import java.io.BufferedOutputStream JavaDoc;
27 import java.io.BufferedWriter JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.OutputStreamWriter JavaDoc;
34 import java.io.StringWriter JavaDoc;
35 import java.io.Writer JavaDoc;
36
37 import org.enhydra.xml.dom.DOMAccess;
38 import org.enhydra.xml.dom.DOMOps;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42 import org.w3c.dom.html.HTMLDocument;
43
44 //FIXME: could cache type-specific formatters...
45

46 //FIXME: Do we really need to create a DOMformatter per output options?
47
//why not make this a cache of fromatters.
48

49 //FIXME: Need ability to output XHTML DOM as HTML and HTML DOM as XHTML
50

51 /**
52  * Class for formatting DOMs into a documents. The class handles XML and
53  * HTML pages, looking up the approriate formatting routines based on the
54  * document type. It has an associated OutputOptions object that controls the
55  * formatting. Instances are reusable and multithreaded.
56  */

57 public class DOMFormatter {
58     /**
59      * XHTML Namespace
60      */

61     private static final String JavaDoc XHTML_NAMESPACE_URI
62         = "http://www.w3.org/1999/xhtml";
63
64     /**
65      * Output options.
66      */

67     private OutputOptions fOptions;
68
69     /**
70      * Construct a new XML formatter with the specified
71      * output options.
72      * @param outputOptions The OutputOptions to use for formatters
73      * obtained from this object.
74      */

75     public DOMFormatter(OutputOptions outputOptions) {
76         fOptions = outputOptions;
77         if (fOptions == null) {
78             fOptions = new OutputOptions();
79         }
80     }
81     
82     /**
83      * Construct a new XML formatter with default output options.
84      */

85     public DOMFormatter() {
86         this(null);
87     }
88
89     /**
90      * Get the output options.
91      */

92     public OutputOptions getOutputOptions() {
93         return fOptions;
94     }
95     
96     /**
97      * Set the output options.
98      */

99     public void setOutputOptions(OutputOptions outputOptions) {
100         fOptions = outputOptions;
101     }
102
103     /**
104      * Get the default OutputOptions for a document. The encoding will
105      * not be set, which signals to use the default encoding.
106      */

107     public static OutputOptions getDefaultOutputOptions(Document JavaDoc doc) {
108         if ((doc instanceof HTMLDocument) && !isXHTMLDocument(doc)) {
109             return HTMLFormatter.getDefaultOutputOptions();
110         } else {
111             return XMLFormatter.getDefaultOutputOptions();
112         }
113     }
114
115     /**
116      * Determine if a document implementing HTMLDocument is an
117      * XHTML document.
118      */

119     private static boolean isXHTMLDocument(Document JavaDoc document) {
120         // Must get element with out expanding if a LazyDOM.
121
Element JavaDoc docElement = DOMAccess.accessDocumentElement(document);
122
123         // Coded this way since URI can be null.
124
return (docElement != null)
125             && XHTML_NAMESPACE_URI.equals(docElement.getNamespaceURI());
126     }
127
128     /**
129      * Determine the type of the formatter to use.
130      */

131     private static OutputOptions.Format determineFormat(OutputOptions outputOptions,
132                                                         Node JavaDoc node) {
133         OutputOptions.Format format = outputOptions.getFormat();
134         if (format == OutputOptions.FORMAT_AUTO) {
135             // XHTML DOM is optional and it implements HTMLDocument, so
136
// we need to check the namespace
137
Document JavaDoc doc = DOMOps.getDocument(node);
138             if ((doc instanceof HTMLDocument) && !isXHTMLDocument(doc)) {
139                 format = OutputOptions.FORMAT_HTML;
140             } else {
141                 format = OutputOptions.FORMAT_XML;
142             }
143         }
144         return format;
145     }
146     
147     /*
148      * Factory for Formatter objects base on the document type.
149      * This is currently used only my XMLC, this interface may change in the
150      * future.
151      */

152     public static Formatter getFormatter(Node JavaDoc node,
153                                          OutputOptions outputOptions,
154                                          boolean forPreFormatting) {
155         OutputOptions.Format format = determineFormat(outputOptions, node);
156         if (format == OutputOptions.FORMAT_HTML) {
157             return new HTMLFormatter(node, outputOptions, forPreFormatting);
158         } else {
159             return new XMLFormatter(node, outputOptions, forPreFormatting);
160         }
161     }
162
163     /**
164      * Format a document or any node and its children into a string.
165      */

166     public String JavaDoc toString(Node JavaDoc node) {
167         Formatter formatter = getFormatter(node, fOptions, false);
168         StringWriter JavaDoc writer = new StringWriter JavaDoc();
169         try {
170             formatter.write(node, writer);
171         } catch (IOException JavaDoc except) {
172             // Should never happen on String output
173
throw new XMLIOError(except);
174         }
175         return writer.toString();
176     }
177
178     /**
179      * Format a document or any node and its children into a byte array using
180      * the current encoding.
181      */

182     public byte[] toBytes(Node JavaDoc node) throws IOException JavaDoc {
183         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
184         write(node, out);
185         return out.toByteArray();
186     }
187
188     /**
189      * Output a document or any node and its children to a Writer.
190      */

191     public void write(Node JavaDoc node,
192                       Writer JavaDoc writer) throws IOException JavaDoc {
193         Formatter formatter = getFormatter(node, fOptions, false);
194         formatter.write(node, writer);
195     }
196
197     /**
198      * Output a document or any node and its children to a OutputStream.
199      */

200     public void write(Node JavaDoc node,
201                       OutputStream JavaDoc out) throws IOException JavaDoc {
202         Formatter formatter = getFormatter(node, fOptions, false);
203         Writer JavaDoc writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(out,
204                                                formatter.getMIMEEncoding()));
205         formatter.write(node, writer);
206         writer.flush();
207     }
208
209     /**
210      * Output a document or any node and its children to a File.
211      */

212     public void write(Node JavaDoc node,
213                       File JavaDoc out) throws IOException JavaDoc {
214         OutputStream JavaDoc outStream
215             = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(out));
216         try {
217             write(node, outStream);
218         } finally {
219             outStream.close();
220         }
221     }
222 }
223
Popular Tags