KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > dom > NthIterator


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

19
20 package com.sun.org.apache.xalan.internal.xsltc.dom;
21
22 import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
23 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
24 import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
25
26 /**
27  * @author Jacek Ambroziak
28  * @author Morten Jorgensen
29  */

30 public final class NthIterator extends DTMAxisIteratorBase {
31     // ...[N]
32
private DTMAxisIterator _source;
33     private final int _position;
34     private boolean _ready;
35
36     public NthIterator(DTMAxisIterator source, int n) {
37     _source = source;
38     _position = n;
39     }
40
41     public void setRestartable(boolean isRestartable) {
42     _isRestartable = isRestartable;
43     _source.setRestartable(isRestartable);
44     }
45     
46     public DTMAxisIterator cloneIterator() {
47     try {
48         final NthIterator clone = (NthIterator) super.clone();
49         clone._source = _source.cloneIterator(); // resets source
50
clone._isRestartable = false;
51         return clone;
52     }
53     catch (CloneNotSupportedException JavaDoc e) {
54         BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
55                       e.toString());
56         return null;
57     }
58     }
59
60     public int next() {
61     if (_ready) {
62         _ready = false;
63         return _source.getNodeByPosition(_position);
64     }
65     return DTMAxisIterator.END;
66     /*
67     if (_ready && _position > 0) {
68             final int pos = _source.isReverse()
69                                        ? _source.getLast() - _position + 1
70                                        : _position;
71
72         _ready = false;
73         int node;
74         while ((node = _source.next()) != DTMAxisIterator.END) {
75         if (pos == _source.getPosition()) {
76             return node;
77         }
78         }
79     }
80     return DTMAxisIterator.END;
81     */

82     }
83
84     public DTMAxisIterator setStartNode(final int node) {
85     if (_isRestartable) {
86         _source.setStartNode(node);
87         _ready = true;
88     }
89     return this;
90     }
91     
92     public DTMAxisIterator reset() {
93     _source.reset();
94     _ready = true;
95     return this;
96     }
97     
98     public int getLast() {
99     return 1;
100     }
101     
102     public int getPosition() {
103     return 1;
104     }
105     
106     public void setMark() {
107     _source.setMark();
108     }
109     
110     public void gotoMark() {
111     _source.gotoMark();
112     }
113 }
114
Popular Tags