serializertoxml.java

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

JAVA
2,458
字号
      catch(IOException ioe)      {        throw new SAXException(ioe);      }      m_isprevtext = false;      m_elemStack.setTop(true);      m_preserves.push(m_ispreserve);    }  }  /**   * Flag the current element as not yet having any   * children.   */  protected void openElementForChildren()  {    // Flag the current element as not yet having any children.    m_elemStack.push(false);  }  /**   * Tell if child nodes have been added to the current   * element.  Must be called in balance with openElementForChildren().   *   * @return true if child nodes were added.   */  protected boolean childNodesWereAdded()  {    return m_elemStack.isEmpty() ? false : m_elemStack.pop();  }  /**   * Receive notification of the end of an element.   *   *   * @param namespaceURI The Namespace URI, or the empty string if the   *        element has no Namespace URI or if Namespace   *        processing is not being performed.   * @param localName The local name (without prefix), or the   *        empty string if Namespace processing is not being   *        performed.   * @param name The element type name   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   *   * @throws org.xml.sax.SAXException   */  public void endElement(String namespaceURI, String localName, String name)          throws org.xml.sax.SAXException  {    if (m_inEntityRef)      return;    m_currentIndent -= m_indentAmount;    boolean hasChildNodes = childNodesWereAdded();    try    {      final Writer writer = m_writer;      if (hasChildNodes)      {        if (shouldIndent())          indent(m_currentIndent);          writer.write('<');        writer.write('/');        writer.write(name);        writer.write('>');      }      else      {        if (m_spaceBeforeClose)          writer.write(" />");        else          writer.write("/>");      }    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }    if (hasChildNodes)    {      m_ispreserve = m_preserves.isEmpty() ? false : m_preserves.pop();    }    m_isprevtext = false;    // m_disableOutputEscapingStates.pop();    m_cdataSectionStates.pop();  }  /**   * Process an attribute.   * @param   name   The name of the attribute.   * @param   value   The value of the attribute.   *   * @throws org.xml.sax.SAXException   */  protected void processAttribute(String name, String value)          throws org.xml.sax.SAXException  {    try    {      final Writer writer = m_writer;      writer.write(' ');      writer.write(name);      writer.write("=\"");      writeAttrString(value, m_encoding);      writer.write('\"');    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }  }  /**   * 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);  }  /**   * Ends an un-escaping section.   *   * @see #startNonEscaping   *   * @throws org.xml.sax.SAXException   */  public void endNonEscaping() throws org.xml.sax.SAXException  {    m_disableOutputEscapingStates.pop();  }  /**   * Starts a whitespace preserving section. All characters printed   * within a preserving section are printed without indentation and   * without consolidating multiple spaces. This is equivalent to   * the <tt>xml:space=&quot;preserve&quot;</tt> attribute. Only XML   * and HTML serializers need to support this method.   * <p>   * The contents of the whitespace preserving section will be delivered   * through the regular <tt>characters</tt> event.   *   * @throws org.xml.sax.SAXException   */  public void startPreserving() throws org.xml.sax.SAXException  {    // Not sure this is really what we want.  -sb    m_preserves.push(true);    m_ispreserve = true;  }  /**   * Ends a whitespace preserving section.   *   * @see #startPreserving   *   * @throws org.xml.sax.SAXException   */  public void endPreserving() throws org.xml.sax.SAXException  {    // Not sure this is really what we want.  -sb    m_ispreserve = m_preserves.isEmpty() ? false : m_preserves.pop();  }  /**   * Receive notification of a processing instruction.   *   * @param target The processing instruction target.   * @param data The processing instruction data, or null if   *        none was supplied.   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   *   * @throws org.xml.sax.SAXException   */  public void processingInstruction(String target, String data)          throws org.xml.sax.SAXException  {    if (m_inEntityRef)      return;    if (target.equals(Result.PI_DISABLE_OUTPUT_ESCAPING))    {      startNonEscaping();    }    else if (target.equals(Result.PI_ENABLE_OUTPUT_ESCAPING))    {      endNonEscaping();    }    else    {      try      {        final Writer writer = m_writer;        writeParentTagEnd();          if (shouldIndent())          indent(m_currentIndent);          writer.write('<');        writer.write('?');        writer.write(target);          if (data.length() > 0 &&!Character.isSpaceChar(data.charAt(0)))          writer.write(' ');          int indexOfQLT = data.indexOf("?>");          if (indexOfQLT >= 0)        {            // See XSLT spec on error recovery of "?>" in PIs.          if (indexOfQLT > 0)          {            writer.write(data.substring(0, indexOfQLT));          }            writer.write("? >");  // add space between.            if ((indexOfQLT + 2) < data.length())          {            writer.write(data.substring(indexOfQLT + 2));          }        }        else        {          writer.write(data);        }          writer.write('?');        writer.write('>');          // Always output a newline char if not inside of an         // element. The whitespace is not significant in that        // case.        if (m_elemStack.isEmpty())          writer.write(m_lineSep, 0, m_lineSepLen);          m_startNewLine = true;      }      catch(IOException ioe)      {        throw new SAXException(ioe);      }    }  }  /**   * Report an XML comment anywhere in the document.   *   * This callback will be used for comments inside or outside the   * document element, including comments in the external DTD   * subset (if read).   *   * @param ch An array holding the characters in the comment.   * @param start The starting position in the array.   * @param length The number of characters to use from the array.   * @throws org.xml.sax.SAXException The application may raise an exception.   */  public void comment(char ch[], int start, int length)          throws org.xml.sax.SAXException  {    if (m_inEntityRef)      return;    writeParentTagEnd();    if (shouldIndent())      indent(m_currentIndent);    try    {      final Writer writer = m_writer;      final int limit = start + length;      boolean wasDash = false;      writer.write("<!--");      // Detect occurrences of two consecutive dashes, handle as necessary.      for (int i = start; i < limit; i++) {        if (wasDash && ch[i] == '-') {          writer.write(ch, start, i - start);          writer.write(" -");          start = i + 1;     	          }        wasDash = (ch[i] == '-');      }      	             // Output the remaining characters.      writer.write(ch, start, limit - start);      // Protect comment end from a single trailing dash      if (ch[limit-1] == '-')        writer.write(' ');      writer.write("-->");    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }    m_startNewLine = true;  }  /**   * Report the start of a CDATA section.   *   * @throws org.xml.sax.SAXException The application may raise an exception.   * @see #endCDATA   */  public void startCDATA() throws org.xml.sax.SAXException  {    m_inCData = true;  }  /**   * Report the end of a CDATA section.   *   * @throws org.xml.sax.SAXException The application may raise an exception.   * @see #startCDATA   */  public void endCDATA() throws org.xml.sax.SAXException  {    m_inCData = false;  }  /**   * 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   * 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   */  public void cdata(char ch[], int start, int length)          throws org.xml.sax.SAXException  {    try    {      writeParentTagEnd();      m_ispreserve = true;      if (shouldIndent())        indent(m_currentIndent);      boolean writeCDataBrackets = (((length >= 1) && canConvert(ch[start])));      if (writeCDataBrackets)      {        m_writer.write("<![CDATA[");      }      // m_writer.write(ch, start, length);      if (isEscapingDisabled())      {        charactersRaw(ch, start, length);      }      else        writeNormalizedChars(ch, start, length, true);      if (writeCDataBrackets)      {        m_writer.write("]]>");      }    }    catch (IOException ioe)    {      throw new org.xml.sax.SAXException(        XSLMessages.createXPATHMessage(XPATHErrorResources.ER_OIERROR, null),        ioe);  //"IO error", ioe);    }  }  /** The current position in the m_charBuf or m_byteBuf. */  protected int m_pos = 0;  /**   * Append a character to the buffer.   *   * @param b byte to be written to result stream.   *   * @throws org.xml.sax.SAXException   */  protected final void accum(char b) throws org.xml.sax.SAXException  {    try    {      m_writer.write(b);    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }  }    /**   * Append a character to the buffer.   *   * @param chars non-null reference to character array.   * @param start Start of characters to be written.   * @param length Number of characters to be written.   *   * @throws org.xml.sax.SAXException   */  protected final void accum(char chars[], int start, int length)          throws org.xml.sax.SAXException  {    try    {      m_writer.write(chars, start, length);    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }  }  /**   * Append a character to the buffer.   *   * @param s non-null reference to string to be written to the character buffer.   *   * @throws org.xml.sax.SAXException   */  protected final void accum(String s) throws org.xml.sax.SAXException  {    try    {      m_writer.write(s);    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }  }  /**   * Flush the formatter's result stream.   *   * @throws org.xml.sax.SAXException   */  public final void flushWriter() throws org.xml.sax.SAXException  {    if (null != m_writer)    {      try

⌨️ 快捷键说明

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