root / trunk / build / distribution / IzPack / src / lib / net / n3 / nanoxml / XMLWriter.java @ 21757
History | View | Annotate | Download (7.33 KB)
1 |
/* XMLWriter.java NanoXML/Java
|
---|---|
2 |
*
|
3 |
* $Revision: 5819 $
|
4 |
* $Date: 2006-06-14 09:29:09 +0200 $
|
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 |
|
32 |
import java.io.IOException; |
33 |
import java.io.OutputStream; |
34 |
import java.io.PrintWriter; |
35 |
import java.io.Writer; |
36 |
import java.util.Enumeration; |
37 |
|
38 |
|
39 |
/**
|
40 |
* An XMLWriter writes XML data to a stream.
|
41 |
*
|
42 |
* @see net.n3.nanoxml.XMLElement
|
43 |
* @see java.io.Writer
|
44 |
*
|
45 |
* @author Marc De Scheemaecker
|
46 |
* @version $Name$, $Revision: 5819 $
|
47 |
*/
|
48 |
public class XMLWriter |
49 |
{ |
50 |
|
51 |
/**
|
52 |
* Where to write the output to.
|
53 |
*/
|
54 |
private PrintWriter writer; |
55 |
|
56 |
|
57 |
/**
|
58 |
* Creates a new XML writer.
|
59 |
*
|
60 |
* @param writer where to write the output to.
|
61 |
*/
|
62 |
public XMLWriter(Writer writer) |
63 |
{ |
64 |
if (writer instanceof PrintWriter) { |
65 |
this.writer = (PrintWriter) writer; |
66 |
} else {
|
67 |
this.writer = new PrintWriter(writer); |
68 |
} |
69 |
} |
70 |
|
71 |
|
72 |
/**
|
73 |
* Creates a new XML writer.
|
74 |
*
|
75 |
* @param stream where to write the output to.
|
76 |
*/
|
77 |
public XMLWriter(OutputStream stream) |
78 |
{ |
79 |
this.writer = new PrintWriter(stream); |
80 |
} |
81 |
|
82 |
|
83 |
/**
|
84 |
* Cleans up the object when it's destroyed.
|
85 |
*/
|
86 |
protected void finalize() |
87 |
throws Throwable |
88 |
{ |
89 |
this.writer = null; |
90 |
super.finalize();
|
91 |
} |
92 |
|
93 |
|
94 |
/**
|
95 |
* Writes an XML element.
|
96 |
*
|
97 |
* @param xml the non-null XML element to write.
|
98 |
*/
|
99 |
public void write(XMLElement xml) |
100 |
throws IOException |
101 |
{ |
102 |
this.write(xml, true, 0); |
103 |
} |
104 |
|
105 |
|
106 |
/**
|
107 |
* Writes an XML element.
|
108 |
*
|
109 |
* @param xml the non-null XML element to write.
|
110 |
* @param prettyPrint if spaces need to be inserted to make the output more
|
111 |
* readable
|
112 |
*/
|
113 |
public void write(XMLElement xml, |
114 |
boolean prettyPrint)
|
115 |
throws IOException |
116 |
{ |
117 |
this.write(xml, prettyPrint, 0); |
118 |
} |
119 |
|
120 |
|
121 |
/**
|
122 |
* Writes an XML element.
|
123 |
*
|
124 |
* @param xml the non-null XML element to write.
|
125 |
* @param prettyPrint if spaces need to be inserted to make the output more
|
126 |
* readable
|
127 |
* @param indent how many spaces to indent the element.
|
128 |
*/
|
129 |
public void write(XMLElement xml, |
130 |
boolean prettyPrint,
|
131 |
int indent)
|
132 |
throws IOException |
133 |
{ |
134 |
if (prettyPrint) {
|
135 |
for (int i = 0; i < indent; i++) { |
136 |
this.writer.print(' '); |
137 |
} |
138 |
} |
139 |
|
140 |
if (xml.getName() == null) { |
141 |
if (xml.getContent() != null) { |
142 |
if (prettyPrint) {
|
143 |
this.writeEncoded(xml.getContent().trim());
|
144 |
writer.println(); |
145 |
} else {
|
146 |
this.writeEncoded(xml.getContent());
|
147 |
} |
148 |
} |
149 |
} else {
|
150 |
this.writer.print('<'); |
151 |
this.writer.print(xml.getName());
|
152 |
Enumeration enum = xml.enumerateAttributeNames(); |
153 |
|
154 |
while (enum.hasMoreElements()) { |
155 |
String key = (String) enum.nextElement(); |
156 |
String value = xml.getAttribute(key);
|
157 |
this.writer.print(" " + key + "=\""); |
158 |
this.writeEncoded(value);
|
159 |
this.writer.print('"'); |
160 |
} |
161 |
|
162 |
if ((xml.getContent() != null) |
163 |
&& (xml.getContent().length() > 0)) {
|
164 |
writer.print('>');
|
165 |
this.writeEncoded(xml.getContent());
|
166 |
writer.print("</" + xml.getName() + '>'); |
167 |
|
168 |
if (prettyPrint) {
|
169 |
writer.println(); |
170 |
} |
171 |
} else if (xml.hasChildren()) { |
172 |
writer.print('>');
|
173 |
|
174 |
if (prettyPrint) {
|
175 |
writer.println(); |
176 |
} |
177 |
|
178 |
enum = xml.enumerateChildren();
|
179 |
|
180 |
while (enum.hasMoreElements()) { |
181 |
XMLElement child = (XMLElement) enum.nextElement();
|
182 |
this.write(child, prettyPrint, indent + 4); |
183 |
} |
184 |
|
185 |
if (prettyPrint) {
|
186 |
for (int i = 0; i < indent; i++) { |
187 |
this.writer.print(' '); |
188 |
} |
189 |
} |
190 |
|
191 |
this.writer.print("</" + xml.getName() + ">"); |
192 |
|
193 |
if (prettyPrint) {
|
194 |
writer.println(); |
195 |
} |
196 |
} else {
|
197 |
this.writer.print("/>"); |
198 |
|
199 |
if (prettyPrint) {
|
200 |
writer.println(); |
201 |
} |
202 |
} |
203 |
} |
204 |
|
205 |
this.writer.flush();
|
206 |
} |
207 |
|
208 |
|
209 |
/**
|
210 |
* Writes a string encoding reserved characters.
|
211 |
*
|
212 |
* @param str the string to write.
|
213 |
*/
|
214 |
private void writeEncoded(String str) |
215 |
{ |
216 |
for (int i = 0; i < str.length(); i++) { |
217 |
char c = str.charAt(i);
|
218 |
|
219 |
switch (c) {
|
220 |
case 0x0D: |
221 |
case 0x0A: |
222 |
this.writer.print(c);
|
223 |
break;
|
224 |
|
225 |
case '<': |
226 |
this.writer.print("<"); |
227 |
break;
|
228 |
|
229 |
case '>': |
230 |
this.writer.print(">"); |
231 |
break;
|
232 |
|
233 |
case '&': |
234 |
this.writer.print("&"); |
235 |
break;
|
236 |
|
237 |
case '\'': |
238 |
this.writer.print("'"); |
239 |
break;
|
240 |
|
241 |
case '"': |
242 |
this.writer.print("""); |
243 |
break;
|
244 |
|
245 |
default:
|
246 |
if ((c < ' ') || (c > 0x7E)) { |
247 |
this.writer.print("&#x"); |
248 |
this.writer.print(Integer.toString(c, 16)); |
249 |
this.writer.print(';'); |
250 |
} else {
|
251 |
this.writer.print(c);
|
252 |
} |
253 |
} |
254 |
} |
255 |
} |
256 |
|
257 |
} |