toxmlsaxhandler.java

来自「JAVA 所有包」· Java 代码 · 共 775 行 · 第 1/2 页

JAVA
775
字号
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: ToXMLSAXHandler.java,v 1.3 2005/09/28 13:49:08 pvedula Exp $ */ package com.sun.org.apache.xml.internal.serializer;import java.io.IOException;import java.io.OutputStream;import java.io.Writer;import java.util.Properties;import javax.xml.transform.Result;import org.w3c.dom.Node;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.ext.LexicalHandler;/** * This class receives notification of SAX-like events, and with gathered * information over these calls it will invoke the equivalent SAX methods * on a handler, the ultimate xsl:output method is known to be "xml". *  * This class is not a public API, it is only public because it is used by Xalan. * @xsl.usage internal */public final class ToXMLSAXHandler extends ToSAXHandler{    /**     * Keeps track of whether output escaping is currently enabled     */    protected boolean m_escapeSetting = false;    public ToXMLSAXHandler()    {        // default constructor (need to set content handler ASAP !)        m_prefixMap = new NamespaceMappings();        initCDATA();    }    /**     * @see Serializer#getOutputFormat()     */    public Properties getOutputFormat()    {        return null;    }    /**     * @see Serializer#getOutputStream()     */    public OutputStream getOutputStream()    {        return null;    }    /**     * @see Serializer#getWriter()     */    public Writer getWriter()    {        return null;    }    /**     * Do nothing for SAX.     */    public void indent(int n) throws SAXException    {    }    /**     * @see DOMSerializer#serialize(Node)     */    public void serialize(Node node) throws IOException    {    }    /**     * @see SerializationHandler#setEscaping(boolean)     */    public boolean setEscaping(boolean escape) throws SAXException    {        boolean oldEscapeSetting = m_escapeSetting;        m_escapeSetting = escape;        if (escape) {            processingInstruction(Result.PI_ENABLE_OUTPUT_ESCAPING, "");        } else {            processingInstruction(Result.PI_DISABLE_OUTPUT_ESCAPING, "");        }        return oldEscapeSetting;    }    /**     * @see Serializer#setOutputFormat(Properties)     */    public void setOutputFormat(Properties format)    {    }    /**     * @see Serializer#setOutputStream(OutputStream)     */    public void setOutputStream(OutputStream output)    {    }    /**     * @see Serializer#setWriter(Writer)     */    public void setWriter(Writer writer)    {    }    /**     * @see org.xml.sax.ext.DeclHandler#attributeDecl(String, String, String, String, String)     */    public void attributeDecl(        String arg0,        String arg1,        String arg2,        String arg3,        String arg4)        throws SAXException    {    }    /**     * @see org.xml.sax.ext.DeclHandler#elementDecl(String, String)     */    public void elementDecl(String arg0, String arg1) throws SAXException    {    }    /**     * @see org.xml.sax.ext.DeclHandler#externalEntityDecl(String, String, String)     */    public void externalEntityDecl(String arg0, String arg1, String arg2)        throws SAXException    {    }    /**     * @see org.xml.sax.ext.DeclHandler#internalEntityDecl(String, String)     */    public void internalEntityDecl(String arg0, String arg1)        throws SAXException    {    }    /**     * Receives notification of the end of the document.     * @see org.xml.sax.ContentHandler#endDocument()     */    public void endDocument() throws SAXException    {        flushPending();        // Close output document        m_saxHandler.endDocument();        if (m_tracer != null)            super.fireEndDoc();    }    /**     * This method is called when all the data needed for a call to the     * SAX handler's startElement() method has been gathered.     */    protected void closeStartTag() throws SAXException    {        m_elemContext.m_startTagOpen = false;        final String localName = getLocalName(m_elemContext.m_elementName);        final String uri = getNamespaceURI(m_elemContext.m_elementName, true);        // Now is time to send the startElement event        if (m_needToCallStartDocument)        {            startDocumentInternal();        }        m_saxHandler.startElement(uri, localName, m_elemContext.m_elementName, m_attributes);        // we've sent the official SAX attributes on their way,        // now we don't need them anymore.        m_attributes.clear();        if(m_state != null)          m_state.setCurrentNode(null);    }    /**     * Closes ane open cdata tag, and     * unlike the this.endCDATA() method (from the LexicalHandler) interface,     * this "internal" method will send the endCDATA() call to the wrapped     * handler.     *      */    public void closeCDATA() throws SAXException    {        // Output closing bracket - "]]>"        if (m_lexHandler != null && m_cdataTagOpen) {            m_lexHandler.endCDATA();        }                // There are no longer any calls made to         // m_lexHandler.startCDATA() without a balancing call to        // m_lexHandler.endCDATA()        // so we set m_cdataTagOpen to false to remember this.        m_cdataTagOpen = false;            }    /**     * @see org.xml.sax.ContentHandler#endElement(String, String, String)     */    public void endElement(String namespaceURI, String localName, String qName)        throws SAXException    {        // Close any open elements etc.        flushPending();                if (namespaceURI == null)        {            if (m_elemContext.m_elementURI != null)                namespaceURI = m_elemContext.m_elementURI;            else                namespaceURI = getNamespaceURI(qName, true);        }                if (localName == null)        {            if (m_elemContext.m_elementLocalName != null)                localName = m_elemContext.m_elementLocalName;            else                localName = getLocalName(qName);        }        m_saxHandler.endElement(namespaceURI, localName, qName);        if (m_tracer != null)            super.fireEndElem(qName);               /* Pop all namespaces at the current element depth.         * We are not waiting for official endPrefixMapping() calls.         */        m_prefixMap.popNamespaces(m_elemContext.m_currentElemDepth,            m_saxHandler);        m_elemContext = m_elemContext.m_prev;    }    /**     * @see org.xml.sax.ContentHandler#endPrefixMapping(String)     */    public void endPrefixMapping(String prefix) throws SAXException    {        /* poping all prefix mappings should have been done         * in endElement() already         */         return;    }    /**     * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)     */    public void ignorableWhitespace(char[] arg0, int arg1, int arg2)        throws SAXException    {        m_saxHandler.ignorableWhitespace(arg0,arg1,arg2);    }    /**     * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator)     */    public void setDocumentLocator(Locator arg0)    {        super.setDocumentLocator(arg0);        m_saxHandler.setDocumentLocator(arg0);    }    /**     * @see org.xml.sax.ContentHandler#skippedEntity(String)     */    public void skippedEntity(String arg0) throws SAXException    {        m_saxHandler.skippedEntity(arg0);    }    /**     * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)     * @param prefix The prefix that maps to the URI     * @param uri The URI for the namespace     */    public void startPrefixMapping(String prefix, String uri)        throws SAXException    {       startPrefixMapping(prefix, uri, true);    }    /**     * Remember the prefix/uri mapping at the current nested element depth.     *     * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)     * @param prefix The prefix that maps to the URI     * @param uri The URI for the namespace     * @param shouldFlush a flag indicating if the mapping applies to the     * current element or an up coming child (not used).     */    public boolean startPrefixMapping(        String prefix,        String uri,        boolean shouldFlush)        throws org.xml.sax.SAXException    {        /* Remember the mapping, and at what depth it was declared         * This is one greater than the current depth because these         * mappings will apply to the next depth. This is in         * consideration that startElement() will soon be called         */        boolean pushed;        int pushDepth;        if (shouldFlush)        {            flushPending();            // the prefix mapping applies to the child element (one deeper)            pushDepth = m_elemContext.m_currentElemDepth + 1;        }        else        {            // the prefix mapping applies to the current element            pushDepth = m_elemContext.m_currentElemDepth;        }        pushed = m_prefixMap.pushNamespace(prefix, uri, pushDepth);        if (pushed)        {            m_saxHandler.startPrefixMapping(prefix,uri);                        if (getShouldOutputNSAttr())             {	              /* Brian M.: don't know if we really needto do this. The	               * callers of this object should have injected both	               * startPrefixMapping and the attributes.  We are	               * just covering our butt here.	               */	              String name;  	            if (EMPTYSTRING.equals(prefix))  	            {  	                name = "xmlns";  	                addAttributeAlways(XMLNS_URI, name, name,"CDATA",uri, false);  	            }  	            else                 {  	                if (!EMPTYSTRING.equals(uri)) // hack for XSLTC attribset16 test  	                {                             // that maps ns1 prefix to "" URI   	                    name = "xmlns:" + prefix;  	  	                    /* for something like xmlns:abc="w3.pretend.org"  	             	 	     *  the uri is the value, that is why we pass it in the  	             	 	     * value, or 5th slot of addAttributeAlways()  	                 	   */  	                    addAttributeAlways(XMLNS_URI, prefix, name,"CDATA",uri, false );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?