📄 staxeventreader.java
字号:
/*
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
*
* This software is open source.
* See the bottom of this file for the licence.
*/
package org.dom4j.io;
import java.io.InputStream;
import java.io.Reader;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.Comment;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.EntityReference;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.events.ProcessingInstruction;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.dom4j.CharacterData;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.Entity;
import org.dom4j.Node;
/**
* Reads a DOM4J {@link Document}, as well as other {@link Node}s, from a StAX
* {@link XMLEventReader}.
*
* @author Christian Niles
*/
public class STAXEventReader {
/** Reference to the DocumentFactory used to build DOM4J nodes. */
private DocumentFactory factory;
/** A StAX input factory, used to construct streams from IO streams. */
private XMLInputFactory inputFactory = XMLInputFactory.newInstance();
/**
* Constructs a default <code>STAXEventReader</code> instance with a
* default {@link DocumentFactory}.
*/
public STAXEventReader() {
this.factory = DocumentFactory.getInstance();
}
/**
* Constructs a <code>STAXEventReader</code> instance that uses the
* specified {@link DocumentFactory}to construct DOM4J {@link Node}s.
*
* @param factory
* The DocumentFactory to use when constructing DOM4J nodes, or
* <code>null</code> if a default should be used.
*/
public STAXEventReader(DocumentFactory factory) {
if (factory != null) {
this.factory = factory;
} else {
this.factory = DocumentFactory.getInstance();
}
}
/**
* Sets the DocumentFactory to be used when constructing DOM4J nodes.
*
* @param documentFactory
* The DocumentFactory to use when constructing DOM4J nodes, or
* <code>null</code> if a default should be used.
*/
public void setDocumentFactory(DocumentFactory documentFactory) {
if (documentFactory != null) {
this.factory = documentFactory;
} else {
this.factory = DocumentFactory.getInstance();
}
}
/**
* Constructs a StAX event stream from the provided I/O stream and reads a
* DOM4J document from it.
*
* @param is
* The I/O stream from which the Document will be read.
*
* @return The Document that was read from the stream.
*
* @throws XMLStreamException
* If an error occurs reading content from the stream.
*/
public Document readDocument(InputStream is) throws XMLStreamException {
return readDocument(is, null);
}
/**
* Constructs a StAX event stream from the provided I/O character stream and
* reads a DOM4J document from it.
*
* @param reader
* The character stream from which the Document will be read.
*
* @return The Document that was read from the stream.
*
* @throws XMLStreamException
* If an error occurs reading content from the stream.
*/
public Document readDocument(Reader reader) throws XMLStreamException {
return readDocument(reader, null);
}
/**
* Constructs a StAX event stream from the provided I/O stream and reads a
* DOM4J document from it.
*
* @param is
* The I/O stream from which the Document will be read.
* @param systemId
* A system id used to resolve entities.
*
* @return The Document that was read from the stream.
*
* @throws XMLStreamException
* If an error occurs reading content from the stream.
*/
public Document readDocument(InputStream is, String systemId)
throws XMLStreamException {
XMLEventReader eventReader = inputFactory.createXMLEventReader(
systemId, is);
try {
return readDocument(eventReader);
} finally {
eventReader.close();
}
}
/**
* Constructs a StAX event stream from the provided I/O character stream and
* reads a DOM4J document from it.
*
* @param reader
* The character stream from which the Document will be read.
* @param systemId
* A system id used to resolve entities.
*
* @return The Document that was read from the stream.
*
* @throws XMLStreamException
* If an error occurs reading content from the stream.
*/
public Document readDocument(Reader reader, String systemId)
throws XMLStreamException {
XMLEventReader eventReader = inputFactory.createXMLEventReader(
systemId, reader);
try {
return readDocument(eventReader);
} finally {
eventReader.close();
}
}
/**
* Reads a {@link Node}from the event stream. If the next event is a
* {@link StartElement}, all events until the closing {@link EndElement}
* will be read, and the resulting nodes will be added to the returned
* {@link Element}.
*
* <p>
* <strong>Pre-Conditions </strong>: The stream must be positioned before an
* event other than an <code>EndElement</code>,<code>EndDocument</code>,
* or any DTD-related events, which are not currently supported.
* </p>
*
* @param reader
* The reader from which events will be read.
*
* @return A DOM4J {@link Node}constructed from the read events.
*
* @throws XMLStreamException
* If an error occurs reading from the stream, or the stream was
* positioned before an unsupported event.
*/
public Node readNode(XMLEventReader reader) throws XMLStreamException {
XMLEvent event = reader.peek();
if (event.isStartElement()) {
return readElement(reader);
} else if (event.isCharacters()) {
return readCharacters(reader);
} else if (event.isStartDocument()) {
return readDocument(reader);
} else if (event.isProcessingInstruction()) {
return readProcessingInstruction(reader);
} else if (event.isEntityReference()) {
return readEntityReference(reader);
} else if (event.isAttribute()) {
return readAttribute(reader);
} else if (event.isNamespace()) {
return readNamespace(reader);
} else {
throw new XMLStreamException("Unsupported event: " + event);
}
}
/**
* Reads a DOM4J {@link Document}from the provided stream. The stream
* should be positioned at the start of a document, or before a {@link
* StartElement} event.
*
* @param reader
* The event stream from which to read the {@link Document}.
*
* @return The {@link Document}that was read from the stream.
*
* @throws XMLStreamException
* If an error occurs reading events from the stream.
*/
public Document readDocument(XMLEventReader reader)
throws XMLStreamException {
Document doc = null;
while (reader.hasNext()) {
XMLEvent nextEvent = reader.peek();
int type = nextEvent.getEventType();
switch (type) {
case XMLStreamConstants.START_DOCUMENT:
StartDocument event = (StartDocument) reader.nextEvent();
if (doc == null) {
// create document
if (event.encodingSet()) {
String encodingScheme = event
.getCharacterEncodingScheme();
doc = factory.createDocument(encodingScheme);
} else {
doc = factory.createDocument();
}
} else {
// duplicate or misplaced xml declaration
String msg = "Unexpected StartDocument event";
throw new XMLStreamException(msg, event.getLocation());
}
break;
case XMLStreamConstants.END_DOCUMENT:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CHARACTERS:
// skip end document and space outside the root element
reader.nextEvent();
break;
default:
if (doc == null) {
// create document
doc = factory.createDocument();
}
Node n = readNode(reader);
doc.add(n);
}
}
return doc;
}
/**
* Reads a DOM4J Element from the provided event stream. The stream must be
* positioned before an {@link StartElement}event. In addition to the
* initial start event, all events up to and including the closing {@link
* EndElement} will be read, and included with the returned element.
*
* @param eventReader
* The event stream from which to read the Element.
*
* @return The Element that was read from the stream.
*
* @throws XMLStreamException
* If an error occured reading events from the stream, or the
* stream was not positioned before a {@linkStartElement}event.
*/
public Element readElement(XMLEventReader eventReader)
throws XMLStreamException {
XMLEvent event = eventReader.peek();
if (event.isStartElement()) {
// advance the reader and get the StartElement event
StartElement startTag = eventReader.nextEvent().asStartElement();
Element elem = createElement(startTag);
// read element content
while (true) {
if (!eventReader.hasNext()) {
String msg = "Unexpected end of stream while reading"
+ " element content";
throw new XMLStreamException(msg);
}
XMLEvent nextEvent = eventReader.peek();
if (nextEvent.isEndElement()) {
EndElement endElem = eventReader.nextEvent().asEndElement();
if (!endElem.getName().equals(startTag.getName())) {
throw new XMLStreamException("Expected "
+ startTag.getName() + " end-tag, but found"
+ endElem.getName());
}
break;
}
Node child = readNode(eventReader);
elem.add(child);
}
return elem;
} else {
throw new XMLStreamException("Expected Element event, found: "
+ event);
}
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -