⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlstreamwriterimpl.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* XMLStreamWriterImpl.java --    Copyright (C) 2005  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package gnu.xml.stream;import java.io.IOException;import java.io.Writer;import java.util.Enumeration;import java.util.HashSet;import java.util.LinkedList;import java.util.Set;import javax.xml.XMLConstants;import javax.xml.namespace.NamespaceContext;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamWriter;import org.xml.sax.helpers.NamespaceSupport;/** * Simple XML stream writer. * * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> */public class XMLStreamWriterImpl  implements XMLStreamWriter{  /**   * The underlying character stream to write to.   */  protected final Writer writer;  /**   * The encoding being used.   * Note that this must match the encoding of the character stream.   */  protected final String encoding;  /**   * Whether prefix defaulting is being used.   * If true and a prefix has not been defined for a namespace specified on   * an element or an attribute, a new prefix and namespace declaration will   * be created.   */  protected final boolean prefixDefaulting;  /**   * The namespace context used to determine the namespace-prefix mappings   * in scope.   */  protected NamespaceContext namespaceContext;    /**   * The stack of elements in scope.   * Used to close the remaining elements.   */  private LinkedList elements;  /**   * Whether a start element has been opened but not yet closed.   */  private boolean inStartElement;  /**   * Whether we are in an empty element.   */  private boolean emptyElement;    private NamespaceSupport namespaces;  private int count = 0;  /**   * Constructor.   * @see #writer   * @see #encoding   * @see #prefixDefaulting   */  protected XMLStreamWriterImpl(Writer writer, String encoding,                                boolean prefixDefaulting)  {    this.writer = writer;    this.encoding = encoding;    this.prefixDefaulting = prefixDefaulting;    elements = new LinkedList();    namespaces = new NamespaceSupport();  }  /**   * Write the end of a start-element event.   * This will close the element if it was defined to be an empty element.   */  private void endStartElement()    throws IOException  {    if (!inStartElement)      return;    if (emptyElement)      {        writer.write('/');        elements.removeLast();        namespaces.popContext();        emptyElement = false;      }    writer.write('>');    inStartElement = false;  }  public void writeStartElement(String localName)    throws XMLStreamException  {    try      {        endStartElement();        namespaces.pushContext();                writer.write('<');        writer.write(localName);                elements.addLast(new String[] { null, localName });        inStartElement = true;      }    catch (IOException e)      {        XMLStreamException e2 = new XMLStreamException(e);        e2.initCause(e);        throw e2;      }  }  public void writeStartElement(String namespaceURI, String localName)    throws XMLStreamException  {    try      {        endStartElement();        namespaces.pushContext();                String prefix = getPrefix(namespaceURI);        boolean isDeclared = (prefix != null);        if (!isDeclared)          {            if (prefixDefaulting)              prefix = createPrefix(namespaceURI);            else              throw new XMLStreamException("namespace " + namespaceURI +                                           " has not been declared");          }        writer.write('<');        if (!"".equals(prefix))          {            writer.write(prefix);            writer.write(':');          }        writer.write(localName);        inStartElement = true;        if (!isDeclared)          {            writeNamespace(prefix, namespaceURI);          }                elements.addLast(new String[] { prefix, localName });      }    catch (IOException e)      {        XMLStreamException e2 = new XMLStreamException(e);        e2.initCause(e);        throw e2;      }  }  /**   * Creates a new unique prefix in the document.   * Subclasses may override this method to provide a suitably unique prefix   * for the given namespace.   * @param namespaceURI the namespace URI   */  protected String createPrefix(String namespaceURI)  {    Set prefixes = new HashSet();    for (Enumeration e = namespaces.getPrefixes(); e.hasMoreElements(); )      prefixes.add(e.nextElement());    String ret;    do      {        ret = "ns" + (count++);      }    while (prefixes.contains(ret));    return ret;  }  public void writeStartElement(String prefix, String localName,                                String namespaceURI)    throws XMLStreamException  {    try      {        endStartElement();        namespaces.pushContext();                String currentPrefix = getPrefix(namespaceURI);        boolean isCurrent = prefix.equals(currentPrefix);        writer.write('<');        if (!"".equals(prefix))          {            writer.write(prefix);            writer.write(':');          }        writer.write(localName);        if (prefixDefaulting && !isCurrent)          {            writeNamespace(prefix, namespaceURI);          }                elements.addLast(new String[] { prefix, localName });        inStartElement = true;      }    catch (IOException e)      {        XMLStreamException e2 = new XMLStreamException(e);        e2.initCause(e);        throw e2;      }  }  public void writeEmptyElement(String namespaceURI, String localName)    throws XMLStreamException  {    writeStartElement(namespaceURI, localName);    emptyElement = true;  }  public void writeEmptyElement(String prefix, String localName,                                String namespaceURI)    throws XMLStreamException  {    writeStartElement(prefix, localName, namespaceURI);    emptyElement = true;  }  public void writeEmptyElement(String localName)    throws XMLStreamException  {    writeStartElement(localName);    emptyElement = true;  }  public void writeEndElement()    throws XMLStreamException  {    try      {        endStartElement();        String[] element = (String[]) elements.removeLast();        namespaces.popContext();        writer.write('<');        writer.write('/');        if (element[0] != null && !"".equals(element[0]))          {            writer.write(element[0]);            writer.write(':');          }        writer.write(element[1]);        writer.write('>');      }    catch (IOException e)      {        XMLStreamException e2 = new XMLStreamException(e);        e2.initCause(e);        throw e2;      }  }  public void writeEndDocument()    throws XMLStreamException  {    while (!elements.isEmpty())      writeEndElement();  }  public void close()    throws XMLStreamException  {    flush();  }  public void flush()    throws XMLStreamException  {    try      {        writer.flush();      }    catch (IOException e)      {        XMLStreamException e2 = new XMLStreamException(e);        e2.initCause(e);        throw e2;      }  }  public void writeAttribute(String localName, String value)    throws XMLStreamException  {    if (!inStartElement)      throw new IllegalStateException();    try      {        writer.write(' ');        writer.write(localName);        writer.write('=');        writer.write('"');        writeEncoded(value, true);        writer.write('"');      }    catch (IOException e)      {        XMLStreamException e2 = new XMLStreamException(e);        e2.initCause(e);        throw e2;      }  }  public void writeAttribute(String prefix, String namespaceURI,                             String localName, String value)    throws XMLStreamException  {    if (!inStartElement)      throw new IllegalStateException();    try      {        String currentPrefix = getPrefix(namespaceURI);        if (currentPrefix == null)          {            if (prefixDefaulting)              writeNamespace(prefix, namespaceURI);            else              throw new XMLStreamException("namespace " + namespaceURI +                                           " is not bound");          }        else if (!currentPrefix.equals(prefix))          throw new XMLStreamException("namespace " + namespaceURI +                                       " is bound to prefix " +                                       currentPrefix);        writer.write(' ');        if (!"".equals(prefix))          {            writer.write(prefix);            writer.write(':');          }        writer.write(localName);        writer.write('=');

⌨️ 快捷键说明

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