001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: CachedNodeListIterator.java 468651 2006-10-28 07:04:25Z minchau $
020     */
021    
022    package org.apache.xalan.xsltc.dom;
023    
024    import org.apache.xml.dtm.DTMAxisIterator;
025    import org.apache.xml.dtm.ref.DTMAxisIteratorBase;
026    import org.apache.xalan.xsltc.util.IntegerArray;
027    
028    /**
029     * CachedNodeListIterator is used for select expressions in a 
030     * variable or parameter. This iterator caches all nodes in an 
031     * IntegerArray. Its cloneIterator() method is overridden to 
032     * return an object of ClonedNodeListIterator.
033     */
034    public final class CachedNodeListIterator extends DTMAxisIteratorBase {
035    
036        /**
037         * Source for this iterator.
038         */
039        private DTMAxisIterator _source;
040        private IntegerArray _nodes = new IntegerArray();
041        private int _numCachedNodes = 0;
042        private int _index = 0;
043        private boolean _isEnded = false;
044    
045        public CachedNodeListIterator(DTMAxisIterator source) {
046            _source = source;
047        }
048    
049        public void setRestartable(boolean isRestartable) {
050            //_isRestartable = isRestartable;
051            //_source.setRestartable(isRestartable);
052        }
053    
054        public DTMAxisIterator setStartNode(int node) {
055            if (_isRestartable) {
056                _startNode = node;
057                _source.setStartNode(node);
058                resetPosition();
059                
060                _isRestartable = false;
061            }
062            return this;
063        }
064    
065        public int next() {
066            return getNode(_index++);
067        }
068        
069        public int getPosition() {
070            return _index == 0 ? 1 : _index;
071        }
072        
073        public int getNodeByPosition(int pos) {
074            return getNode(pos);
075        }
076            
077        public int getNode(int index) {
078            if (index < _numCachedNodes) {
079                return _nodes.at(index);
080            }
081            else if (!_isEnded){
082                int node = _source.next();
083                if (node != END) {
084                    _nodes.add(node);
085                    _numCachedNodes++;
086                }
087                else {
088                    _isEnded = true;
089                }
090                return node;
091            }
092            else
093                return END;
094        }
095    
096        public DTMAxisIterator cloneIterator() {
097            ClonedNodeListIterator clone = new ClonedNodeListIterator(this);
098            return clone;
099        }
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    }