|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| package org.dom4j.io; |
|
9 |
| |
|
10 |
| import java.io.File; |
|
11 |
| import java.io.FileWriter; |
|
12 |
| import java.io.IOException; |
|
13 |
| import java.io.OutputStream; |
|
14 |
| import java.io.StringWriter; |
|
15 |
| import java.io.Writer; |
|
16 |
| import java.util.Iterator; |
|
17 |
| |
|
18 |
| import javax.xml.namespace.QName; |
|
19 |
| import javax.xml.stream.XMLEventFactory; |
|
20 |
| import javax.xml.stream.XMLOutputFactory; |
|
21 |
| import javax.xml.stream.XMLStreamException; |
|
22 |
| import javax.xml.stream.events.Characters; |
|
23 |
| import javax.xml.stream.events.DTD; |
|
24 |
| import javax.xml.stream.events.EndDocument; |
|
25 |
| import javax.xml.stream.events.EndElement; |
|
26 |
| import javax.xml.stream.events.EntityReference; |
|
27 |
| import javax.xml.stream.events.ProcessingInstruction; |
|
28 |
| import javax.xml.stream.events.StartDocument; |
|
29 |
| import javax.xml.stream.events.StartElement; |
|
30 |
| import javax.xml.stream.util.XMLEventConsumer; |
|
31 |
| |
|
32 |
| import org.dom4j.Attribute; |
|
33 |
| import org.dom4j.Branch; |
|
34 |
| import org.dom4j.CDATA; |
|
35 |
| import org.dom4j.Comment; |
|
36 |
| import org.dom4j.Document; |
|
37 |
| import org.dom4j.DocumentType; |
|
38 |
| import org.dom4j.Element; |
|
39 |
| import org.dom4j.Entity; |
|
40 |
| import org.dom4j.Namespace; |
|
41 |
| import org.dom4j.Node; |
|
42 |
| import org.dom4j.Text; |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| public class STAXEventWriter { |
|
52 |
| |
|
53 |
| private XMLEventConsumer consumer; |
|
54 |
| |
|
55 |
| |
|
56 |
| private XMLEventFactory factory = XMLEventFactory.newInstance(); |
|
57 |
| |
|
58 |
| private XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); |
|
59 |
| |
|
60 |
0
| public STAXEventWriter() {
|
|
61 |
| } |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
0
| public STAXEventWriter(File file) throws XMLStreamException, IOException {
|
|
76 |
0
| consumer = outputFactory.createXMLEventWriter(new FileWriter(file));
|
|
77 |
| } |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
1
| public STAXEventWriter(Writer writer) throws XMLStreamException {
|
|
91 |
1
| consumer = outputFactory.createXMLEventWriter(writer);
|
|
92 |
| } |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
0
| public STAXEventWriter(OutputStream stream) throws XMLStreamException {
|
|
106 |
0
| consumer = outputFactory.createXMLEventWriter(stream);
|
|
107 |
| } |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
0
| public STAXEventWriter(XMLEventConsumer consumer) {
|
|
117 |
0
| this.consumer = consumer;
|
|
118 |
| } |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
0
| public XMLEventConsumer getConsumer() {
|
|
127 |
0
| return consumer;
|
|
128 |
| } |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
0
| public void setConsumer(XMLEventConsumer consumer) {
|
|
137 |
0
| this.consumer = consumer;
|
|
138 |
| } |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
0
| public XMLEventFactory getEventFactory() {
|
|
146 |
0
| return factory;
|
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
0
| public void setEventFactory(XMLEventFactory eventFactory) {
|
|
156 |
0
| this.factory = eventFactory;
|
|
157 |
| } |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
24
| public void writeNode(Node n) throws XMLStreamException {
|
|
170 |
24
| switch (n.getNodeType()) {
|
|
171 |
8
| case Node.ELEMENT_NODE:
|
|
172 |
8
| writeElement((Element) n);
|
|
173 |
| |
|
174 |
8
| break;
|
|
175 |
| |
|
176 |
16
| case Node.TEXT_NODE:
|
|
177 |
16
| writeText((Text) n);
|
|
178 |
| |
|
179 |
16
| break;
|
|
180 |
| |
|
181 |
0
| case Node.ATTRIBUTE_NODE:
|
|
182 |
0
| writeAttribute((Attribute) n);
|
|
183 |
| |
|
184 |
0
| break;
|
|
185 |
| |
|
186 |
0
| case Node.NAMESPACE_NODE:
|
|
187 |
0
| writeNamespace((Namespace) n);
|
|
188 |
| |
|
189 |
0
| break;
|
|
190 |
| |
|
191 |
0
| case Node.COMMENT_NODE:
|
|
192 |
0
| writeComment((Comment) n);
|
|
193 |
| |
|
194 |
0
| break;
|
|
195 |
| |
|
196 |
0
| case Node.CDATA_SECTION_NODE:
|
|
197 |
0
| writeCDATA((CDATA) n);
|
|
198 |
| |
|
199 |
0
| break;
|
|
200 |
| |
|
201 |
0
| case Node.PROCESSING_INSTRUCTION_NODE:
|
|
202 |
0
| writeProcessingInstruction((org.dom4j.ProcessingInstruction) n);
|
|
203 |
| |
|
204 |
0
| break;
|
|
205 |
| |
|
206 |
0
| case Node.ENTITY_REFERENCE_NODE:
|
|
207 |
0
| writeEntity((Entity) n);
|
|
208 |
| |
|
209 |
0
| break;
|
|
210 |
| |
|
211 |
0
| case Node.DOCUMENT_NODE:
|
|
212 |
0
| writeDocument((Document) n);
|
|
213 |
| |
|
214 |
0
| break;
|
|
215 |
| |
|
216 |
0
| case Node.DOCUMENT_TYPE_NODE:
|
|
217 |
0
| writeDocumentType((DocumentType) n);
|
|
218 |
| |
|
219 |
0
| break;
|
|
220 |
| |
|
221 |
0
| default:
|
|
222 |
0
| throw new XMLStreamException("Unsupported DOM4J Node: " + n);
|
|
223 |
| } |
|
224 |
| } |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
9
| public void writeChildNodes(Branch branch) throws XMLStreamException {
|
|
238 |
9
| for (int i = 0, s = branch.nodeCount(); i < s; i++) {
|
|
239 |
24
| Node n = branch.node(i);
|
|
240 |
24
| writeNode(n);
|
|
241 |
| } |
|
242 |
| } |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
8
| public void writeElement(Element elem) throws XMLStreamException {
|
|
254 |
8
| consumer.add(createStartElement(elem));
|
|
255 |
8
| writeChildNodes(elem);
|
|
256 |
8
| consumer.add(createEndElement(elem));
|
|
257 |
| } |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
8
| public StartElement createStartElement(Element elem) {
|
|
269 |
| |
|
270 |
8
| QName tagName = createQName(elem.getQName());
|
|
271 |
| |
|
272 |
| |
|
273 |
8
| Iterator attrIter = new AttributeIterator(elem.attributeIterator());
|
|
274 |
8
| Iterator nsIter = new NamespaceIterator(elem.declaredNamespaces()
|
|
275 |
| .iterator()); |
|
276 |
| |
|
277 |
| |
|
278 |
8
| return factory.createStartElement(tagName, attrIter, nsIter);
|
|
279 |
| } |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
8
| public EndElement createEndElement(Element elem) {
|
|
290 |
8
| QName tagName = createQName(elem.getQName());
|
|
291 |
8
| Iterator nsIter = new NamespaceIterator(elem.declaredNamespaces()
|
|
292 |
| .iterator()); |
|
293 |
| |
|
294 |
8
| return factory.createEndElement(tagName, nsIter);
|
|
295 |
| } |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
0
| public void writeAttribute(Attribute attr) throws XMLStreamException {
|
|
307 |
0
| consumer.add(createAttribute(attr));
|
|
308 |
| } |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
0
| public javax.xml.stream.events.Attribute createAttribute(Attribute attr) {
|
|
321 |
0
| QName attrName = createQName(attr.getQName());
|
|
322 |
0
| String value = attr.getValue();
|
|
323 |
| |
|
324 |
0
| return factory.createAttribute(attrName, value);
|
|
325 |
| } |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
0
| public void writeNamespace(Namespace ns) throws XMLStreamException {
|
|
337 |
0
| consumer.add(createNamespace(ns));
|
|
338 |
| } |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
0
| public javax.xml.stream.events.Namespace createNamespace(Namespace ns) {
|
|
350 |
0
| String prefix = ns.getPrefix();
|
|
351 |
0
| String uri = ns.getURI();
|
|
352 |
| |
|
353 |
0
| return factory.createNamespace(prefix, uri);
|
|
354 |
| } |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
16
| public void writeText(Text text) throws XMLStreamException {
|
|
366 |
16
| consumer.add(createCharacters(text));
|
|
367 |
| } |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
16
| public Characters createCharacters(Text text) {
|
|
378 |
16
| return factory.createCharacters(text.getText());
|
|
379 |
| } |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
0
| public void writeCDATA(CDATA cdata) throws XMLStreamException {
|
|
391 |
0
| consumer.add(createCharacters(cdata));
|
|
392 |
| } |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
0
| public Characters createCharacters(CDATA cdata) {
|
|
403 |
0
| return factory.createCData(cdata.getText());
|
|
404 |
| } |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
0
| public void writeComment(Comment comment) throws XMLStreamException {
|
|
416 |
0
| consumer.add(createComment(comment));
|
|
417 |
| } |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
0
| public javax.xml.stream.events.Comment createComment(Comment comment) {
|
|
429 |
0
| return factory.createComment(comment.getText());
|
|
430 |
| } |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
0
| public void writeProcessingInstruction(org.dom4j.ProcessingInstruction pi)
|
|
442 |
| throws XMLStreamException { |
|
443 |
0
| consumer.add(createProcessingInstruction(pi));
|
|
444 |
| } |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
0
| public ProcessingInstruction createProcessingInstruction(
|
|
458 |
| org.dom4j.ProcessingInstruction pi) { |
|
459 |
0
| String target = pi.getTarget();
|
|
460 |
0
| String data = pi.getText();
|
|
461 |
| |
|
462 |
0
| return factory.createProcessingInstruction(target, data);
|
|
463 |
| } |
|
464 |
| |
|
465 |
| |
|
466 |
| |
|
467 |
| |
|
468 |
| |
|
469 |
| |
|
470 |
| |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
0
| public void writeEntity(Entity entity) throws XMLStreamException {
|
|
475 |
0
| consumer.add(createEntityReference(entity));
|
|
476 |
| } |
|
477 |
| |
|
478 |
| |
|
479 |
| |
|
480 |
| |
|
481 |
| |
|
482 |
| |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
0
| private EntityReference createEntityReference(Entity entity) {
|
|
488 |
0
| return factory.createEntityReference(entity.getName(), null);
|
|
489 |
| } |
|
490 |
| |
|
491 |
| |
|
492 |
| |
|
493 |
| |
|
494 |
| |
|
495 |
| |
|
496 |
| |
|
497 |
| |
|
498 |
| |
|
499 |
| |
|
500 |
0
| public void writeDocumentType(DocumentType docType)
|
|
501 |
| throws XMLStreamException { |
|
502 |
0
| consumer.add(createDTD(docType));
|
|
503 |
| } |
|
504 |
| |
|
505 |
| |
|
506 |
| |
|
507 |
| |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
| |
|
512 |
| |
|
513 |
| |
|
514 |
| |
|
515 |
| |
|
516 |
0
| public DTD createDTD(DocumentType docType) {
|
|
517 |
0
| StringWriter decl = new StringWriter();
|
|
518 |
| |
|
519 |
0
| try {
|
|
520 |
0
| docType.write(decl);
|
|
521 |
| } catch (IOException e) { |
|
522 |
0
| throw new RuntimeException("Error writing DTD", e);
|
|
523 |
| } |
|
524 |
| |
|
525 |
0
| return factory.createDTD(decl.toString());
|
|
526 |
| } |
|
527 |
| |
|
528 |
| |
|
529 |
| |
|
530 |
| |
|
531 |
| |
|
532 |
| |
|
533 |
| |
|
534 |
| |
|
535 |
| |
|
536 |
| |
|
537 |
| |
|
538 |
1
| public void writeDocument(Document doc) throws XMLStreamException {
|
|
539 |
1
| consumer.add(createStartDocument(doc));
|
|
540 |
| |
|
541 |
1
| writeChildNodes(doc);
|
|
542 |
| |
|
543 |
1
| consumer.add(createEndDocument(doc));
|
|
544 |
| } |
|
545 |
| |
|
546 |
| |
|
547 |
| |
|
548 |
| |
|
549 |
| |
|
550 |
| |
|
551 |
| |
|
552 |
| |
|
553 |
| |
|
554 |
| |
|
555 |
1
| public StartDocument createStartDocument(Document doc) {
|
|
556 |
1
| String encoding = doc.getXMLEncoding();
|
|
557 |
| |
|
558 |
1
| if (encoding != null) {
|
|
559 |
1
| return factory.createStartDocument(encoding);
|
|
560 |
| } else { |
|
561 |
0
| return factory.createStartDocument();
|
|
562 |
| } |
|
563 |
| } |
|
564 |
| |
|
565 |
| |
|
566 |
| |
|
567 |
| |
|
568 |
| |
|
569 |
| |
|
570 |
| |
|
571 |
| |
|
572 |
| |
|
573 |
| |
|
574 |
1
| public EndDocument createEndDocument(Document doc) {
|
|
575 |
1
| return factory.createEndDocument();
|
|
576 |
| } |
|
577 |
| |
|
578 |
| |
|
579 |
| |
|
580 |
| |
|
581 |
| |
|
582 |
| |
|
583 |
| |
|
584 |
| |
|
585 |
| |
|
586 |
| |
|
587 |
16
| public QName createQName(org.dom4j.QName qname) {
|
|
588 |
16
| return new QName(qname.getNamespaceURI(), qname.getName(), qname
|
|
589 |
| .getNamespacePrefix()); |
|
590 |
| } |
|
591 |
| |
|
592 |
| |
|
593 |
| |
|
594 |
| |
|
595 |
| |
|
596 |
| private class AttributeIterator implements Iterator { |
|
597 |
| |
|
598 |
| private Iterator iter; |
|
599 |
| |
|
600 |
8
| public AttributeIterator(Iterator iter) {
|
|
601 |
8
| this.iter = iter;
|
|
602 |
| } |
|
603 |
| |
|
604 |
8
| public boolean hasNext() {
|
|
605 |
8
| return iter.hasNext();
|
|
606 |
| } |
|
607 |
| |
|
608 |
0
| public Object next() {
|
|
609 |
0
| Attribute attr = (Attribute) iter.next();
|
|
610 |
0
| QName attrName = createQName(attr.getQName());
|
|
611 |
0
| String value = attr.getValue();
|
|
612 |
| |
|
613 |
0
| return factory.createAttribute(attrName, value);
|
|
614 |
| } |
|
615 |
| |
|
616 |
0
| public void remove() {
|
|
617 |
0
| throw new UnsupportedOperationException();
|
|
618 |
| } |
|
619 |
| } |
|
620 |
| |
|
621 |
| |
|
622 |
| |
|
623 |
| |
|
624 |
| |
|
625 |
| private class NamespaceIterator implements Iterator { |
|
626 |
| private Iterator iter; |
|
627 |
| |
|
628 |
16
| public NamespaceIterator(Iterator iter) {
|
|
629 |
16
| this.iter = iter;
|
|
630 |
| } |
|
631 |
| |
|
632 |
16
| public boolean hasNext() {
|
|
633 |
16
| return iter.hasNext();
|
|
634 |
| } |
|
635 |
| |
|
636 |
0
| public Object next() {
|
|
637 |
0
| Namespace ns = (Namespace) iter.next();
|
|
638 |
0
| String prefix = ns.getPrefix();
|
|
639 |
0
| String nsURI = ns.getURI();
|
|
640 |
| |
|
641 |
0
| return factory.createNamespace(prefix, nsURI);
|
|
642 |
| } |
|
643 |
| |
|
644 |
0
| public void remove() {
|
|
645 |
0
| throw new UnsupportedOperationException();
|
|
646 |
| } |
|
647 |
| } |
|
648 |
| } |
|
649 |
| |
|
650 |
| |
|
651 |
| |
|
652 |
| |
|
653 |
| |
|
654 |
| |
|
655 |
| |
|
656 |
| |
|
657 |
| |
|
658 |
| |
|
659 |
| |
|
660 |
| |
|
661 |
| |
|
662 |
| |
|
663 |
| |
|
664 |
| |
|
665 |
| |
|
666 |
| |
|
667 |
| |
|
668 |
| |
|
669 |
| |
|
670 |
| |
|
671 |
| |
|
672 |
| |
|
673 |
| |
|
674 |
| |
|
675 |
| |
|
676 |
| |
|
677 |
| |
|
678 |
| |
|
679 |
| |
|
680 |
| |
|
681 |
| |
|
682 |
| |
|
683 |
| |
|
684 |
| |
|
685 |
| |