📄 staxeventwriter.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.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.DTD;
import javax.xml.stream.events.EndDocument;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.EntityReference;
import javax.xml.stream.events.ProcessingInstruction;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.util.XMLEventConsumer;
import org.dom4j.Attribute;
import org.dom4j.Branch;
import org.dom4j.CDATA;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.Entity;
import org.dom4j.Namespace;
import org.dom4j.Node;
import org.dom4j.Text;
/**
* Writes DOM4J {@link Node}s to a StAX event stream. In addition the
* <code>createXXX</code> methods are provided to directly create STAX events
* from DOM4J nodes.
*
* @author Christian Niles
*/
public class STAXEventWriter {
/** The event stream to which events are written. */
private XMLEventConsumer consumer;
/** The event factory used to construct events. */
private XMLEventFactory factory = XMLEventFactory.newInstance();
private XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
public STAXEventWriter() {
}
/**
* Constructs a <code>STAXEventWriter</code> that writes events to the
* provided file.
*
* @param file
* The file to which events will be written.
*
* @throws XMLStreamException
* If an error occurs creating an event writer from the file.
* @throws IOException
* If an error occurs openin the file for writing.
*/
public STAXEventWriter(File file) throws XMLStreamException, IOException {
consumer = outputFactory.createXMLEventWriter(new FileWriter(file));
}
/**
* Constructs a <code>STAXEventWriter</code> that writes events to the
* provided character stream.
*
* @param writer
* The character stream to which events will be written.
*
* @throws XMLStreamException
* If an error occurs constructing an event writer from the
* character stream.
*/
public STAXEventWriter(Writer writer) throws XMLStreamException {
consumer = outputFactory.createXMLEventWriter(writer);
}
/**
* Constructs a <code>STAXEventWriter</code> that writes events to the
* provided stream.
*
* @param stream
* The output stream to which events will be written.
*
* @throws XMLStreamException
* If an error occurs constructing an event writer from the
* stream.
*/
public STAXEventWriter(OutputStream stream) throws XMLStreamException {
consumer = outputFactory.createXMLEventWriter(stream);
}
/**
* Constructs a <code>STAXEventWriter</code> that writes events to the
* provided event stream.
*
* @param consumer
* The event stream to which events will be written.
*/
public STAXEventWriter(XMLEventConsumer consumer) {
this.consumer = consumer;
}
/**
* Returns a reference to the underlying event consumer to which events are
* written.
*
* @return The underlying event consumer to which events are written.
*/
public XMLEventConsumer getConsumer() {
return consumer;
}
/**
* Sets the underlying event consumer to which events are written.
*
* @param consumer
* The event consumer to which events should be written.
*/
public void setConsumer(XMLEventConsumer consumer) {
this.consumer = consumer;
}
/**
* Returns a reference to the event factory used to construct STAX events.
*
* @return The event factory used to construct STAX events.
*/
public XMLEventFactory getEventFactory() {
return factory;
}
/**
* Sets the event factory used to construct STAX events.
*
* @param eventFactory
* The new event factory.
*/
public void setEventFactory(XMLEventFactory eventFactory) {
this.factory = eventFactory;
}
/**
* Writes a DOM4J {@link Node}to the stream. This method is simply a
* gateway to the overloaded methods such as {@link#writeElement(Element)}.
*
* @param n
* The DOM4J {@link Node}to write to the stream.
*
* @throws XMLStreamException
* If an error occurs writing to the stream.
*/
public void writeNode(Node n) throws XMLStreamException {
switch (n.getNodeType()) {
case Node.ELEMENT_NODE:
writeElement((Element) n);
break;
case Node.TEXT_NODE:
writeText((Text) n);
break;
case Node.ATTRIBUTE_NODE:
writeAttribute((Attribute) n);
break;
case Node.NAMESPACE_NODE:
writeNamespace((Namespace) n);
break;
case Node.COMMENT_NODE:
writeComment((Comment) n);
break;
case Node.CDATA_SECTION_NODE:
writeCDATA((CDATA) n);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
writeProcessingInstruction((org.dom4j.ProcessingInstruction) n);
break;
case Node.ENTITY_REFERENCE_NODE:
writeEntity((Entity) n);
break;
case Node.DOCUMENT_NODE:
writeDocument((Document) n);
break;
case Node.DOCUMENT_TYPE_NODE:
writeDocumentType((DocumentType) n);
break;
default:
throw new XMLStreamException("Unsupported DOM4J Node: " + n);
}
}
/**
* Writes each child node within the provided {@link Branch}instance. This
* method simply iterates through the {@link Branch}'s nodes and calls
* {@link #writeNode(Node)}.
*
* @param branch
* The node whose children will be written to the stream.
*
* @throws XMLStreamException
* If an error occurs writing to the stream.
*/
public void writeChildNodes(Branch branch) throws XMLStreamException {
for (int i = 0, s = branch.nodeCount(); i < s; i++) {
Node n = branch.node(i);
writeNode(n);
}
}
/**
* Writes a DOM4J {@link Element}node and its children to the stream.
*
* @param elem
* The {@link Element}node to write to the stream.
*
* @throws XMLStreamException
* If an error occurs writing to the stream.
*/
public void writeElement(Element elem) throws XMLStreamException {
consumer.add(createStartElement(elem));
writeChildNodes(elem);
consumer.add(createEndElement(elem));
}
/**
* Constructs a STAX {@link StartElement}event from a DOM4J {@link
* Element}.
*
* @param elem
* The {@link Element}from which to construct the event.
*
* @return The newly constructed {@link StartElement}event.
*/
public StartElement createStartElement(Element elem) {
// create name
QName tagName = createQName(elem.getQName());
// create attribute & namespace iterators
Iterator attrIter = new AttributeIterator(elem.attributeIterator());
Iterator nsIter = new NamespaceIterator(elem.declaredNamespaces()
.iterator());
// create start event
return factory.createStartElement(tagName, attrIter, nsIter);
}
/**
* Constructs a STAX {@link EndElement}event from a DOM4J {@link Element}.
*
* @param elem
* The {@link Element}from which to construct the event.
*
* @return The newly constructed {@link EndElement}event.
*/
public EndElement createEndElement(Element elem) {
QName tagName = createQName(elem.getQName());
Iterator nsIter = new NamespaceIterator(elem.declaredNamespaces()
.iterator());
return factory.createEndElement(tagName, nsIter);
}
/**
* Writes a DOM4J {@link Attribute}to the stream.
*
* @param attr
* The {@link Attribute}to write to the stream.
*
* @throws XMLStreamException
* If an error occurs writing to the stream.
*/
public void writeAttribute(Attribute attr) throws XMLStreamException {
consumer.add(createAttribute(attr));
}
/**
* Constructs a STAX {@link javax.xml.stream.events.Attribute}event from a
* DOM4J {@link Attribute}.
*
* @param attr
* The {@link Attribute}from which to construct the event.
*
* @return The newly constructed {@link javax.xml.stream.events.Attribute}
* event.
*/
public javax.xml.stream.events.Attribute createAttribute(Attribute attr) {
QName attrName = createQName(attr.getQName());
String value = attr.getValue();
return factory.createAttribute(attrName, value);
}
/**
* Writes a DOM4J {@link Namespace}to the stream.
*
* @param ns
* The {@link Namespace}to write to the stream.
*
* @throws XMLStreamException
* If an error occurs writing to the stream.
*/
public void writeNamespace(Namespace ns) throws XMLStreamException {
consumer.add(createNamespace(ns));
}
/**
* Constructs a STAX {@link javax.xml.stream.events.Namespace}event from a
* DOM4J {@link Namespace}.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -