xmlstreamreaderimpl.java

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

JAVA
1,416
字号
    case COMMENT:    case SPACE:      return true;    default:      return false;    }  }  public boolean isCharacters()  {    return _current == CHARACTERS;  }  public boolean isEndElement()  {    if (_current == END_ELEMENT)      return true;    // php/4618    if (_current == START_ELEMENT && _isShortTag)      return true;    return false;  }  public boolean isStandalone()  {    return false;  }  public boolean isStartElement()  {    return _current == START_ELEMENT;  }  public boolean isWhiteSpace()  {    return (_isWhitespace	    && (_current == CHARACTERS || _current == SPACE));  }  /**   * Skips until the next START_ELEMENT or END_ELEMENT   */  public int nextTag() throws XMLStreamException  {    while (true) {      int tag = next();      if (tag < 0          || tag == START_ELEMENT          || tag == END_ELEMENT) {        return tag;      }    }  }  public void require(int type, String namespaceURI, String localName)    throws XMLStreamException  {    if (type != _current) {      StringBuilder sb = new StringBuilder();      sb.append("expected ");      sb.append(StaxUtil.constantToString(type));      if (type == START_ELEMENT || type == END_ELEMENT) {        sb.append('(');        sb.append(localName);        sb.append(')');      }      sb.append(", found ");      sb.append(StaxUtil.constantToString(_current));      if (_current == START_ELEMENT || _current == END_ELEMENT) {        sb.append('(');        sb.append(getLocalName());        sb.append(')');      }      sb.append(" at ");      sb.append(getLocation());      throw new XMLStreamException(sb.toString());    }    if (localName != null && !localName.equals(getLocalName())) {      if (type == START_ELEMENT) {        throw new XMLStreamException("expected <" + localName + ">, found " +                                     "<" + getLocalName() + "> at " +                                     getLocation());      }      else if (type == END_ELEMENT) {        throw new XMLStreamException("expected </" + localName + ">, found " +                                     "</" + getLocalName() + "> at " +                                     getLocation());      }    }    if (namespaceURI != null && !namespaceURI.equals(getNamespaceURI()))      throw new XMLStreamException("expected xmlns="+namespaceURI+                                   ", found xmlns="+getNamespaceURI() +                                   " at " + getLocation());  }  public boolean standaloneSet()  {    return isStandalone();  }  public boolean hasNext() throws XMLStreamException  {    if (_is == null && _reader == null)      return false;    return _current != END_DOCUMENT;  }  public int next() throws XMLStreamException  {    try {      _current = readNext();    } catch (IOException e) {      throw new XMLStreamException(e);    }    if (_current > 0)      return _current;    else {      if (_eofEncountered)        return _current = -1;      _eofEncountered = true;            return _current = END_DOCUMENT;    }  }  private int readNext()    throws IOException, XMLStreamException  {    _cBufLength = 0;    // we pop the namespace context when the user is finished    // working with the END_ELEMENT event    if (_current == END_ELEMENT)      _namespaceTracker.pop();        if (_isShortTag) {      _isShortTag = false;      return END_ELEMENT;    }    _name = null;    int ch = read();    if (ch == '<') {      ch = read();      switch (ch) {      case '/':        _name = readName(false).getQName();        expect('>');        return END_ELEMENT;      case '!':        expect('-');        expect('-');        return readComment();      case '?':        readProcessingDirective();        return PROCESSING_INSTRUCTION;      default:        unread();        readElementBegin();        return START_ELEMENT;      }    }    else if (ch < 0) {      close();            return -1;    }    else {      unread();      return readData();    }  }  private void readElementBegin()    throws IOException, XMLStreamException  {    _namespaceTracker.push();    StaxIntern.Entry eltName = readName(false);    _isShortTag = false;    int ch = readAttributes();    if (ch == '>') {    }    else if (ch == '/') {      _isShortTag = true;      expect('>');    }    else      throw error(L.l("Expected {0} at {1}", ">", charName(ch)));    for (int i = _attrCount - 1; i >= 0; i--)      _attrNames[i] = _attrRawNames[i].getQName();    _name = eltName.getQName();  }  private int readAttributes()    throws IOException, XMLStreamException  {    int ch;    int attrCount = 0;    while ((ch = skipWhitespace()) >= 0 && IS_XML_NAME[ch]) {      unread();      if (_attrRawNames.length <= attrCount)        extendAttrs();      StaxIntern.Entry rawName = readName(true);      ch = skipWhitespace();      if (ch != '=')        throw error(L.l("attribute expects '=' at {0}", charName(ch)));      ch = skipWhitespace();      if (ch == '\'' || ch == '"') {        if ("xmlns".equals(rawName.getPrefix())) {          _namespaceTracker.declare(rawName.getLocalName(), readValue(ch));        }        else if ("xmlns".equals(rawName.getLocalName())) {          _namespaceTracker.declare(XMLConstants.DEFAULT_NS_PREFIX,                                     readValue(ch));        }        else {          _attrRawNames[attrCount] = rawName;          _attrValues[attrCount++] = readValue(ch);        }      }      else        throw error(L.l("attribute expects value at {0}", charName(ch)));    }    _attrCount = attrCount;    return ch;  }  private String readValue(int end)    throws XMLStreamException  {    char []valueBuffer = _cBuf;    int valueIndex = 0;    while (true) {      int ch = read();      switch (ch) {        case -1:          return new String(valueBuffer, 0, valueIndex);        case '"': case '\'':          if (ch == end)            return new String(valueBuffer, 0, valueIndex);          else            valueBuffer[valueIndex++] = (char) ch;          break;        case '&':          valueBuffer[valueIndex++] = (char) ch;          break;        default:          valueBuffer[valueIndex++] = (char) ch;          break;      }    }  }  private void extendAttrs()  {    int length = _attrRawNames.length;    StaxIntern.Entry []attrRawNames = new StaxIntern.Entry[length + 16];    System.arraycopy(_attrRawNames, 0, attrRawNames, 0, length);    _attrRawNames = attrRawNames;    QName []attrNames = new QName[length + 16];    System.arraycopy(_attrNames, 0, attrNames, 0, length);    _attrNames = attrNames;    String []attrValues = new String[length + 16];    System.arraycopy(_attrValues, 0, attrValues, 0, length);    _attrValues = attrValues;  }  private int readData()    throws IOException, XMLStreamException  {    int ch = 0;    _isWhitespace = true;    int index = 0;    char []cBuf = _cBuf;    int length = cBuf.length;    int entity = -1;        loop:    for (; index < length && (ch = read()) >= 0; index++) {      switch (ch) {      case '<':        unread();        break loop;	      case '&':        if (cBuf.length <= index + 256) {          unread();          break loop;        }        cBuf[index] = (char) ch;        entity = index;        break;	      case '\r':        ch = read();        if (ch != '\n') { ch = '\r'; unread(); }	      case ' ': case '\t': case '\n':        cBuf[index] = (char) ch;        break;	      case ';':        if (entity >= 0) {          String unresolved = new String(cBuf, entity + 1, index - entity - 1);          String resolved = resolveEntity(unresolved);          // the loop will advance index + 1          index = entity + resolved.length() - 1;          resolved.getChars(0, resolved.length(), cBuf, entity);          entity = -1;          break;        }      default:        _isWhitespace = false;        cBuf[index] = (char) ch;        break;      }    }    if (entity > 0)      throw new XMLStreamException("XXX: unclosed entity at end of file");    _cBufLength = index;    if (ch < 0 && _isWhitespace)      return -1;    // whitespace surrounding the root element is "ignorable" per the XML spec    boolean isIgnorableWhitespace      = _isWhitespace && _namespaceTracker.getDepth() == 0;    return isIgnorableWhitespace ? SPACE : CHARACTERS;  }  private String resolveEntity(String s)    throws XMLStreamException  {    if ("amp".equals(s))    return "&";    if ("apos".equals(s))   return "\'";    if ("quot".equals(s))   return "\"";    if ("lt".equals(s))     return "<";    if ("gt".equals(s))     return ">";    if (s.startsWith("#x"))      return ""+((char)Integer.parseInt(s.substring(2), 16));    if (s.startsWith("#"))      return ""+((char)Integer.parseInt(s.substring(1)));    throw new XMLStreamException("unknown entity: \"" + s + "\"");  }  private void readProcessingDirective()    throws XMLStreamException  {    CharBuffer target = new CharBuffer();    CharBuffer data   = null;    while(true) {      int ch = read();      if (ch == -1)        return;  /* XXX: error? */      if (ch == '?') {        int next = read();        if (next == '>') {          _processingInstructionTarget = target.toString();          _processingInstructionData = data == null ? null : data.toString();          return;        }        unread();      }      if (data == null && (ch == ' ' || ch == '\r' || ch == '\n')) {        data = new CharBuffer();        continue;      }      if (data != null)        data.append((char)ch);      else        target.append((char)ch);    }  }  private int readComment()    throws XMLStreamException  {    int ch = 0;    int index = 0;    char []cBuf = _cBuf;    int length = cBuf.length;    loop:    for (; index < length && (ch = read()) >= 0; index++) {      cBuf[index] = (char) ch;      if (index > 3          && cBuf[index-2] == '-'          && cBuf[index-1] == '-'          && cBuf[index-0] == '>') {        index -= 2;        break;      }    }    _cBufLength = index;    return COMMENT;  }  private void readRawName(RawName name)    throws IOException, XMLStreamException  {    int length = 0;    char []nameBuffer = name._buffer;    int bufferLength = nameBuffer.length;    int prefix = -1;    int ch;    while ((ch = read()) >= 0 && IS_XML_NAME[ch]) {      if (bufferLength <= length) {        name.expandCapacity();        nameBuffer = name._buffer;        bufferLength = nameBuffer.length;

⌨️ 快捷键说明

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