xmlstreamreaderimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,416 行 · 第 1/3 页

JAVA
1,416
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson, Adam Megacz */package com.caucho.xml.stream;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import com.caucho.vfs.*;import javax.xml.XMLConstants;import javax.xml.namespace.NamespaceContext;import javax.xml.namespace.QName;import javax.xml.stream.Location;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import java.util.logging.Logger;/** * XML pull-parser interface. */public class XMLStreamReaderImpl implements XMLStreamReader {  private static final Logger log    = Logger.getLogger(XMLStreamReaderImpl.class.getName());  private static final L10N L = new L10N(XMLStreamReaderImpl.class);  private static final boolean []IS_XML_NAME = new boolean[65536];  private StaxIntern _intern;  private ReadStream _is;  private Reader _reader;  private int _lastCol = 1;  private int _col = 1;  private int _row = 1;  private int _offset = 1;  private NamespaceReaderContext _namespaceTracker;  private String _version = "1.0";  private String _encoding = "utf-8";  private String _encodingScheme;  private String _publicId;  private String _systemId;  private boolean _seenDocumentStart = false;  private int _current;  private int _state;  private boolean _isShortTag;  private boolean _isWhitespace = false;  private boolean _eofEncountered = false;  private String _processingInstructionTarget;  private String _processingInstructionData;  private RawName _rawTagName = new RawName();  private QName _name;  private StaxIntern.Entry []_attrRawNames = new StaxIntern.Entry[16];  private QName []_attrNames = new QName[16];  private String []_attrValues = new String[16];  private int _attrCount;  private final StringBuilder _sb = new StringBuilder();    private TempCharBuffer _tempInputBuffer;  private char []_inputBuf;  private int _inputOffset;  private int _inputLength;  private TempCharBuffer _tempCharBuffer;  private char []_cBuf;  private int _cBufLength;  public XMLStreamReaderImpl(InputStream is)    throws XMLStreamException  {    this(Vfs.openRead(is));  }  public XMLStreamReaderImpl(Reader r)    throws XMLStreamException  {    _reader = r;    init();  }  public XMLStreamReaderImpl(InputStream is, String systemId)    throws XMLStreamException  {    this(Vfs.openRead(is));        _systemId = systemId;  }  public XMLStreamReaderImpl(Reader reader, String systemId)    throws XMLStreamException  {    this(reader);        _systemId = systemId;  }  public XMLStreamReaderImpl(ReadStream is)    throws XMLStreamException  {    _is = is;        init();  }  public void init()    throws XMLStreamException  {    _namespaceTracker = new NamespaceReaderContext();    _intern = new StaxIntern(_namespaceTracker);        _tempCharBuffer = TempCharBuffer.allocate();    _cBuf = _tempCharBuffer.getBuffer();    _tempInputBuffer = TempCharBuffer.allocate();    _inputBuf = _tempInputBuffer.getBuffer();    _inputOffset = _inputLength = 0;    readHeader();    _current = START_DOCUMENT;  }    public int available()  {    return _inputLength - _inputOffset;  }  public int getAttributeCount()  {    return _attrCount;  }  public String getAttributeLocalName(int index)  {    if (_attrCount <= index)      throw new IllegalArgumentException(L.l("element only has {0} attributes, given index {1}",               															 _attrCount, index));    return _attrNames[index].getLocalPart();  }  public QName getAttributeName(int index)  {    if (_attrCount <= index)      throw new IllegalArgumentException(L.l("element only has {0} attributes, given index {1}",                                             _attrCount, index));    return _attrNames[index];  }  public String getAttributeNamespace(int index)  {    if (_attrCount <= index)      throw new IllegalArgumentException(L.l("element only has {0} attributes, given index {1}",                                             _attrCount, index));    String ret = _attrNames[index].getNamespaceURI();    // API quirk    if ("".equals(ret))      return null;    return ret;  }  public String getAttributePrefix(int index)  {    if (_attrCount <= index)      throw new IllegalArgumentException(L.l("element only has {0} attributes, given index {1}",                                             _attrCount, index));    String ret = _attrNames[index].getPrefix();    return ret;  }  public String getAttributeType(int index)  {    return "CDATA";  }  public String getAttributeValue(int index)  {    if (_attrCount <= index)      throw new IllegalArgumentException(L.l("element only has {0} attributes, given index {1}",                                             _attrCount, index));    return _attrValues[index];  }  public boolean isAttributeSpecified(int index)  {    return index < _attrCount;  }  public String getAttributeValue(String namespaceURI, String localName)  {    for (int i = _attrCount - 1; i >= 0; i--) {      QName name = _attrNames[i];      // namespaceURI == null means ignore namespace      if (namespaceURI == null) {        if (name.getLocalPart().equals(localName))           return _attrValues[i];      }      else if (name.getLocalPart().equals(localName)	       && name.getNamespaceURI().equals(namespaceURI))        return _attrValues[i];    }    return null;  }  public String getCharacterEncodingScheme()  {    return _encodingScheme;  }  public String getElementText() throws XMLStreamException  {    if (_current != START_ELEMENT)      throw new XMLStreamException(L.l("START_ELEMENT expected when calling getElementText()"));    StringBuilder sb = new StringBuilder();    for (int eventType = next(); eventType != END_ELEMENT; eventType = next()) {      switch (eventType) {        case CHARACTERS:        case CDATA:        case SPACE:        case ENTITY_REFERENCE:          sb.append(_cBuf, 0, _cBufLength);          break;        case PROCESSING_INSTRUCTION:        case COMMENT:          break;        case END_DOCUMENT:          throw new XMLStreamException(L.l("Document ended unexpectedly while reading element text"));        case START_ELEMENT:          throw new XMLStreamException(L.l("getElementText() encountered a START_ELEMENT; text only element expected"));        default:          throw new XMLStreamException(L.l("Unexpected event during getElementText(): {0}", eventType));      }    }    return sb.toString();  }  public String getEncoding()  {    return _encoding;  }  public int getEventType()  {    return _current;  }  public Location getLocation()  {    return new StreamReaderLocation(_offset, _row, _col);  }  public String getLocalName()  {    if (_name == null)      throw new IllegalStateException();    return _name.getLocalPart();  }  public String getNamespaceURI()  {    if (_name == null)      return null;    String uri = _name.getNamespaceURI();    if ("".equals(uri))      return null;    else      return uri;  }  public QName getName()  {    return _name;  }  public NamespaceContext getNamespaceContext()  {    return _namespaceTracker;  }  public int getNamespaceCount()  {    return _namespaceTracker.getNumDecls();  }  public String getNamespacePrefix(int index)  {    String prefix = _namespaceTracker.getPrefix(index);    // The API specifies that this function return a different value for    // the default namespace, null, than any other function, which all return    // the constant defined in XMLConstants.    if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix))      return null;    else      return prefix;  }  public String getNamespaceURI(int index)  {    return _namespaceTracker.getUri(index);  }  public String getNamespaceURI(String prefix)  {    return _namespaceTracker.getUri(prefix);  }  public String getPIData()  {    if (_current != PROCESSING_INSTRUCTION)      return null;    return _processingInstructionData;  }  public String getPITarget()  {    if (_current != PROCESSING_INSTRUCTION)      return null;    return _processingInstructionTarget;  }  public String getPrefix()  {    if (_name == null)      return null;    String prefix = _name.getPrefix();    // xml/3000, xml/3009    if ("" == prefix && "" == _name.getNamespaceURI())      return null;    return prefix;  }  public Object getProperty(String name) throws IllegalArgumentException  {    if ("javax.xml.stream.notations".equals(name)) {      throw new UnsupportedOperationException(getClass().getName());    }    else if ("javax.xml.stream.entities".equals(name)) {      throw new UnsupportedOperationException(getClass().getName());    }    else {      throw        new IllegalArgumentException("property \""+name+"+\" not supported");    }  }  /**   * Returns the current text string.   */  public String getText()  {    return new String(_cBuf, 0, _cBufLength);  }  /**   * Returns a character buffer for the current text.   */  public char[] getTextCharacters()  {    return _cBuf;  }  /**   * Reads the current text into a buffer.   */  public int getTextCharacters(int sourceStart, char[] target,                               int targetStart, int length)    throws XMLStreamException  {    int sublen = _cBufLength - sourceStart;        if (length < sublen)      sublen = length;        System.arraycopy(_cBuf, sourceStart, target, targetStart, sublen);        return sublen;  }  /**   * Returns the length of the current text.   */  public int getTextLength()  {    return _cBufLength;  }  /**   * Returns the offset of the current text.   */  public int getTextStart()  {    return 0;  }  public String getVersion()  {    return _version;  }  public boolean hasName()  {    return _name != null;  }  public boolean hasText()  {    switch(getEventType()) {    case CHARACTERS:    case DTD:    case ENTITY_REFERENCE:

⌨️ 快捷键说明

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