📄 saxoutputter.java
字号:
/*--
$Id: SAXOutputter.java,v 1.40 2007/11/10 05:29:01 jhunter Exp $
Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact <request_AT_jdom_DOT_org>.
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management <request_AT_jdom_DOT_org>.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
This software consists of voluntary contributions made by many
individuals on behalf of the JDOM Project and was originally
created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
on the JDOM Project, please see <http://www.jdom.org/>.
*/
package org.jdom.output;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import org.jdom.*;
import org.xml.sax.*;
import org.xml.sax.ext.*;
import org.xml.sax.helpers.*;
/**
* Outputs a JDOM document as a stream of SAX2 events.
* <p>
* Most ContentHandler callbacks are supported. Both
* <code>ignorableWhitespace()</code> and <code>skippedEntity()</code> have not
* been implemented. The <code>{@link JDOMLocator}</code> class returned by
* <code>{@link #getLocator}</code> exposes the current node being operated
* upon.
* <p>
* At this time, it is not possible to access notations and unparsed entity
* references in a DTD from JDOM. Therefore, <code>DTDHandler</code> callbacks
* have not been implemented yet.
* <p>
* The <code>ErrorHandler</code> callbacks have not been implemented, since
* these are supposed to be invoked when the document is parsed and at this
* point the document exists in memory and is known to have no errors. </p>
*
* @version $Revision: 1.40 $, $Date: 2007/11/10 05:29:01 $
* @author Brett McLaughlin
* @author Jason Hunter
* @author Fred Trimble
* @author Bradley S. Huffman
*/
public class SAXOutputter {
private static final String CVS_ID =
"@(#) $RCSfile: SAXOutputter.java,v $ $Revision: 1.40 $ $Date: 2007/11/10 05:29:01 $ $Name: jdom_1_1 $";
/** Shortcut for SAX namespaces core feature */
private static final String NAMESPACES_SAX_FEATURE =
"http://xml.org/sax/features/namespaces";
/** Shortcut for SAX namespace-prefixes core feature */
private static final String NS_PREFIXES_SAX_FEATURE =
"http://xml.org/sax/features/namespace-prefixes";
/** Shortcut for SAX validation core feature */
private static final String VALIDATION_SAX_FEATURE =
"http://xml.org/sax/features/validation";
/** Shortcut for SAX-ext. lexical handler property */
private static final String LEXICAL_HANDLER_SAX_PROPERTY =
"http://xml.org/sax/properties/lexical-handler";
/** Shortcut for SAX-ext. declaration handler property */
private static final String DECL_HANDLER_SAX_PROPERTY =
"http://xml.org/sax/properties/declaration-handler";
/**
* Shortcut for SAX-ext. lexical handler alternate property.
* Although this property URI is not the one defined by the SAX
* "standard", some parsers use it instead of the official one.
*/
private static final String LEXICAL_HANDLER_ALT_PROPERTY =
"http://xml.org/sax/handlers/LexicalHandler";
/** Shortcut for SAX-ext. declaration handler alternate property */
private static final String DECL_HANDLER_ALT_PROPERTY =
"http://xml.org/sax/handlers/DeclHandler";
/**
* Array to map JDOM attribute type (as entry index) to SAX
* attribute type names.
*/
private static final String[] attrTypeToNameMap = new String[] {
"CDATA", // Attribute.UNDEFINED_ATTRIBUTE, as per SAX 2.0 spec.
"CDATA", // Attribute.CDATA_TYPE
"ID", // Attribute.ID_TYPE
"IDREF", // Attribute.IDREF_TYPE
"IDREFS", // Attribute.IDREFS_TYPE
"ENTITY", // Attribute.ENTITY_TYPE
"ENTITIES", // Attribute.ENTITIES_TYPE
"NMTOKEN", // Attribute.NMTOKEN_TYPE
"NMTOKENS", // Attribute.NMTOKENS_TYPE
"NOTATION", // Attribute.NOTATION_TYPE
"NMTOKEN", // Attribute.ENUMERATED_TYPE, as per SAX 2.0 spec.
};
/** registered <code>ContentHandler</code> */
private ContentHandler contentHandler;
/** registered <code>ErrorHandler</code> */
private ErrorHandler errorHandler;
/** registered <code>DTDHandler</code> */
private DTDHandler dtdHandler;
/** registered <code>EntityResolver</code> */
private EntityResolver entityResolver;
/** registered <code>LexicalHandler</code> */
private LexicalHandler lexicalHandler;
/** registered <code>DeclHandler</code> */
private DeclHandler declHandler;
/**
* Whether to report attribute namespace declarations as xmlns attributes.
* Defaults to <code>false</code> as per SAX specifications.
*
* @see <a href="http://www.megginson.com/SAX/Java/namespaces.html">
* SAX namespace specifications</a>
*/
private boolean declareNamespaces = false;
/**
* Whether to report DTD events to DeclHandlers and LexicalHandlers.
* Defaults to <code>true</code>.
*/
private boolean reportDtdEvents = true;
/**
* A SAX Locator that points at the JDOM node currently being
* outputted.
*/
private JDOMLocator locator = null;
/**
* This will create a <code>SAXOutputter</code> without any
* registered handler. The application is then responsible for
* registering them using the <code>setXxxHandler()</code> methods.
*/
public SAXOutputter() {
}
/**
* This will create a <code>SAXOutputter</code> with the
* specified <code>ContentHandler</code>.
*
* @param contentHandler contains <code>ContentHandler</code>
* callback methods
*/
public SAXOutputter(ContentHandler contentHandler) {
this(contentHandler, null, null, null, null);
}
/**
* This will create a <code>SAXOutputter</code> with the
* specified SAX2 handlers. At this time, only <code>ContentHandler</code>
* and <code>EntityResolver</code> are supported.
*
* @param contentHandler contains <code>ContentHandler</code>
* callback methods
* @param errorHandler contains <code>ErrorHandler</code> callback methods
* @param dtdHandler contains <code>DTDHandler</code> callback methods
* @param entityResolver contains <code>EntityResolver</code>
* callback methods
*/
public SAXOutputter(ContentHandler contentHandler,
ErrorHandler errorHandler,
DTDHandler dtdHandler,
EntityResolver entityResolver) {
this(contentHandler, errorHandler, dtdHandler, entityResolver, null);
}
/**
* This will create a <code>SAXOutputter</code> with the
* specified SAX2 handlers. At this time, only <code>ContentHandler</code>
* and <code>EntityResolver</code> are supported.
*
* @param contentHandler contains <code>ContentHandler</code>
* callback methods
* @param errorHandler contains <code>ErrorHandler</code> callback methods
* @param dtdHandler contains <code>DTDHandler</code> callback methods
* @param entityResolver contains <code>EntityResolver</code>
* callback methods
* @param lexicalHandler contains <code>LexicalHandler</code> callbacks.
*/
public SAXOutputter(ContentHandler contentHandler,
ErrorHandler errorHandler,
DTDHandler dtdHandler,
EntityResolver entityResolver,
LexicalHandler lexicalHandler) {
this.contentHandler = contentHandler;
this.errorHandler = errorHandler;
this.dtdHandler = dtdHandler;
this.entityResolver = entityResolver;
this.lexicalHandler = lexicalHandler;
}
/**
* This will set the <code>ContentHandler</code>.
*
* @param contentHandler contains <code>ContentHandler</code>
* callback methods.
*/
public void setContentHandler(ContentHandler contentHandler) {
this.contentHandler = contentHandler;
}
/**
* Returns the registered <code>ContentHandler</code>.
*
* @return the current <code>ContentHandler</code> or
* <code>null</code> if none was registered.
*/
public ContentHandler getContentHandler() {
return this.contentHandler;
}
/**
* This will set the <code>ErrorHandler</code>.
*
* @param errorHandler contains <code>ErrorHandler</code> callback methods.
*/
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
/**
* Return the registered <code>ErrorHandler</code>.
*
* @return the current <code>ErrorHandler</code> or
* <code>null</code> if none was registered.
*/
public ErrorHandler getErrorHandler() {
return this.errorHandler;
}
/**
* This will set the <code>DTDHandler</code>.
*
* @param dtdHandler contains <code>DTDHandler</code> callback methods.
*/
public void setDTDHandler(DTDHandler dtdHandler) {
this.dtdHandler = dtdHandler;
}
/**
* Return the registered <code>DTDHandler</code>.
*
* @return the current <code>DTDHandler</code> or
* <code>null</code> if none was registered.
*/
public DTDHandler getDTDHandler() {
return this.dtdHandler;
}
/**
* This will set the <code>EntityResolver</code>.
*
* @param entityResolver contains EntityResolver callback methods.
*/
public void setEntityResolver(EntityResolver entityResolver) {
this.entityResolver = entityResolver;
}
/**
* Return the registered <code>EntityResolver</code>.
*
* @return the current <code>EntityResolver</code> or
* <code>null</code> if none was registered.
*/
public EntityResolver getEntityResolver() {
return this.entityResolver;
}
/**
* This will set the <code>LexicalHandler</code>.
*
* @param lexicalHandler contains lexical callback methods.
*/
public void setLexicalHandler(LexicalHandler lexicalHandler) {
this.lexicalHandler = lexicalHandler;
}
/**
* Return the registered <code>LexicalHandler</code>.
*
* @return the current <code>LexicalHandler</code> or
* <code>null</code> if none was registered.
*/
public LexicalHandler getLexicalHandler() {
return this.lexicalHandler;
}
/**
* This will set the <code>DeclHandler</code>.
*
* @param declHandler contains declaration callback methods.
*/
public void setDeclHandler(DeclHandler declHandler) {
this.declHandler = declHandler;
}
/**
* Return the registered <code>DeclHandler</code>.
*
* @return the current <code>DeclHandler</code> or
* <code>null</code> if none was registered.
*/
public DeclHandler getDeclHandler() {
return this.declHandler;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -