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