KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > dom > CachedNodeListIterator


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: CachedNodeListIterator.java,v 1.2 2004/02/16 22:54:59 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.dom;
21
22 import org.apache.xml.dtm.DTMAxisIterator;
23 import org.apache.xml.dtm.ref.DTMAxisIteratorBase;
24 import org.apache.xalan.xsltc.util.IntegerArray;
25
26 /**
27  * CachedNodeListIterator is used for select expressions in a
28  * variable or parameter. This iterator caches all nodes in an
29  * IntegerArray. Its cloneIterator() method is overridden to
30  * return an object of ClonedNodeListIterator.
31  */

32 public final class CachedNodeListIterator extends DTMAxisIteratorBase {
33
34     /**
35      * Source for this iterator.
36      */

37     private DTMAxisIterator _source;
38     private IntegerArray _nodes = new IntegerArray();
39     private int _numCachedNodes = 0;
40     private int _index = 0;
41     private boolean _isEnded = false;
42
43     public CachedNodeListIterator(DTMAxisIterator source) {
44     _source = source;
45     }
46
47     public void setRestartable(boolean isRestartable) {
48     //_isRestartable = isRestartable;
49
//_source.setRestartable(isRestartable);
50
}
51
52     public DTMAxisIterator setStartNode(int node) {
53     if (_isRestartable) {
54         _startNode = node;
55         _source.setStartNode(node);
56         resetPosition();
57         
58         _isRestartable = false;
59     }
60     return this;
61     }
62
63     public int next() {
64         return getNode(_index++);
65     }
66     
67     public int getPosition() {
68         return _index == 0 ? 1 : _index;
69     }
70     
71     public int getNodeByPosition(int pos) {
72         return getNode(pos);
73     }
74         
75     public int getNode(int index) {
76         if (index < _numCachedNodes) {
77             return _nodes.at(index);
78         }
79         else if (!_isEnded){
80             int node = _source.next();
81             if (node != END) {
82                 _nodes.add(node);
83                 _numCachedNodes++;
84             }
85             else {
86                 _isEnded = true;
87             }
88             return node;
89         }
90         else
91             return END;
92     }
93
94     public DTMAxisIterator cloneIterator() {
95     ClonedNodeListIterator clone = new ClonedNodeListIterator(this);
96     return clone;
97     }
98
99     public DTMAxisIterator reset() {
100         _index = 0;
101         return this;
102     }
103     
104     public void setMark() {
105     _source.setMark();
106     }
107
108     public void gotoMark() {
109     _source.gotoMark();
110     }
111 }
112
Popular Tags