KickJava   Java API By Example, From Geeks To Geeks.

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


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: DupFilterIterator.java,v 1.13 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.xalan.xsltc.util.IntegerArray;
24 import org.apache.xml.dtm.DTMAxisIterator;
25 import org.apache.xml.dtm.ref.DTMAxisIteratorBase;
26 import org.apache.xml.dtm.ref.DTMDefaultBase;
27
28 /**
29  * Removes duplicates and sorts a source iterator. The nodes from the
30  * source are collected in an array upon calling setStartNode(). This
31  * array is later sorted and duplicates are ignored in next().
32  * @author G. Todd Miller
33  */

34 public final class DupFilterIterator extends DTMAxisIteratorBase {
35
36     /**
37      * Reference to source iterator.
38      */

39     private DTMAxisIterator _source;
40
41     /**
42      * Array to cache all nodes from source.
43      */

44     private IntegerArray _nodes = new IntegerArray();
45
46     /**
47      * Index in _nodes array to current node.
48      */

49     private int _current = 0;
50
51     /**
52      * Cardinality of _nodes array.
53      */

54     private int _nodesSize = 0;
55
56     /**
57      * Last value returned by next().
58      */

59     private int _lastNext = END;
60
61     /**
62      * Temporary variable to store _lastNext.
63      */

64     private int _markedLastNext = END;
65
66     public DupFilterIterator(DTMAxisIterator source) {
67     _source = source;
68 // System.out.println("DFI source = " + source + " this = " + this);
69

70     // Cache contents of id() or key() index right away. Necessary for
71
// union expressions containing multiple calls to the same index, and
72
// correct as well since start-node is irrelevant for id()/key() exrp.
73
if (source instanceof KeyIndex) {
74         setStartNode(DTMDefaultBase.ROOTNODE);
75     }
76     }
77     
78     /**
79      * Set the start node for this iterator
80      * @param node The start node
81      * @return A reference to this node iterator
82      */

83     public DTMAxisIterator setStartNode(int node) {
84     if (_isRestartable) {
85         // KeyIndex iterators are always relative to the root node, so there
86
// is never any point in re-reading the iterator (and we SHOULD NOT).
87
if (_source instanceof KeyIndex
88                     && _startNode == DTMDefaultBase.ROOTNODE) {
89         return this;
90         }
91
92         if (node != _startNode) {
93         _source.setStartNode(_startNode = node);
94
95         _nodes.clear();
96         while ((node = _source.next()) != END) {
97             _nodes.add(node);
98         }
99         _nodes.sort();
100         _nodesSize = _nodes.cardinality();
101         _current = 0;
102         _lastNext = END;
103         resetPosition();
104         }
105     }
106     return this;
107     }
108
109     public int next() {
110     while (_current < _nodesSize) {
111         final int next = _nodes.at(_current++);
112         if (next != _lastNext) {
113         return returnNode(_lastNext = next);
114         }
115     }
116     return END;
117     }
118
119     public DTMAxisIterator cloneIterator() {
120     try {
121         final DupFilterIterator clone =
122         (DupFilterIterator) super.clone();
123         clone._nodes = (IntegerArray) _nodes.clone();
124         clone._source = _source.cloneIterator();
125         clone._isRestartable = false;
126         return clone.reset();
127     }
128     catch (CloneNotSupportedException JavaDoc e) {
129         BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
130                       e.toString());
131         return null;
132     }
133     }
134
135     public void setRestartable(boolean isRestartable) {
136     _isRestartable = isRestartable;
137     _source.setRestartable(isRestartable);
138     }
139    
140     public void setMark() {
141     _markedNode = _current;
142         _markedLastNext = _lastNext; // Bugzilla 25924
143
}
144
145     public void gotoMark() {
146     _current = _markedNode;
147         _lastNext = _markedLastNext; // Bugzilla 25924
148
}
149
150     public DTMAxisIterator reset() {
151     _current = 0;
152     _lastNext = END;
153     return resetPosition();
154     }
155 }
156
Popular Tags