KickJava   Java API By Example, From Geeks To Geeks.

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


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: StepIterator.java,v 1.15 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.ref.DTMAxisIteratorBase;
25
26 /**
27  * A step iterator is used to evaluate expressions like "BOOK/TITLE".
28  * A better name for this iterator would have been ParentIterator since
29  * both "BOOK" and "TITLE" are steps in XPath lingo. Step iterators are
30  * constructed from two other iterators which we are going to refer to
31  * as "outer" and "inner". Every node from the outer iterator (the one
32  * for BOOK in our example) is used to initialize the inner iterator.
33  * After this initialization, every node from the inner iterator is
34  * returned (in essence, implementing a "nested loop").
35  * @author Jacek Ambroziak
36  * @author Santiago Pericas-Geertsen
37  * @author Erwin Bolwidt <ejb@klomp.org>
38  * @author Morten Jorgensen
39  */

40 public class StepIterator extends DTMAxisIteratorBase {
41
42     /**
43      * A reference to the "outer" iterator.
44      */

45     protected DTMAxisIterator _source;
46
47     /**
48      * A reference to the "inner" iterator.
49      */

50     protected DTMAxisIterator _iterator;
51
52     /**
53      * Temp variable to store a marked position.
54      */

55     private int _pos = -1;
56
57     public StepIterator(DTMAxisIterator source, DTMAxisIterator iterator) {
58     _source = source;
59     _iterator = iterator;
60 // System.out.println("SI source = " + source + " this = " + this);
61
// System.out.println("SI iterator = " + iterator + " this = " + this);
62
}
63
64
65     public void setRestartable(boolean isRestartable) {
66     _isRestartable = isRestartable;
67     _source.setRestartable(isRestartable);
68     _iterator.setRestartable(true); // must be restartable
69
}
70
71     public DTMAxisIterator cloneIterator() {
72     _isRestartable = false;
73     try {
74         final StepIterator clone = (StepIterator) super.clone();
75         clone._source = _source.cloneIterator();
76         clone._iterator = _iterator.cloneIterator();
77         clone._iterator.setRestartable(true); // must be restartable
78
clone._isRestartable = false;
79         return clone.reset();
80     }
81     catch (CloneNotSupportedException JavaDoc e) {
82         BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
83                       e.toString());
84         return null;
85     }
86     }
87     
88     public DTMAxisIterator setStartNode(int node) {
89     if (_isRestartable) {
90         // Set start node for left-hand iterator...
91
_source.setStartNode(_startNode = node);
92
93         // ... and get start node for right-hand iterator from left-hand,
94
// with special case for //* path - see ParentLocationPath
95
_iterator.setStartNode(_includeSelf ? _startNode : _source.next());
96         return resetPosition();
97     }
98     return this;
99     }
100
101     public DTMAxisIterator reset() {
102     _source.reset();
103     // Special case for //* path - see ParentLocationPath
104
_iterator.setStartNode(_includeSelf ? _startNode : _source.next());
105     return resetPosition();
106     }
107     
108     public int next() {
109     for (int node;;) {
110         // Try to get another node from the right-hand iterator
111
if ((node = _iterator.next()) != END) {
112         return returnNode(node);
113         }
114         // If not, get the next starting point from left-hand iterator...
115
else if ((node = _source.next()) == END) {
116         return END;
117         }
118         // ...and pass it on to the right-hand iterator
119
else {
120         _iterator.setStartNode(node);
121         }
122     }
123     }
124
125     public void setMark() {
126     _source.setMark();
127     _iterator.setMark();
128     //_pos = _position;
129
}
130
131     public void gotoMark() {
132     _source.gotoMark();
133     _iterator.gotoMark();
134     //_position = _pos;
135
}
136 }
137
Popular Tags