KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > serialize > XMLSerializer


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * XMLSerializer.java
25  *
26  */

27
28 package org.xquark.serialize;
29
30 import java.io.OutputStream JavaDoc;
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.io.Writer JavaDoc;
33
34 import org.xquark.util.NSPrefixDecorator;
35
36 /**
37  * A simple SAX2 XML serializer implementing SAX ContentHandler and
38  * LexicalHandler. <B>Warning: </B>Default behavior is to perform automatic
39  * indentation and to ignore ignorable whitespace generated by the XML
40  * processor.
41  *
42  * <B>IMPORTANT </B>: SAX2 XMLReader
43  * <code>http://xml.org/sax/features/namespaces</code> feature is supposed to
44  * be set to true which is the default.
45  */

46 public class XMLSerializer extends NSPrefixDecorator {
47     private static final String JavaDoc RCSRevision = "$Revision: 1.5 $";
48
49     private static final String JavaDoc RCSName = "$Name: $";
50
51     private BasicXMLSerializer serializer = new BasicXMLSerializer();
52
53     private XMLSerializer() {
54         setContentHandler(serializer);
55         setLexicalHandler(serializer);
56         setEnabled(true);
57     }
58
59     /**
60      * Creates a new XMLSerializer
61      *
62      * @param out
63      * the OutputStream where the serializer writes bytes. For
64      * instance a FileOutputStream. The Unicode character encoding
65      * used will be the platform default.
66      * @throws UnsupportedEncodingException
67      * if the UTF-8 encoding is not supported by the JAVA platform
68      */

69     public XMLSerializer(OutputStream JavaDoc out) throws UnsupportedEncodingException JavaDoc {
70         this();
71         serializer.setOutputStream(out);
72     }
73
74     /**
75      * Creates a new XMLSerializer
76      *
77      * @param out
78      * the OutputStream where the serializer writes bytes. For
79      * instance a FileOutputStream.
80      * @param encoding
81      * a string specifying the character encoding, which must be
82      * supported by the JDK. Example: UTF-8, ISO-8859-1
83      * @throws UnsupportedEncodingException
84      * if the provided encoding is not supported by the JAVA
85      * platform
86      * @see java.io.OutputStream
87      */

88     public XMLSerializer(OutputStream JavaDoc out, String JavaDoc encoding)
89             throws UnsupportedEncodingException JavaDoc {
90         this();
91         serializer.setDefaultEncoding(encoding);
92         serializer.setOutputStream(out);
93     }
94
95     /**
96      * Creates a new XMLSerializer
97      *
98      * @param out
99      * the writer where the serializer writes characters. For
100      * instance a FileWriter.
101      */

102     public XMLSerializer(Writer JavaDoc out) {
103         this();
104         serializer.setWriter(out);
105     }
106
107     /**
108      * Creates a new XMLSerializer
109      *
110      * @param out
111      * the writer where the serializer writes characters. For
112      * instance a FileWriter.
113      * @param encoding
114      * a string specifying the character encoding (must be compatible
115      * with the encoding used by the writer). This value is used to
116      * generate the encoding information at the beginning of the XML
117      * file
118      * @see java.io.OutputStream
119      */

120     public XMLSerializer(Writer JavaDoc out, String JavaDoc encoding) {
121         this();
122         serializer.setDefaultEncoding(encoding);
123         serializer.setWriter(out);
124     }
125
126     /**
127      * Creates a new XMLSerializer
128      *
129      * @param encoding
130      * a string for the character encoding (must be supported by the
131      * JDK used). Example: UTF-8, ISO-8859-1
132      * @param indent
133      * if true, tabs are used at the beginning of lines to indent the
134      * output file.
135      */

136     public XMLSerializer(String JavaDoc encoding, boolean indent) {
137         this();
138         serializer.setDefaultEncoding(encoding);
139         serializer.setIndent(indent);
140     }
141
142     /**
143      * Set the OutputStream where the serializer writes bytes.
144      *
145      * When this method is called, the output Writer is created with the
146      * currently specified encoding. Further calls to setEncoding will not
147      * change the encoding used by the Writer.
148      *
149      * @param out
150      * the OutputStream where the serializer writes bytes. For
151      * instance a FileOutputStream.
152      * @throws UnsupportedEncodingException
153      * if the provided encoding is not supported by the JAVA
154      * platform
155      */

156     public void setOutputStream(OutputStream JavaDoc out)
157             throws UnsupportedEncodingException JavaDoc {
158         serializer.setOutputStream(out);
159     }
160
161     /**
162      * Set the Writer where the serializer writes characters.
163      *
164      * @param out
165      * the writer where the serializer writes characters.
166      */

167     public void setWriter(Writer JavaDoc out) {
168         serializer.setWriter(out);
169     }
170
171     /**
172      * Set the indenting mode. <B>Warning: </B>Default behavior is to perform
173      * automatic indentation ( ignorable whitespaces generated by the XML
174      * processor are ignored).
175      *
176      * @param indent
177      * if true, tabs are used at the beginning of lines to indent the
178      * output file.
179      */

180     public void setIndent(boolean indent) {
181         serializer.setIndent(indent);
182     }
183
184     /**
185      * Set the ordering mode for attributes. The default behavior is to perform
186      * no ordering.
187      *
188      * @param mode
189      * If true, ordering is performed following the
190      * {@link <a HREF="http://www.w3.org/TR/xml-c14n">Canonical XML<a>}
191      * W3C recommendation. If false, attributes are serialized as
192      * passed by the SAX2 XMLReader.
193      */

194     public void setCanonicalOutput(boolean mode) {
195         serializer.setCanonicalOutput(mode);
196     }
197
198     /**
199      * turn on or off the use of ignorable whitespace for indentation.
200      * <B>Warning: </B>This flag is ignored if automatic indentation is on.
201      *
202      * @param use
203      * if true (the default), ignorable whitespace are used for
204      * indentation unless automatic indentation is on.
205      */

206     public void setUseIgnorableWhitespaces(boolean use) {
207         serializer.setUseIgnorableWhitespaces(use);
208     }
209
210     /**
211      * Set the character encoding.
212      *
213      * @param encoding
214      * a string specifying the character encoding, which must be
215      * supported by the JDK. Example: UTF-8, ISO-8859-1.
216      */

217     public void setEncoding(String JavaDoc encoding) {
218         serializer.setDefaultEncoding(encoding);
219     }
220
221     /**
222      * Enable XML declaration generation on startDocument() event.
223      *
224      * @param enable
225      * XML Declaration is generated if true. Default is true.
226      */

227     public void setGenerateXMLDeclaration(boolean enable) {
228         serializer.setGenerateXMLDeclaration(enable);
229     }
230
231     /**
232      * Enable encoding attribute generation in XML declaration on
233      * startDocument() event. Considered only if XML decleration enabled.
234      *
235      * @param enable
236      * encoding declaration generation if true. Default is true.
237      */

238     public void setGenerateEncodingDeclaration(boolean enable) {
239         serializer.setGenerateEncodingDeclaration(enable);
240     }
241
242     /**
243      * Enable automatic close of the output user stream on endDocument() event.
244      *
245      * @param close
246      * Output stream will be closed if true. Default is false.
247      */

248     public void setAutoStreamClose(boolean close) {
249         serializer.setAutoStreamClose(close);
250     }
251
252     /**
253      * Accessor to the current indenting mode.
254      *
255      * @return the current indenting mode.
256      */

257     public boolean getIndent() {
258         return serializer.getIndent();
259     }
260
261     /**
262      * Accessor to the current ordering mode for attributes.
263      *
264      * @return the current ordering mode for attributes.
265      */

266     public boolean getCanonicalOutput() {
267         return serializer.getCanonicalOutput();
268     }
269
270     /**
271      * Accessor to the current the status of the use of ignorable whitespace by
272      * the serializer.
273      *
274      * @return the status of the use of ignorable whitespace by the serializer.
275      */

276     public boolean getUseIgnorableWhitespaces() {
277         return serializer.getUseIgnorableWhitespaces();
278     }
279
280     /**
281      * Accessor to the current character encoding in use.
282      *
283      * @return the character encoding in use, if previously specified.
284      */

285     public String JavaDoc getEncoding() {
286         return serializer.getDefaultEncoding();
287     }
288
289     /**
290      * Accessor to the current flag for XML declaration generation.
291      *
292      * @return if true, the XML declaration will be generated.
293      */

294     public boolean getGenerateXMLDeclaration() {
295         return serializer.getGenerateXMLDeclaration();
296     }
297
298     /**
299      * Return the value set for stream automatic close feature.
300      *
301      * @see #setAutoStreamClose(boolean).
302      */

303     public boolean getAutoStreamClose() {
304         return serializer.getAutoStreamClose();
305     }
306
307     /**
308      * Closes the potentially open element start tag waiting to know by the
309      * following events if the tag is empty. By this additional event the user
310      * indicates to the serializer that the current element must not be
311      * processed with an open-close tag.
312      */

313     public void completeStartTag() {
314         serializer.completeStartTag();
315     }
316
317     /**
318      * Flushes the user stream.
319      */

320     public void flush() {
321         serializer.flush();
322     }
323
324     /**
325      * Reset internal state before reuse.
326      */

327     public void reset() {
328         serializer.reset();
329     }
330
331     /**
332      * Closes the user stream.
333      */

334     public void close() {
335         serializer.close();
336     }
337 }
Popular Tags