KickJava   Java API By Example, From Geeks To Geeks.

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


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: MatchingIterator.java,v 1.9 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  * This is a special kind of iterator that takes a source iterator and a
28  * node N. If initialized with a node M (the parent of N) it computes the
29  * position of N amongst the children of M. This position can be obtained
30  * by calling getPosition().
31  * It is an iterator even though next() will never be called. It is used to
32  * match patterns with a single predicate like:
33  *
34  * BOOK[position() = last()]
35  *
36  * In this example, the source iterator will return elements of type BOOK,
37  * a call to position() will return the position of N. Notice that because
38  * of the way the pattern matching is implemented, N will always be a node
39  * in the source since (i) it is a BOOK or the test sequence would not be
40  * considered and (ii) the source iterator is initialized with M which is
41  * the parent of N. Also, and still in this example, a call to last() will
42  * return the number of elements in the source (i.e. the number of BOOKs).
43  * @author Jacek Ambroziak
44  * @author Santiago Pericas-Geertsen
45  */

46 public final class MatchingIterator extends DTMAxisIteratorBase {
47
48     /**
49      * A reference to a source iterator.
50      */

51     private DTMAxisIterator _source;
52
53     /**
54      * The node to match.
55      */

56     private final int _match;
57
58     public MatchingIterator(int match, DTMAxisIterator source) {
59     _source = source;
60     _match = match;
61     }
62
63
64     public void setRestartable(boolean isRestartable) {
65     _isRestartable = isRestartable;
66     _source.setRestartable(isRestartable);
67     }
68
69     public DTMAxisIterator cloneIterator() {
70
71     try {
72         final MatchingIterator clone = (MatchingIterator) super.clone();
73         clone._source = _source.cloneIterator();
74         clone._isRestartable = false;
75         return clone.reset();
76     }
77     catch (CloneNotSupportedException JavaDoc e) {
78         BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
79                       e.toString());
80         return null;
81     }
82     }
83     
84     public DTMAxisIterator setStartNode(int node) {
85     if (_isRestartable) {
86         // iterator is not a clone
87
_source.setStartNode(node);
88
89         // Calculate the position of the node in the set
90
_position = 1;
91         while ((node = _source.next()) != END && node != _match) {
92         _position++;
93         }
94     }
95     return this;
96     }
97
98     public DTMAxisIterator reset() {
99     _source.reset();
100     return resetPosition();
101     }
102     
103     public int next() {
104     return _source.next();
105     }
106     
107     public int getLast() {
108         if (_last == -1) {
109             _last = _source.getLast();
110         }
111         return _last;
112     }
113
114     public int getPosition() {
115     return _position;
116     }
117
118     public void setMark() {
119     _source.setMark();
120     }
121
122     public void gotoMark() {
123     _source.gotoMark();
124     }
125 }
126
Popular Tags