serializertoxml.java

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

JAVA
2,458
字号
      catch (javax.xml.transform.TransformerException te)      {        throw new org.apache.xml.utils.WrappedRuntimeException(te);      }    }  }  /**   * Initialize the serializer with the specified output stream and output format.   * Must be called before calling any of the serialize methods.   *   * @param output The output stream to use   * @param format The output format   * @throws UnsupportedEncodingException The encoding specified   *   in the output format is not supported   */  public synchronized void init(OutputStream output, Properties format)          throws UnsupportedEncodingException  {    if (null == format)    {      OutputProperties op = new OutputProperties(Method.XML);      format = op.getProperties();    }    m_encoding =      Encodings.getMimeEncoding(format.getProperty(OutputKeys.ENCODING));    if (m_encoding.equalsIgnoreCase("UTF-8"))    {      m_isUTF8 = true;      if(output instanceof java.io.BufferedOutputStream)      {        init(new WriterToUTF8(output), format, true);      }      else if(output instanceof java.io.FileOutputStream)      {        init(new WriterToUTF8Buffered(output), format, true);      }      else      {        // Not sure what to do in this case.  I'm going to be conservative         // and not buffer.        init(new WriterToUTF8(output), format, true);      }          }    else if (m_encoding.equals("WINDOWS-1250")             || m_encoding.equals("US-ASCII") || m_encoding.equals("ASCII"))    {      init(new WriterToASCI(output), format, true);    }    else    {      Writer osw;      try      {        osw = Encodings.getWriter(output, m_encoding);      }      catch (UnsupportedEncodingException uee)      {        System.out.println("Warning: encoding \"" + m_encoding                           + "\" not supported" + ", using "                           + Encodings.DEFAULT_MIME_ENCODING);        m_encoding = Encodings.DEFAULT_MIME_ENCODING;        osw = Encodings.getWriter(output, m_encoding);      }      m_maxCharacter = Encodings.getLastPrintable(m_encoding);      init(osw, format, true);    }      }  /**   * Receive an object for locating the origin of SAX document events.   *   * @param locator An object that can return the location of   *                any SAX document event.   * @see org.xml.sax.Locator   */  public void setDocumentLocator(Locator locator)  {    // I don't do anything with this yet.  }  /**   * Output the doc type declaration.   *   * @param name non-null reference to document type name.   * NEEDSDOC @param closeDecl   *   * @throws org.xml.sax.SAXException   */  void outputDocTypeDecl(String name, boolean closeDecl)          throws org.xml.sax.SAXException  {    try    {      final Writer writer = m_writer;        writer.write("<!DOCTYPE ");      writer.write(name);        if (null != m_doctypePublic)      {        writer.write(" PUBLIC \"");        writer.write(m_doctypePublic);        writer.write('\"');      }        if (null != m_doctypeSystem)      {        if (null == m_doctypePublic)          writer.write(" SYSTEM \"");        else          writer.write(" \"");          writer.write(m_doctypeSystem);          if (closeDecl)        {          writer.write("\">");          writer.write(m_lineSep, 0, m_lineSepLen);;        }        else          writer.write('\"');      }    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }  }  /**   * Output the doc type declaration.   *   * @param name non-null reference to document type name.   * NEEDSDOC @param value   *   * @throws org.xml.sax.SAXException   */  void outputEntityDecl(String name, String value)          throws org.xml.sax.SAXException  {    try    {      final Writer writer = m_writer;      writer.write("<!ENTITY ");      writer.write(name);      writer.write(" \"");      writer.write(value);      writer.write("\">");      writer.write(m_lineSep, 0, m_lineSepLen);    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }  }  /**   * Receive notification of the beginning of a document.   *   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   *   * @throws org.xml.sax.SAXException   */  public void startDocument() throws org.xml.sax.SAXException  {    if (m_inEntityRef)      return;    m_needToOutputDocTypeDecl = true;    m_startNewLine = false;    if (m_shouldNotWriteXMLHeader == false)    {      String encoding = Encodings.getMimeEncoding(m_encoding);      String version = (null == m_version) ? "1.0" : m_version;      String standalone;      if (m_standaloneWasSpecified)      {        standalone = " standalone=\"" + (m_standalone ? "yes" : "no") + "\"";      }      else      {        standalone = "";      }      try      {        final Writer writer = m_writer;        writer.write("<?xml version=\"");        writer.write(version);        writer.write("\" encoding=\"");        writer.write(encoding);        writer.write('\"');        writer.write(standalone);        writer.write("?>");        writer.write(m_lineSep, 0, m_lineSepLen);      }      catch(IOException ioe)      {        throw new SAXException(ioe);      }    }  }  /**   * Receive notification of the end of a document.   *   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   *   * @throws org.xml.sax.SAXException   */  public void endDocument() throws org.xml.sax.SAXException  {    if (m_doIndent &&!m_isprevtext)    {      outputLineSep();    }    flushWriter();  }  /**   * Report the start of DTD declarations, if any.   *   * Any declarations are assumed to be in the internal subset   * unless otherwise indicated.   *   * @param name The document type name.   * @param publicId The declared public identifier for the   *        external DTD subset, or null if none was declared.   * @param systemId The declared system identifier for the   *        external DTD subset, or null if none was declared.   * @throws org.xml.sax.SAXException The application may raise an   *            exception.   * @see #endDTD   * @see #startEntity   */  public void startDTD(String name, String publicId, String systemId)          throws org.xml.sax.SAXException  {    m_doctypeSystem = systemId;    m_doctypePublic = publicId;    if ((true == m_needToOutputDocTypeDecl))  // && (null != m_doctypeSystem))    {      outputDocTypeDecl(name, false);    }    m_needToOutputDocTypeDecl = false;    m_inDoctype = true;  }  /**   * Report the end of DTD declarations.   *   * @throws org.xml.sax.SAXException The application may raise an exception.   * @see #startDTD   */  public void endDTD() throws org.xml.sax.SAXException  {    try    {      if (!m_inDoctype)        m_writer.write("]>");      else      {        m_writer.write('>');      }        m_writer.write(m_lineSep, 0, m_lineSepLen);    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }    // Do nothing for now.  }  /**   * Begin the scope of a prefix-URI Namespace mapping.   * @see org.xml.sax.ContentHandler#startPrefixMapping   *   * @param prefix The Namespace prefix being declared.   * @param uri The Namespace URI the prefix is mapped to.   * @throws org.xml.sax.SAXException The client may throw   *            an exception during processing.   */  public void startPrefixMapping(String prefix, String uri)          throws org.xml.sax.SAXException{}  /**   * End the scope of a prefix-URI Namespace mapping.   * @see org.xml.sax.ContentHandler#endPrefixMapping   *   * @param prefix The prefix that was being mapping.   * @throws org.xml.sax.SAXException The client may throw   *            an exception during processing.   */  public void endPrefixMapping(String prefix)          throws org.xml.sax.SAXException{}  /**   * Tell if two strings are equal, without worry if the first string is null.   *   * @param p String reference, which may be null.   * @param t String reference, which may be null.   *   * @return true if strings are equal.   */  protected static final boolean subPartMatch(String p, String t)  {    return (p == t) || ((null != p) && (p.equals(t)));  }  /**   * Push a boolean state based on if the name of the element   * is found in the list of qnames.  A state is always pushed,   * one way or the other.   *   * @param namespaceURI Should be a non-null reference to the namespace URL   *        of the element that owns the state, or empty string.   * @param localName Should be a non-null reference to the local name   *        of the element that owns the state.   * @param qnames Vector of qualified names of elements, or null.   * @param state The stack where the state should be pushed.   */  protected void pushState(String namespaceURI, String localName,                           Vector qnames, BoolStack state)  {    boolean b;    if (null != qnames)    {      b = false;      if ((null != namespaceURI) && namespaceURI.length() == 0)        namespaceURI = null;      int nElems = qnames.size();      for (int i = 0; i < nElems; i++)      {        QName q = (QName) qnames.elementAt(i);        if (q.getLocalName().equals(localName)                && subPartMatch(namespaceURI, q.getNamespaceURI()))        {          b = true;          break;        }      }    }    else    {      b = state.peekOrFalse();    }    state.push(b);  }  /**   * Receive notification of the beginning 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.   * @param atts The attributes attached to the element, if any.   * @throws org.xml.sax.SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#startElement   * @see org.xml.sax.ContentHandler#endElement   * @see org.xml.sax.AttributeList   *   * @throws org.xml.sax.SAXException   */  public void startElement(          String namespaceURI, String localName, String name, Attributes atts)            throws org.xml.sax.SAXException  {    if (DEBUG)    {      System.out.println("SerializerToXML - startElement: " + namespaceURI                         + ", " + localName);      int n = atts.getLength();      for (int i = 0; i < n; i++)      {        System.out.println("atts[" + i + "]: " + atts.getQName(i) + " = "                           + atts.getValue(i));      }      if (null == namespaceURI)      {        (new RuntimeException(localName                              + " has a null namespace!")).printStackTrace();      }    }    if (m_inEntityRef)      return;    if ((true == m_needToOutputDocTypeDecl) && (null != m_doctypeSystem))    {      outputDocTypeDecl(name, true);    }    m_needToOutputDocTypeDecl = false;    writeParentTagEnd();    pushState(namespaceURI, localName, m_cdataSectionNames,              m_cdataSectionStates);    // pushState(namespaceURI, localName, m_format.getNonEscapingElements(),    //          m_disableOutputEscapingStates);    m_ispreserve = false;    //  System.out.println(name+": m_doIndent = "+m_doIndent+", m_ispreserve = "+m_ispreserve+", m_isprevtext = "+m_isprevtext);    if (shouldIndent() && m_startNewLine)    {      indent(m_currentIndent);    }    m_startNewLine = true;    try    {      m_writer.write('<');      m_writer.write(name);    }    catch(IOException ioe)    {      throw new SAXException(ioe);    }    int nAttrs = atts.getLength();    for (int i = 0; i < nAttrs; i++)    {      processAttribute(atts.getQName(i), atts.getValue(i));    }    // Flag the current element as not yet having any children.    openElementForChildren();    m_currentIndent += m_indentAmount;    m_isprevtext = false;  }  /**   * Check to see if a parent's ">" has been written, and, if   * it has not, write it.   *   * @throws org.xml.sax.SAXException   */  protected void writeParentTagEnd() throws org.xml.sax.SAXException  {    // See if the parent element has already been flagged as having children.    if (!m_elemStack.peekOrTrue())    {      try      {        m_writer.write('>');      }

⌨️ 快捷键说明

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