tostream.java

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

JAVA
2,016
字号
     *        none of these applies.     * @param value A string representing the attribute's default value,     *        or null if there is none.     * @exception SAXException The application may raise an exception.     */    public void attributeDecl(        String eName,        String aName,        String type,        String valueDefault,        String value)        throws SAXException    {        // Do not inline external DTD        if (m_inExternalDTD)            return;        try        {            final java.io.Writer writer = m_writer;            DTDprolog();            writer.write("<!ATTLIST ");            writer.write(eName);            writer.write(' ');            writer.write(aName);            writer.write(' ');            writer.write(type);            if (valueDefault != null)            {                writer.write(' ');                writer.write(valueDefault);            }            //writer.write(" ");            //writer.write(value);            writer.write('>');            writer.write(m_lineSep, 0, m_lineSepLen);        }        catch (IOException e)        {            throw new SAXException(e);        }    }    /**     * Get the character stream where the events will be serialized to.     *     * @return Reference to the result Writer, or null.     */    public Writer getWriter()    {        return m_writer;    }    /**     * Report a parsed external entity declaration.     *     * <p>Only the effective (first) declaration for each entity     * will be reported.</p>     *     * @param name The name of the entity.  If it is a parameter     *        entity, the name will begin with '%'.     * @param publicId The declared public identifier of the entity, or     *        null if none was declared.     * @param systemId The declared system identifier of the entity.     * @exception SAXException The application may raise an exception.     * @see #internalEntityDecl     * @see org.xml.sax.DTDHandler#unparsedEntityDecl     */    public void externalEntityDecl(        String name,        String publicId,        String systemId)        throws SAXException    {        try {            DTDprolog();                        m_writer.write("<!ENTITY ");                        m_writer.write(name);            if (publicId != null) {                m_writer.write(" PUBLIC \"");                m_writer.write(publicId);              }            else {                m_writer.write(" SYSTEM \"");                m_writer.write(systemId);            }            m_writer.write("\" >");            m_writer.write(m_lineSep, 0, m_lineSepLen);        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    /**     * Tell if this character can be written without escaping.     */    protected boolean escapingNotNeeded(char ch)    {        final boolean ret;        if (ch < 127)        {            // This is the old/fast code here, but is this             // correct for all encodings?            if (ch >= 0x20 || (0x0A == ch || 0x0D == ch || 0x09 == ch))                ret= true;            else                ret = false;        }        else {                        ret = m_encodingInfo.isInEncoding(ch);        }        return ret;            }        /**     * Once a surrogate has been detected, write out the pair of     * characters if it is in the encoding, or if there is no     * encoding, otherwise write out an entity reference     * of the value of the unicode code point of the character     * represented by the high/low surrogate pair.     * <p>     * An exception is thrown if there is no low surrogate in the pair,     * because the array ends unexpectely, or if the low char is there     * but its value is such that it is not a low surrogate.     *     * @param c the first (high) part of the surrogate, which     * must be confirmed before calling this method.     * @param ch Character array.     * @param i position Where the surrogate was detected.     * @param end The end index of the significant characters.     * @return 0 if the pair of characters was written out as-is,     * the unicode code point of the character represented by     * the surrogate pair if an entity reference with that value     * was written out.      *      * @throws IOException     * @throws org.xml.sax.SAXException if invalid UTF-16 surrogate detected.     */    protected int writeUTF16Surrogate(char c, char ch[], int i, int end)        throws IOException    {        int codePoint = 0;        if (i + 1 >= end)        {            throw new IOException(                Utils.messages.createMessage(                    MsgKey.ER_INVALID_UTF16_SURROGATE,                    new Object[] { Integer.toHexString((int) c)}));        }                final char high = c;        final char low = ch[i+1];        if (!Encodings.isLowUTF16Surrogate(low)) {            throw new IOException(                Utils.messages.createMessage(                    MsgKey.ER_INVALID_UTF16_SURROGATE,                    new Object[] {                        Integer.toHexString((int) c)                            + " "                            + Integer.toHexString(low)}));        }        final java.io.Writer writer = m_writer;                        // If we make it to here we have a valid high, low surrogate pair        if (m_encodingInfo.isInEncoding(c,low)) {            // If the character formed by the surrogate pair            // is in the encoding, so just write it out            writer.write(ch,i,2);        }        else {            // Don't know what to do with this char, it is            // not in the encoding and not a high char in            // a surrogate pair, so write out as an entity ref            final String encoding = getEncoding();            if (encoding != null) {                /* The output encoding is known,                  * so somthing is wrong.                  */                codePoint = Encodings.toCodePoint(high, low);                // not in the encoding, so write out a character reference                writer.write('&');                writer.write('#');                writer.write(Integer.toString(codePoint));                writer.write(';');            } else {                /* The output encoding is not known,                 * so just write it out as-is.                 */                writer.write(ch, i, 2);            }        }        // non-zero only if character reference was written out.        return codePoint;    }    /**     * Handle one of the default entities, return false if it     * is not a default entity.     *     * @param ch character to be escaped.     * @param i index into character array.     * @param chars non-null reference to character array.     * @param len length of chars.     * @param fromTextNode true if the characters being processed     * are from a text node, false if they are from an attribute value     * @param escLF true if the linefeed should be escaped.     *     * @return i+1 if the character was written, else i.     *     * @throws java.io.IOException     */    protected int accumDefaultEntity(        java.io.Writer writer,        char ch,        int i,        char[] chars,        int len,        boolean fromTextNode,        boolean escLF)        throws IOException    {        if (!escLF && CharInfo.S_LINEFEED == ch)        {            writer.write(m_lineSep, 0, m_lineSepLen);        }        else        {            // if this is text node character and a special one of those,            // or if this is a character from attribute value and a special one of those            if ((fromTextNode && m_charInfo.isSpecialTextChar(ch)) || (!fromTextNode && m_charInfo.isSpecialAttrChar(ch)))            {                String outputStringForChar = m_charInfo.getOutputStringForChar(ch);                if (null != outputStringForChar)                {                    writer.write(outputStringForChar);                }                else                    return i;            }            else                return i;        }        return i + 1;    }    /**     * Normalize the characters, but don't escape.     *     * @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.     * @param isCData true if a CDATA block should be built around the characters.     * @param useSystemLineSeparator true if the operating systems      * end-of-line separator should be output rather than a new-line character.     *     * @throws IOException     * @throws org.xml.sax.SAXException     */    void writeNormalizedChars(        char ch[],        int start,        int length,        boolean isCData,        boolean useSystemLineSeparator)        throws IOException, org.xml.sax.SAXException    {        final java.io.Writer writer = m_writer;        int end = start + length;        for (int i = start; i < end; i++)        {            char c = ch[i];            if (CharInfo.S_LINEFEED == c && useSystemLineSeparator)            {                writer.write(m_lineSep, 0, m_lineSepLen);            }            else if (isCData && (!escapingNotNeeded(c)))            {                //                if (i != 0)                if (m_cdataTagOpen)                    closeCDATA();                // This needs to go into a function...                 if (Encodings.isHighUTF16Surrogate(c))                {                    writeUTF16Surrogate(c, ch, i, end);                    i++ ; // process two input characters                }                else                {                    writer.write("&#");                    String intStr = Integer.toString((int) c);                    writer.write(intStr);                    writer.write(';');                }                //                if ((i != 0) && (i < (end - 1)))                //                if (!m_cdataTagOpen && (i < (end - 1)))                //                {                //                    writer.write(CDATA_DELIMITER_OPEN);                //                    m_cdataTagOpen = true;                //                }            }            else if (                isCData                    && ((i < (end - 2))                        && (']' == c)                        && (']' == ch[i + 1])                        && ('>' == ch[i + 2])))            {                writer.write(CDATA_CONTINUE);                i += 2;            }            else            {                if (escapingNotNeeded(c))                {                    if (isCData && !m_cdataTagOpen)                    {                        writer.write(CDATA_DELIMITER_OPEN);                        m_cdataTagOpen = true;                    }                    writer.write(c);                }                // This needs to go into a function...                 else if (Encodings.isHighUTF16Surrogate(c))                {                    if (m_cdataTagOpen)                        closeCDATA();                    writeUTF16Surrogate(c, ch, i, end);                    i++; // process two input characters                }                else                {                    if (m_cdataTagOpen)                        closeCDATA();                    writer.write("&#");                    String intStr = Integer.toString((int) c);                    writer.write(intStr);                    writer.write(';');                }            }        }    }    /**     * Ends an un-escaping section.     *     * @see #startNonEscaping     *     * @throws org.xml.sax.SAXException     */    public void endNonEscaping() throws org.xml.sax.SAXException    {        m_disableOutputEscapingStates.pop();    }    /**     * Starts an un-escaping section. All characters printed within an un-     * escaping section are printed as is, without escaping special characters     * into entity references. Only XML and HTML serializers need to support     * this method.     * <p> The contents of the un-escaping section will be delivered through the     * regular <tt>characters</tt> event.     *     * @throws org.xml.sax.SAXException     */    public void startNonEscaping() throws org.xml.sax.SAXException    {        m_disableOutputEscapingStates.push(true);    }    /**     * Receive notification of cdata.     *     * <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

⌨️ 快捷键说明

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