dtmdefaultbaseiterators.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,931 行 · 第 1/4 页

JAVA
1,931
字号
    /**     * Returns a deep copy of this iterator.  The cloned iterator is not reset.     *     * @return a deep copy of this iterator.     */    public DTMAxisIterator cloneIterator()    {      _isRestartable = false;  // must set to false for any clone      try      {        final AncestorIterator clone = (AncestorIterator) super.clone();        clone._startNode = _startNode;        // return clone.reset();        return clone;      }      catch (CloneNotSupportedException e)      {        throw new DTMException(XSLMessages.createMessage(XSLTErrorResources.ER_ITERATOR_CLONE_NOT_SUPPORTED, null)); //"Iterator clone not supported.");      }    }    /**     * Set start to END should 'close' the iterator,     * i.e. subsequent call to next() should return END.     *     * @param node Sets the root of the iteration.     *     * @return A DTMAxisIterator set to the start of the iteration.     */    public DTMAxisIterator setStartNode(int node)    {      m_realStartNode = node;      if (_isRestartable)      {        if (_includeSelf)          _startNode = node;        else          _startNode = getParent(node);        node = _startNode;        while (node != END)        {          m_ancestors.addElement(node);          node = getParent(node);        }        m_ancestorsPos = m_ancestors.size()-1;        _currentNode = (m_ancestorsPos>=0)                               ? m_ancestors.elementAt(m_ancestorsPos)                               : DTM.NULL;        return resetPosition();      }      return this;    }    /**     * Resets the iterator to the last start node.     *     * @return A DTMAxisIterator, which may or may not be the same as this     *         iterator.     */    public DTMAxisIterator reset()    {      m_ancestorsPos = m_ancestors.size()-1;      _currentNode = (m_ancestorsPos>=0) ? m_ancestors.elementAt(m_ancestorsPos)                                         : DTM.NULL;      return resetPosition();    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {      int next = _currentNode;            int pos = --m_ancestorsPos;      _currentNode = (pos >= 0) ? m_ancestors.elementAt(m_ancestorsPos)                                : DTM.NULL;            return returnNode(next);    }  }  // end of AncestorIterator  /**   * Typed iterator that returns the ancestors of a given node.   */  private final class TypedAncestorIterator extends AncestorIterator  {    /** The extended type ID that was requested. */    private final int _nodeType;    /**     * Constructor TypedAncestorIterator     *     *     * @param type The extended type ID being requested.     */    public TypedAncestorIterator(int type)    {      _nodeType = type;    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {      int node;      while ((node = super.next()) != NULL)      {        if (getExpandedTypeID(node) == _nodeType)          return returnNode(node);      }      return (NULL);    }    /**     * Returns the last element in this interation.     *     * @return the last element in this interation.     */    public int getLast()    {      int last = NULL;      int curr = _startNode;      while (curr != NULL)      {        if (getExpandedTypeID(curr) == _nodeType)          last = curr;        curr = getParent(curr);      }      return (last);    }  }  // end of TypedAncestorIterator  /**   * Iterator that returns the descendants of a given node.   */  private class DescendantIterator extends InternalAxisIteratorBase  {    /**     * Set start to END should 'close' the iterator,     * i.e. subsequent call to next() should return END.     *     * @param node Sets the root of the iteration.     *     * @return A DTMAxisIterator set to the start of the iteration.     */    public DTMAxisIterator setStartNode(int node)    {      if (_isRestartable)      {        node = makeNodeIdentity(node);        _startNode = node;        if (_includeSelf)          node--;        _currentNode = node;        return resetPosition();      }      return this;    }    /**     * Tell if this node identity is a descendant.  Assumes that     * the node info for the element has already been obtained.     *     * This one-sided test works only if the parent has been     * previously tested and is known to be a descendent. It fails if     * the parent is the _startNode's next sibling, or indeed any node     * that follows _startNode in document order.  That may suffice     * for this iterator, but it's not really an isDescendent() test.     * %REVIEW% rename?     *     * @param identity The index number of the node in question.     * @return true if the index is a descendant of _startNode.     */    protected boolean isDescendant(int identity)    {      return (_startNode == identity) || _parent(identity) >= _startNode;    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {      while (true)      {        int node = ++_currentNode;        int type = _type(node);        if (NULL == type ||!isDescendant(node))          return END;        if (ATTRIBUTE_NODE == type || NAMESPACE_NODE == type)          continue;        return returnNode(makeNodeHandle(node));  // make handle.      }    }  }  // end of DescendantIterator  /**   * Typed iterator that returns the descendants of a given node.   */  private final class TypedDescendantIterator extends DescendantIterator  {    /** The extended type ID that was requested. */    private final int _nodeType;    /**     * Constructor TypedDescendantIterator     *     *     * @param nodeType Extended type ID being requested.     */    public TypedDescendantIterator(int nodeType)    {      _nodeType = nodeType;    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {      int node;      while ((node = super.next()) != NULL             && getExpandedTypeID(node) != _nodeType){}      return node;    }  }  // end of TypedDescendantIterator  /**   * Iterator that returns the descendants of a given node.   * I'm not exactly clear about this one... -sb   */  private class NthDescendantIterator extends DescendantIterator  {    /** The current nth position. */    int _pos;    /**     * Constructor NthDescendantIterator     *     *     * @param pos The nth position being requested.     */    public NthDescendantIterator(int pos)    {      _pos = pos;    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {      // I'm not exactly clear yet what this is doing... -sb      int node;      while ((node = super.next()) != END)      {        node = makeNodeIdentity(node);        int parent = _parent(node);        int child = _firstch(parent);        int pos = 0;        do        {          int type = _type(child);          if (ELEMENT_NODE == type)            pos++;        }        while ((pos < _pos) && (child = _nextsib(child)) != END);        if (node == child)          return node;      }      return (END);    }  }  // end of NthDescendantIterator  /**   * Class SingletonIterator.   */  private class SingletonIterator extends InternalAxisIteratorBase  {    /** (not sure yet what this is.  -sb)  (sc & sb remove final to compile in JDK 1.1.8) */    private boolean _isConstant;    /**     * Constructor SingletonIterator     *     */    public SingletonIterator()    {      this(Integer.MIN_VALUE, false);    }    /**     * Constructor SingletonIterator     *     *     * @param node The node handle to return.     */    public SingletonIterator(int node)    {      this(node, false);    }    /**     * Constructor SingletonIterator     *     *     * @param node the node handle to return.     * @param constant (Not sure what this is yet.  -sb)     */    public SingletonIterator(int node, boolean constant)    {      _currentNode = _startNode = node;      _isConstant = constant;    }    /**     * Set start to END should 'close' the iterator,     * i.e. subsequent call to next() should return END.     *     * @param node Sets the root of the iteration.     *     * @return A DTMAxisIterator set to the start of the iteration.     */    public DTMAxisIterator setStartNode(int node)    {      if (_isConstant)      {        _currentNode = _startNode;        return resetPosition();      }      else if (_isRestartable)      {        if (_currentNode == Integer.MIN_VALUE)        {          _currentNode = _startNode = node;        }        return resetPosition();      }      return this;    }    /**     * Resets the iterator to the last start node.     *     * @return A DTMAxisIterator, which may or may not be the same as this     *         iterator.     */    public DTMAxisIterator reset()    {      if (_isConstant)      {        _currentNode = _startNode;        return resetPosition();      }      else      {        final boolean temp = _isRestartable;        _isRestartable = true;        setStartNode(_startNode);        _isRestartable = temp;      }      return this;    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {      final int result = _currentNode;      _currentNode = END;      return returnNode(result);    }  }  /**   * Iterator that returns a given node only if it is of a given type.   */  private final class TypedSingletonIterator extends SingletonIterator  {    /** The extended type ID that was requested. */    private final int _nodeType;    /**     * Constructor TypedSingletonIterator     *     *     * @param nodeType The extended type ID being requested.     */    public TypedSingletonIterator(int nodeType)    {      _nodeType = nodeType;    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {      final int result = super.next();      return getExpandedTypeID(result) == _nodeType ? result : NULL;    }  }  // end of TypedSingletonIterator}

⌨️ 快捷键说明

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