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 |
| |
502 |
| |
503 |
| |
504 |
| |
505 |
| |
506 |
| |
507 |
| |
508 |
0
| public void write(Text text) throws IOException {
|
509 |
0
| writeString(text.getText());
|
510 |
| |
511 |
0
| if (autoFlush) {
|
512 |
0
| flush();
|
513 |
| } |
514 |
| } |
515 |
| |
516 |
| |
517 |
| |
518 |
| |
519 |
| |
520 |
| |
521 |
| |
522 |
| |
523 |
| |
524 |
| |
525 |
1
| public void write(Node node) throws IOException {
|
526 |
1
| writeNode(node);
|
527 |
| |
528 |
1
| if (autoFlush) {
|
529 |
0
| flush();
|
530 |
| } |
531 |
| } |
532 |
| |
533 |
| |
534 |
| |
535 |
| |
536 |
| |
537 |
| |
538 |
| |
539 |
| |
540 |
| |
541 |
| |
542 |
| |
543 |
1
| public void write(Object object) throws IOException {
|
544 |
1
| if (object instanceof Node) {
|
545 |
1
| write((Node) object);
|
546 |
0
| } else if (object instanceof String) {
|
547 |
0
| write((String) object);
|
548 |
0
| } else if (object instanceof List) {
|
549 |
0
| List list = (List) object;
|
550 |
| |
551 |
0
| for (int i = 0, size = list.size(); i < size; i++) {
|
552 |
0
| write(list.get(i));
|
553 |
| } |
554 |
0
| } else if (object != null) {
|
555 |
0
| throw new IOException("Invalid object: " + object);
|
556 |
| } |
557 |
| } |
558 |
| |
559 |
| |
560 |
| |
561 |
| |
562 |
| |
563 |
| |
564 |
| |
565 |
| |
566 |
| |
567 |
| |
568 |
| |
569 |
| |
570 |
| |
571 |
0
| public void writeOpen(Element element) throws IOException {
|
572 |
0
| writer.write("<");
|
573 |
0
| writer.write(element.getQualifiedName());
|
574 |
0
| writeAttributes(element);
|
575 |
0
| writer.write(">");
|
576 |
| } |
577 |
| |
578 |
| |
579 |
| |
580 |
| |
581 |
| |
582 |
| |
583 |
| |
584 |
| |
585 |
| |
586 |
| |
587 |
| |
588 |
| |
589 |
0
| public void writeClose(Element element) throws IOException {
|
590 |
0
| writeClose(element.getQualifiedName());
|
591 |
| } |
592 |
| |
593 |
| |
594 |
| |
595 |
0
| public void parse(InputSource source) throws IOException, SAXException {
|
596 |
0
| installLexicalHandler();
|
597 |
0
| super.parse(source);
|
598 |
| } |
599 |
| |
600 |
0
| public void setProperty(String name, Object value)
|
601 |
| throws SAXNotRecognizedException, SAXNotSupportedException { |
602 |
0
| for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) {
|
603 |
0
| if (LEXICAL_HANDLER_NAMES[i].equals(name)) {
|
604 |
0
| setLexicalHandler((LexicalHandler) value);
|
605 |
| |
606 |
0
| return;
|
607 |
| } |
608 |
| } |
609 |
| |
610 |
0
| super.setProperty(name, value);
|
611 |
| } |
612 |
| |
613 |
0
| public Object getProperty(String name) throws SAXNotRecognizedException,
|
614 |
| SAXNotSupportedException { |
615 |
0
| for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) {
|
616 |
0
| if (LEXICAL_HANDLER_NAMES[i].equals(name)) {
|
617 |
0
| return getLexicalHandler();
|
618 |
| } |
619 |
| } |
620 |
| |
621 |
0
| return super.getProperty(name);
|
622 |
| } |
623 |
| |
624 |
0
| public void setLexicalHandler(LexicalHandler handler) {
|
625 |
0
| if (handler == null) {
|
626 |
0
| throw new NullPointerException("Null lexical handler");
|
627 |
| } else { |
628 |
0
| this.lexicalHandler = handler;
|
629 |
| } |
630 |
| } |
631 |
| |
632 |
0
| public LexicalHandler getLexicalHandler() {
|
633 |
0
| return lexicalHandler;
|
634 |
| } |
635 |
| |
636 |
| |
637 |
| |
638 |
1
| public void setDocumentLocator(Locator locator) {
|
639 |
1
| super.setDocumentLocator(locator);
|
640 |
| } |
641 |
| |
642 |
4
| public void startDocument() throws SAXException {
|
643 |
4
| try {
|
644 |
4
| writeDeclaration();
|
645 |
4
| super.startDocument();
|
646 |
| } catch (IOException e) { |
647 |
0
| handleException(e);
|
648 |
| } |
649 |
| } |
650 |
| |
651 |
4
| public void endDocument() throws SAXException {
|
652 |
4
| super.endDocument();
|
653 |
| |
654 |
4
| if (autoFlush) {
|
655 |
0
| try {
|
656 |
0
| flush();
|
657 |
| } catch (IOException e) { |
658 |
| } |
659 |
| } |
660 |
| } |
661 |
| |
662 |
0
| public void startPrefixMapping(String prefix, String uri)
|
663 |
| throws SAXException { |
664 |
0
| if (namespacesMap == null) {
|
665 |
0
| namespacesMap = new HashMap();
|
666 |
| } |
667 |
| |
668 |
0
| namespacesMap.put(prefix, uri);
|
669 |
0
| super.startPrefixMapping(prefix, uri);
|
670 |
| } |
671 |
| |
672 |
0
| public void endPrefixMapping(String prefix) throws SAXException {
|
673 |
0
| super.endPrefixMapping(prefix);
|
674 |
| } |
675 |
| |
676 |
8
| public void startElement(String namespaceURI, String localName,
|
677 |
| String qName, Attributes attributes) throws SAXException { |
678 |
8
| try {
|
679 |
8
| charsAdded = false;
|
680 |
| |
681 |
8
| writePrintln();
|
682 |
8
| indent();
|
683 |
8
| writer.write("<");
|
684 |
8
| writer.write(qName);
|
685 |
8
| writeNamespaces();
|
686 |
8
| writeAttributes(attributes);
|
687 |
8
| writer.write(">");
|
688 |
8
| ++indentLevel;
|
689 |
8
| lastOutputNodeType = Node.ELEMENT_NODE;
|
690 |
8
| lastElementClosed = false;
|
691 |
| |
692 |
8
| super.startElement(namespaceURI, localName, qName, attributes);
|
693 |
| } catch (IOException e) { |
694 |
0
| handleException(e);
|
695 |
| } |
696 |
| } |
697 |
| |
698 |
8
| public void endElement(String namespaceURI, String localName, String qName)
|
699 |
| throws SAXException { |
700 |
8
| try {
|
701 |
8
| charsAdded = false;
|
702 |
8
| --indentLevel;
|
703 |
| |
704 |
8
| if (lastElementClosed) {
|
705 |
4
| writePrintln();
|
706 |
4
| indent();
|
707 |
| } |
708 |
| |
709 |
| |
710 |
| |
711 |
8
| boolean hadContent = true;
|
712 |
| |
713 |
8
| if (hadContent) {
|
714 |
8
| writeClose(qName);
|
715 |
| } else { |
716 |
0
| writeEmptyElementClose(qName);
|
717 |
| } |
718 |
| |
719 |
8
| lastOutputNodeType = Node.ELEMENT_NODE;
|
720 |
8
| lastElementClosed = true;
|
721 |
| |
722 |
8
| super.endElement(namespaceURI, localName, qName);
|
723 |
| } catch (IOException e) { |
724 |
0
| handleException(e);
|
725 |
| } |
726 |
| } |
727 |
| |
728 |
16
| public void characters(char[] ch, int start, int length)
|
729 |
| throws SAXException { |
730 |
16
| if ((ch == null) || (ch.length == 0) || (length <= 0)) {
|
731 |
1
| return;
|
732 |
| } |
733 |
| |
734 |
15
| try {
|
735 |
| |
736 |
| |
737 |
| |
738 |
| |
739 |
| |
740 |
| |
741 |
15
| String string = String.valueOf(ch, start, length);
|
742 |
| |
743 |
15
| if (escapeText) {
|
744 |
14
| string = escapeElementEntities(string);
|
745 |
| } |
746 |
| |
747 |
15
| if (format.isTrimText()) {
|
748 |
14
| if ((lastOutputNodeType == Node.TEXT_NODE) && !charsAdded) {
|
749 |
0
| writer.write(' ');
|
750 |
14
| } else if (charsAdded && Character.isWhitespace(lastChar)) {
|
751 |
1
| writer.write(' ');
|
752 |
13
| } else if (lastOutputNodeType == Node.ELEMENT_NODE
|
753 |
| && format.isPadText() && lastElementClosed |
754 |
| && Character.isWhitespace(ch[0])) { |
755 |
1
| writer.write(PAD_TEXT);
|
756 |
| } |
757 |
| |
758 |
14
| String delim = "";
|
759 |
14
| StringTokenizer tokens = new StringTokenizer(string);
|
760 |
| |
761 |
14
| while (tokens.hasMoreTokens()) {
|
762 |
17
| writer.write(delim);
|
763 |
17
| writer.write(tokens.nextToken());
|
764 |
17
| delim = " ";
|
765 |
| } |
766 |
| } else { |
767 |
1
| writer.write(string);
|
768 |
| } |
769 |
| |
770 |
15
| charsAdded = true;
|
771 |
15
| lastChar = ch[(start + length) - 1];
|
772 |
15
| lastOutputNodeType = Node.TEXT_NODE;
|
773 |
| |
774 |
15
| super.characters(ch, start, length);
|
775 |
| } catch (IOException e) { |
776 |
0
| handleException(e);
|
777 |
| } |
778 |
| } |
779 |
| |
780 |
0
| public void ignorableWhitespace(char[] ch, int start, int length)
|
781 |
| throws SAXException { |
782 |
0
| super.ignorableWhitespace(ch, start, length);
|
783 |
| } |
784 |
| |
785 |
0
| public void processingInstruction(String target, String data)
|
786 |
| throws SAXException { |
787 |
0
| try {
|
788 |
0
| indent();
|
789 |
0
| writer.write("<?");
|
790 |
0
| writer.write(target);
|
791 |
0
| writer.write(" ");
|
792 |
0
| writer.write(data);
|
793 |
0
| writer.write("?>");
|
794 |
0
| writePrintln();
|
795 |
0
| lastOutputNodeType = Node.PROCESSING_INSTRUCTION_NODE;
|
796 |
| |
797 |
0
| super.processingInstruction(target, data);
|
798 |
| } catch (IOException e) { |
799 |
0
| handleException(e);
|
800 |
| } |
801 |
| } |
802 |
| |
803 |
| |
804 |
| |
805 |
0
| public void notationDecl(String name, String publicID, String systemID)
|
806 |
| throws SAXException { |
807 |
0
| super.notationDecl(name, publicID, systemID);
|
808 |
| } |
809 |
| |
810 |
0
| public void unparsedEntityDecl(String name, String publicID,
|
811 |
| String systemID, String notationName) throws SAXException { |
812 |
0
| super.unparsedEntityDecl(name, publicID, systemID, notationName);
|
813 |
| } |
814 |
| |
815 |
| |
816 |
| |
817 |
0
| public void startDTD(String name, String publicID, String systemID)
|
818 |
| throws SAXException { |
819 |
0
| inDTD = true;
|
820 |
| |
821 |
0
| try {
|
822 |
0
| writeDocType(name, publicID, systemID);
|
823 |
| } catch (IOException e) { |
824 |
0
| handleException(e);
|
825 |
| } |
826 |
| |
827 |
0
| if (lexicalHandler != null) {
|
828 |
0
| lexicalHandler.startDTD(name, publicID, systemID);
|
829 |
| } |
830 |
| } |
831 |
| |
832 |
0
| public void endDTD() throws SAXException {
|
833 |
0
| inDTD = false;
|
834 |
| |
835 |
0
| if (lexicalHandler != null) {
|
836 |
0
| lexicalHandler.endDTD();
|
837 |
| } |
838 |
| } |
839 |
| |
840 |
0
| public void startCDATA() throws SAXException {
|
841 |
0
| try {
|
842 |
0
| writer.write("<![CDATA[");
|
843 |
| } catch (IOException e) { |
844 |
0
| handleException(e);
|
845 |
| } |
846 |
| |
847 |
0
| if (lexicalHandler != null) {
|
848 |
0
| lexicalHandler.startCDATA();
|
849 |
| } |
850 |
| } |
851 |
| |
852 |
0
| public void endCDATA() throws SAXException {
|
853 |
0
| try {
|
854 |
0
| writer.write("]]>");
|
855 |
| } catch (IOException e) { |
856 |
0
| handleException(e);
|
857 |
| } |
858 |
| |
859 |
0
| if (lexicalHandler != null) {
|
860 |
0
| lexicalHandler.endCDATA();
|
861 |
| } |
862 |
| } |
863 |
| |
864 |
0
| public void startEntity(String name) throws SAXException {
|
865 |
0
| try {
|
866 |
0
| writeEntityRef(name);
|
867 |
| } catch (IOException e) { |
868 |
0
| handleException(e);
|
869 |
| } |
870 |
| |
871 |
0
| if (lexicalHandler != null) {
|
872 |
0
| lexicalHandler.startEntity(name);
|
873 |
| } |
874 |
| } |
875 |
| |
876 |
0
| public void endEntity(String name) throws SAXException {
|
877 |
0
| if (lexicalHandler != null) {
|
878 |
0
| lexicalHandler.endEntity(name);
|
879 |
| } |
880 |
| } |
881 |
| |
882 |
0
| public void comment(char[] ch, int start, int length) throws SAXException {
|
883 |
0
| if (showCommentsInDTDs || !inDTD) {
|
884 |
0
| try {
|
885 |
0
| charsAdded = false;
|
886 |
0
| writeComment(new String(ch, start, length));
|
887 |
| } catch (IOException e) { |
888 |
0
| handleException(e);
|
889 |
| } |
890 |
| } |
891 |
| |
892 |
0
| if (lexicalHandler != null) {
|
893 |
0
| lexicalHandler.comment(ch, start, length);
|
894 |
| } |
895 |
| } |
896 |
| |
897 |
| |
898 |
| |
899 |
20077
| protected void writeElement(Element element) throws IOException {
|
900 |
20077
| int size = element.nodeCount();
|
901 |
20077
| String qualifiedName = element.getQualifiedName();
|
902 |
| |
903 |
20077
| writePrintln();
|
904 |
20077
| indent();
|
905 |
| |
906 |
20077
| writer.write("<");
|
907 |
20077
| writer.write(qualifiedName);
|
908 |
| |
909 |
20077
| int previouslyDeclaredNamespaces = namespaceStack.size();
|
910 |
20077
| Namespace ns = element.getNamespace();
|
911 |
| |
912 |
20077
| if (isNamespaceDeclaration(ns)) {
|
913 |
602
| namespaceStack.push(ns);
|
914 |
602
| writeNamespace(ns);
|
915 |
| } |
916 |
| |
917 |
| |
918 |
20077
| boolean textOnly = true;
|
919 |
| |
920 |
20077
| for (int i = 0; i < size; i++) {
|
921 |
56305
| Node node = element.node(i);
|
922 |
| |
923 |
56305
| if (node instanceof Namespace) {
|
924 |
198
| Namespace additional = (Namespace) node;
|
925 |
| |
926 |
198
| if (isNamespaceDeclaration(additional)) {
|
927 |
16
| namespaceStack.push(additional);
|
928 |
16
| writeNamespace(additional);
|
929 |
| } |
930 |
56107
| } else if (node instanceof Element) {
|
931 |
17921
| textOnly = false;
|
932 |
38186
| } else if (node instanceof Comment) {
|
933 |
445
| textOnly = false;
|
934 |
| } |
935 |
| } |
936 |
| |
937 |
20077
| writeAttributes(element);
|
938 |
| |
939 |
20077
| lastOutputNodeType = Node.ELEMENT_NODE;
|
940 |
| |
941 |
20077
| if (size <= 0) {
|
942 |
300
| writeEmptyElementClose(qualifiedName);
|
943 |
| } else { |
944 |
19777
| writer.write(">");
|
945 |
| |
946 |
19777
| if (textOnly) {
|
947 |
| |
948 |
| |
949 |
14954
| writeElementContent(element);
|
950 |
| } else { |
951 |
| |
952 |
4823
| ++indentLevel;
|
953 |
| |
954 |
4823
| writeElementContent(element);
|
955 |
| |
956 |
4823
| --indentLevel;
|
957 |
| |
958 |
4823
| writePrintln();
|
959 |
4823
| indent();
|
960 |
| } |
961 |
| |
962 |
19777
| writer.write("</");
|
963 |
19777
| writer.write(qualifiedName);
|
964 |
19777
| writer.write(">");
|
965 |
| } |
966 |
| |
967 |
| |
968 |
20077
| while (namespaceStack.size() > previouslyDeclaredNamespaces) {
|
969 |
620
| namespaceStack.pop();
|
970 |
| } |
971 |
| |
972 |
20077
| lastOutputNodeType = Node.ELEMENT_NODE;
|
973 |
| } |
974 |
| |
975 |
| |
976 |
| |
977 |
| |
978 |
| |
979 |
| |
980 |
| |
981 |
| |
982 |
| |
983 |
| |
984 |
93
| protected final boolean isElementSpacePreserved(Element element) {
|
985 |
93
| final Attribute attr = (Attribute) element.attribute("space");
|
986 |
93
| boolean preserveFound = preserve;
|
987 |
| |
988 |
93
| if (attr != null) {
|
989 |
5
| if ("xml".equals(attr.getNamespacePrefix())
|
990 |
| && "preserve".equals(attr.getText())) { |
991 |
4
| preserveFound = true;
|
992 |
| } else { |
993 |
1
| preserveFound = false;
|
994 |
| } |
995 |
| } |
996 |
| |
997 |
93
| return preserveFound;
|
998 |
| } |
999 |
| |
1000 |
| |
1001 |
| |
1002 |
| |
1003 |
| |
1004 |
| |
1005 |
| |
1006 |
| |
1007 |
| |
1008 |
| |
1009 |
| |
1010 |
| |
1011 |
| |
1012 |
| |
1013 |
19777
| protected void writeElementContent(Element element) throws IOException {
|
1014 |
19777
| boolean trim = format.isTrimText();
|
1015 |
19777
| boolean oldPreserve = preserve;
|
1016 |
| |
1017 |
19777
| if (trim) {
|
1018 |
93
| preserve = isElementSpacePreserved(element);
|
1019 |
93
| trim = !preserve;
|
1020 |
| } |
1021 |
| |
1022 |
19777
| if (trim) {
|
1023 |
| |
1024 |
| |
1025 |
82
| Text lastTextNode = null;
|
1026 |
82
| StringBuffer buff = null;
|
1027 |
82
| boolean textOnly = true;
|
1028 |
| |
1029 |
82
| for (int i = 0, size = element.nodeCount(); i < size; i++) {
|
1030 |
146
| Node node = element.node(i);
|
1031 |
| |
1032 |
146
| if (node instanceof Text) {
|
1033 |
76
| if (lastTextNode == null) {
|
1034 |
53
| lastTextNode = (Text) node;
|
1035 |
| } else { |
1036 |
23
| if (buff == null) {
|
1037 |
9
| buff = new StringBuffer(lastTextNode.getText());
|
1038 |
| } |
1039 |
| |
1040 |
23
| buff.append(((Text) node).getText());
|
1041 |
| } |
1042 |
| } else { |
1043 |
70
| if (!textOnly && format.isPadText()) {
|
1044 |
| |
1045 |
| |
1046 |
7
| char firstChar = 'a';
|
1047 |
7
| if (buff != null) {
|
1048 |
0
| firstChar = buff.charAt(0);
|
1049 |
7
| } else if (lastTextNode != null) {
|
1050 |
0
| firstChar = lastTextNode.getText().charAt(0);
|
1051 |
| } |
1052 |
| |
1053 |
7
| if (Character.isWhitespace(firstChar)) {
|
1054 |
0
| writer.write(PAD_TEXT);
|
1055 |
| } |
1056 |
| } |
1057 |
| |
1058 |
70
| if (lastTextNode != null) {
|
1059 |
17
| if (buff != null) {
|
1060 |
2
| writeString(buff.toString());
|
1061 |
2
| buff = null;
|
1062 |
| } else { |
1063 |
15
| writeString(lastTextNode.getText());
|
1064 |
| } |
1065 |
| |
1066 |
17
| if (format.isPadText()) {
|
1067 |
| |
1068 |
| |
1069 |
10
| char lastTextChar = 'a';
|
1070 |
10
| if (buff != null) {
|
1071 |
0
| lastTextChar = buff.charAt(buff.length() - 1);
|
1072 |
10
| } else if (lastTextNode != null) {
|
1073 |
10
| String txt = lastTextNode.getText();
|
1074 |
10
| lastTextChar = txt.charAt(txt.length() - 1);
|
1075 |
| } |
1076 |
| |
1077 |
10
| if (Character.isWhitespace(lastTextChar)) {
|
1078 |
1
| writer.write(PAD_TEXT);
|
1079 |
| } |
1080 |
| } |
1081 |
| |
1082 |
17
| lastTextNode = null;
|
1083 |
| } |
1084 |
| |
1085 |
70
| textOnly = false;
|
1086 |
70
| writeNode(node);
|
1087 |
| } |
1088 |
| } |
1089 |
| |
1090 |
82
| if (lastTextNode != null) {
|
1091 |
36
| if (!textOnly && format.isPadText()) {
|
1092 |
| |
1093 |
| |
1094 |
3
| char firstChar = 'a';
|
1095 |
3
| if (buff != null) {
|
1096 |
0
| firstChar = buff.charAt(0);
|
1097 |
| } else { |
1098 |
3
| firstChar = lastTextNode.getText().charAt(0);
|
1099 |
| } |
1100 |
| |
1101 |
3
| if (Character.isWhitespace(firstChar)) {
|
1102 |
2
| writer.write(PAD_TEXT);
|
1103 |
| } |
1104 |
| } |
1105 |
| |
1106 |
36
| if (buff != null) {
|
1107 |
7
| writeString(buff.toString());
|
1108 |
7
| buff = null;
|
1109 |
| } else { |
1110 |
29
| writeString(lastTextNode.getText());
|
1111 |
| } |
1112 |
| |
1113 |
36
| lastTextNode = null;
|
1114 |
| } |
1115 |
| } else { |
1116 |
19695
| Node lastTextNode = null;
|
1117 |
| |
1118 |
19695
| for (int i = 0, size = element.nodeCount(); i < size; i++) {
|
1119 |
56159
| Node node = element.node(i);
|
1120 |
| |
1121 |
56159
| if (node instanceof Text) {
|
1122 |
37643
| writeNode(node);
|
1123 |
37643
| lastTextNode = node;
|
1124 |
| } else { |
1125 |
18516
| if ((lastTextNode != null) && format.isPadText()) {
|
1126 |
| |
1127 |
| |
1128 |
0
| String txt = lastTextNode.getText();
|
1129 |
0
| char lastTextChar = txt.charAt(txt.length() - 1);
|
1130 |
| |
1131 |
0
| if (Character.isWhitespace(lastTextChar)) {
|
1132 |
0
| writer.write(PAD_TEXT);
|
1133 |
| } |
1134 |
| } |
1135 |
| |
1136 |
18516
| writeNode(node);
|
1137 |
| |
1138 |
| |
1139 |
| |
1140 |
| |
1141 |
| |
1142 |
18516
| lastTextNode = null;
|
1143 |
| } |
1144 |
| } |
1145 |
| } |
1146 |
| |
1147 |
19777
| preserve = oldPreserve;
|
1148 |
| } |
1149 |
| |
1150 |
21
| protected void writeCDATA(String text) throws IOException {
|
1151 |
21
| writer.write("<![CDATA[");
|
1152 |
| |
1153 |
21
| if (text != null) {
|
1154 |
17
| writer.write(text);
|
1155 |
| } |
1156 |
| |
1157 |
21
| writer.write("]]>");
|
1158 |
| |
1159 |
21
| lastOutputNodeType = Node.CDATA_SECTION_NODE;
|
1160 |
| } |
1161 |
| |
1162 |
1
| protected void writeDocType(DocumentType docType) throws IOException {
|
1163 |
1
| if (docType != null) {
|
1164 |
1
| docType.write(writer);
|
1165 |
1
| writePrintln();
|
1166 |
| } |
1167 |
| } |
1168 |
| |
1169 |
618
| protected void writeNamespace(Namespace namespace) throws IOException {
|
1170 |
618
| if (namespace != null) {
|
1171 |
618
| writeNamespace(namespace.getPrefix(), namespace.getURI());
|
1172 |
| } |
1173 |
| } |
1174 |
| |
1175 |
| |
1176 |
| |
1177 |
| |
1178 |
| |
1179 |
| |
1180 |
| |
1181 |
8
| protected void writeNamespaces() throws IOException {
|
1182 |
8
| if (namespacesMap != null) {
|
1183 |
0
| for (Iterator iter = namespacesMap.entrySet().iterator(); iter
|
1184 |
| .hasNext();) { |
1185 |
0
| Map.Entry entry = (Map.Entry) iter.next();
|
1186 |
0
| String prefix = (String) entry.getKey();
|
1187 |
0
| String uri = (String) entry.getValue();
|
1188 |
0
| writeNamespace(prefix, uri);
|
1189 |
| } |
1190 |
| |
1191 |
0
| namespacesMap = null;
|
1192 |
| } |
1193 |
| } |
1194 |
| |
1195 |
| |
1196 |
| |
1197 |
| |
1198 |
| |
1199 |
| |
1200 |
| |
1201 |
| |
1202 |
| |
1203 |
| |
1204 |
| |
1205 |
620
| protected void writeNamespace(String prefix, String uri)
|
1206 |
| throws IOException { |
1207 |
620
| if ((prefix != null) && (prefix.length() > 0)) {
|
1208 |
566
| writer.write(" xmlns:");
|
1209 |
566
| writer.write(prefix);
|
1210 |
566
| writer.write("=\"");
|
1211 |
| } else { |
1212 |
54
| writer.write(" xmlns=\"");
|
1213 |
| } |
1214 |
| |
1215 |
620
| writer.write(uri);
|
1216 |
620
| writer.write("\"");
|
1217 |
| } |
1218 |
| |
1219 |
1
| protected void writeProcessingInstruction(ProcessingInstruction pi)
|
1220 |
| throws IOException { |
1221 |
| |
1222 |
1
| writer.write("<?");
|
1223 |
1
| writer.write(pi.getName());
|
1224 |
1
| writer.write(" ");
|
1225 |
1
| writer.write(pi.getText());
|
1226 |
1
| writer.write("?>");
|
1227 |
1
| writePrintln();
|
1228 |
| |
1229 |
1
| lastOutputNodeType = Node.PROCESSING_INSTRUCTION_NODE;
|
1230 |
| } |
1231 |
| |
1232 |
53
| protected void writeString(String text) throws IOException {
|
1233 |
53
| if ((text != null) && (text.length() > 0)) {
|
1234 |
51
| if (escapeText) {
|
1235 |
51
| text = escapeElementEntities(text);
|
1236 |
| } |
1237 |
| |
1238 |
| |
1239 |
| |
1240 |
| |
1241 |
| |
1242 |
| |
1243 |
51
| if (format.isTrimText()) {
|
1244 |
51
| boolean first = true;
|
1245 |
51
| StringTokenizer tokenizer = new StringTokenizer(text);
|
1246 |
| |
1247 |
51
| while (tokenizer.hasMoreTokens()) {
|
1248 |
134
| String token = tokenizer.nextToken();
|
1249 |
| |
1250 |
134
| if (first) {
|
1251 |
47
| first = false;
|
1252 |
| |
1253 |
47
| if (lastOutputNodeType == Node.TEXT_NODE) {
|
1254 |
0
| writer.write(" ");
|
1255 |
| } |
1256 |
| } else { |
1257 |
87
| writer.write(" ");
|
1258 |
| } |
1259 |
| |
1260 |
134
| writer.write(token);
|
1261 |
134
| lastOutputNodeType = Node.TEXT_NODE;
|
1262 |
134
| lastChar = token.charAt(token.length() - 1);
|
1263 |
| } |
1264 |
| } else { |
1265 |
0
| lastOutputNodeType = Node.TEXT_NODE;
|
1266 |
0
| writer.write(text);
|
1267 |
0
| lastChar = text.charAt(text.length() - 1);
|
1268 |
| } |
1269 |
| } |
1270 |
| } |
1271 |
| |
1272 |
| |
1273 |
| |
1274 |
| |
1275 |
| |
1276 |
| |
1277 |
| |
1278 |
| |
1279 |
| |
1280 |
| |
1281 |
| |
1282 |
37643
| protected void writeNodeText(Node node) throws IOException {
|
1283 |
37643
| String text = node.getText();
|
1284 |
| |
1285 |
37643
| if ((text != null) && (text.length() > 0)) {
|
1286 |
37643
| if (escapeText) {
|
1287 |
37643
| text = escapeElementEntities(text);
|
1288 |
| } |
1289 |
| |
1290 |
37643
| lastOutputNodeType = Node.TEXT_NODE;
|
1291 |
37643
| writer.write(text);
|
1292 |
37643
| lastChar = text.charAt(text.length() - 1);
|
1293 |
| } |
1294 |
| } |
1295 |
| |
1296 |
56346
| protected void writeNode(Node node) throws IOException {
|
1297 |
56346
| int nodeType = node.getNodeType();
|
1298 |
| |
1299 |
56346
| switch (nodeType) {
|
1300 |
18034
| case Node.ELEMENT_NODE:
|
1301 |
18034
| writeElement((Element) node);
|
1302 |
| |
1303 |
18034
| break;
|
1304 |
| |
1305 |
0
| case Node.ATTRIBUTE_NODE:
|
1306 |
0
| writeAttribute((Attribute) node);
|
1307 |
| |
1308 |
0
| break;
|
1309 |
| |
1310 |
37643
| case Node.TEXT_NODE:
|
1311 |
37643
| writeNodeText(node);
|
1312 |
| |
1313 |
| |
1314 |
37643
| break;
|
1315 |
| |
1316 |
22
| case Node.CDATA_SECTION_NODE:
|
1317 |
22
| writeCDATA(node.getText());
|
1318 |
| |
1319 |
22
| break;
|
1320 |
| |
1321 |
0
| case Node.ENTITY_REFERENCE_NODE:
|
1322 |
0
| writeEntity((Entity) node);
|
1323 |
| |
1324 |
0
| break;
|
1325 |
| |
1326 |
1
| case Node.PROCESSING_INSTRUCTION_NODE:
|
1327 |
1
| writeProcessingInstruction((ProcessingInstruction) node);
|
1328 |
| |
1329 |
1
| break;
|
1330 |
| |
1331 |
447
| case Node.COMMENT_NODE:
|
1332 |
447
| writeComment(node.getText());
|
1333 |
| |
1334 |
447
| break;
|
1335 |
| |
1336 |
1
| case Node.DOCUMENT_NODE:
|
1337 |
1
| write((Document) node);
|
1338 |
| |
1339 |
1
| break;
|
1340 |
| |
1341 |
0
| case Node.DOCUMENT_TYPE_NODE:
|
1342 |
0
| writeDocType((DocumentType) node);
|
1343 |
| |
1344 |
0
| break;
|
1345 |
| |
1346 |
198
| case Node.NAMESPACE_NODE:
|
1347 |
| |
1348 |
| |
1349 |
| |
1350 |
198
| break;
|
1351 |
| |
1352 |
0
| default:
|
1353 |
0
| throw new IOException("Invalid node type: " + node);
|
1354 |
| } |
1355 |
| } |
1356 |
| |
1357 |
0
| protected void installLexicalHandler() {
|
1358 |
0
| XMLReader parent = getParent();
|
1359 |
| |
1360 |
0
| if (parent == null) {
|
1361 |
0
| throw new NullPointerException("No parent for filter");
|
1362 |
| } |
1363 |
| |
1364 |
| |
1365 |
0
| for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) {
|
1366 |
0
| try {
|
1367 |
0
| parent.setProperty(LEXICAL_HANDLER_NAMES[i], this);
|
1368 |
| |
1369 |
0
| break;
|
1370 |
| } catch (SAXNotRecognizedException ex) { |
1371 |
| |
1372 |
| } catch (SAXNotSupportedException ex) { |
1373 |
| |
1374 |
| } |
1375 |
| } |
1376 |
| } |
1377 |
| |
1378 |
0
| protected void writeDocType(String name, String publicID, String systemID)
|
1379 |
| throws IOException { |
1380 |
0
| boolean hasPublic = false;
|
1381 |
| |
1382 |
0
| writer.write("<!DOCTYPE ");
|
1383 |
0
| writer.write(name);
|
1384 |
| |
1385 |
0
| if ((publicID != null) && (!publicID.equals(""))) {
|
1386 |
0
| writer.write(" PUBLIC \"");
|
1387 |
0
| writer.write(publicID);
|
1388 |
0
| writer.write("\"");
|
1389 |
0
| hasPublic = true;
|
1390 |
| } |
1391 |
| |
1392 |
0
| if ((systemID != null) && (!systemID.equals(""))) {
|
1393 |
0
| if (!hasPublic) {
|
1394 |
0
| writer.write(" SYSTEM");
|
1395 |
| } |
1396 |
| |
1397 |
0
| writer.write(" \"");
|
1398 |
0
| writer.write(systemID);
|
1399 |
0
| writer.write("\"");
|
1400 |
| } |
1401 |
| |
1402 |
0
| writer.write(">");
|
1403 |
0
| writePrintln();
|
1404 |
| } |
1405 |
| |
1406 |
0
| protected void writeEntity(Entity entity) throws IOException {
|
1407 |
0
| if (!resolveEntityRefs()) {
|
1408 |
0
| writeEntityRef(entity.getName());
|
1409 |
| } else { |
1410 |
0
| writer.write(entity.getText());
|
1411 |
| } |
1412 |
| } |
1413 |
| |
1414 |
0
| protected void writeEntityRef(String name) throws IOException {
|
1415 |
0
| writer.write("&");
|
1416 |
0
| writer.write(name);
|
1417 |
0
| writer.write(";");
|
1418 |
| |
1419 |
0
| lastOutputNodeType = Node.ENTITY_REFERENCE_NODE;
|
1420 |
| } |
1421 |
| |
1422 |
447
| protected void writeComment(String text) throws IOException {
|
1423 |
447
| if (format.isNewlines()) {
|
1424 |
3
| println();
|
1425 |
3
| indent();
|
1426 |
| } |
1427 |
| |
1428 |
447
| writer.write("<!--");
|
1429 |
447
| writer.write(text);
|
1430 |
447
| writer.write("-->");
|
1431 |
| |
1432 |
447
| lastOutputNodeType = Node.COMMENT_NODE;
|
1433 |
| } |
1434 |
| |
1435 |
| |
1436 |
| |
1437 |
| |
1438 |
| |
1439 |
| |
1440 |
| |
1441 |
| |
1442 |
| |
1443 |
| |
1444 |
20077
| protected void writeAttributes(Element element) throws IOException {
|
1445 |
| |
1446 |
| |
1447 |
| |
1448 |
| |
1449 |
20077
| for (int i = 0, size = element.attributeCount(); i < size; i++) {
|
1450 |
4276
| Attribute attribute = element.attribute(i);
|
1451 |
4276
| Namespace ns = attribute.getNamespace();
|
1452 |
| |
1453 |
4276
| if ((ns != null) && (ns != Namespace.NO_NAMESPACE)
|
1454 |
| && (ns != Namespace.XML_NAMESPACE)) { |
1455 |
14
| String prefix = ns.getPrefix();
|
1456 |
14
| String uri = namespaceStack.getURI(prefix);
|
1457 |
| |
1458 |
14
| if (!ns.getURI().equals(uri)) {
|
1459 |
0
| writeNamespace(ns);
|
1460 |
0
| namespaceStack.push(ns);
|
1461 |
| } |
1462 |
| } |
1463 |
| |
1464 |
| |
1465 |
| |
1466 |
| |
1467 |
4276
| String attName = attribute.getName();
|
1468 |
| |
1469 |
4276
| if (attName.startsWith("xmlns:")) {
|
1470 |
3
| String prefix = attName.substring(6);
|
1471 |
| |
1472 |
3
| if (namespaceStack.getNamespaceForPrefix(prefix) == null) {
|
1473 |
2
| String uri = attribute.getValue();
|
1474 |
2
| namespaceStack.push(prefix, uri);
|
1475 |
2
| writeNamespace(prefix, uri);
|
1476 |
| } |
1477 |
4273
| } else if (attName.equals("xmlns")) {
|
1478 |
1
| if (namespaceStack.getDefaultNamespace() == null) {
|
1479 |
0
| String uri = attribute.getValue();
|
1480 |
0
| namespaceStack.push(null, uri);
|
1481 |
0
| writeNamespace(null, uri);
|
1482 |
| } |
1483 |
| } else { |
1484 |
4272
| char quote = format.getAttributeQuoteCharacter();
|
1485 |
4272
| writer.write(" ");
|
1486 |
4272
| writer.write(attribute.getQualifiedName());
|
1487 |
4272
| writer.write("=");
|
1488 |
4272
| writer.write(quote);
|
1489 |
4272
| writeEscapeAttributeEntities(attribute.getValue());
|
1490 |
4272
| writer.write(quote);
|
1491 |
| } |
1492 |
| } |
1493 |
| } |
1494 |
| |
1495 |
0
| protected void writeAttribute(Attribute attribute) throws IOException {
|
1496 |
0
| writer.write(" ");
|
1497 |
0
| writer.write(attribute.getQualifiedName());
|
1498 |
0
| writer.write("=");
|
1499 |
| |
1500 |
0
| char quote = format.getAttributeQuoteCharacter();
|
1501 |
0
| writer.write(quote);
|
1502 |
| |
1503 |
0
| writeEscapeAttributeEntities(attribute.getValue());
|
1504 |
| |
1505 |
0
| writer.write(quote);
|
1506 |
0
| lastOutputNodeType = Node.ATTRIBUTE_NODE;
|
1507 |
| } |
1508 |
| |
1509 |
8
| protected void writeAttributes(Attributes attributes) throws IOException {
|
1510 |
8
| for (int i = 0, size = attributes.getLength(); i < size; i++) {
|
1511 |
5
| writeAttribute(attributes, i);
|
1512 |
| } |
1513 |
| } |
1514 |
| |
1515 |
5
| protected void writeAttribute(Attributes attributes, int index)
|
1516 |
| throws IOException { |
1517 |
5
| char quote = format.getAttributeQuoteCharacter();
|
1518 |
5
| writer.write(" ");
|
1519 |
5
| writer.write(attributes.getQName(index));
|
1520 |
5
| writer.write("=");
|
1521 |
5
| writer.write(quote);
|
1522 |
5
| writeEscapeAttributeEntities(attributes.getValue(index));
|
1523 |
5
| writer.write(quote);
|
1524 |
| } |
1525 |
| |
1526 |
24916
| protected void indent() throws IOException {
|
1527 |
24916
| String indent = format.getIndent();
|
1528 |
| |
1529 |
24916
| if ((indent != null) && (indent.length() > 0)) {
|
1530 |
151
| for (int i = 0; i < indentLevel; i++) {
|
1531 |
172
| writer.write(indent);
|
1532 |
| } |
1533 |
| } |
1534 |
| } |
1535 |
| |
1536 |
| |
1537 |
| |
1538 |
| |
1539 |
| |
1540 |
| |
1541 |
| |
1542 |
| |
1543 |
| |
1544 |
25027
| protected void writePrintln() throws IOException {
|
1545 |
25027
| if (format.isNewlines()) {
|
1546 |
179
| String seperator = format.getLineSeparator();
|
1547 |
179
| if (lastChar != seperator.charAt(seperator.length() - 1)) {
|
1548 |
176
| writer.write(format.getLineSeparator());
|
1549 |
| } |
1550 |
| } |
1551 |
| } |
1552 |
| |
1553 |
| |
1554 |
| |
1555 |
| |
1556 |
| |
1557 |
| |
1558 |
| |
1559 |
| |
1560 |
| |
1561 |
| |
1562 |
| |
1563 |
| |
1564 |
| |
1565 |
| |
1566 |
25
| protected Writer createWriter(OutputStream outStream, String encoding)
|
1567 |
| throws UnsupportedEncodingException { |
1568 |
25
| return new BufferedWriter(new OutputStreamWriter(outStream, encoding));
|
1569 |
| } |
1570 |
| |
1571 |
| |
1572 |
| |
1573 |
| |
1574 |
| |
1575 |
| |
1576 |
| |
1577 |
| |
1578 |
| |
1579 |
| |
1580 |
114
| protected void writeDeclaration() throws IOException {
|
1581 |
114
| String encoding = format.getEncoding();
|
1582 |
| |
1583 |
| |
1584 |
114
| if (!format.isSuppressDeclaration()) {
|
1585 |
| |
1586 |
110
| if (encoding.equals("UTF8")) {
|
1587 |
0
| writer.write("<?xml version=\"1.0\"");
|
1588 |
| |
1589 |
0
| if (!format.isOmitEncoding()) {
|
1590 |
0
| writer.write(" encoding=\"UTF-8\"");
|
1591 |
| } |
1592 |
| |
1593 |
0
| writer.write("?>");
|
1594 |
| } else { |
1595 |
110
| writer.write("<?xml version=\"1.0\"");
|
1596 |
| |
1597 |
110
| if (!format.isOmitEncoding()) {
|
1598 |
110
| writer.write(" encoding=\"" + encoding + "\"");
|
1599 |
| } |
1600 |
| |
1601 |
110
| writer.write("?>");
|
1602 |
| } |
1603 |
| |
1604 |
110
| if (format.isNewLineAfterDeclaration()) {
|
1605 |
110
| println();
|
1606 |
| } |
1607 |
| } |
1608 |
| } |
1609 |
| |
1610 |
8
| protected void writeClose(String qualifiedName) throws IOException {
|
1611 |
8
| writer.write("</");
|
1612 |
8
| writer.write(qualifiedName);
|
1613 |
8
| writer.write(">");
|
1614 |
| } |
1615 |
| |
1616 |
299
| protected void writeEmptyElementClose(String qualifiedName)
|
1617 |
| throws IOException { |
1618 |
| |
1619 |
299
| if (!format.isExpandEmptyElements()) {
|
1620 |
295
| writer.write("/>");
|
1621 |
| } else { |
1622 |
4
| writer.write("></");
|
1623 |
4
| writer.write(qualifiedName);
|
1624 |
4
| writer.write(">");
|
1625 |
| } |
1626 |
| } |
1627 |
| |
1628 |
0
| protected boolean isExpandEmptyElements() {
|
1629 |
0
| return format.isExpandEmptyElements();
|
1630 |
| } |
1631 |
| |
1632 |
| |
1633 |
| |
1634 |
| |
1635 |
| |
1636 |
| |
1637 |
| |
1638 |
| |
1639 |
| |
1640 |
| |
1641 |
| |
1642 |
37708
| protected String escapeElementEntities(String text) {
|
1643 |
37708
| char[] block = null;
|
1644 |
37708
| int i;
|
1645 |
37708
| int last = 0;
|
1646 |
37708
| int size = text.length();
|
1647 |
| |
1648 |
37708
| for (i = 0; i < size; i++) {
|
1649 |
467325
| String entity = null;
|
1650 |
467325
| char c = text.charAt(i);
|
1651 |
| |
1652 |
467325
| switch (c) {
|
1653 |
93
| case '<':
|
1654 |
93
| entity = "<";
|
1655 |
| |
1656 |
93
| break;
|
1657 |
| |
1658 |
84
| case '>':
|
1659 |
84
| entity = ">";
|
1660 |
| |
1661 |
84
| break;
|
1662 |
| |
1663 |
63
| case '&':
|
1664 |
63
| entity = "&";
|
1665 |
| |
1666 |
63
| break;
|
1667 |
| |
1668 |
3455
| case '\t':
|
1669 |
27550
| case '\n':
|
1670 |
0
| case '\r':
|
1671 |
| |
1672 |
| |
1673 |
31005
| if (preserve) {
|
1674 |
2
| entity = String.valueOf(c);
|
1675 |
| } |
1676 |
| |
1677 |
31005
| break;
|
1678 |
| |
1679 |
436080
| default:
|
1680 |
| |
1681 |
436080
| if ((c < 32) || shouldEncodeChar(c)) {
|
1682 |
1
| entity = "&#" + (int) c + ";";
|
1683 |
| } |
1684 |
| |
1685 |
436080
| break;
|
1686 |
| } |
1687 |
| |
1688 |
467325
| if (entity != null) {
|
1689 |
243
| if (block == null) {
|
1690 |
145
| block = text.toCharArray();
|
1691 |
| } |
1692 |
| |
1693 |
243
| buffer.append(block, last, i - last);
|
1694 |
243
| buffer.append(entity);
|
1695 |
243
| last = i + 1;
|
1696 |
| } |
1697 |
| } |
1698 |
| |
1699 |
37708
| if (last == 0) {
|
1700 |
37563
| return text;
|
1701 |
| } |
1702 |
| |
1703 |
145
| if (last < size) {
|
1704 |
93
| if (block == null) {
|
1705 |
0
| block = text.toCharArray();
|
1706 |
| } |
1707 |
| |
1708 |
93
| buffer.append(block, last, i - last);
|
1709 |
| } |
1710 |
| |
1711 |
145
| String answer = buffer.toString();
|
1712 |
145
| buffer.setLength(0);
|
1713 |
| |
1714 |
145
| return answer;
|
1715 |
| } |
1716 |
| |
1717 |
4277
| protected void writeEscapeAttributeEntities(String txt) throws IOException {
|
1718 |
4277
| if (txt != null) {
|
1719 |
4277
| String escapedText = escapeAttributeEntities(txt);
|
1720 |
4277
| writer.write(escapedText);
|
1721 |
| } |
1722 |
| } |
1723 |
| |
1724 |
| |
1725 |
| |
1726 |
| |
1727 |
| |
1728 |
| |
1729 |
| |
1730 |
| |
1731 |
| |
1732 |
| |
1733 |
| |
1734 |
4277
| protected String escapeAttributeEntities(String text) {
|
1735 |
4277
| char quote = format.getAttributeQuoteCharacter();
|
1736 |
| |
1737 |
4277
| char[] block = null;
|
1738 |
4277
| int i;
|
1739 |
4277
| int last = 0;
|
1740 |
4277
| int size = text.length();
|
1741 |
| |
1742 |
4277
| for (i = 0; i < size; i++) {
|
1743 |
48016
| String entity = null;
|
1744 |
48016
| char c = text.charAt(i);
|
1745 |
| |
1746 |
48016
| switch (c) {
|
1747 |
0
| case '<':
|
1748 |
0
| entity = "<";
|
1749 |
| |
1750 |
0
| break;
|
1751 |
| |
1752 |
0
| case '>':
|
1753 |
0
| entity = ">";
|
1754 |
| |
1755 |
0
| break;
|
1756 |
| |
1757 |
1
| case '\'':
|
1758 |
| |
1759 |
1
| if (quote == '\'') {
|
1760 |
0
| entity = "'";
|
1761 |
| } |
1762 |
| |
1763 |
1
| break;
|
1764 |
| |
1765 |
0
| case '\"':
|
1766 |
| |
1767 |
0
| if (quote == '\"') {
|
1768 |
0
| entity = """;
|
1769 |
| } |
1770 |
| |
1771 |
0
| break;
|
1772 |
| |
1773 |
0
| case '&':
|
1774 |
0
| entity = "&";
|
1775 |
| |
1776 |
0
| break;
|
1777 |
| |
1778 |
0
| case '\t':
|
1779 |
0
| case '\n':
|
1780 |
0
| case '\r':
|
1781 |
| |
1782 |
| |
1783 |
0
| break;
|
1784 |
| |
1785 |
48015
| default:
|
1786 |
| |
1787 |
48015
| if ((c < 32) || shouldEncodeChar(c)) {
|
1788 |
0
| entity = "&#" + (int) c + ";";
|
1789 |
| } |
1790 |
| |
1791 |
48015
| break;
|
1792 |
| } |
1793 |
| |
1794 |
48016
| if (entity != null) {
|
1795 |
0
| if (block == null) {
|
1796 |
0
| block = text.toCharArray();
|
1797 |
| } |
1798 |
| |
1799 |
0
| buffer.append(block, last, i - last);
|
1800 |
0
| buffer.append(entity);
|
1801 |
0
| last = i + 1;
|
1802 |
| } |
1803 |
| } |
1804 |
| |
1805 |
4277
| if (last == 0) {
|
1806 |
4277
| return text;
|
1807 |
| } |
1808 |
| |
1809 |
0
| if (last < size) {
|
1810 |
0
| if (block == null) {
|
1811 |
0
| block = text.toCharArray();
|
1812 |
| } |
1813 |
| |
1814 |
0
| buffer.append(block, last, i - last);
|
1815 |
| } |
1816 |
| |
1817 |
0
| String answer = buffer.toString();
|
1818 |
0
| buffer.setLength(0);
|
1819 |
| |
1820 |
0
| return answer;
|
1821 |
| } |
1822 |
| |
1823 |
| |
1824 |
| |
1825 |
| |
1826 |
| |
1827 |
| |
1828 |
| |
1829 |
| |
1830 |
| |
1831 |
| |
1832 |
484095
| protected boolean shouldEncodeChar(char c) {
|
1833 |
484095
| int max = getMaximumAllowedCharacter();
|
1834 |
| |
1835 |
484095
| return (max > 0) && (c > max);
|
1836 |
| } |
1837 |
| |
1838 |
| |
1839 |
| |
1840 |
| |
1841 |
| |
1842 |
| |
1843 |
| |
1844 |
| |
1845 |
2147
| protected int defaultMaximumAllowedCharacter() {
|
1846 |
2147
| String encoding = format.getEncoding();
|
1847 |
| |
1848 |
2147
| if (encoding != null) {
|
1849 |
2147
| if (encoding.equals("US-ASCII")) {
|
1850 |
0
| return 127;
|
1851 |
| } |
1852 |
| } |
1853 |
| |
1854 |
| |
1855 |
2147
| return -1;
|
1856 |
| } |
1857 |
| |
1858 |
20275
| protected boolean isNamespaceDeclaration(Namespace ns) {
|
1859 |
20275
| if ((ns != null) && (ns != Namespace.XML_NAMESPACE)) {
|
1860 |
20275
| String uri = ns.getURI();
|
1861 |
| |
1862 |
20275
| if (uri != null) {
|
1863 |
20275
| if (!namespaceStack.contains(ns)) {
|
1864 |
618
| return true;
|
1865 |
| } |
1866 |
| } |
1867 |
| } |
1868 |
| |
1869 |
19657
| return false;
|
1870 |
| } |
1871 |
| |
1872 |
0
| protected void handleException(IOException e) throws SAXException {
|
1873 |
0
| throw new SAXException(e);
|
1874 |
| } |
1875 |
| |
1876 |
| |
1877 |
| |
1878 |
| |
1879 |
| |
1880 |
| |
1881 |
| |
1882 |
| |
1883 |
| |
1884 |
| |
1885 |
| |
1886 |
7
| protected OutputFormat getOutputFormat() {
|
1887 |
7
| return format;
|
1888 |
| } |
1889 |
| |
1890 |
0
| public boolean resolveEntityRefs() {
|
1891 |
0
| return resolveEntityRefs;
|
1892 |
| } |
1893 |
| |
1894 |
0
| public void setResolveEntityRefs(boolean resolve) {
|
1895 |
0
| this.resolveEntityRefs = resolve;
|
1896 |
| } |
1897 |
| } |
1898 |
| |
1899 |
| |
1900 |
| |
1901 |
| |
1902 |
| |
1903 |
| |
1904 |
| |
1905 |
| |
1906 |
| |
1907 |
| |
1908 |
| |
1909 |
| |
1910 |
| |
1911 |
| |
1912 |
| |
1913 |
| |
1914 |
| |
1915 |
| |
1916 |
| |
1917 |
| |
1918 |
| |
1919 |
| |
1920 |
| |
1921 |
| |
1922 |
| |
1923 |
| |
1924 |
| |
1925 |
| |
1926 |
| |
1927 |
| |
1928 |
| |
1929 |
| |
1930 |
| |
1931 |
| |
1932 |
| |
1933 |
| |
1934 |
| |