dtmdefaultbaseiterators.java
来自「JAVA 所有包」· Java 代码 · 共 2,195 行 · 第 1/4 页
JAVA
2,195 行
* 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) {//%HZ%: Added reference to DTMDefaultBase.ROOTNODE back in, temporarily if (node == DTMDefaultBase.ROOTNODE) node = getDocument(); if (_isRestartable) { _startNode = node; node = _startNodeID = makeNodeIdentity(node); if(node == NULL) { _currentNode = node; return resetPosition(); } int type = m_expandedNameTable.getType(_exptype(node)); if(ExpandedNameTable.ATTRIBUTE == type || ExpandedNameTable.NAMESPACE == type ) { _currentNode = node; } else { // Be careful to handle the Document node properly _currentNode = _parent(node); if(NULL!=_currentNode) _currentNode = _firstch(_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 == _startNodeID || _currentNode == DTM.NULL) { return NULL; } else { final int node = _currentNode; _currentNode = _nextsib(node); return returnNode(makeNodeHandle(node)); } } } // end of PrecedingSiblingIterator /** * Iterator that returns preceding siblings of a given type for * a given node */ public 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 = _currentNode; int expType; int nodeType = _nodeType; int startID = _startNodeID; if (nodeType >= DTM.NTYPES) { while (node != NULL && node != startID && _exptype(node) != nodeType) { node = _nextsib(node); } } else { while (node != NULL && node != startID) { expType = _exptype(node); if (expType < DTM.NTYPES) { if (expType == nodeType) { break; } } else { if (m_expandedNameTable.getType(expType) == nodeType) { break; } } node = _nextsib(node); } } if (node == DTM.NULL || node == _startNodeID) { _currentNode = NULL; return NULL; } else { _currentNode = _nextsib(node); return returnNode(makeNodeHandle(node)); } } } // end of TypedPrecedingSiblingIterator /** * 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. */ public 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. */ protected int[] _stack = new int[_maxAncestors]; /** (not sure yet... -sb) */ protected int _sp, _oldsp; protected int _markedsp, _markedNode, _markedDescendant; /* _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(XMLMessages.createXMLMessage(XMLErrorResources.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) {//%HZ%: Added reference to DTMDefaultBase.ROOTNODE back in, temporarily if (node == DTMDefaultBase.ROOTNODE) node = getDocument(); if (_isRestartable) { node = makeNodeIdentity(node); // iterator is not a clone int parent, index; if (_type(node) == DTM.ATTRIBUTE_NODE) node = _parent(node); _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(); } public void setMark() { _markedsp = _sp; _markedNode = _currentNode; _markedDescendant = _stack[0]; } public void gotoMark() { _sp = _markedsp; _currentNode = _markedNode; } } // 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. */ public 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 = _currentNode; int nodeType = _nodeType; if (nodeType >= DTM.NTYPES) { while (true) { node = node + 1; if (_sp < 0) { node = NULL; break; } else if (node >= _stack[_sp]) { if (--_sp < 0) { node = NULL; break; } } else if (_exptype(node) == nodeType) { break; } } } else { int expType; while (true) { node = node + 1; if (_sp < 0) { node = NULL; break; } else if (node >= _stack[_sp]) { if (--_sp < 0) { node = NULL; break; } } else { expType = _exptype(node); if (expType < DTM.NTYPES) { if (expType == nodeType) { break; } } else { if (m_expandedNameTable.getType(expType) == nodeType) { break; } } } } } _currentNode = node; return (node == NULL) ? NULL : returnNode(makeNodeHandle(node)); } } // end of TypedPrecedingIterator /** * Iterator that returns following nodes of for a given node. */ public 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) {//%HZ%: Added reference to DTMDefaultBase.ROOTNODE back in, temporarily if (node == DTMDefaultBase.ROOTNODE) node = getDocument(); 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. */ public 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; do{ node = _currentNode; _currentNode = m_traverser.next(_startNode, _currentNode); } while (node != DTM.NULL && (getExpandedTypeID(node) != _nodeType && getNodeType(node) != _nodeType)); return (node == DTM.NULL ? DTM.NULL :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!) */ public class AncestorIterator extends InternalAxisIteratorBase { com.sun.org.apache.xml.internal.utils.NodeVector m_ancestors = new com.sun.org.apache.xml.internal.utils.NodeVector(); int m_ancestorsPos; int m_markedPos; /** 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 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(XMLMessages.createXMLMessage(XMLErrorResources.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.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?