xmlstreamreaderimpl.java

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

JAVA
1,416
字号
      }      if (ch == ':' && prefix < 0)        prefix = length;      nameBuffer[length++] = (char) ch;    }    unread();    name._length = length;    name._prefix = prefix;  }  /**   * Parses a name.   */  private StaxIntern.Entry readName(boolean isAttribute)    throws XMLStreamException  {    char []inputBuf = _inputBuf;    int inputLength = _inputLength;    int inputOffset = _inputOffset;        char []valueBuf = _cBuf;    int valueOffset = 0;    int colon = 0;    while (true) {      if (inputOffset < inputLength) {        char ch = inputBuf[inputOffset++];        if (IS_XML_NAME[ch]) {          valueBuf[valueOffset++] = ch;        }        else if (ch == ':') {          if (colon <= 0)            colon = valueOffset;          valueBuf[valueOffset++] = ch;        }        else {          _inputOffset = inputOffset - 1;          return _intern.add(valueBuf, 0, valueOffset, colon, isAttribute);        }      }      else if (fillBuffer()) {        inputLength = _inputLength;        inputOffset = _inputOffset;      }      else {        return _intern.add(valueBuf, 0, valueOffset, colon, isAttribute);      }    }  }  private static boolean isXmlName(int ch)  {    return ('a' <= ch && ch <= 'z'            || 'A' <= ch && ch <= 'Z'            || '0' <= ch && ch <= '9'            || ch == ':'            || ch == '+'            || ch == '_'            || ch == '-');  }  private int skipWhitespace()    throws XMLStreamException  {    int ch;    while ((ch = read()) == ' ' || ch == '\t' || ch == '\r' || ch == '\n') {    }    return ch;  }  /**   * Reads the <?xml ... ?> declaraction   */  private void readHeader()    throws XMLStreamException  {    // The reading at this point must use the underlying stream because    // the encoding is not determined until the end of the declaration    int ch;    ch = readByte();    if (ch == (char)0xFE) {      if (readByte() != (char)0xFF)        throw new XMLStreamException("found unrecognized BOM");            ch = readByte();    }     else if (ch == (char)0xFF) {      if (readByte() != (char)0xFE)        throw new UnsupportedOperationException("found byte-swapped BOM");      else        throw new XMLStreamException("found unrecognized BOM");    }    if (ch != '<') {      unreadByte();    }    else if ((ch = readByte()) != '?') {      unreadByte();      unreadByte();    }    else if ((ch = readByte()) != 'x') {      unreadByte();      unreadByte();      unreadByte();    }    else if ((ch = readByte()) != 'm') {      unreadByte();      unreadByte();      unreadByte();      unreadByte();    }    else if ((ch = readByte()) != 'l') {      unreadByte();      unreadByte();      unreadByte();      unreadByte();      unreadByte();    }    else {      CharBuffer directive = new CharBuffer();      while ((ch = readByte()) >= 0 && ch != '?')        directive.append((char)ch);      String data = directive.toString().trim();      if (data.startsWith("version")) {        data = data.substring(7).trim();        data = data.substring(1).trim();  // remove "="        char quot = data.charAt(0);        _version = data.substring(1, data.indexOf(quot, 1));        data = data.substring(data.indexOf(quot, 1)+1).trim();      }      if (data.startsWith("encoding")) {        data = data.substring(8).trim();        data = data.substring(1).trim();  // remove "="        char quot = data.charAt(0);        _encoding = data.substring(1, data.indexOf(quot, 1));        data = data.substring(data.indexOf(quot, 1)+1).trim();        try {          if (_is != null)            _is.setEncoding(_encoding);        }        catch (java.io.UnsupportedEncodingException e) {          throw new XMLStreamException(e);        }      }      ch = readByte();      if (ch != '>')        throw error(L.l("Expected '>' at end of '<?xml' declaration at {0}",                        charName(ch)));    }    skipWhitespace();    unread();  }  /**   * Reads and validate next character.   */  private void expect(int expect)    throws XMLStreamException  {    int ch = read();    if (ch != expect)      throw error(L.l("expected {0} at {1}", charName(expect), charName(ch)));  }  /**   * Reads a character.   */  private int read()    throws XMLStreamException  {    if (_inputLength <= _inputOffset && ! fillBuffer())      return -1;    int ch = _inputBuf[_inputOffset++];    _offset++;    // XXX '\r'    if (ch == '\n') {      _row++;      _lastCol = _col;      _col = 1;    }    else      _col++;    return ch;  }  /**   * Reads a character.   */  private void unread()  {    if (_inputOffset > 0) {      _inputOffset--;      _offset--;      if (_col > 1)        _col--;      if (_inputBuf[_inputOffset] == '\n') {        _row--;        _col = _lastCol;      }    }  }  /**   * Reads a character.   */  private int readByte()    throws XMLStreamException  {    try {      if (_inputLength <= _inputOffset) {        int ch = -1;                if (_is != null)          ch = _is.read();        else if (_reader != null)          ch = _reader.read();        if (ch < 0)          return ch;        if (_inputBuf.length <= _inputLength)          _inputLength = 0;        _inputBuf[_inputLength++] = (char) ch;        _inputOffset = _inputLength;        _offset++;        // XXX '\r'        if (ch == '\n') {          _row++;          _col = 1;        }        else          _col++;        return ch;      }      else        return _inputBuf[_inputOffset++];    }     catch (IOException e) {      throw new XMLStreamException(e);    }  }  /**   * Unreads a byte.   */  private void unreadByte()  {    if (_inputOffset > 0)      _inputOffset--;  }  /**   * Fills the input buffer.   */  private final boolean fillBuffer()    throws XMLStreamException  {    try {      if (_is != null) {        _inputOffset = 0;        _inputLength = _is.read(_inputBuf, 0, _inputBuf.length);        return _inputLength > 0;      }      else if (_reader != null) {        _inputOffset = 0;        _inputLength = _reader.read(_inputBuf, 0, _inputBuf.length);        return _inputLength > 0;      }      else {        _inputOffset = 0;        _inputLength = 0;        return false;      }    } catch (IOException e) {      throw new XMLStreamException(e);    }  }  private String charName(int ch)  {    if (ch > 0x20 && ch <= 0x7f)      return "'" + (char) ch + "'";    else      return "0x" + Integer.toHexString(ch);  }  private XMLStreamException error(String s)  {    return new XMLStreamException(location() + s);  }  private String location()  {    return ":" + _row + ":" + _col + " ";  }  public void close() throws XMLStreamException  {    TempCharBuffer tempCharBuffer = _tempCharBuffer;    _tempCharBuffer = null;    _cBuf = null;          TempCharBuffer tempInputBuffer = _tempInputBuffer;    _tempInputBuffer = null;    _inputBuf = null;    _inputOffset = _inputLength = 0;          if (tempCharBuffer != null)      TempCharBuffer.free(tempCharBuffer);          if (tempInputBuffer != null)      TempCharBuffer.free(tempInputBuffer);          ReadStream is = _is;    _is = null;    if (is != null)      is.close();    Reader r = _reader;    _reader = null;    if (r != null) {      try {        r.close();      }      catch (IOException e) {        throw new XMLStreamException(e);      }    }  }  static class RawName {    char []_buffer = new char[64];    int _prefix;    int _length;    public QName resolve(NamespaceContext nsc)    {      if (getPrefix() == null)        return new QName(nsc.getNamespaceURI(null),                         getLocalName());      return new QName(nsc.getNamespaceURI(getPrefix()),                       getLocalName(),                       getPrefix());    }    public String toString()    {      return new String(_buffer, 0, _length);    }    String getLocalName()    {      return new String(_buffer, _prefix + 1, _length - _prefix - 1);    }    String getPrefix()    {      if (_prefix==-1) return null;      return new String(_buffer, 0, _prefix);    }    void expandCapacity()    {      char []newBuffer = new char[_buffer.length + 64];      System.arraycopy(_buffer, 0, newBuffer, 0, _buffer.length);      _buffer = newBuffer;    }  }  /*  static class NSContext {    NSContext _parent;    public NSContext(NSContext parent)    {      _parent = parent;    }  }  */  static {    for (int i = 0; i < IS_XML_NAME.length; i++) {      if (isXmlName(i) && i != ':')        IS_XML_NAME[i] = isXmlName(i);    }  }  private class StreamReaderLocation implements Location {    private int _offset;    private int _row;    private int _col;    public StreamReaderLocation(int ofs, int row, int col)    {      _offset = ofs;      _row = row;      _col = col;    }    public int getCharacterOffset()    {      return _offset;    }    public int getColumnNumber()    {      return _col;    }    public int getLineNumber()    {      return _row;    }    public String getPublicId()    {      return _publicId;    }    public String getSystemId()    {      return _systemId;    }    public String toString() {      return _row + ":" + _col;    }  }}

⌨️ 快捷键说明

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