xmlstreamwriterimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 650 行 · 第 1/2 页

JAVA
650
字号
        pushContext(qname);      }      _pendingTagName = qname;      _shortTag = true;    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }  public void writeEmptyElement(String prefix, String localName,                                String namespaceURI)    throws XMLStreamException  {    flushPending();    try {      QName qname = new QName(namespaceURI, localName, prefix);      if (_repair && _tracker.getPrefix(namespaceURI) == null) {        // NOTE: We have to push before we declare because declare will        // declare the namespace in the parent context if we don't        flushContext();        _tracker.push();        _tracker.declare(prefix, namespaceURI, true);        _tracker.setElementName(qname);        _flushed = false;      }      else        pushContext(qname);      _pendingTagName = qname;      _shortTag = true;    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }  public void writeEndDocument()    throws XMLStreamException  {  }  public void writeEndElement()    throws XMLStreamException  {    writeEndElement(null, null);  }  public void writeEndElement(String localName)    throws XMLStreamException  {    writeEndElement(null, localName);  }  public void writeEndElement(String namespaceURI, String localName)    throws XMLStreamException  {    flushPending();    try {      QName name = popContext();      if ((localName != null && !localName.equals(name.getLocalPart()))          || (namespaceURI != null && !namespaceURI.equals(name.getNamespaceURI())))        throw new XMLStreamException(L.l("unbalanced close, expecting `{0}' not `{1}'",                                         name, new QName(namespaceURI, localName)));      _out.print("</");      _out.print(printQName(name));      _out.print(">");      if (_indent >= 0) {        _out.println();        _currentIndent -= _indent;      }    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }  private static String printQName(QName name) {    if (name.getPrefix() == null || name.getPrefix().equals(""))      return name.getLocalPart();    return name.getPrefix() + ":" + name.getLocalPart();  }  public void writeEntityRef(String name)    throws XMLStreamException  {    flushPending();    try {      _out.print("&");      _out.print(name);      _out.print(";");    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }  public void writeNamespace(String prefix, String namespaceURI)    throws XMLStreamException  {    if (_pendingTagName == null)      throw new XMLStreamException("Namespace written before element");    if (prefix == null || "".equals(prefix) || "xmlns".equals(prefix))      writeDefaultNamespace(namespaceURI);    else      _tracker.declare(prefix, namespaceURI, true);  }  public void writeProcessingInstruction(String target)    throws XMLStreamException  {    flushPending();    try {      _out.print("<?");      _out.print(target);      _out.print("?>");    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }  public void writeProcessingInstruction(String target, String data)    throws XMLStreamException  {    flushPending();    try {      _out.print("<?");      _out.print(target);      _out.print(" ");      _out.print(data);      _out.print("?>");    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }  public void writeStartDocument()    throws XMLStreamException  {    writeStartDocument("1.0");  }  public void writeStartDocument(String version)    throws XMLStreamException  {    writeStartDocument("utf-8", version);  }  public void writeStartDocument(String encoding, String version)    throws XMLStreamException  {    try {      _out.print("<?xml version=\""+version+"\" encoding=\""+encoding+"\"?>");    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }  public void writeStartElement(String localName)    throws XMLStreamException  {    flushPending();    try {      QName qname = new QName(localName);      pushContext(qname);      _pendingTagName = qname;    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }  public void writeStartElement(String namespaceURI, String localName)    throws XMLStreamException  {    flushPending();    try {      QName qname = null;      if (_repair) {        // NOTE: We have to push before we declare because declare will        // declare the namespace in the parent context if we don't        flushContext();        _tracker.push();        String prefix = _tracker.declare(namespaceURI);        if (prefix == null)          qname = new QName(namespaceURI, localName);        else          qname = new QName(namespaceURI, localName, prefix);        _tracker.setElementName(qname);        _flushed = false;      }      else {        String prefix = _tracker.getPrefix(namespaceURI);        if (prefix == null)          throw new XMLStreamException(L.l("No prefix defined for namespace {0}", namespaceURI));        qname = new QName(namespaceURI, localName, prefix);        pushContext(qname);      }      _pendingTagName = qname;    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }  public void writeStartElement(String prefix, String localName,                                String namespaceURI)    throws XMLStreamException  {    flushPending();    try {      QName qname = new QName(namespaceURI, localName, prefix);      if (_repair && _tracker.getPrefix(namespaceURI) == null) {        // NOTE: We have to push before we declare because declare will        // declare the namespace in the parent context if we don't        flushContext();        _tracker.push();        _tracker.declare(prefix, namespaceURI, true);        _tracker.setElementName(qname);        _flushed = false;      }      else        pushContext(qname);      _pendingTagName = qname;    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }  /////////////////////////////////////////////////////////////////////////  private void pushContext(QName elementName)    throws IOException  {    flushContext();    _tracker.push();    _tracker.setElementName(elementName);    _flushed = false;  }  private QName popContext()    throws IOException, XMLStreamException  {    flushContext();    QName name = _tracker.getElementName();    _tracker.pop();    return name;  }  private void flushContext()    throws IOException  {    if (_flushed)      return;        _tracker.emitDeclarations(_out);    _flushed = true;  }  private void flushPending()    throws XMLStreamException  {    try {      if (_pendingTagName == null)	return;      _out.print("<");      _out.print(printQName(_pendingTagName));            for(int i = 0; i < _pendingAttributeNames.size(); i++) {        _out.print(" ");        _out.print(printQName(_pendingAttributeNames.get(i)));        _out.print("=\"");        Escapifier.escape(_pendingAttributeValues.get(i), _out);        _out.print('"');      }      flushContext();      if (_shortTag) {        _out.print("/>");        popContext();      } else {        _out.print(">");        if (_indent > -1)          _currentIndent += _indent;      }            _pendingTagName = null;      _pendingAttributeNames.clear();      _pendingAttributeValues.clear();      _shortTag = false;    }    catch (IOException e) {      throw new XMLStreamException(e);    }  }}

⌨️ 快捷键说明

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