dtmdefaultbaseiterators.java
来自「JAVA 所有包」· Java 代码 · 共 2,195 行 · 第 1/4 页
JAVA
2,195 行
* * @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(); m_realStartNode = node; if (_isRestartable) { int nodeID = makeNodeIdentity(node); if (!_includeSelf && node != DTM.NULL) { nodeID = _parent(nodeID); node = makeNodeHandle(nodeID); } _startNode = node; while (nodeID != END) { m_ancestors.addElement(node); nodeID = _parent(nodeID); node = makeNodeHandle(nodeID); } 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); } public void setMark() { m_markedPos = m_ancestorsPos; } public void gotoMark() { m_ancestorsPos = m_markedPos; _currentNode = m_ancestorsPos>=0 ? m_ancestors.elementAt(m_ancestorsPos) : DTM.NULL; } } // end of AncestorIterator /** * Typed iterator that returns the ancestors of a given node. */ public 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; } /** * 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(); m_realStartNode = node; if (_isRestartable) { int nodeID = makeNodeIdentity(node); int nodeType = _nodeType; if (!_includeSelf && node != DTM.NULL) { nodeID = _parent(nodeID); } _startNode = node; if (nodeType >= DTM.NTYPES) { while (nodeID != END) { int eType = _exptype(nodeID); if (eType == nodeType) { m_ancestors.addElement(makeNodeHandle(nodeID)); } nodeID = _parent(nodeID); } } else { while (nodeID != END) { int eType = _exptype(nodeID); if ((eType >= DTM.NTYPES && m_expandedNameTable.getType(eType) == nodeType) || (eType < DTM.NTYPES && eType == nodeType)) { m_ancestors.addElement(makeNodeHandle(nodeID)); } nodeID = _parent(nodeID); } } m_ancestorsPos = m_ancestors.size()-1; _currentNode = (m_ancestorsPos>=0) ? m_ancestors.elementAt(m_ancestorsPos) : DTM.NULL; return resetPosition(); } return this; } } // end of TypedAncestorIterator /** * Iterator that returns the descendants of a given node. */ public 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) {//%HZ%: Added reference to DTMDefaultBase.ROOTNODE back in, temporarily if (node == DTMDefaultBase.ROOTNODE) node = getDocument(); 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 (_parent(identity) >= _startNode) || (_startNode == identity); } /** * Get the next node in the iteration. * * @return The next node handle in the iteration, or END. */ public int next() { if (_startNode == NULL) { return NULL; } if (_includeSelf && (_currentNode + 1) == _startNode) return returnNode(makeNodeHandle(++_currentNode)); // | m_dtmIdent); int node = _currentNode; int type; do { node++; type = _type(node); if (NULL == type ||!isDescendant(node)) { _currentNode = NULL; return END; } } while(ATTRIBUTE_NODE == type || TEXT_NODE == type || NAMESPACE_NODE == type); _currentNode = node; return returnNode(makeNodeHandle(node)); // make handle. } /** * Reset. * */ public DTMAxisIterator reset() { final boolean temp = _isRestartable; _isRestartable = true; setStartNode(makeNodeHandle(_startNode)); _isRestartable = temp; return this; } } // end of DescendantIterator /** * Typed iterator that returns the descendants of a given node. */ public 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; int type; if (_startNode == NULL) { return NULL; } node = _currentNode; do { node++; type = _type(node); if (NULL == type ||!isDescendant(node)) { _currentNode = NULL; return END; } } while (type != _nodeType && _exptype(node) != _nodeType); _currentNode = node; return returnNode(makeNodeHandle(node)); } } // end of TypedDescendantIterator /** * Iterator that returns the descendants of a given node. * I'm not exactly clear about this one... -sb */ public 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. */ public 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) {//%HZ%: Added reference to DTMDefaultBase.ROOTNODE back in, temporarily if (node == DTMDefaultBase.ROOTNODE) node = getDocument(); 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); } } // end of SingletonIterator /** * Iterator that returns a given node only if it is of a given type. */ public 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(); final int result = _currentNode; int nodeType = _nodeType; _currentNode = END; if (nodeType >= DTM.NTYPES) { if (getExpandedTypeID(result) == nodeType) { return returnNode(result); } } else { if (getNodeType(result) == nodeType) { return returnNode(result); } } return NULL; } } // end of TypedSingletonIterator}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?