1 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 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 public STAXEventWriter() { 61 } 62 63 75 public STAXEventWriter(File file) throws XMLStreamException, IOException { 76 consumer = outputFactory.createXMLEventWriter(new FileWriter (file)); 77 } 78 79 90 public STAXEventWriter(Writer writer) throws XMLStreamException { 91 consumer = outputFactory.createXMLEventWriter(writer); 92 } 93 94 105 public STAXEventWriter(OutputStream stream) throws XMLStreamException { 106 consumer = outputFactory.createXMLEventWriter(stream); 107 } 108 109 116 public STAXEventWriter(XMLEventConsumer consumer) { 117 this.consumer = consumer; 118 } 119 120 126 public XMLEventConsumer getConsumer() { 127 return consumer; 128 } 129 130 136 public void setConsumer(XMLEventConsumer consumer) { 137 this.consumer = consumer; 138 } 139 140 145 public XMLEventFactory getEventFactory() { 146 return factory; 147 } 148 149 155 public void setEventFactory(XMLEventFactory eventFactory) { 156 this.factory = eventFactory; 157 } 158 159 169 public void writeNode(Node n) throws XMLStreamException { 170 switch (n.getNodeType()) { 171 case Node.ELEMENT_NODE: 172 writeElement((Element) n); 173 174 break; 175 176 case Node.TEXT_NODE: 177 writeText((Text) n); 178 179 break; 180 181 case Node.ATTRIBUTE_NODE: 182 writeAttribute((Attribute) n); 183 184 break; 185 186 case Node.NAMESPACE_NODE: 187 writeNamespace((Namespace) n); 188 189 break; 190 191 case Node.COMMENT_NODE: 192 writeComment((Comment) n); 193 194 break; 195 196 case Node.CDATA_SECTION_NODE: 197 writeCDATA((CDATA) n); 198 199 break; 200 201 case Node.PROCESSING_INSTRUCTION_NODE: 202 writeProcessingInstruction((org.dom4j.ProcessingInstruction) n); 203 204 break; 205 206 case Node.ENTITY_REFERENCE_NODE: 207 writeEntity((Entity) n); 208 209 break; 210 211 case Node.DOCUMENT_NODE: 212 writeDocument((Document) n); 213 214 break; 215 216 case Node.DOCUMENT_TYPE_NODE: 217 writeDocumentType((DocumentType) n); 218 219 break; 220 221 default: 222 throw new XMLStreamException("Unsupported DOM4J Node: " + n); 223 } 224 } 225 226 237 public void writeChildNodes(Branch branch) throws XMLStreamException { 238 for (int i = 0, s = branch.nodeCount(); i < s; i++) { 239 Node n = branch.node(i); 240 writeNode(n); 241 } 242 } 243 244 253 public void writeElement(Element elem) throws XMLStreamException { 254 consumer.add(createStartElement(elem)); 255 writeChildNodes(elem); 256 consumer.add(createEndElement(elem)); 257 } 258 259 268 public StartElement createStartElement(Element elem) { 269 QName tagName = createQName(elem.getQName()); 271 272 Iterator attrIter = new AttributeIterator(elem.attributeIterator()); 274 Iterator nsIter = new NamespaceIterator(elem.declaredNamespaces() 275 .iterator()); 276 277 return factory.createStartElement(tagName, attrIter, nsIter); 279 } 280 281 289 public EndElement createEndElement(Element elem) { 290 QName tagName = createQName(elem.getQName()); 291 Iterator nsIter = new NamespaceIterator(elem.declaredNamespaces() 292 .iterator()); 293 294 return factory.createEndElement(tagName, nsIter); 295 } 296 297 306 public void writeAttribute(Attribute attr) throws XMLStreamException { 307 consumer.add(createAttribute(attr)); 308 } 309 310 320 public javax.xml.stream.events.Attribute createAttribute(Attribute attr) { 321 QName attrName = createQName(attr.getQName()); 322 String value = attr.getValue(); 323 324 return factory.createAttribute(attrName, value); 325 } 326 327 336 public void writeNamespace(Namespace ns) throws XMLStreamException { 337 consumer.add(createNamespace(ns)); 338 } 339 340 349 public javax.xml.stream.events.Namespace createNamespace(Namespace ns) { 350 String prefix = ns.getPrefix(); 351 String uri = ns.getURI(); 352 353 return factory.createNamespace(prefix, uri); 354 } 355 356 365 public void writeText(Text text) throws XMLStreamException { 366 consumer.add(createCharacters(text)); 367 } 368 369 377 public Characters createCharacters(Text text) { 378 return factory.createCharacters(text.getText()); 379 } 380 381 390 public void writeCDATA(CDATA cdata) throws XMLStreamException { 391 consumer.add(createCharacters(cdata)); 392 } 393 394 402 public Characters createCharacters(CDATA cdata) { 403 return factory.createCData(cdata.getText()); 404 } 405 406 415 public void writeComment(Comment comment) throws XMLStreamException { 416 consumer.add(createComment(comment)); 417 } 418 419 428 public javax.xml.stream.events.Comment createComment(Comment comment) { 429 return factory.createComment(comment.getText()); 430 } 431 432 441 public void writeProcessingInstruction(org.dom4j.ProcessingInstruction pi) 442 throws XMLStreamException { 443 consumer.add(createProcessingInstruction(pi)); 444 } 445 446 457 public ProcessingInstruction createProcessingInstruction( 458 org.dom4j.ProcessingInstruction pi) { 459 String target = pi.getTarget(); 460 String data = pi.getText(); 461 462 return factory.createProcessingInstruction(target, data); 463 } 464 465 474 public void writeEntity(Entity entity) throws XMLStreamException { 475 consumer.add(createEntityReference(entity)); 476 } 477 478 487 private EntityReference createEntityReference(Entity entity) { 488 return factory.createEntityReference(entity.getName(), null); 489 } 490 491 500 public void writeDocumentType(DocumentType docType) 501 throws XMLStreamException { 502 consumer.add(createDTD(docType)); 503 } 504 505 516 public DTD createDTD(DocumentType docType) { 517 StringWriter decl = new StringWriter (); 518 519 try { 520 docType.write(decl); 521 } catch (IOException e) { 522 throw new RuntimeException ("Error writing DTD", e); 523 } 524 525 return factory.createDTD(decl.toString()); 526 } 527 528 538 public void writeDocument(Document doc) throws XMLStreamException { 539 consumer.add(createStartDocument(doc)); 540 541 writeChildNodes(doc); 542 543 consumer.add(createEndDocument(doc)); 544 } 545 546 555 public StartDocument createStartDocument(Document doc) { 556 String encoding = doc.getXMLEncoding(); 557 558 if (encoding != null) { 559 return factory.createStartDocument(encoding); 560 } else { 561 return factory.createStartDocument(); 562 } 563 } 564 565 574 public EndDocument createEndDocument(Document doc) { 575 return factory.createEndDocument(); 576 } 577 578 587 public QName createQName(org.dom4j.QName qname) { 588 return new QName (qname.getNamespaceURI(), qname.getName(), qname 589 .getNamespacePrefix()); 590 } 591 592 596 private class AttributeIterator implements Iterator { 597 598 private Iterator iter; 599 600 public AttributeIterator(Iterator iter) { 601 this.iter = iter; 602 } 603 604 public boolean hasNext() { 605 return iter.hasNext(); 606 } 607 608 public Object next() { 609 Attribute attr = (Attribute) iter.next(); 610 QName attrName = createQName(attr.getQName()); 611 String value = attr.getValue(); 612 613 return factory.createAttribute(attrName, value); 614 } 615 616 public void remove() { 617 throw new UnsupportedOperationException (); 618 } 619 } 620 621 625 private class NamespaceIterator implements Iterator { 626 private Iterator iter; 627 628 public NamespaceIterator(Iterator iter) { 629 this.iter = iter; 630 } 631 632 public boolean hasNext() { 633 return iter.hasNext(); 634 } 635 636 public Object next() { 637 Namespace ns = (Namespace) iter.next(); 638 String prefix = ns.getPrefix(); 639 String nsURI = ns.getURI(); 640 641 return factory.createNamespace(prefix, nsURI); 642 } 643 644 public void remove() { 645 throw new UnsupportedOperationException (); 646 } 647 } 648 } 649 650 686 | Popular Tags |