|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| package org.dom4j.io; |
|
9 |
| |
|
10 |
| import java.io.BufferedWriter; |
|
11 |
| import java.io.IOException; |
|
12 |
| import java.io.OutputStream; |
|
13 |
| import java.io.OutputStreamWriter; |
|
14 |
| import java.io.UnsupportedEncodingException; |
|
15 |
| import java.io.Writer; |
|
16 |
| import java.util.HashMap; |
|
17 |
| import java.util.Iterator; |
|
18 |
| import java.util.List; |
|
19 |
| import java.util.Map; |
|
20 |
| import java.util.StringTokenizer; |
|
21 |
| |
|
22 |
| import org.dom4j.Attribute; |
|
23 |
| import org.dom4j.CDATA; |
|
24 |
| import org.dom4j.Comment; |
|
25 |
| import org.dom4j.Document; |
|
26 |
| import org.dom4j.DocumentType; |
|
27 |
| import org.dom4j.Element; |
|
28 |
| import org.dom4j.Entity; |
|
29 |
| import org.dom4j.Namespace; |
|
30 |
| import org.dom4j.Node; |
|
31 |
| import org.dom4j.ProcessingInstruction; |
|
32 |
| import org.dom4j.Text; |
|
33 |
| import org.dom4j.tree.NamespaceStack; |
|
34 |
| |
|
35 |
| import org.xml.sax.Attributes; |
|
36 |
| import org.xml.sax.InputSource; |
|
37 |
| import org.xml.sax.Locator; |
|
38 |
| import org.xml.sax.SAXException; |
|
39 |
| import org.xml.sax.SAXNotRecognizedException; |
|
40 |
| import org.xml.sax.SAXNotSupportedException; |
|
41 |
| import org.xml.sax.XMLReader; |
|
42 |
| import org.xml.sax.ext.LexicalHandler; |
|
43 |
| import org.xml.sax.helpers.XMLFilterImpl; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| public class XMLWriter extends XMLFilterImpl implements LexicalHandler { |
|
72 |
| private static final String PAD_TEXT = " "; |
|
73 |
| |
|
74 |
| protected static final String[] LEXICAL_HANDLER_NAMES = { |
|
75 |
| "http://xml.org/sax/properties/lexical-handler", |
|
76 |
| "http://xml.org/sax/handlers/LexicalHandler"}; |
|
77 |
| |
|
78 |
| protected static final OutputFormat DEFAULT_FORMAT = new OutputFormat(); |
|
79 |
| |
|
80 |
| |
|
81 |
| private boolean resolveEntityRefs = true; |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| protected int lastOutputNodeType; |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| private boolean lastElementClosed = false; |
|
94 |
| |
|
95 |
| |
|
96 |
| protected boolean preserve = false; |
|
97 |
| |
|
98 |
| |
|
99 |
| protected Writer writer; |
|
100 |
| |
|
101 |
| |
|
102 |
| private NamespaceStack namespaceStack = new NamespaceStack(); |
|
103 |
| |
|
104 |
| |
|
105 |
| private OutputFormat format; |
|
106 |
| |
|
107 |
| |
|
108 |
| private boolean escapeText = true; |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| private int indentLevel = 0; |
|
115 |
| |
|
116 |
| |
|
117 |
| private StringBuffer buffer = new StringBuffer(); |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| private boolean charsAdded = false; |
|
123 |
| |
|
124 |
| private char lastChar; |
|
125 |
| |
|
126 |
| |
|
127 |
| private boolean autoFlush; |
|
128 |
| |
|
129 |
| |
|
130 |
| private LexicalHandler lexicalHandler; |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| private boolean showCommentsInDTDs; |
|
137 |
| |
|
138 |
| |
|
139 |
| private boolean inDTD; |
|
140 |
| |
|
141 |
| |
|
142 |
| private Map namespacesMap; |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| private int maximumAllowedCharacter; |
|
150 |
| |
|
151 |
18
| public XMLWriter(Writer writer) {
|
|
152 |
18
| this(writer, DEFAULT_FORMAT);
|
|
153 |
| } |
|
154 |
| |
|
155 |
2143
| public XMLWriter(Writer writer, OutputFormat format) {
|
|
156 |
2143
| this.writer = writer;
|
|
157 |
2143
| this.format = format;
|
|
158 |
2143
| namespaceStack.push(Namespace.NO_NAMESPACE);
|
|
159 |
| } |
|
160 |
| |
|
161 |
2
| public XMLWriter() {
|
|
162 |
2
| this.format = DEFAULT_FORMAT;
|
|
163 |
2
| this.writer = new BufferedWriter(new OutputStreamWriter(System.out));
|
|
164 |
2
| this.autoFlush = true;
|
|
165 |
2
| namespaceStack.push(Namespace.NO_NAMESPACE);
|
|
166 |
| } |
|
167 |
| |
|
168 |
2
| public XMLWriter(OutputStream out) throws UnsupportedEncodingException {
|
|
169 |
2
| this.format = DEFAULT_FORMAT;
|
|
170 |
2
| this.writer = createWriter(out, format.getEncoding());
|
|
171 |
2
| this.autoFlush = true;
|
|
172 |
2
| namespaceStack.push(Namespace.NO_NAMESPACE);
|
|
173 |
| } |
|
174 |
| |
|
175 |
7
| public XMLWriter(OutputStream out, OutputFormat format)
|
|
176 |
| throws UnsupportedEncodingException { |
|
177 |
7
| this.format = format;
|
|
178 |
7
| this.writer = createWriter(out, format.getEncoding());
|
|
179 |
7
| this.autoFlush = true;
|
|
180 |
7
| namespaceStack.push(Namespace.NO_NAMESPACE);
|
|
181 |
| } |
|
182 |
| |
|
183 |
8
| public XMLWriter(OutputFormat format) throws UnsupportedEncodingException {
|
|
184 |
8
| this.format = format;
|
|
185 |
8
| this.writer = createWriter(System.out, format.getEncoding());
|
|
186 |
8
| this.autoFlush = true;
|
|
187 |
8
| namespaceStack.push(Namespace.NO_NAMESPACE);
|
|
188 |
| } |
|
189 |
| |
|
190 |
2
| public void setWriter(Writer writer) {
|
|
191 |
2
| this.writer = writer;
|
|
192 |
2
| this.autoFlush = false;
|
|
193 |
| } |
|
194 |
| |
|
195 |
8
| public void setOutputStream(OutputStream out)
|
|
196 |
| throws UnsupportedEncodingException { |
|
197 |
8
| this.writer = createWriter(out, format.getEncoding());
|
|
198 |
8
| this.autoFlush = true;
|
|
199 |
| } |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
0
| public boolean isEscapeText() {
|
|
209 |
0
| return escapeText;
|
|
210 |
| } |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
1
| public void setEscapeText(boolean escapeText) {
|
|
221 |
1
| this.escapeText = escapeText;
|
|
222 |
| } |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
0
| public void setIndentLevel(int indentLevel) {
|
|
233 |
0
| this.indentLevel = indentLevel;
|
|
234 |
| } |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
484095
| public int getMaximumAllowedCharacter() {
|
|
244 |
484095
| if (maximumAllowedCharacter == 0) {
|
|
245 |
2147
| maximumAllowedCharacter = defaultMaximumAllowedCharacter();
|
|
246 |
| } |
|
247 |
| |
|
248 |
484095
| return maximumAllowedCharacter;
|
|
249 |
| } |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
1
| public void setMaximumAllowedCharacter(int maximumAllowedCharacter) {
|
|
262 |
1
| this.maximumAllowedCharacter = maximumAllowedCharacter;
|
|
263 |
| } |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
2119
| public void flush() throws IOException {
|
|
272 |
2119
| writer.flush();
|
|
273 |
| } |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
21
| public void close() throws IOException {
|
|
282 |
21
| writer.close();
|
|
283 |
| } |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
113
| public void println() throws IOException {
|
|
292 |
113
| writer.write(format.getLineSeparator());
|
|
293 |
| } |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
0
| public void write(Attribute attribute) throws IOException {
|
|
305 |
0
| writeAttribute(attribute);
|
|
306 |
| |
|
307 |
0
| if (autoFlush) {
|
|
308 |
0
| flush();
|
|
309 |
| } |
|
310 |
| } |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
113
| public void write(Document doc) throws IOException {
|
|
335 |
113
| writeDeclaration();
|
|
336 |
| |
|
337 |
113
| if (doc.getDocType() != null) {
|
|
338 |
1
| indent();
|
|
339 |
1
| writeDocType(doc.getDocType());
|
|
340 |
| } |
|
341 |
| |
|
342 |
113
| for (int i = 0, size = doc.nodeCount(); i < size; i++) {
|
|
343 |
116
| Node node = doc.node(i);
|
|
344 |
116
| writeNode(node);
|
|
345 |
| } |
|
346 |
| |
|
347 |
113
| writePrintln();
|
|
348 |
| |
|
349 |
113
| if (autoFlush) {
|
|
350 |
16
| flush();
|
|
351 |
| } |
|
352 |
| } |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
2043
| public void write(Element element) throws IOException {
|
|
369 |
2043
| writeElement(element);
|
|
370 |
| |
|
371 |
2043
| if (autoFlush) {
|
|
372 |
2
| flush();
|
|
373 |
| } |
|
374 |
| } |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
0
| public void write(CDATA cdata) throws IOException {
|
|
386 |
0
| writeCDATA(cdata.getText());
|
|
387 |
| |
|
388 |
0
| if (autoFlush) {
|
|
389 |
0
| flush();
|
|
390 |
| } |
|
391 |
| } |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
0
| public void write(Comment comment) throws IOException {
|
|
403 |
0
| writeComment(comment.getText());
|
|
404 |
| |
|
405 |
0
| if (autoFlush) {
|
|
406 |
0
| flush();
|
|
407 |
| } |
|
408 |
| } |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
0
| public void write(DocumentType docType) throws IOException {
|
|
420 |
0
| writeDocType(docType);
|
|
421 |
| |
|
422 |
0
| if (autoFlush) {
|
|
423 |
0
| flush();
|
|
424 |
| } |
|
425 |
| } |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
0
| public void write(Entity entity) throws IOException {
|
|
437 |
0
| writeEntity(entity);
|
|
438 |
| |
|
439 |
0
| if (autoFlush) {
|
|
440 |
0
| flush();
|
|
441 |
| } |
|
442 |
| } |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
0
| public void write(Namespace namespace) throws IOException {
|
|
454 |
0
| writeNamespace(namespace);
|
|
455 |
| |
|
456 |
0
| if (autoFlush) {
|
|
457 |
0
| flush();
|
|
458 |
| } |
|
459 |
| } |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
| |
|
464 |
| |
|
465 |
| |
|
466 |
| |
|
467 |
| |
|
468 |
| |
|
469 |
| |
|
470 |
0
| public void write(ProcessingInstruction processingInstruction)
|
|
471 |
| throws IOException { |
|
472 |
0
| writeProcessingInstruction(processingInstruction);
|
|
473 |
| |
|
474 |
0
| if (autoFlush) {
|
|
475 |
0
| flush();
|
|
476 |
| } |
|
477 |
| } |
|
478 |
| |
|
479 |
| |
|
480 |
| |
|
481 |
| |
|
482 |
| |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
| |
|
490 |
| |
|
491 |
0
| public void write(String text) throws IOException {
|
|
492 |
0
| writeString(text);
|
|
493 |
| |
|
494 |
0
| if (autoFlush) {
|
|
495 |
0
| flush();
|
|
496 |
| } |
|
497 |
| } |
|
498 |
| |
|
499 |
| |
|
500 |
| |
|
501 |
| |
|