toxmlsaxhandler.java

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

JAVA
775
字号
  	                }  	            }            }        }        return pushed;    }            /**     * @see org.xml.sax.ext.LexicalHandler#comment(char[], int, int)     */    public void comment(char[] arg0, int arg1, int arg2) throws SAXException    {        flushPending();        if (m_lexHandler != null)            m_lexHandler.comment(arg0, arg1, arg2);                    if (m_tracer != null)                        super.fireCommentEvent(arg0, arg1, arg2);    }    /**     * @see org.xml.sax.ext.LexicalHandler#endCDATA()     */    public void endCDATA() throws SAXException    {        /* Normally we would do somthing with this but we ignore it.         * The neccessary call to m_lexHandler.endCDATA() will be made         * in flushPending().         *          * This is so that if we get calls like these:         *   this.startCDATA();         *   this.characters(chars1, off1, len1);         *   this.endCDATA();         *   this.startCDATA();         *   this.characters(chars2, off2, len2);         *   this.endCDATA();         *          * that we will only make these calls to the wrapped handlers:         *          *   m_lexHandler.startCDATA();         *   m_saxHandler.characters(chars1, off1, len1);         *   m_saxHandler.characters(chars1, off2, len2);         *   m_lexHandler.endCDATA();         *          * We will merge adjacent CDATA blocks.         */     }    /**     * @see org.xml.sax.ext.LexicalHandler#endDTD()     */    public void endDTD() throws SAXException    {        if (m_lexHandler != null)            m_lexHandler.endDTD();    }    /**     * @see org.xml.sax.ext.LexicalHandler#startEntity(String)     */    public void startEntity(String arg0) throws SAXException    {        if (m_lexHandler != null)            m_lexHandler.startEntity(arg0);    }    /**     * @see ExtendedContentHandler#characters(String)     */    public void characters(String chars) throws SAXException    {        final int length = chars.length();        if (length > m_charsBuff.length)        {            m_charsBuff = new char[length*2 + 1];        }        chars.getChars(0, length, m_charsBuff, 0);        this.characters(m_charsBuff, 0, length);     }    /////////////////// from XSLTC //////////////    public ToXMLSAXHandler(ContentHandler handler, String encoding)    {        super(handler, encoding);        initCDATA();        // initNamespaces();        m_prefixMap = new NamespaceMappings();    }    public ToXMLSAXHandler(        ContentHandler handler,        LexicalHandler lex,        String encoding)    {        super(handler, lex, encoding);        initCDATA();        //      initNamespaces();        m_prefixMap = new NamespaceMappings();    }    /**     * Start an element in the output document. This might be an XML element     * (<elem>data</elem> type) or a CDATA section.     */    public void startElement(    String elementNamespaceURI,    String elementLocalName,    String elementName) throws SAXException    {        startElement(            elementNamespaceURI,elementLocalName,elementName, null);    }    public void startElement(String elementName) throws SAXException    {        startElement(null, null, elementName, null);    }    public void characters(char[] ch, int off, int len) throws SAXException    {        // We do the first two things in flushPending() but we don't        // close any open CDATA calls.                if (m_needToCallStartDocument)        {            startDocumentInternal();            m_needToCallStartDocument = false;        }        if (m_elemContext.m_startTagOpen)        {            closeStartTag();            m_elemContext.m_startTagOpen = false;        }        if (m_elemContext.m_isCdataSection && !m_cdataTagOpen        && m_lexHandler != null)         {            m_lexHandler.startCDATA();            // We have made a call to m_lexHandler.startCDATA() with            // no balancing call to m_lexHandler.endCDATA()            // so we set m_cdataTagOpen true to remember this.            m_cdataTagOpen = true;        }                /* If there are any occurances of "]]>" in the character data         * let m_saxHandler worry about it, we've already warned them with         * the previous call of m_lexHandler.startCDATA();         */         m_saxHandler.characters(ch, off, len);        // time to generate characters event        if (m_tracer != null)            fireCharEvent(ch, off, len);    }        /**     * @see ExtendedContentHandler#endElement(String)     */    public void endElement(String elemName) throws SAXException    {        endElement(null, null, elemName);    }        /**     * Send a namespace declaration in the output document. The namespace     * declaration will not be include if the namespace is already in scope     * with the same prefix.     */    public void namespaceAfterStartElement(        final String prefix,        final String uri)        throws SAXException    {        startPrefixMapping(prefix,uri,false);    }    /**     *     * @see org.xml.sax.ContentHandler#processingInstruction(String, String)     * Send a processing instruction to the output document     */    public void processingInstruction(String target, String data)        throws SAXException    {        flushPending();        // Pass the processing instruction to the SAX handler        m_saxHandler.processingInstruction(target, data);        // we don't want to leave serializer to fire off this event,        // so do it here.        if (m_tracer != null)            super.fireEscapingEvent(target, data);    }    /**     * Undeclare the namespace that is currently pointed to by a given     * prefix. Inform SAX handler if prefix was previously mapped.     */    protected boolean popNamespace(String prefix)    {        try        {            if (m_prefixMap.popNamespace(prefix))            {                m_saxHandler.endPrefixMapping(prefix);                return true;            }        }        catch (SAXException e)        {            // falls through        }        return false;    }    public void startCDATA() throws SAXException    {        /* m_cdataTagOpen can only be true here if we have ignored the         * previous call to this.endCDATA() and the previous call          * this.startCDATA() before that is still "open". In this way         * we merge adjacent CDATA. If anything else happened after the          * ignored call to this.endCDATA() and this call then a call to          * flushPending() would have been made which would have         * closed the CDATA and set m_cdataTagOpen to false.         */        if (!m_cdataTagOpen )         {            flushPending();            if (m_lexHandler != null) {                m_lexHandler.startCDATA();                // We have made a call to m_lexHandler.startCDATA() with                // no balancing call to m_lexHandler.endCDATA()                // so we set m_cdataTagOpen true to remember this.                                m_cdataTagOpen = true;                 }                      }            }    /**     * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)     */    public void startElement(    String namespaceURI,    String localName,    String name,    Attributes atts)        throws SAXException    {        flushPending();        super.startElement(namespaceURI, localName, name, atts);        // Handle document type declaration (for first element only)         if (m_needToOutputDocTypeDecl)         {             String doctypeSystem = getDoctypeSystem();             if (doctypeSystem != null && m_lexHandler != null)             {                 String doctypePublic = getDoctypePublic();                 if (doctypeSystem != null)                     m_lexHandler.startDTD(                         name,                         doctypePublic,                         doctypeSystem);             }             m_needToOutputDocTypeDecl = false;         }        m_elemContext = m_elemContext.push(namespaceURI, localName, name);        // ensurePrefixIsDeclared depends on the current depth, so        // the previous increment is necessary where it is.        if (namespaceURI != null)            ensurePrefixIsDeclared(namespaceURI, name);        // add the attributes to the collected ones        if (atts != null)            addAttributes(atts);                 // do we really need this CDATA section state?        m_elemContext.m_isCdataSection = isCdataSection();       }     private void ensurePrefixIsDeclared(String ns, String rawName)        throws org.xml.sax.SAXException    {        if (ns != null && ns.length() > 0)        {            int index;            final boolean no_prefix = ((index = rawName.indexOf(":")) < 0);            String prefix = (no_prefix) ? "" : rawName.substring(0, index);            if (null != prefix)            {                String foundURI = m_prefixMap.lookupNamespace(prefix);                if ((null == foundURI) || !foundURI.equals(ns))                {                    this.startPrefixMapping(prefix, ns, false);                    if (getShouldOutputNSAttr()) {                        // Bugzilla1133: Generate attribute as well as namespace event.                        // SAX does expect both.                        this.addAttributeAlways(                            "http://www.w3.org/2000/xmlns/",                            no_prefix ? "xmlns" : prefix,  // local name                            no_prefix ? "xmlns" : ("xmlns:"+ prefix), // qname                            "CDATA",                            ns,                            false);                    }                }            }        }    }    /**     * Adds the given attribute to the set of attributes, and also makes sure     * that the needed prefix/uri mapping is declared, but only if there is a     * currently open element.     *      * @param uri the URI of the attribute     * @param localName the local name of the attribute     * @param rawName    the qualified name of the attribute     * @param type the type of the attribute (probably CDATA)     * @param value the value of the attribute     * @param XSLAttribute true if this attribute is coming from an xsl:attribute element     * @see ExtendedContentHandler#addAttribute(String, String, String, String, String)     */    public void addAttribute(        String uri,        String localName,        String rawName,        String type,        String value,        boolean XSLAttribute)        throws SAXException    {              if (m_elemContext.m_startTagOpen)        {            ensurePrefixIsDeclared(uri, rawName);            addAttributeAlways(uri, localName, rawName, type, value, false);        }    }            /**     * Try's to reset the super class and reset this class for      * re-use, so that you don't need to create a new serializer      * (mostly for performance reasons).     *      * @return true if the class was successfuly reset.     * @see Serializer#reset()     */    public boolean reset()    {        boolean wasReset = false;        if (super.reset())        {            resetToXMLSAXHandler();            wasReset = true;        }        return wasReset;    }        /**     * Reset all of the fields owned by ToXMLSAXHandler class     *     */    private void resetToXMLSAXHandler()    {        this.m_escapeSetting = false;    }  }

⌨️ 快捷键说明

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