KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > dom > NodeIteratorBase


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: NodeIteratorBase.java,v 1.10 2004/02/16 22:54:59 minchau Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.dom;
21
22 import com.sun.org.apache.xalan.internal.xsltc.NodeIterator;
23 import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
24
25 /**
26  * @author Jacek Ambroziak
27  * @author Santiago Pericas-Geertsen
28  * @author Morten Jorgensen
29  */

30 public abstract class NodeIteratorBase implements NodeIterator {
31
32     /**
33      * Cached computed value of last().
34      */

35     protected int _last = -1;
36
37     /**
38      * Value of position() in this iterator. Incremented in
39      * returnNode().
40      */

41     protected int _position = 0;
42
43     /**
44      * Store node in call to setMark().
45      */

46     protected int _markedNode;
47
48     /**
49      * Store node in call to setStartNode().
50      */

51     protected int _startNode = NodeIterator.END;
52
53     /**
54      * Flag indicating if "self" should be returned.
55      */

56     protected boolean _includeSelf = false;
57
58     /**
59      * Flag indicating if iterator can be restarted.
60      */

61     protected boolean _isRestartable = true;
62
63     /**
64      * Setter for _isRestartable flag.
65      */

66     public void setRestartable(boolean isRestartable) {
67     _isRestartable = isRestartable;
68     }
69
70     /**
71      * Initialize iterator using a node. If iterator is not
72      * restartable, then do nothing. If node is equal to END then
73      * subsequent calls to next() must return END.
74      */

75     abstract public NodeIterator setStartNode(int node);
76
77     /**
78      * Reset this iterator using state from last call to
79      * setStartNode().
80      */

81     public NodeIterator reset() {
82     final boolean temp = _isRestartable;
83     _isRestartable = true;
84     // Must adjust _startNode if self is included
85
setStartNode(_includeSelf ? _startNode + 1 : _startNode);
86     _isRestartable = temp;
87     return this;
88     }
89
90     /**
91      * Setter for _includeSelf flag.
92      */

93     public NodeIterator includeSelf() {
94     _includeSelf = true;
95     return this;
96     }
97
98     /**
99      * 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.clone();
142         clone._isRestartable = false;
143         return clone.reset();
144     }
145     catch (CloneNotSupportedException JavaDoc 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 }
169
Popular Tags