xmlreader.java

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

JAVA
735
字号

    if (_currentNodeType != XMLStreamConstants.END_ELEMENT)
      return StringValue.create(_streamReader.getText());

    return StringValue.create(null);
  }

  /**
   * Returns the node type of the current element.
   *
   * @return the node type, otherwise null
   */
  public Value getXmlLang() {
    if (! streamIsOpen())
      return NullValue.NULL;

    // XXX: Defaulted for now.
    return StringValue.create("");
  }

  /**
   * Closes the reader.
   *
   * @return true if success, false otherwise
   */
  public BooleanValue close() {
    if (! streamIsOpen())
      return BooleanValue.TRUE;

    try {
      _streamReader.close();
    }
    catch (XMLStreamException ex) {
      log.log(Level.WARNING, ex.toString(), ex);

      return BooleanValue.FALSE;
    }

    return BooleanValue.TRUE;
  }

  /**
   *
   * @return
   */
  public Value expand() {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param name
   * @return
   */
  public StringValue getAttribute(String name) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param index
   * @return
   */
  public StringValue getAttributeNo(int index) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param localName
   * @param namespaceURI
   * @return
   */
  public StringValue getAttributeNS(String localName, String namespaceURI) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param property
   * @return
   */
  public BooleanValue getParserProperty(int property) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @return
   */
  public BooleanValue isValid() {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param prefix
   * @return
   */
  public BooleanValue lookupNamespace(String prefix) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param name
   * @return
   */
  public BooleanValue moveToAttribute(String name) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param index
   * @return
   */
  public BooleanValue moveToAttributeNo(int index) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param localName
   * @param namespaceURI
   * @return
   */
  public BooleanValue moveToAttributeNs(String localName, String namespaceURI) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @return
   */
  public BooleanValue moveToElement() {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @return
   */
  public BooleanValue moveToFirstAttribute() {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @return
   */
  public BooleanValue moveToNextAttribute() {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param localname
   * @return
   */
  public BooleanValue next(@Optional String localname) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   * Opens a stream using the uniform resource locator.
   *
   * @param uri uniform resource locator to open
   * @return true if success, false otherwise
   */
  public BooleanValue open(Env env, Path path) {
    try {
      XMLInputFactory factory = XMLInputFactory.newInstance();

      _streamReader = factory.createXMLStreamReader(path.getNativePath(), path.openRead());
    }
    catch (XMLStreamException ex) {
      log.log(Level.WARNING, ex.toString(), ex);

      env.warning(L.l("XML input file '{0}' cannot be opened for reading.",
                      path));

      return BooleanValue.FALSE;
    }
    catch (IOException ex) {
      log.log(Level.WARNING, ex.toString(), ex);

      env.warning(L.l("Unable to open source data"));

      return BooleanValue.FALSE;
    }

    return BooleanValue.TRUE;
  }

  /**
   * Updates the depth.
   *
   */
  private void updateDepth(Env env) {
    if (_lastNodeType == XMLStreamConstants.START_ELEMENT &&
        ! _streamReader.isEndElement())
      _depth++;
    else if ((_lastNodeType == XMLStreamConstants.CHARACTERS ||
              _lastNodeType == XMLStreamConstants.COMMENT) &&
                                                           _currentNodeType == XMLStreamConstants.END_ELEMENT)
      _depth--;
  }

  /**
   * Maintains the _hasAttribute variable.
   *
   */
  private void updateAttribute(Env env) {
    _hasAttribute = false;

    String key = getName(env).toString() + _depth;

    if (_currentNodeType == XMLStreamConstants.START_ELEMENT &&
        _streamReader.getAttributeCount() > 0) {
      _startElements.put(key, _depth);

      _hasAttribute = true;
    }

    if (_currentNodeType == XMLStreamConstants.END_ELEMENT &&
        _startElements.containsKey(key))  {
      _hasAttribute = true;

      _startElements.remove(key);
    }
  }

  /**
   * Moves the cursor to the next node.
   *
   * @return true if success, false otherwise
   */
  public BooleanValue read(Env env) {
    if (! streamIsOpen(env, "read"))
      return BooleanValue.FALSE;

    try {
      if (! _streamReader.hasNext())
        return BooleanValue.FALSE;

      _lastNodeType = _currentNodeType;

      Value isEmptyElement = getIsEmptyElement();
      
      _currentNodeType = _streamReader.next();

      // php/4618
      if (isEmptyElement.toBoolean())
        return read(env);

      if (_currentNodeType == XMLStreamConstants.SPACE)
        return read(env);

      if (_currentNodeType == XMLStreamConstants.END_DOCUMENT)
        return BooleanValue.FALSE;

      updateDepth(env);

      updateAttribute(env);

    }
    catch (XMLStreamException ex) {
      log.log(Level.WARNING, ex.toString(), ex);

      env.warning(L.l("Unable to read :" + ex.toString()));

      return BooleanValue.FALSE;
    }

    return BooleanValue.TRUE;
  }

  public LongValue getNextType() {
    return LongValue.create(_currentNodeType);
  }

  /**
   *
   * @param property
   * @param value
   * @return
   */
  public BooleanValue setParserProperty(int property, boolean value) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param filename
   * @return
   */
  public BooleanValue setRelaxNGSchema(String filename) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param source
   * @return
   */
  public BooleanValue setRelaxNGSchemaSource(String source) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   *
   * @param source
   * @return
   */
  public BooleanValue XML(String source) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  static {
    _constConvertMap.put(XMLStreamConstants.ATTRIBUTE,
                         ATTRIBUTE);
    _constConvertMap.put(XMLStreamConstants.CDATA,
                         CDATA);
    _constConvertMap.put(XMLStreamConstants.CHARACTERS,
                         TEXT);
    _constConvertMap.put(XMLStreamConstants.COMMENT,
                         COMMENT);
    _constConvertMap.put(XMLStreamConstants.END_ELEMENT,
                         END_ELEMENT);
    /*
      _constConvertMap.put(XMLStreamConstants.END_ENTITY,
                      END_ENTITY);
    */
    // XXX: XMLStreamConstants.ENTITY_DECLARATION is 17 in the BAE docs
    // but is 15 in the Resin implementation.
    _constConvertMap.put(XMLStreamConstants.ENTITY_DECLARATION,
                         ENTITY); // ENTITY used twice
    _constConvertMap.put(XMLStreamConstants.ENTITY_REFERENCE,
                         ENTITY_REF);
    _constConvertMap.put(XMLStreamConstants.NOTATION_DECLARATION,
                         NOTATION);
    _constConvertMap.put(XMLStreamConstants.PROCESSING_INSTRUCTION,
                         PI);
    _constConvertMap.put(XMLStreamConstants.SPACE,
                         WHITESPACE);
    _constConvertMap.put(XMLStreamConstants.START_ELEMENT,
                         ELEMENT);
    /*
      _constConvertMap.put(XMLStreamConstants.START_ENTITY,
                      ENTITY);
    */
    // Following constants did not match
    _constConvertMap.put(XMLStreamConstants.DTD, NONE);
    _constConvertMap.put(XMLStreamConstants.END_DOCUMENT, NONE);
    _constConvertMap.put(XMLStreamConstants.NAMESPACE, NONE);
    _constConvertMap.put(XMLStreamConstants.START_DOCUMENT, NONE);
    _constConvertMap.put(0, NONE); // Pre-Read
    _constConvertMap.put(-1, NONE);
    _constConvertMap.put(-1, DOC);
    _constConvertMap.put(-1, DOC_TYPE);
    _constConvertMap.put(-1, DOC_FRAGMENT);
    _constConvertMap.put(-1, DOC_TYPE);
    _constConvertMap.put(-1, XML_DECLARATION);
  }
}

⌨️ 快捷键说明

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