KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > LookaheadEnumerator


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.*;
4
5 import java.util.*;
6
7 /**
8 * A LookaheadEnumerator passes the nodes from a base enumerator throgh unchanged.
9 * The complication is that on request, it must determine the value of the last() position,
10 * which requires a lookahead.
11 *
12 * A LookaheadEnumerator should only be used to wrap a NodeEnumeration that cannot
13 * determine the last() position for itself, i.e. one that is not a LastPositionFinder.
14 */

15
16
17 public class LookaheadEnumerator implements NodeEnumeration, LastPositionFinder {
18
19     // The way this class works is that all calls to hasMoreElements() and nextElement() are
20
// simply delegated to the underlying enumeration, until such time as the client calls
21
// getLastPosition() to find out how many nodes there are. At this point the remaining nodes
22
// are read from the underlying enumeration into a reservoir to find out how many there are;
23
// and from this point on, requests for more nodes are met from the reservoir rather than
24
// from the underlying enumeration. The reason for all this is to avoid allocating temporary
25
// storage for the nodes unless the user actually calls last() to find out how many there are.
26

27
28     private NodeEnumeration base;
29     private Vector reservoir = null;
30     private int reservoirPosition = -1;
31     private int position = 0;
32     private int last = -1;
33
34     /**
35     * Constructor
36     * @param base An NodeEnumerator that delivers the nodes, but that cannot determine the
37     * last position count.
38     */

39
40     public LookaheadEnumerator(NodeEnumeration base) {
41         this.base = base;
42     }
43
44     /**
45     * Determine whether there are any more nodes to hand to the client
46     */

47
48     public boolean hasMoreElements() /*throws XPathException*/ {
49         if (reservoir==null) {
50             return base.hasMoreElements();
51         } else {
52             return reservoirPosition < reservoir.size();
53         }
54     }
55
56     /**
57     * Hand the next node to the client
58     */

59
60     public NodeInfo nextElement() throws XPathException {
61         if (reservoir==null) {
62             position++;
63             return base.nextElement();
64         } else {
65             if (reservoirPosition<reservoir.size()) {
66                 position++;
67                 return (NodeInfo)reservoir.elementAt(reservoirPosition++);
68             } else {
69                 return null;
70             }
71         }
72     }
73
74     /**
75     * Do lookahead to find the last position, if required
76     */

77
78     public int getLastPosition() throws XPathException {
79         if (last>0) {
80             return last;
81         } else {
82             // load the reservoir with all remaining input nodes
83
reservoir = new Vector();
84             reservoirPosition = 0;
85             last = position;
86             while (base.hasMoreElements()) {
87                 reservoir.addElement(base.nextElement());
88                 last++;
89             }
90             return last;
91         }
92     }
93
94     /**
95     * Determine whether the nodes are guaranteed to be in document order
96     */

97
98     public boolean isSorted() {
99         return base.isSorted();
100     }
101
102     public boolean isReverseSorted() {
103         return base.isReverseSorted();
104     }
105
106
107     /**
108     * Determine whether the nodes are guaranteed to be peers
109     */

110
111     public boolean isPeer() {
112         return base.isPeer();
113     }
114 }
115
116
117
118 //
119
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
120
// you may not use this file except in compliance with the License. You may obtain a copy of the
121
// License at http://www.mozilla.org/MPL/
122
//
123
// Software distributed under the License is distributed on an "AS IS" basis,
124
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
125
// See the License for the specific language governing rights and limitations under the License.
126
//
127
// The Original Code is: all this file.
128
//
129
// The Initial Developer of the Original Code is
130
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
131
//
132
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
133
//
134
// Contributor(s): none.
135
//
136
Popular Tags