001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: NodeIteratorBase.java,v 1.10 2004/02/16 22:54:59 minchau Exp $
018: */
019:
020: package org.apache.xalan.xsltc.dom;
021:
022: import org.apache.xalan.xsltc.NodeIterator;
023: import org.apache.xalan.xsltc.runtime.BasisLibrary;
024:
025: /**
026: * @author Jacek Ambroziak
027: * @author Santiago Pericas-Geertsen
028: * @author Morten Jorgensen
029: */
030: public abstract class NodeIteratorBase implements NodeIterator {
031:
032: /**
033: * Cached computed value of last().
034: */
035: protected int _last = -1;
036:
037: /**
038: * Value of position() in this iterator. Incremented in
039: * returnNode().
040: */
041: protected int _position = 0;
042:
043: /**
044: * Store node in call to setMark().
045: */
046: protected int _markedNode;
047:
048: /**
049: * Store node in call to setStartNode().
050: */
051: protected int _startNode = NodeIterator.END;
052:
053: /**
054: * Flag indicating if "self" should be returned.
055: */
056: protected boolean _includeSelf = false;
057:
058: /**
059: * Flag indicating if iterator can be restarted.
060: */
061: protected boolean _isRestartable = true;
062:
063: /**
064: * Setter for _isRestartable flag.
065: */
066: public void setRestartable(boolean isRestartable) {
067: _isRestartable = isRestartable;
068: }
069:
070: /**
071: * Initialize iterator using a node. If iterator is not
072: * restartable, then do nothing. If node is equal to END then
073: * subsequent calls to next() must return END.
074: */
075: abstract public NodeIterator setStartNode(int node);
076:
077: /**
078: * Reset this iterator using state from last call to
079: * setStartNode().
080: */
081: public NodeIterator reset() {
082: final boolean temp = _isRestartable;
083: _isRestartable = true;
084: // Must adjust _startNode if self is included
085: setStartNode(_includeSelf ? _startNode + 1 : _startNode);
086: _isRestartable = temp;
087: return this ;
088: }
089:
090: /**
091: * Setter for _includeSelf flag.
092: */
093: public NodeIterator includeSelf() {
094: _includeSelf = true;
095: return this ;
096: }
097:
098: /**
099: * Default implementation of getLast(). Stores current position
100: * and current node, resets the iterator, counts all nodes and
101: * restores iterator to original state.
102: */
103: public int getLast() {
104: if (_last == -1) {
105: final int temp = _position;
106: setMark();
107: reset();
108: do {
109: _last++;
110: } while (next() != END);
111: gotoMark();
112: _position = temp;
113: }
114: return _last;
115: }
116:
117: /**
118: * Returns the position() in this iterator.
119: */
120: public int getPosition() {
121: return _position == 0 ? 1 : _position;
122: }
123:
124: /**
125: * Indicates if position in this iterator is computed in reverse
126: * document order. Note that nodes are always returned in document
127: * order.
128: */
129: public boolean isReverse() {
130: return false;
131: }
132:
133: /**
134: * Clones and resets this iterator. Note that the cloned iterator is
135: * not restartable. This is because cloning is needed for variable
136: * references, and the context node of the original variable
137: * declaration must be preserved.
138: */
139: public NodeIterator cloneIterator() {
140: try {
141: final NodeIteratorBase clone = (NodeIteratorBase) super
142: .clone();
143: clone._isRestartable = false;
144: return clone.reset();
145: } catch (CloneNotSupportedException e) {
146: BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
147: e.toString());
148: return null;
149: }
150: }
151:
152: /**
153: * Utility method that increments position and returns its
154: * argument.
155: */
156: protected final int returnNode(final int node) {
157: _position++;
158: return node;
159: }
160:
161: /**
162: * Reset the position in this iterator.
163: */
164: protected final NodeIterator resetPosition() {
165: _position = 0;
166: return this;
167: }
168: }
|