KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > n3 > nanoxml > XMLWriter


1 /* XMLWriter.java NanoXML/Java
2  *
3  * $Revision: 1421 $
4  * $Date: 2006-03-12 17:32:32 +0100 (Sun, 12 Mar 2006) $
5  * $Name$
6  *
7  * This file is part of NanoXML 2 for Java.
8  * Copyright (C) 2001 Marc De Scheemaecker, All Rights Reserved.
9  *
10  * This software is provided 'as-is', without any express or implied warranty.
11  * In no event will the authors be held liable for any damages arising from the
12  * use of this software.
13  *
14  * Permission is granted to anyone to use this software for any purpose,
15  * including commercial applications, and to alter it and redistribute it
16  * freely, subject to the following restrictions:
17  *
18  * 1. The origin of this software must not be misrepresented; you must not
19  * claim that you wrote the original software. If you use this software in
20  * a product, an acknowledgment in the product documentation would be
21  * appreciated but is not required.
22  *
23  * 2. Altered source versions must be plainly marked as such, and must not be
24  * misrepresented as being the original software.
25  *
26  * 3. This notice may not be removed or altered from any source distribution.
27  */

28
29 package net.n3.nanoxml;
30
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.io.Writer JavaDoc;
35 import java.util.Enumeration JavaDoc;
36
37 /**
38  * An XMLWriter writes XML data to a stream.
39  *
40  * @see net.n3.nanoxml.XMLElement
41  * @see java.io.Writer
42  *
43  * @author Marc De Scheemaecker
44  * @version $Name$, $Revision: 1421 $
45  */

46 public class XMLWriter
47 {
48
49     /**
50      * Where to write the output to.
51      */

52     private PrintWriter JavaDoc writer;
53
54     /**
55      * Creates a new XML writer.
56      *
57      * @param writer where to write the output to.
58      */

59     public XMLWriter(Writer JavaDoc writer)
60     {
61         if (writer instanceof PrintWriter JavaDoc)
62         {
63             this.writer = (PrintWriter JavaDoc) writer;
64         }
65         else
66         {
67             this.writer = new PrintWriter JavaDoc(writer);
68         }
69     }
70
71     /**
72      * Creates a new XML writer.
73      *
74      * @param stream where to write the output to.
75      */

76     public XMLWriter(OutputStream JavaDoc stream)
77     {
78         this.writer = new PrintWriter JavaDoc(stream);
79     }
80
81     /**
82      * Cleans up the object when it's destroyed.
83      */

84     protected void finalize() throws Throwable JavaDoc
85     {
86         this.writer = null;
87         super.finalize();
88     }
89
90     /**
91      * Writes an XML element.
92      *
93      * @param xml the non-null XML element to write.
94      */

95     public void write(XMLElement xml) throws IOException JavaDoc
96     {
97         this.write(xml, true, 0);
98     }
99
100     /**
101      * Writes an XML element.
102      *
103      * @param xml the non-null XML element to write.
104      * @param prettyPrint if spaces need to be inserted to make the output more readable
105      */

106     public void write(XMLElement xml, boolean prettyPrint) throws IOException JavaDoc
107     {
108         this.write(xml, prettyPrint, 0);
109     }
110
111     /**
112      * Writes an XML element.
113      *
114      * @param xml the non-null XML element to write.
115      * @param prettyPrint if spaces need to be inserted to make the output more readable
116      * @param indent how many spaces to indent the element.
117      */

118     public void write(XMLElement xml, boolean prettyPrint, int indent) throws IOException JavaDoc
119     {
120         if (prettyPrint)
121         {
122             for (int i = 0; i < indent; i++)
123             {
124                 this.writer.print(' ');
125             }
126         }
127
128         if (xml.getName() == null)
129         {
130             if (xml.getContent() != null)
131             {
132                 if (prettyPrint)
133                 {
134                     this.writeEncoded(xml.getContent().trim());
135                     writer.println();
136                 }
137                 else
138                 {
139                     this.writeEncoded(xml.getContent());
140                 }
141             }
142         }
143         else
144         {
145             this.writer.print('<');
146             this.writer.print(xml.getName());
147             Enumeration JavaDoc enumeration = xml.enumerateAttributeNames();
148
149             while (enumeration.hasMoreElements())
150             {
151                 String JavaDoc key = (String JavaDoc) enumeration.nextElement();
152                 String JavaDoc value = xml.getAttribute(key);
153                 this.writer.print(" " + key + "=\"");
154                 this.writeEncoded(value);
155                 this.writer.print('"');
156             }
157
158             if ((xml.getContent() != null) && (xml.getContent().length() > 0))
159             {
160                 writer.print('>');
161                 this.writeEncoded(xml.getContent());
162                 writer.print("</" + xml.getName() + '>');
163
164                 if (prettyPrint)
165                 {
166                     writer.println();
167                 }
168             }
169             else if (xml.hasChildren())
170             {
171                 writer.print('>');
172
173                 if (prettyPrint)
174                 {
175                     writer.println();
176                 }
177
178                 enumeration = xml.enumerateChildren();
179
180                 while (enumeration.hasMoreElements())
181                 {
182                     XMLElement child = (XMLElement) enumeration.nextElement();
183                     this.write(child, prettyPrint, indent + 4);
184                 }
185
186                 if (prettyPrint)
187                 {
188                     for (int i = 0; i < indent; i++)
189                     {
190                         this.writer.print(' ');
191                     }
192                 }
193
194                 this.writer.print("</" + xml.getName() + ">");
195
196                 if (prettyPrint)
197                 {
198                     writer.println();
199                 }
200             }
201             else
202             {
203                 this.writer.print("/>");
204
205                 if (prettyPrint)
206                 {
207                     writer.println();
208                 }
209             }
210         }
211
212         this.writer.flush();
213     }
214
215     /**
216      * Writes a string encoding reserved characters.
217      *
218      * @param str the string to write.
219      */

220     private void writeEncoded(String JavaDoc str)
221     {
222         for (int i = 0; i < str.length(); i++)
223         {
224             char c = str.charAt(i);
225
226             switch (c)
227             {
228             case 0x0D:
229             case 0x0A:
230                 this.writer.print(c);
231                 break;
232
233             case '<':
234                 this.writer.print("&lt;");
235                 break;
236
237             case '>':
238                 this.writer.print("&gt;");
239                 break;
240
241             case '&':
242                 this.writer.print("&amp;");
243                 break;
244
245             case '\'':
246                 this.writer.print("&apos;");
247                 break;
248
249             case '"':
250                 this.writer.print("&quot;");
251                 break;
252
253             default:
254                 if ((c < ' ') || (c > 0x7E))
255                 {
256                     this.writer.print("&#x");
257                     this.writer.print(Integer.toString(c, 16));
258                     this.writer.print(';');
259                 }
260                 else
261                 {
262                     this.writer.print(c);
263                 }
264             }
265         }
266     }
267
268 }
269
Popular Tags