tostream.java

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

JAVA
2,016
字号
     * ignorableWhitespace() method rather than this one (validating     * parsers must do so).</p>     *     * @param ch The characters from the XML document.     * @param start The start position in the array.     * @param length The number of characters to read from the array.     * @throws org.xml.sax.SAXException Any SAX exception, possibly     *            wrapping another exception.     * @see #ignorableWhitespace     * @see org.xml.sax.Locator     *     * @throws org.xml.sax.SAXException     */    protected void cdata(char ch[], int start, final int length)        throws org.xml.sax.SAXException    {        try        {            final int old_start = start;            if (m_elemContext.m_startTagOpen)            {                closeStartTag();                m_elemContext.m_startTagOpen = false;            }            m_ispreserve = true;            if (shouldIndent())                indent();            boolean writeCDataBrackets =                (((length >= 1) && escapingNotNeeded(ch[start])));            /* Write out the CDATA opening delimiter only if             * we are supposed to, and if we are not already in             * the middle of a CDATA section               */            if (writeCDataBrackets && !m_cdataTagOpen)            {                m_writer.write(CDATA_DELIMITER_OPEN);                m_cdataTagOpen = true;            }            // writer.write(ch, start, length);            if (isEscapingDisabled())            {                charactersRaw(ch, start, length);            }            else                writeNormalizedChars(ch, start, length, true, m_lineSepUse);            /* used to always write out CDATA closing delimiter here,             * but now we delay, so that we can merge CDATA sections on output.                 * need to write closing delimiter later             */            if (writeCDataBrackets)            {                /* if the CDATA section ends with ] don't leave it open                 * as there is a chance that an adjacent CDATA sections                 * starts with ]>.                   * We don't want to merge ]] with > , or ] with ]>                  */                if (ch[start + length - 1] == ']')                    closeCDATA();            }            // time to fire off CDATA event            if (m_tracer != null)                super.fireCDATAEvent(ch, old_start, length);        }        catch (IOException ioe)        {            throw new org.xml.sax.SAXException(                Utils.messages.createMessage(                    MsgKey.ER_OIERROR,                    null),                ioe);            //"IO error", ioe);        }    }    /**     * Tell if the character escaping should be disabled for the current state.     *     * @return true if the character escaping should be disabled.     */    private boolean isEscapingDisabled()    {        return m_disableOutputEscapingStates.peekOrFalse();    }    /**     * If available, when the disable-output-escaping attribute is used,     * output raw text without escaping.     *     * @param ch The characters from the XML document.     * @param start The start position in the array.     * @param length The number of characters to read from the array.     *     * @throws org.xml.sax.SAXException     */    protected void charactersRaw(char ch[], int start, int length)        throws org.xml.sax.SAXException    {        if (m_inEntityRef)            return;        try        {            if (m_elemContext.m_startTagOpen)            {                closeStartTag();                m_elemContext.m_startTagOpen = false;            }            m_ispreserve = true;            m_writer.write(ch, start, length);        }        catch (IOException e)        {            throw new SAXException(e);        }    }    /**     * Receive notification of character data.     *     * <p>The Parser will call this method to report each chunk of     * character data.  SAX parsers may return all contiguous character     * data in a single chunk, or they may split it into several     * chunks; however, all of the characters in any single event     * must come from the same external entity, so that the Locator     * provides useful information.</p>     *     * <p>The application must not attempt to read from the array     * outside of the specified range.</p>     *     * <p>Note that some parsers will report whitespace using the     * ignorableWhitespace() method rather than this one (validating     * parsers must do so).</p>     *     * @param chars The characters from the XML document.     * @param start The start position in the array.     * @param length The number of characters to read from the array.     * @throws org.xml.sax.SAXException Any SAX exception, possibly     *            wrapping another exception.     * @see #ignorableWhitespace     * @see org.xml.sax.Locator     *     * @throws org.xml.sax.SAXException     */    public void characters(final char chars[], final int start, final int length)        throws org.xml.sax.SAXException    {        // It does not make sense to continue with rest of the method if the number of         // characters to read from array is 0.        // Section 7.6.1 of XSLT 1.0 (http://www.w3.org/TR/xslt#value-of) suggest no text node        // is created if string is empty.	        if (length == 0 || (m_inEntityRef && !m_expandDTDEntities))            return;        if (m_elemContext.m_startTagOpen)        {            closeStartTag();            m_elemContext.m_startTagOpen = false;        }        else if (m_needToCallStartDocument)        {            startDocumentInternal();        }        if (m_cdataStartCalled || m_elemContext.m_isCdataSection)        {            /* either due to startCDATA() being called or due to              * cdata-section-elements atribute, we need this as cdata             */            cdata(chars, start, length);            return;        }        if (m_cdataTagOpen)            closeCDATA();        // the check with _escaping is a bit of a hack for XLSTC        if (m_disableOutputEscapingStates.peekOrFalse() || (!m_escaping))        {            charactersRaw(chars, start, length);            // time to fire off characters generation event            if (m_tracer != null)                super.fireCharEvent(chars, start, length);            return;        }        if (m_elemContext.m_startTagOpen)        {            closeStartTag();            m_elemContext.m_startTagOpen = false;        }                try        {            int i;            char ch1;            int startClean;                        // skip any leading whitspace             // don't go off the end and use a hand inlined version            // of isWhitespace(ch)            final int end = start + length;            int lastDirty = start - 1; // last character that needed processing            for (i = start;                ((i < end)                                    && ((ch1 = chars[i]) == 0x20                        || (ch1 == 0xA && m_lineSepUse)                        || ch1 == 0xD                        || ch1 == 0x09));                i++)            {                /*                 * We are processing leading whitespace, but are doing the same                 * processing for dirty characters here as for non-whitespace.                 *                  */                if (!m_charInfo.isTextASCIIClean(ch1))                {                    lastDirty = processDirty(chars,end, i,ch1, lastDirty, true);                    i = lastDirty;                }            }            /* If there is some non-whitespace, mark that we may need             * to preserve this. This is only important if we have indentation on.             */                        if (i < end)                 m_ispreserve = true;                //            int lengthClean;    // number of clean characters in a row//            final boolean[] isAsciiClean = m_charInfo.getASCIIClean();                        final boolean isXML10 = XMLVERSION10.equals(getVersion());            // we've skipped the leading whitespace, now deal with the rest            for (; i < end; i++)            {                                      {                    // A tight loop to skip over common clean chars                    // This tight loop makes it easier for the JIT                    // to optimize.                    char ch2;                    while (i<end                             && ((ch2 = chars[i])<127)                            && m_charInfo.isTextASCIIClean(ch2))                            i++;                    if (i == end)                        break;                }                                     final char ch = chars[i];                /*  The check for isCharacterInC0orC1Ranger and                  *  isNELorLSEPCharacter has been added                 *  to support Control Characters in XML 1.1                 */                     if (!isCharacterInC0orC1Range(ch) &&                     (isXML10 || !isNELorLSEPCharacter(ch)) &&                    (escapingNotNeeded(ch) && (!m_charInfo.isSpecialTextChar(ch)))                        || ('"' == ch))                {                    ; // a character needing no special processing                }                else                {                    lastDirty = processDirty(chars,end, i, ch, lastDirty, true);                    i = lastDirty;                }            }                        // we've reached the end. Any clean characters at the            // end of the array than need to be written out?            startClean = lastDirty + 1;            if (i > startClean)            {                int lengthClean = i - startClean;                m_writer.write(chars, startClean, lengthClean);            }            // For indentation purposes, mark that we've just writen text out            m_isprevtext = true;        }        catch (IOException e)        {            throw new SAXException(e);        }        // time to fire off characters generation event        if (m_tracer != null)            super.fireCharEvent(chars, start, length);    }         /**     * This method checks if a given character is between C0 or C1 range     * of Control characters.     * This method is added to support Control Characters for XML 1.1     * If a given character is TAB (0x09), LF (0x0A) or CR (0x0D), this method     * return false. Since they are whitespace characters, no special processing is needed.     *      * @param ch     * @return boolean     */    private static boolean isCharacterInC0orC1Range(char ch)    {        if(ch == 0x09 || ch == 0x0A || ch == 0x0D)        	return false;        else        	    	        	return (ch >= 0x7F && ch <= 0x9F)|| (ch >= 0x01 && ch <= 0x1F);    }    /**     * This method checks if a given character either NEL (0x85) or LSEP (0x2028)     * These are new end of line charcters added in XML 1.1.  These characters must be     * written as Numeric Character References (NCR) in XML 1.1 output document.     *      * @param ch     * @return boolean     */    private static boolean isNELorLSEPCharacter(char ch)    {        return (ch == 0x85 || ch == 0x2028);    }    /**     * Process a dirty character and any preeceding clean characters     * that were not yet processed.     * @param chars array of characters being processed     * @param end one (1) beyond the last character      * in chars to be processed     * @param i the index of the dirty character     * @param ch the character in chars[i]     * @param lastDirty the last dirty character previous to i     * @param fromTextNode true if the characters being processed are     * from a text node, false if they are from an attribute value.     * @return the index of the last character processed     */    private int processDirty(        char[] chars,         int end,        int i,         char ch,        int lastDirty,        boolean fromTextNode) throws IOException    {        int startClean = lastDirty + 1;        // if we have some clean characters accumulated        // process them before the dirty one.                           if (i > startClean)        {            int lengthClean = i - startClean;            m_writer.write(chars, startClean, lengthClean);        }        // process the "dirty" character        if (CharInfo.S_LINEFEED == ch && fromTextNode)        {            m_writer.write(m_lineSep, 0, m_lineSepLen);        }        else        {            startClean =                accumDefaultEscape(                    m_writer,                    (char)ch,                    i,                    chars,                    end,                    fromTextNode,                    false);            i = startClean - 1;        }        // Return the index of the last character that we just processed         // which is a dirty character.        return i;    }    /**     * Receive notification of character data.     *     * @param s The string of characters to process.     *     * @throws org.xml.sax.SAXException     */    public void characters(String s) throws org.xml.sax.SAXException    {        if (m_inEntityRef && !m_expandDTDEntities)            return;        final int length = s.length();        if (length > m_charsBuff.length)        {            m_charsBuff = new char[length * 2 + 1];        }        s.getChars(0, length, m_charsBuff, 0);        characters(m_charsBuff, 0, length);    }    /**

⌨️ 快捷键说明

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