serializertoxml.java

来自「java jdk 1.4的源码」· Java 代码 · 共 2,458 行 · 第 1/5 页

JAVA
2,458
字号
      {        if (m_writer instanceof WriterToUTF8Buffered)        {          if(m_shouldFlush)            ((WriterToUTF8Buffered) m_writer).flush();          else            ((WriterToUTF8Buffered) m_writer).flushBuffer();        }        if (m_writer instanceof WriterToUTF8)        {          if(m_shouldFlush)            m_writer.flush();         }        else if (m_writer instanceof WriterToASCI)        {          if(m_shouldFlush)            m_writer.flush();        }        else        {          // Flush always.           // Not a great thing if the writer was created           // by this class, but don't have a choice.          m_writer.flush();         }      }      catch (IOException ioe)      {        throw new org.xml.sax.SAXException(ioe);      }    }  }  /**   * 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(char chars[], int start, int length)          throws org.xml.sax.SAXException  {    if(0 == length)      return;    //    if (m_inEntityRef)    //      return;    //    else     if (m_inCData || m_cdataSectionStates.peekOrFalse())    {      cdata(chars, start, length);      return;    }        try    {            if (m_disableOutputEscapingStates.peekOrFalse())      {        charactersRaw(chars, start, length);          return;      }          final Writer writer = m_writer;      if (!m_elemStack.peekOrTrue())      {        writer.write('>');          m_isprevtext = false;          m_elemStack.setTop(true);        m_preserves.push(m_ispreserve);      }        int startClean = start;      int lengthClean = 0;        // int pos = 0;      int end = start + length;      boolean checkWhite = true;      final int maxCharacter = m_maxCharacter;      final BitSet specialsMap = m_charInfo.m_specialsMap;        for (int i = start; i < end; i++)      {        char ch = chars[i];          if (checkWhite                && ((ch > 0x20)                    ||!((ch == 0x20) || (ch == 0x09) || (ch == 0xD)                        || (ch == 0xA))))        {          m_ispreserve = true;          checkWhite = false;        }          if ((canConvert(ch) && (!specialsMap.get(ch))) || ('"' == ch))        {          lengthClean++;        }        else        {          if (lengthClean > 0)          {            writer.write(chars, startClean, lengthClean);              lengthClean = 0;          }            if (CharInfo.S_LINEFEED == ch)          {            writer.write(m_lineSep, 0, m_lineSepLen);              startClean = i + 1;          }          else          {            startClean = accumDefaultEscape(ch, i, chars, end, false);            i = startClean - 1;          }        }      }        if (lengthClean > 0)      {        writer.write(chars, startClean, lengthClean);      }        }    catch(IOException ioe)    {      throw new SAXException(ioe);    }    m_isprevtext = true;  }  /**   * 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   */  public void charactersRaw(char ch[], int start, int length)          throws org.xml.sax.SAXException  {    try    {      if (m_inEntityRef)        return;        writeParentTagEnd();        m_ispreserve = true;        m_writer.write(ch, start, length);    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }  }  /**   * Return true if the character is the high member of a surrogate pair.   *   * NEEDSDOC @param c   *   * NEEDSDOC ($objectName$) @return   */  static final boolean isUTF16Surrogate(char c)  {    return (c & 0xFC00) == 0xD800;  }  /**   * Once a surrogate has been detected, get the pair as a single   * integer value.   *   * @param c the first part of the surrogate.   * @param ch Character array.   * @param i position Where the surrogate was detected.   * @param end The end index of the significant characters.   * @return i+1.   * @throws org.xml.sax.SAXException if invalid UTF-16 surrogate detected.   */  int getURF16SurrogateValue(char c, char ch[], int i, int end)          throws org.xml.sax.SAXException  {    int next;    if (i + 1 >= end)    {      throw new org.xml.sax.SAXException(        XSLMessages.createXPATHMessage(          XPATHErrorResources.ER_INVALID_UTF16_SURROGATE,          new Object[]{ Integer.toHexString((int) c) }));  //"Invalid UTF-16 surrogate detected: "      //+Integer.toHexString((int)c)+ " ?");    }    else    {      next = ch[++i];      if (!(0xdc00 <= next && next < 0xe000))        throw new org.xml.sax.SAXException(          XSLMessages.createXPATHMessage(            XPATHErrorResources.ER_INVALID_UTF16_SURROGATE,            new Object[]{              Integer.toHexString((int) c) + " "              + Integer.toHexString(next) }));  //"Invalid UTF-16 surrogate detected: "      //+Integer.toHexString((int)c)+" "+Integer.toHexString(next));      next = ((c - 0xd800) << 10) + next - 0xdc00 + 0x00010000;    }    return next;  }  /**   * Once a surrogate has been detected, write the pair as a single   * character reference.   *   * @param c the first part of the surrogate.   * @param ch Character array.   * @param i position Where the surrogate was detected.   * @param end The end index of the significant characters.   * @return i+1.   * @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, org.xml.sax.SAXException  {    // UTF-16 surrogate    int surrogateValue = getURF16SurrogateValue(c, ch, i, end);    i++;    m_writer.write('&');    m_writer.write('#');    // m_writer.write('x');    m_writer.write(Integer.toString(surrogateValue));    m_writer.write(';');    return i;  }  /**   * 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.   *   * @throws IOException   * @throws org.xml.sax.SAXException   */  void writeNormalizedChars(char ch[], int start, int length, boolean isCData)          throws IOException, org.xml.sax.SAXException  {    int end = start + length;    for (int i = start; i < end; i++)    {      char c = ch[i];      if (CharInfo.S_LINEFEED == c)      {        m_writer.write(m_lineSep, 0, m_lineSepLen);      }      else if (isCData && (!canConvert(c)))      {        if (i != 0)          m_writer.write("]]>");        // This needs to go into a function...         if (isUTF16Surrogate(c))        {          i = writeUTF16Surrogate(c, ch, i, end);        }        else        {          m_writer.write("&#");          String intStr = Integer.toString((int) c);          m_writer.write(intStr);          m_writer.write(';');        }        if ((i != 0) && (i < (end - 1)))          m_writer.write("<![CDATA[");      }      else if (isCData               && ((i < (end - 2)) && (']' == c) && (']' == ch[i + 1])                   && ('>' == ch[i + 2])))      {        m_writer.write("]]]]><![CDATA[>");        i += 2;      }      else      {        if (canConvert(c))        {          m_writer.write(c);        }        // This needs to go into a function...         else if (isUTF16Surrogate(c))        {          i = writeUTF16Surrogate(c, ch, i, end);        }        else        {          m_writer.write("&#");          String intStr = Integer.toString((int) c);          m_writer.write(intStr);          m_writer.write(';');        }      }    }  }  /**   * Receive notification of ignorable whitespace in element content.   *   * Not sure how to get this invoked quite yet.   *   * @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 #characters   *   * @throws org.xml.sax.SAXException   */  public void ignorableWhitespace(char ch[], int start, int length)          throws org.xml.sax.SAXException  {    if (0 == length)      return;    characters(ch, start, length);  }  /**   * Receive notification of a skipped entity.   * @see org.xml.sax.ContentHandler#skippedEntity   *   * @param name The name of the skipped entity.  If it is a   *        parameter entity, the name will begin with '%', and if   *        it is the external DTD subset, it will be the string   *        "[dtd]".   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   */  public void skippedEntity(String name) throws org.xml.sax.SAXException  {    // TODO: Should handle  }  /**   * Report the beginning of an entity.   *   * The start and end of the document entity are not reported.   * The start and end of the external DTD subset are reported   * using the pseudo-name "[dtd]".  All other events must be   * properly nested within start/end entity events.   *   * @param name The name of the entity.  If it is a parameter   *        entity, the name will begin with '%'.   * @throws org.xml.sax.SAXException The application may raise an exception.   * @see #endEntity   * @see org.xml.sax.ext.DeclHandler#internalEntityDecl   * @see org.xml.sax.ext.DeclHandler#externalEntityDecl   */  public void startEntity(String name) throws org.xml.sax.SAXException  {    if (name.equals("[dtd]"))       m_inExternalDTD = true;    m_inEntityRef = true;  }  /**   * Report the end of an entity.   *   * @param name The name of the entity that is ending.   * @throws org.xml.sax.SAXException The application may raise an exception.   * @see #startEntity   */  public void endEntity(String name) throws org.xml.sax.SAXException  {    if (name.equals("[dtd]"))       m_inExternalDTD = false;    m_inEntityRef = false;  }  /**   * Receive notivication of a entityReference.   *   * @param name The name of the entity.   *   * @throws org.xml.sax.SAXException   */  public void entityReference(String name) throws org.xml.sax.SAXException  {    writeParentTagEnd();    if (shouldIndent())      indent(m_currentIndent);    try    {      final Writer writer = m_writer;      writer.write("&");      writer.write(name);      writer.write(";");    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }  }  // Implement DeclHandler  /**   *   Report an element type declaration.   *     *   <p>The content model will consist of the string "EMPTY", the   *   string "ANY", or a parenthesised group, optionally followed   *   by an occurrence indicator.  The model will be normalized so   *   that all whitespace is removed,and will include the enclosing   *   parentheses.</p>   *     *   @param name The element type name.   *   @param model The content model as a normalized string.   *   @exception SAXException The application may raise an exception.   */  public void elementDecl(String name, String model) throws SAXException  {    // Do not inline external DTD    if (m_inExternalDTD) return;         try    {      final Writer writer = m_writer;      if (m_inDoctype)      {        writer.write(" [");

⌨️ 快捷键说明

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