dtmdefaultbaseiterators.java

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

JAVA
1,931
字号
      {        _startNode = node;        for (node = getFirstAttribute(node); node != END;                node = getNextAttribute(node))        {          if (getExpandedTypeID(node) == _nodeType)            break;        }        _currentNode = node;        return resetPosition();      }      return this;    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {      final int node = _currentNode;      // singleton iterator, since there can only be one attribute of       // a given type.      _currentNode = NULL;      return returnNode(node);    }  }  // end of TypedAttributeIterator  /**   * Iterator that returns preceding siblings of a given node   */  private class PrecedingSiblingIterator extends InternalAxisIteratorBase  {    /**     * True if this iterator has a reversed axis.     *     * @return true.     */    public boolean isReverse()    {      return true;    }    /**     * 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)      {        _startNode = node;        if(node == NULL)        {          _currentNode = node;          return resetPosition();        }                  int type = m_expandedNameTable.getType(getExpandedTypeID(node));        if(ExpandedNameTable.ATTRIBUTE == type            || ExpandedNameTable.NAMESPACE == type )        {          _currentNode = node;        }        else				{					// Be careful to handle the Document node properly					_currentNode = getParent(node);					if(NULL!=_currentNode)							_currentNode = getFirstChild(_currentNode);					else						_currentNode = node;				}        return resetPosition();      }      return this;    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {      if (_currentNode == _startNode)      {        return NULL;      }      else      {        final int node = _currentNode;        _currentNode = getNextSibling(node);        return returnNode(node);      }    }  }  // end of PrecedingSiblingIterator  /**   * Iterator that returns preceding siblings of a given type for   * a given node   */  private final class TypedPrecedingSiblingIterator          extends PrecedingSiblingIterator  {    /** The extended type ID that was requested. */    private final int _nodeType;    /**     * Constructor TypedPrecedingSiblingIterator     *     *     * @param type The extended type ID being requested.     */    public TypedPrecedingSiblingIterator(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             && getExpandedTypeID(node) != _nodeType){}      return node;    }  }  // end of PrecedingSiblingIterator  /**   * Iterator that returns preceding nodes of a given node.   * This includes the node set {root+1, start-1}, but excludes   * all ancestors, attributes, and namespace nodes.   */  private class PrecedingIterator extends InternalAxisIteratorBase  {    /** The max ancestors, but it can grow... */    private final int _maxAncestors = 8;    /**     * The stack of start node + ancestors up to the root of the tree,     *  which we must avoid.     */    private int[] _stack = new int[_maxAncestors];    /** (not sure yet... -sb) */    private int _sp, _oldsp;    /* _currentNode precedes candidates.  This is the identity, not the handle! */    /**     * True if this iterator has a reversed axis.     *     * @return true since this iterator is a reversed axis.     */    public boolean isReverse()    {      return true;    }    /**     * 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;      try      {        final PrecedingIterator clone = (PrecedingIterator) super.clone();        final int[] stackCopy = new int[_stack.length];        System.arraycopy(_stack, 0, stackCopy, 0, _stack.length);        clone._stack = stackCopy;        // 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)    {      if (_isRestartable)      {        node = makeNodeIdentity(node);        // iterator is not a clone        int parent, index;        _startNode = node;        _stack[index = 0] = node;		parent=node;		while ((parent = _parent(parent)) != NULL)		{			if (++index == _stack.length)			{				final int[] stack = new int[index + 4];				System.arraycopy(_stack, 0, stack, 0, index);				_stack = stack;			}			_stack[index] = parent;        }        if(index>0)	        --index; // Pop actual root node (if not start) back off the stack        _currentNode=_stack[index]; // Last parent before root node        _oldsp = _sp = index;        return resetPosition();      }      return this;    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {    	// Bugzilla 8324: We were forgetting to skip Attrs and NS nodes.    	// Also recoded the loop controls for clarity and to flatten out    	// the tail-recursion.   		for(++_currentNode;    			_sp>=0;    			++_currentNode)   		{   			if(_currentNode < _stack[_sp])   			{   				if(_type(_currentNode) != ATTRIBUTE_NODE &&   					_type(_currentNode) != NAMESPACE_NODE)   					return returnNode(makeNodeHandle(_currentNode));   			}   			else   				--_sp;   		}   		return NULL;    }    // redefine DTMAxisIteratorBase's reset    /**     * 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()    {      _sp = _oldsp;      return resetPosition();    }  }  // end of PrecedingIterator  /**   * Iterator that returns preceding nodes of agiven type for a   * given node. This includes the node set {root+1, start-1}, but   * excludes all ancestors.   */  private final class TypedPrecedingIterator extends PrecedingIterator  {    /** The extended type ID that was requested. */    private final int _nodeType;    /**     * Constructor TypedPrecedingIterator     *     *     * @param type The extended type ID being requested.     */    public TypedPrecedingIterator(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             && getExpandedTypeID(node) != _nodeType){}      return node;    }  }  // end of TypedPrecedingIterator  /**   * Iterator that returns following nodes of for a given node.   */  private class FollowingIterator extends InternalAxisIteratorBase  {    DTMAxisTraverser m_traverser; // easier for now        public FollowingIterator()    {      m_traverser = getAxisTraverser(Axis.FOLLOWING);    }    /**     * 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)      {        _startNode = node;        // ?? -sb        // find rightmost descendant (or self)        // int current;        // while ((node = getLastChild(current = node)) != NULL){}        // _currentNode = current;        _currentNode = m_traverser.first(node);        // _currentNode precedes possible following(node) nodes        return resetPosition();      }      return this;    }    /**     * Get the next node in the iteration.     *     * @return The next node handle in the iteration, or END.     */    public int next()    {      int node = _currentNode;      _currentNode = m_traverser.next(_startNode, _currentNode);      return returnNode(node);    }  }  // end of FollowingIterator  /**   * Iterator that returns following nodes of a given type for a given node.   */  private final class TypedFollowingIterator extends FollowingIterator  {    /** The extended type ID that was requested. */    private final int _nodeType;    /**     * Constructor TypedFollowingIterator     *     *     * @param type The extended type ID being requested.     */    public TypedFollowingIterator(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             && getExpandedTypeID(node) != _nodeType){}      return returnNode(node);    }  }  // end of TypedFollowingIterator  /**   * Iterator that returns the ancestors of a given node in document   * order.  (NOTE!  This was changed from the XSLTC code!)   */  private class AncestorIterator extends InternalAxisIteratorBase  {    org.apache.xml.utils.NodeVector m_ancestors =          new org.apache.xml.utils.NodeVector();             int m_ancestorsPos;        /** The real start node for this axes, since _startNode will be adjusted. */    int m_realStartNode;        /**     * Get start to END should 'close' the iterator,     * i.e. subsequent call to next() should return END.     *     * @return The root node of the iteration.     */    public int getStartNode()    {      return m_realStartNode;    }    /**     * True if this iterator has a reversed axis.     *     * @return true since this iterator is a reversed axis.     */    public final boolean isReverse()    {      return true;    }    /**     * Returns the last element in this interation.     *     * %TBD% %BUG% This is returning a nodeHandle rather than a _position     * value. That conflicts with what everyone else is doing. And it's     * talking about the start node, which conflicts with some of the     * other reverse iterators. DEFINITE BUG; needs to be reconciled.     *     * @return the last element in this interation.     */    public int getLast()    {      return (_startNode);    }

⌨️ 快捷键说明

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