1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xalan.internal.xsltc.dom;
23
24import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
25import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
26import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
27
28/**
29 * @author Jacek Ambroziak
30 * @author Morten Jorgensen
31 */
32public final class NthIterator extends DTMAxisIteratorBase {
33    // ...[N]
34    private DTMAxisIterator _source;
35    private final int _position;
36    private boolean _ready;
37
38    public NthIterator(DTMAxisIterator source, int n) {
39        _source = source;
40        _position = n;
41    }
42
43    public void setRestartable(boolean isRestartable) {
44        _isRestartable = isRestartable;
45        _source.setRestartable(isRestartable);
46    }
47
48    public DTMAxisIterator cloneIterator() {
49        try {
50            final NthIterator clone = (NthIterator) super.clone();
51            clone._source = _source.cloneIterator();    // resets source
52            clone._isRestartable = false;
53            return clone;
54        }
55        catch (CloneNotSupportedException e) {
56            BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
57                                      e.toString());
58            return null;
59        }
60    }
61
62    public int next() {
63        if (_ready) {
64            _ready = false;
65            return _source.getNodeByPosition(_position);
66        }
67        return DTMAxisIterator.END;
68        /*
69        if (_ready && _position > 0) {
70            final int pos = _source.isReverse()
71                                       ? _source.getLast() - _position + 1
72                                       : _position;
73
74            _ready = false;
75            int node;
76            while ((node = _source.next()) != DTMAxisIterator.END) {
77                if (pos == _source.getPosition()) {
78                    return node;
79                }
80            }
81        }
82        return DTMAxisIterator.END;
83        */
84    }
85
86    public DTMAxisIterator setStartNode(final int node) {
87        if (_isRestartable) {
88            _source.setStartNode(node);
89            _ready = true;
90        }
91        return this;
92    }
93
94    public DTMAxisIterator reset() {
95        _source.reset();
96        _ready = true;
97        return this;
98    }
99
100    public int getLast() {
101        return 1;
102    }
103
104    public int getPosition() {
105        return 1;
106    }
107
108    public void setMark() {
109        _source.setMark();
110    }
111
112    public void gotoMark() {
113        _source.gotoMark();
114    }
115}
116