KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > PositionIterator


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.ArrayIterator;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.LookaheadIterator;
5 import net.sf.saxon.om.SequenceIterator;
6 import net.sf.saxon.trans.XPathException;
7
8
9 /**
10 * A PositionIterator selects a subsequence of a sequence
11 */

12
13 public class PositionIterator implements SequenceIterator, LookaheadIterator {
14
15     /**
16     * Static factory method. Creates a PositionIterator, unless the base Iterator is an
17     * ArrayIterator, in which case it optimizes by creating a new ArrayIterator directly over the
18     * underlying array. This optimization is important when doing recursion over a node-set using
19     * repeated calls of $nodes[position()>1]
20     * @param base An iteration of the items to be filtered
21     * @param min The position of the first item to be included (base 1)
22     * @param max The position of the last item to be included (base 1)
23     */

24
25     public static SequenceIterator make(SequenceIterator base, int min, int max) throws XPathException {
26         // System.err.println("PositionIterator.make base=" + base.getClass() + " min=" + min + " max=" + max);
27
if (base instanceof ArrayIterator) {
28             return ((ArrayIterator)base).makeSliceIterator(min, max);
29         } else {
30             return new PositionIterator(base, min, max);
31         }
32     }
33
34
35
36     private SequenceIterator base;
37     private int position = 0;
38     private int min = 1;
39     private int max = Integer.MAX_VALUE;
40     private Item nextItem = null;
41     private Item current = null;
42
43     /**
44     * Private Constructor: use the factory method instead!
45     * @param base An iteration of the items to be filtered
46     * @param min The position of the first item to be included
47     * @param max The position of the last item to be included
48     */

49
50     private PositionIterator(SequenceIterator base, int min, int max) throws XPathException {
51         this.base = base;
52         this.min = min;
53         if (min<1) min=1;
54         this.max = max;
55         if (max<min) {
56             nextItem = null;
57             return;
58         }
59         int i=1;
60         while ( i++ <= min ) {
61             nextItem = base.next();
62             if (nextItem == null) {
63                 break;
64             }
65         }
66         current = nextItem;
67     }
68
69     /**
70     * Test whether there are any more items available in the enumeration
71     */

72
73     public boolean hasNext() {
74         return nextItem != null;
75     }
76
77     /**
78     * Get the next item if there is one
79     */

80
81     public Item next() throws XPathException {
82         if (nextItem == null) {
83             current = null;
84             position = -1;
85             return null;
86         }
87         current = nextItem;
88         position++;
89         if (base.position() < max) {
90             nextItem = base.next();
91         } else {
92             nextItem = null;
93         }
94         return current;
95     }
96
97
98     public Item current() {
99         return current;
100     }
101
102     public int position() {
103         return position;
104     }
105
106     /**
107     * Get another iterator to return the same nodes
108     */

109
110     public SequenceIterator getAnother() throws XPathException {
111         return new PositionIterator(base.getAnother(), min, max);
112     }
113
114     /**
115      * Get properties of this iterator, as a bit-significant integer.
116      *
117      * @return the properties of this iterator. This will be some combination of
118      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
119      * and {@link LOOKAHEAD}. It is always
120      * acceptable to return the value zero, indicating that there are no known special properties.
121      * It is acceptable for the properties of the iterator to change depending on its state.
122      */

123
124     public int getProperties() {
125         return LOOKAHEAD;
126     }
127
128 }
129
130
131
132 //
133
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
134
// you may not use this file except in compliance with the License. You may obtain a copy of the
135
// License at http://www.mozilla.org/MPL/
136
//
137
// Software distributed under the License is distributed on an "AS IS" basis,
138
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
139
// See the License for the specific language governing rights and limitations under the License.
140
//
141
// The Original Code is: all this file.
142
//
143
// The Initial Developer of the Original Code is Michael H. Kay.
144
//
145
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
146
//
147
// Contributor(s): none.
148
//
149
Popular Tags