📄 saxwriter.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.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Branch;
import org.dom4j.CDATA;
import org.dom4j.CharacterData;
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.ProcessingInstruction;
import org.dom4j.Text;
import org.dom4j.tree.NamespaceStack;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.LocatorImpl;
/**
* <p>
* <code>SAXWriter</code> writes a DOM4J tree to a SAX ContentHandler.
* </p>
*
* @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
* @version $Revision: 1.24 $
*/
public class SAXWriter implements XMLReader {
protected static final String[] LEXICAL_HANDLER_NAMES = {
"http://xml.org/sax/properties/lexical-handler",
"http://xml.org/sax/handlers/LexicalHandler" };
protected static final String FEATURE_NAMESPACE_PREFIXES
= "http://xml.org/sax/features/namespace-prefixes";
protected static final String FEATURE_NAMESPACES
= "http://xml.org/sax/features/namespaces";
/** <code>ContentHandler</code> to which SAX events are raised */
private ContentHandler contentHandler;
/** <code>DTDHandler</code> fired when a document has a DTD */
private DTDHandler dtdHandler;
/** <code>EntityResolver</code> fired when a document has a DTD */
private EntityResolver entityResolver;
private ErrorHandler errorHandler;
/** <code>LexicalHandler</code> fired on Entity and CDATA sections */
private LexicalHandler lexicalHandler;
/** <code>AttributesImpl</code> used when generating the Attributes */
private AttributesImpl attributes = new AttributesImpl();
/** Stores the features */
private Map features = new HashMap();
/** Stores the properties */
private Map properties = new HashMap();
/** Whether namespace declarations are exported as attributes or not */
private boolean declareNamespaceAttributes;
public SAXWriter() {
properties.put(FEATURE_NAMESPACE_PREFIXES, Boolean.FALSE);
properties.put(FEATURE_NAMESPACE_PREFIXES, Boolean.TRUE);
}
public SAXWriter(ContentHandler contentHandler) {
this();
this.contentHandler = contentHandler;
}
public SAXWriter(ContentHandler contentHandler,
LexicalHandler lexicalHandler) {
this();
this.contentHandler = contentHandler;
this.lexicalHandler = lexicalHandler;
}
public SAXWriter(ContentHandler contentHandler,
LexicalHandler lexicalHandler, EntityResolver entityResolver) {
this();
this.contentHandler = contentHandler;
this.lexicalHandler = lexicalHandler;
this.entityResolver = entityResolver;
}
/**
* A polymorphic method to write any Node to this SAX stream
*
* @param node
* DOCUMENT ME!
*
* @throws SAXException
* DOCUMENT ME!
*/
public void write(Node node) throws SAXException {
int nodeType = node.getNodeType();
switch (nodeType) {
case Node.ELEMENT_NODE:
write((Element) node);
break;
case Node.ATTRIBUTE_NODE:
write((Attribute) node);
break;
case Node.TEXT_NODE:
write(node.getText());
break;
case Node.CDATA_SECTION_NODE:
write((CDATA) node);
break;
case Node.ENTITY_REFERENCE_NODE:
write((Entity) node);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
write((ProcessingInstruction) node);
break;
case Node.COMMENT_NODE:
write((Comment) node);
break;
case Node.DOCUMENT_NODE:
write((Document) node);
break;
case Node.DOCUMENT_TYPE_NODE:
write((DocumentType) node);
break;
case Node.NAMESPACE_NODE:
// Will be output with attributes
// write((Namespace) node);
break;
default:
throw new SAXException("Invalid node type: " + node);
}
}
/**
* Generates SAX events for the given Document and all its content
*
* @param document
* is the Document to parse
*
* @throws SAXException
* if there is a SAX error processing the events
*/
public void write(Document document) throws SAXException {
if (document != null) {
checkForNullHandlers();
documentLocator(document);
startDocument();
entityResolver(document);
dtdHandler(document);
writeContent(document, new NamespaceStack());
endDocument();
}
}
/**
* Generates SAX events for the given Element and all its content
*
* @param element
* is the Element to parse
*
* @throws SAXException
* if there is a SAX error processing the events
*/
public void write(Element element) throws SAXException {
write(element, new NamespaceStack());
}
/**
* <p>
* Writes the opening tag of an {@link Element}, including its {@link
* Attribute}s but without its content.
* </p>
*
* @param element
* <code>Element</code> to output.
*
* @throws SAXException
* DOCUMENT ME!
*/
public void writeOpen(Element element) throws SAXException {
startElement(element, null);
}
/**
* <p>
* Writes the closing tag of an {@link Element}
* </p>
*
* @param element
* <code>Element</code> to output.
*
* @throws SAXException
* DOCUMENT ME!
*/
public void writeClose(Element element) throws SAXException {
endElement(element);
}
/**
* Generates SAX events for the given text
*
* @param text
* is the text to send to the SAX ContentHandler
*
* @throws SAXException
* if there is a SAX error processing the events
*/
public void write(String text) throws SAXException {
if (text != null) {
char[] chars = text.toCharArray();
contentHandler.characters(chars, 0, chars.length);
}
}
/**
* Generates SAX events for the given CDATA
*
* @param cdata
* is the CDATA to parse
*
* @throws SAXException
* if there is a SAX error processing the events
*/
public void write(CDATA cdata) throws SAXException {
String text = cdata.getText();
if (lexicalHandler != null) {
lexicalHandler.startCDATA();
write(text);
lexicalHandler.endCDATA();
} else {
write(text);
}
}
/**
* Generates SAX events for the given Comment
*
* @param comment
* is the Comment to parse
*
* @throws SAXException
* if there is a SAX error processing the events
*/
public void write(Comment comment) throws SAXException {
if (lexicalHandler != null) {
String text = comment.getText();
char[] chars = text.toCharArray();
lexicalHandler.comment(chars, 0, chars.length);
}
}
/**
* Generates SAX events for the given Entity
*
* @param entity
* is the Entity to parse
*
* @throws SAXException
* if there is a SAX error processing the events
*/
public void write(Entity entity) throws SAXException {
String text = entity.getText();
if (lexicalHandler != null) {
String name = entity.getName();
lexicalHandler.startEntity(name);
write(text);
lexicalHandler.endEntity(name);
} else {
write(text);
}
}
/**
* Generates SAX events for the given ProcessingInstruction
*
* @param pi
* is the ProcessingInstruction to parse
*
* @throws SAXException
* if there is a SAX error processing the events
*/
public void write(ProcessingInstruction pi) throws SAXException {
String target = pi.getTarget();
String text = pi.getText();
contentHandler.processingInstruction(target, text);
}
/**
* Should namespace declarations be converted to "xmlns" attributes. This
* property defaults to <code>false</code> as per the SAX specification.
* This property is set via the SAX feature
* "http://xml.org/sax/features/namespace-prefixes"
*
* @return DOCUMENT ME!
*/
public boolean isDeclareNamespaceAttributes() {
return declareNamespaceAttributes;
}
/**
* Sets whether namespace declarations should be exported as "xmlns"
* attributes or not. This property is set from the SAX feature
* "http://xml.org/sax/features/namespace-prefixes"
*
* @param declareNamespaceAttrs
* DOCUMENT ME!
*/
public void setDeclareNamespaceAttributes(boolean declareNamespaceAttrs) {
this.declareNamespaceAttributes = declareNamespaceAttrs;
}
// XMLReader methods
// -------------------------------------------------------------------------
/**
* DOCUMENT ME!
*
* @return the <code>ContentHandler</code> called when SAX events are
* raised
*/
public ContentHandler getContentHandler() {
return contentHandler;
}
/**
* Sets the <code>ContentHandler</code> called when SAX events are raised
*
* @param contentHandler
* is the <code>ContentHandler</code> called when SAX events
* are raised
*/
public void setContentHandler(ContentHandler contentHandler) {
this.contentHandler = contentHandler;
}
/**
* DOCUMENT ME!
*
* @return the <code>DTDHandler</code>
*/
public DTDHandler getDTDHandler() {
return dtdHandler;
}
/**
* Sets the <code>DTDHandler</code>.
*
* @param handler
* DOCUMENT ME!
*/
public void setDTDHandler(DTDHandler handler) {
this.dtdHandler = handler;
}
/**
* DOCUMENT ME!
*
* @return the <code>ErrorHandler</code>
*/
public ErrorHandler getErrorHandler() {
return errorHandler;
}
/**
* Sets the <code>ErrorHandler</code>.
*
* @param errorHandler
* DOCUMENT ME!
*/
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
/**
* DOCUMENT ME!
*
* @return the <code>EntityResolver</code> used when a Document contains a
* DTD
*/
public EntityResolver getEntityResolver() {
return entityResolver;
}
/**
* Sets the <code>EntityResolver</code>.
*
* @param entityResolver
* is the <code>EntityResolver</code>
*/
public void setEntityResolver(EntityResolver entityResolver) {
this.entityResolver = entityResolver;
}
/**
* DOCUMENT ME!
*
* @return the <code>LexicalHandler</code> used when a Document contains a
* DTD
*/
public LexicalHandler getLexicalHandler() {
return lexicalHandler;
}
/**
* Sets the <code>LexicalHandler</code>.
*
* @param lexicalHandler
* is the <code>LexicalHandler</code>
*/
public void setLexicalHandler(LexicalHandler lexicalHandler) {
this.lexicalHandler = lexicalHandler;
}
/**
* Sets the <code>XMLReader</code> used to write SAX events to
*
* @param xmlReader
* is the <code>XMLReader</code>
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -