KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > utils > xml > XMLSerializer


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

24
25 package com.bull.eclipse.jonas.utils.xml;
26
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29
30 import org.apache.xml.serialize.DOMSerializer;
31 import org.apache.xml.serialize.LineSeparator;
32 import org.apache.xml.serialize.Method;
33 import org.apache.xml.serialize.OutputFormat;
34 import org.apache.xml.serialize.Serializer;
35 import org.apache.xml.serialize.SerializerFactory;
36 import org.w3c.dom.Document JavaDoc;
37
38
39 /**
40  * Serialize a given DOM Document into an outpoutStream.
41  *
42  * @author Guillaume Sauthier
43  */

44 public class XMLSerializer {
45     /**
46      * Unique XMLSerilalizer instance
47      */

48     private static XMLSerializer instance = null;
49
50     /**
51      * Document to be formated and serialized
52      */

53     private Document JavaDoc doc;
54
55     /**
56      * XML Format
57      */

58     private OutputFormat format;
59
60     /**
61      * Serializer Factory
62      */

63     private SerializerFactory factory;
64
65     /**
66      * Creates a new XMLSerializer object.
67      */

68     private XMLSerializer() {
69         // define the format for the xml document
70
format = new OutputFormat();
71         format.setLineSeparator(LineSeparator.Unix);
72         format.setLineWidth(80);
73         format.setIndent(4);
74
75         // document serialization and writing
76
factory = SerializerFactory.getSerializerFactory(Method.XML);
77
78     }
79
80     /**
81      * Returns the unique XMLSerializer instance
82      * configured for the given Document
83      *
84      * @param d Document to be formated
85      *
86      * @return configured XMLSerializer
87      */

88     public static XMLSerializer getInstance(Document JavaDoc d) {
89         if(instance == null) {
90             instance = new XMLSerializer();
91         }
92
93         instance.setDocument(d);
94
95         return instance;
96     }
97
98     /**
99      * Set the Current Doucment
100      *
101      * @param d current document
102      */

103     private void setDocument(Document JavaDoc d) {
104         doc = d;
105     }
106
107     /**
108      * Serialize the encapsulated Document into the OutputStream
109      *
110      * @param os output stream
111      *
112      * @throws IOException When serialization fails
113      */

114     public void serialize(OutputStream JavaDoc os) throws IOException JavaDoc {
115         Serializer genericSerializer = factory.makeSerializer(os, format);
116         DOMSerializer domSerializer = genericSerializer.asDOMSerializer();
117
118         domSerializer.serialize(doc);
119     }
120 }
121
Popular Tags