1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xalan.internal.xsltc.dom;
23
24import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
25import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
26import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray;
27
28/**
29 * CachedNodeListIterator is used for select expressions in a
30 * variable or parameter. This iterator caches all nodes in an
31 * IntegerArray. Its cloneIterator() method is overridden to
32 * return an object of ClonedNodeListIterator.
33 */
34public final class CachedNodeListIterator extends DTMAxisIteratorBase {
35
36    /**
37     * Source for this iterator.
38     */
39    private DTMAxisIterator _source;
40    private IntegerArray _nodes = new IntegerArray();
41    private int _numCachedNodes = 0;
42    private int _index = 0;
43    private boolean _isEnded = false;
44
45    public CachedNodeListIterator(DTMAxisIterator source) {
46        _source = source;
47    }
48
49    public void setRestartable(boolean isRestartable) {
50        //_isRestartable = isRestartable;
51        //_source.setRestartable(isRestartable);
52    }
53
54    public DTMAxisIterator setStartNode(int node) {
55        if (_isRestartable) {
56            _startNode = node;
57            _source.setStartNode(node);
58            resetPosition();
59
60            _isRestartable = false;
61        }
62        return this;
63    }
64
65    public int next() {
66        return getNode(_index++);
67    }
68
69    public int getPosition() {
70        return _index == 0 ? 1 : _index;
71    }
72
73    public int getNodeByPosition(int pos) {
74        return getNode(pos);
75    }
76
77    public int getNode(int index) {
78        if (index < _numCachedNodes) {
79            return _nodes.at(index);
80        }
81        else if (!_isEnded){
82            int node = _source.next();
83            if (node != END) {
84                _nodes.add(node);
85                _numCachedNodes++;
86            }
87            else {
88                _isEnded = true;
89            }
90            return node;
91        }
92        else
93            return END;
94    }
95
96    public DTMAxisIterator cloneIterator() {
97        ClonedNodeListIterator clone = new ClonedNodeListIterator(this);
98        return clone;
99    }
100
101    public DTMAxisIterator reset() {
102        _index = 0;
103        return this;
104    }
105
106    public void setMark() {
107        _source.setMark();
108    }
109
110    public void gotoMark() {
111        _source.gotoMark();
112    }
113}
114