KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package org.apache.xalan.xsltc.dom;
21
22 import org.apache.xalan.xsltc.runtime.BasisLibrary;
23 import org.apache.xml.dtm.DTMAxisIterator;
24 import org.apache.xml.dtm.DTMFilter;
25 import org.apache.xml.dtm.DTMIterator;
26 import org.apache.xml.dtm.ref.DTMAxisIteratorBase;
27
28 /**
29  * Similar to a CurrentNodeListIterator except that the filter has a
30  * simpler interface (only needs the node, no position, last, etc.)
31  * It takes a source iterator and a Filter object and returns nodes
32  * from the source after filtering them by calling filter.test(node).
33  * @author Jacek Ambroziak
34  * @author Santiago Pericas-Geertsen
35  */

36 public final class FilterIterator extends DTMAxisIteratorBase {
37
38     /**
39      * Reference to source iterator.
40      */

41     private DTMAxisIterator _source;
42
43     /**
44      * Reference to a filter object that to be applied to each node.
45      */

46     private final DTMFilter _filter;
47
48     /**
49      * A flag indicating if position is reversed.
50      */

51     private final boolean _isReverse;
52     
53     public FilterIterator(DTMAxisIterator source, DTMFilter filter) {
54     _source = source;
55 // System.out.println("FI souce = " + source + " this = " + this);
56
_filter = filter;
57     _isReverse = source.isReverse();
58     }
59
60     public boolean isReverse() {
61     return _isReverse;
62     }
63
64
65     public void setRestartable(boolean isRestartable) {
66     _isRestartable = isRestartable;
67     _source.setRestartable(isRestartable);
68     }
69
70     public DTMAxisIterator cloneIterator() {
71
72     try {
73         final FilterIterator clone = (FilterIterator) super.clone();
74         clone._source = _source.cloneIterator();
75         clone._isRestartable = false;
76         return clone.reset();
77     }
78     catch (CloneNotSupportedException JavaDoc e) {
79         BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
80                       e.toString());
81         return null;
82     }
83     }
84     
85     public DTMAxisIterator reset() {
86     _source.reset();
87     return resetPosition();
88     }
89     
90     public int next() {
91     int node;
92     while ((node = _source.next()) != END) {
93         if (_filter.acceptNode(node, DTMFilter.SHOW_ALL) == DTMIterator.FILTER_ACCEPT) {
94         return returnNode(node);
95         }
96     }
97     return END;
98     }
99
100     public DTMAxisIterator setStartNode(int node) {
101     if (_isRestartable) {
102         _source.setStartNode(_startNode = node);
103         return resetPosition();
104     }
105     return this;
106     }
107
108     public void setMark() {
109     _source.setMark();
110     }
111
112     public void gotoMark() {
113     _source.gotoMark();
114     }
115
116 }
117
Popular Tags