KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > om > PrependIterator


1 package net.sf.saxon.om;
2
3 /**
4  * An iterator over nodes, that prepends a given node to the nodes
5  * returned by another iterator. Used to modify an iterator over axis A
6  * to one that iterates over A-OR-SELF.
7  */

8
9 public class PrependIterator implements AxisIterator {
10
11     NodeInfo start;
12     AxisIterator base;
13     int position = 0;
14
15     public PrependIterator(NodeInfo start, AxisIterator base) {
16         this.start = start;
17         this.base = base;
18     }
19
20     /**
21      * Get the next item in the sequence. <BR>
22      *
23      * @return the next Item. If there are no more nodes, return null.
24      */

25
26     public Item next() {
27         if (position == 0) {
28             position = 1;
29             return start;
30         }
31         Item n = base.next();
32         if (n == null) {
33             position = -1;
34         } else {
35             position++;
36         }
37         return n;
38     }
39
40     /**
41      * Get the current item in the sequence.
42      *
43      * @return the current item, that is, the item most recently returned by
44      * next()
45      */

46
47     public Item current() {
48         if (position() == 1) {
49             return start;
50         } else if (position < 1) {
51             return null;
52         } else {
53             return base.current();
54         }
55     }
56
57     /**
58      * Get the current position
59      *
60      * @return the position of the current item (the item most recently
61      * returned by next()), starting at 1 for the first node
62      */

63
64     public int position() {
65        return position;
66     }
67
68     /**
69      * Get another iterator over the same sequence of items, positioned at the
70      * start of the sequence
71      *
72      * @return a new iterator over the same sequence
73      */

74
75     public SequenceIterator getAnother() {
76         return new PrependIterator(start, base);
77     }
78
79     /**
80      * Get properties of this iterator, as a bit-significant integer.
81      *
82      * @return the properties of this iterator. This will be some combination of
83      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
84      * and {@link LOOKAHEAD}. It is always
85      * acceptable to return the value zero, indicating that there are no known special properties.
86      * It is acceptable for the properties of the iterator to change depending on its state.
87      */

88
89     public int getProperties() {
90         return 0;
91     }
92
93
94 }
95
96 //
97
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
98
// you may not use this file except in compliance with the License. You may obtain a copy of the
99
// License at http://www.mozilla.org/MPL/
100
//
101
// Software distributed under the License is distributed on an "AS IS" basis,
102
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
103
// See the License for the specific language governing rights and limitations under the License.
104
//
105
// The Original Code is: all this file.
106
//
107
// The Initial Developer of the Original Code is Michael H. Kay.
108
//
109
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
110
//
111
// Contributor(s): none.
112
//
Popular Tags