KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > xml > XMLSerializer


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: XMLSerializer.java,v 1.1 2004/10/13 11:31:33 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_lib.xml;
26
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.Writer JavaDoc;
31
32 import org.w3c.dom.Document JavaDoc;
33
34 import org.apache.xml.serialize.DOMSerializer;
35 import org.apache.xml.serialize.Method;
36 import org.apache.xml.serialize.OutputFormat;
37 import org.apache.xml.serialize.Serializer;
38 import org.apache.xml.serialize.SerializerFactory;
39
40
41 /**
42  * Serialize a given DOM Document.
43  * TODO refactore WsGen to use this XMLSerializer instead of the one in wsgen/util
44  *
45  * @author Guillaume Sauthier
46  */

47 public class XMLSerializer {
48
49     /**
50      * Indent size (char)
51      */

52     private static final int DEF_INDENT_SIZE = 4;
53
54     /**
55      * Maximum Line Width (char)
56      */

57     private static final int DEF_LINE_WIDTH = 80;
58
59     /**
60      * Maximum Line Width (char)
61      */

62     private static final String JavaDoc DEF_LINE_SEP = "\n";
63
64     /**
65      * Document to be formated and serialized
66      */

67     private Document JavaDoc doc;
68
69     /**
70      * XML Format
71      */

72     private OutputFormat format;
73
74     /**
75      * Serializer Factory
76      */

77     private SerializerFactory factory;
78
79     /**
80      * Creates a new XMLSerializer object.
81      * @param doc Document to be serialized
82      */

83     public XMLSerializer(Document JavaDoc doc) {
84         this.doc = doc;
85         // define the format for the xml document
86
format = new OutputFormat();
87         setIndent(DEF_INDENT_SIZE);
88         setLineSeparator(DEF_LINE_SEP);
89         setLineWidth(DEF_LINE_WIDTH);
90
91         // document serialization and writing
92
factory = SerializerFactory.getSerializerFactory(Method.XML);
93     }
94
95     /**
96      * Set the format line separator character.
97      * @param sep line separator character
98      */

99     public void setLineSeparator(String JavaDoc sep) {
100         format.setLineSeparator(sep);
101     }
102
103     /**
104      * Set the format line width value.
105      * @param width line width value
106      */

107     public void setLineWidth(int width) {
108         format.setLineWidth(width);
109     }
110
111     /**
112      * Set the format line indent value.
113      * @param indent line indent value
114      */

115     public void setIndent(int indent) {
116         format.setIndent(indent);
117     }
118
119     /**
120      * Serialize the encapsulated Document into the OutputStream
121      *
122      * @param os output stream
123      *
124      * @throws IOException When serialization fails
125      */

126     public void serialize(OutputStream JavaDoc os) throws IOException JavaDoc {
127         serialize(new OutputStreamWriter JavaDoc(os));
128     }
129
130     /**
131      * Serialize the encapsulated Document into the Writer
132      *
133      * @param writer writer
134      *
135      * @throws IOException When serialization fails
136      */

137     public void serialize(Writer JavaDoc writer) throws IOException JavaDoc {
138         Serializer genericSerializer = factory.makeSerializer(writer, format);
139         DOMSerializer domSerializer = genericSerializer.asDOMSerializer();
140
141         domSerializer.serialize(doc);
142     }
143 }
144
Popular Tags