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: FilterIterator.java,v 1.2.4.1 2005/09/06 06:21:10 pvedula Exp $
  22  */
  23 
  24 package com.sun.org.apache.xalan.internal.xsltc.dom;
  25 
  26 import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
  27 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  28 import com.sun.org.apache.xml.internal.dtm.DTMFilter;
  29 import com.sun.org.apache.xml.internal.dtm.DTMIterator;
  30 import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
  31 
  32 /**
  33  * Similar to a CurrentNodeListIterator except that the filter has a
  34  * simpler interface (only needs the node, no position, last, etc.)
  35  * It takes a source iterator and a Filter object and returns nodes
  36  * from the source after filtering them by calling filter.test(node).
  37  * @author Jacek Ambroziak
  38  * @author Santiago Pericas-Geertsen
  39  */
  40 public final class FilterIterator extends DTMAxisIteratorBase {
  41 
  42     /**
  43      * Reference to source iterator.
  44      */
  45     private DTMAxisIterator _source;
  46 
  47     /**
  48      * Reference to a filter object that to be applied to each node.
  49      */
  50     private final DTMFilter _filter;
  51 
  52     /**
  53      * A flag indicating if position is reversed.
  54      */
  55     private final boolean _isReverse;
  56 
  57     public FilterIterator(DTMAxisIterator source, DTMFilter filter) {
  58         _source = source;
  59 // System.out.println("FI souce = " + source + " this = " + this);
  60         _filter = filter;
  61         _isReverse = source.isReverse();
  62     }
  63 
  64     public boolean isReverse() {
  65         return _isReverse;
  66     }
  67 
  68 
  69     public void setRestartable(boolean isRestartable) {
  70         _isRestartable = isRestartable;
  71         _source.setRestartable(isRestartable);
  72     }
  73 
  74     public DTMAxisIterator cloneIterator() {
  75 
  76         try {
  77             final FilterIterator clone = (FilterIterator) super.clone();
  78             clone._source = _source.cloneIterator();
  79             clone._isRestartable = false;
  80             return clone.reset();
  81         }
  82         catch (CloneNotSupportedException e) {
  83             BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
  84                                       e.toString());
  85             return null;
  86         }
  87     }
  88 
  89     public DTMAxisIterator reset() {
  90         _source.reset();
  91         return resetPosition();
  92     }
  93 
  94     public int next() {
  95         int node;
  96         while ((node = _source.next()) != END) {
  97             if (_filter.acceptNode(node, DTMFilter.SHOW_ALL) == DTMIterator.FILTER_ACCEPT) {
  98                 return returnNode(node);
  99             }
 100         }
 101         return END;
 102     }
 103 
 104     public DTMAxisIterator setStartNode(int node) {
 105         if (_isRestartable) {
 106             _source.setStartNode(_startNode = node);
 107             return resetPosition();
 108         }
 109         return this;
 110     }
 111 
 112     public void setMark() {
 113         _source.setMark();
 114     }
 115 
 116     public void gotoMark() {
 117         _source.gotoMark();
 118     }
 119 
 120 }