📄 saxwriter.java
字号:
/* * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. * * This software is open source. * See the bottom of this file for the licence. * * $Id: SAXWriter.java,v 1.4 2003/11/02 18:04:59 per_nyfelt Exp $ */package org.dom4j.io;import org.dom4j.*;import org.dom4j.CharacterData;import org.dom4j.tree.AbstractNamespace;import org.dom4j.tree.NamespaceStack;import org.xml.sax.*;import org.xml.sax.ext.LexicalHandler;import org.xml.sax.helpers.AttributesImpl;import org.xml.sax.helpers.LocatorImpl;import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;/** <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.4 $ */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 String FEATURE_NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes"; protected static 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 */ 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((AbstractNamespace) 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. */ 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. */ 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" */ 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" */ public void setDeclareNamespaceAttributes(boolean declareNamespaceAttributes) { this.declareNamespaceAttributes = declareNamespaceAttributes; } // XMLReader methods //------------------------------------------------------------------------- /** @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; } /** @return the <code>DTDHandler</code> */ public DTDHandler getDTDHandler() { return dtdHandler; } /** Sets the <code>DTDHandler</code>. */ public void setDTDHandler(DTDHandler dtdHandler) { this.dtdHandler = dtdHandler; } /** @return the <code>ErrorHandler</code> */ public ErrorHandler getErrorHandler() { return errorHandler; } /** Sets the <code>ErrorHandler</code>. */ public void setErrorHandler(ErrorHandler errorHandler) { this.errorHandler = errorHandler; } /** @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; } /** @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> */ public void setXMLReader(XMLReader xmlReader) { setContentHandler( xmlReader.getContentHandler() ); setDTDHandler( xmlReader.getDTDHandler() );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -