1 7 8 package org.dom4j.io; 9 10 import java.io.InputStream ; 11 import java.io.Reader ; 12 import java.util.Iterator ; 13 14 import javax.xml.namespace.QName ; 15 import javax.xml.stream.XMLEventReader; 16 import javax.xml.stream.XMLInputFactory; 17 import javax.xml.stream.XMLStreamConstants; 18 import javax.xml.stream.XMLStreamException; 19 import javax.xml.stream.events.Attribute; 20 import javax.xml.stream.events.Characters; 21 import javax.xml.stream.events.Comment; 22 import javax.xml.stream.events.EndElement; 23 import javax.xml.stream.events.EntityReference; 24 import javax.xml.stream.events.Namespace; 25 import javax.xml.stream.events.ProcessingInstruction; 26 import javax.xml.stream.events.StartDocument; 27 import javax.xml.stream.events.StartElement; 28 import javax.xml.stream.events.XMLEvent; 29 30 import org.dom4j.CharacterData; 31 import org.dom4j.Document; 32 import org.dom4j.DocumentFactory; 33 import org.dom4j.Element; 34 import org.dom4j.Entity; 35 import org.dom4j.Node; 36 37 43 public class STAXEventReader { 44 45 private DocumentFactory factory; 46 47 48 private XMLInputFactory inputFactory = XMLInputFactory.newInstance(); 49 50 54 public STAXEventReader() { 55 this.factory = DocumentFactory.getInstance(); 56 } 57 58 66 public STAXEventReader(DocumentFactory factory) { 67 if (factory != null) { 68 this.factory = factory; 69 } else { 70 this.factory = DocumentFactory.getInstance(); 71 } 72 } 73 74 81 public void setDocumentFactory(DocumentFactory documentFactory) { 82 if (documentFactory != null) { 83 this.factory = documentFactory; 84 } else { 85 this.factory = DocumentFactory.getInstance(); 86 } 87 } 88 89 101 public Document readDocument(InputStream is) throws XMLStreamException { 102 return readDocument(is, null); 103 } 104 105 117 public Document readDocument(Reader reader) throws XMLStreamException { 118 return readDocument(reader, null); 119 } 120 121 135 public Document readDocument(InputStream is, String systemId) 136 throws XMLStreamException { 137 XMLEventReader eventReader = inputFactory.createXMLEventReader( 138 systemId, is); 139 140 try { 141 return readDocument(eventReader); 142 } finally { 143 eventReader.close(); 144 } 145 } 146 147 161 public Document readDocument(Reader reader, String systemId) 162 throws XMLStreamException { 163 XMLEventReader eventReader = inputFactory.createXMLEventReader( 164 systemId, reader); 165 166 try { 167 return readDocument(eventReader); 168 } finally { 169 eventReader.close(); 170 } 171 } 172 173 194 public Node readNode(XMLEventReader reader) throws XMLStreamException { 195 XMLEvent event = reader.peek(); 196 197 if (event.isStartElement()) { 198 return readElement(reader); 199 } else if (event.isCharacters()) { 200 return readCharacters(reader); 201 } else if (event.isStartDocument()) { 202 return readDocument(reader); 203 } else if (event.isProcessingInstruction()) { 204 return readProcessingInstruction(reader); 205 } else if (event.isEntityReference()) { 206 return readEntityReference(reader); 207 } else if (event.isAttribute()) { 208 return readAttribute(reader); 209 } else if (event.isNamespace()) { 210 return readNamespace(reader); 211 } else { 212 throw new XMLStreamException("Unsupported event: " + event); 213 } 214 } 215 216 229 public Document readDocument(XMLEventReader reader) 230 throws XMLStreamException { 231 Document doc = null; 232 233 while (reader.hasNext()) { 234 XMLEvent nextEvent = reader.peek(); 235 int type = nextEvent.getEventType(); 236 237 switch (type) { 238 case XMLStreamConstants.START_DOCUMENT: 239 240 StartDocument event = (StartDocument) reader.nextEvent(); 241 242 if (doc == null) { 243 if (event.encodingSet()) { 245 String encodingScheme = event 246 .getCharacterEncodingScheme(); 247 doc = factory.createDocument(encodingScheme); 248 } else { 249 doc = factory.createDocument(); 250 } 251 } else { 252 String msg = "Unexpected StartDocument event"; 254 throw new XMLStreamException(msg, event.getLocation()); 255 } 256 257 break; 258 259 case XMLStreamConstants.END_DOCUMENT: 260 case XMLStreamConstants.SPACE: 261 case XMLStreamConstants.CHARACTERS: 262 263 reader.nextEvent(); 265 266 break; 267 268 default: 269 270 if (doc == null) { 271 doc = factory.createDocument(); 273 } 274 275 Node n = readNode(reader); 276 doc.add(n); 277 } 278 } 279 280 return doc; 281 } 282 283 298 public Element readElement(XMLEventReader eventReader) 299 throws XMLStreamException { 300 XMLEvent event = eventReader.peek(); 301 302 if (event.isStartElement()) { 303 StartElement startTag = eventReader.nextEvent().asStartElement(); 305 Element elem = createElement(startTag); 306 307 while (true) { 309 if (!eventReader.hasNext()) { 310 String msg = "Unexpected end of stream while reading" 311 + " element content"; 312 throw new XMLStreamException(msg); 313 } 314 315 XMLEvent nextEvent = eventReader.peek(); 316 317 if (nextEvent.isEndElement()) { 318 EndElement endElem = eventReader.nextEvent().asEndElement(); 319 320 if (!endElem.getName().equals(startTag.getName())) { 321 throw new XMLStreamException("Expected " 322 + startTag.getName() + " end-tag, but found" 323 + endElem.getName()); 324 } 325 326 break; 327 } 328 329 Node child = readNode(eventReader); 330 elem.add(child); 331 } 332 333 return elem; 334 } else { 335 throw new XMLStreamException("Expected Element event, found: " 336 + event); 337 } 338 } 339 340 353 public org.dom4j.Attribute readAttribute(XMLEventReader reader) 354 throws XMLStreamException { 355 XMLEvent event = reader.peek(); 356 357 if (event.isAttribute()) { 358 Attribute attr = (Attribute) reader.nextEvent(); 359 360 return createAttribute(null, attr); 361 } else { 362 throw new XMLStreamException("Expected Attribute event, found: " 363 + event); 364 } 365 } 366 367 380 public org.dom4j.Namespace readNamespace(XMLEventReader reader) 381 throws XMLStreamException { 382 XMLEvent event = reader.peek(); 383 384 if (event.isNamespace()) { 385 Namespace ns = (Namespace) reader.nextEvent(); 386 387 return createNamespace(ns); 388 } else { 389 throw new XMLStreamException("Expected Namespace event, found: " 390 + event); 391 } 392 } 393 394 407 public CharacterData readCharacters(XMLEventReader reader) 408 throws XMLStreamException { 409 XMLEvent event = reader.peek(); 410 411 if (event.isCharacters()) { 412 Characters characters = reader.nextEvent().asCharacters(); 413 414 return createCharacterData(characters); 415 } else { 416 throw new XMLStreamException("Expected Characters event, found: " 417 + event); 418 } 419 } 420 421 434 public org.dom4j.Comment readComment(XMLEventReader reader) 435 throws XMLStreamException { 436 XMLEvent event = reader.peek(); 437 438 if (event instanceof Comment) { 439 return createComment((Comment) reader.nextEvent()); 440 } else { 441 throw new XMLStreamException("Expected Comment event, found: " 442 + event); 443 } 444 } 445 446 461 public Entity readEntityReference(XMLEventReader reader) 462 throws XMLStreamException { 463 XMLEvent event = reader.peek(); 464 465 if (event.isEntityReference()) { 466 EntityReference entityRef = (EntityReference) reader.nextEvent(); 467 468 return createEntity(entityRef); 469 } else { 470 throw new XMLStreamException("Expected EntityRef event, found: " 471 + event); 472 } 473 } 474 475 490 public org.dom4j.ProcessingInstruction readProcessingInstruction( 491 XMLEventReader reader) throws XMLStreamException { 492 XMLEvent event = reader.peek(); 493 494 if (event.isProcessingInstruction()) { 495 ProcessingInstruction pi = (ProcessingInstruction) reader 496 .nextEvent(); 497 498 return createProcessingInstruction(pi); 499 } else { 500 throw new XMLStreamException("Expected PI event, found: " + event); 501 } 502 } 503 504 514 public Element createElement(StartElement startEvent) { 515 QName qname = startEvent.getName(); 516 org.dom4j.QName elemName = createQName(qname); 517 518 Element elem = factory.createElement(elemName); 519 520 for (Iterator i = startEvent.getAttributes(); i.hasNext();) { 522 Attribute attr = (Attribute) i.next(); 523 elem.addAttribute(createQName(attr.getName()), attr.getValue()); 524 } 525 526 for (Iterator i = startEvent.getNamespaces(); i.hasNext();) { 528 Namespace ns = (Namespace) i.next(); 529 elem.addNamespace(ns.getPrefix(), ns.getNamespaceURI()); 530 } 531 532 return elem; 533 } 534 535 546 public org.dom4j.Attribute createAttribute(Element elem, Attribute attr) { 547 return factory.createAttribute(elem, createQName(attr.getName()), attr 548 .getValue()); 549 } 550 551 560 public org.dom4j.Namespace createNamespace(Namespace ns) { 561 return factory.createNamespace(ns.getPrefix(), ns.getNamespaceURI()); 562 } 563 564 575 public CharacterData createCharacterData(Characters characters) { 576 String data = characters.getData(); 577 578 if (characters.isCData()) { 579 return factory.createCDATA(data); 580 } else { 581 return factory.createText(data); 582 } 583 } 584 585 594 public org.dom4j.Comment createComment(Comment comment) { 595 return factory.createComment(comment.getText()); 596 } 597 598 608 public org.dom4j.Entity createEntity(EntityReference entityRef) { 609 return factory.createEntity(entityRef.getName(), entityRef 610 .getDeclaration().getReplacementText()); 611 } 612 613 624 public org.dom4j.ProcessingInstruction createProcessingInstruction( 625 ProcessingInstruction pi) { 626 return factory 627 .createProcessingInstruction(pi.getTarget(), pi.getData()); 628 } 629 630 638 public org.dom4j.QName createQName(QName qname) { 639 return factory.createQName(qname.getLocalPart(), qname.getPrefix(), 640 qname.getNamespaceURI()); 641 } 642 } 643 644 680 | Popular Tags |