KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.expr;
2
3 import net.sf.saxon.om.ArrayIterator;
4 import net.sf.saxon.om.Item;
5 import net.sf.saxon.om.NamePool;
6 import net.sf.saxon.om.SequenceIterator;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.type.ItemType;
9
10 import java.io.PrintStream JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * A TailExpression represents a FilterExpression of the form EXPR[position() > n]
15  * Here n is usually 2, but we allow other values
16  */

17 public class TailExpression extends ComputedExpression {
18
19     Expression base;
20     int start; // 1-based offset of first item from base expression
21
// to be included
22

23     /**
24      * Construct a TailExpression, representing a filter expression of the form
25      * $base[position() >= $start]
26      * @param base the expression to be filtered
27      * @param start the position (1-based) of the first item to be included
28      */

29
30     public TailExpression(Expression base, int start) {
31         this.base = base;
32         this.start = start;
33         adoptChildExpression(base);
34     }
35
36     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
37         base = base.typeCheck(env, contextItemType);
38         return this;
39     }
40
41     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
42         base = base.optimize(opt, env, contextItemType);
43         return this;
44     }
45
46     public Expression promote(PromotionOffer offer) throws XPathException {
47         Expression exp = offer.accept(this);
48         if (exp != null) {
49             return exp;
50         } else {
51             if (offer.action != PromotionOffer.UNORDERED) {
52                 base = doPromotion(base, offer);
53             }
54             return this;
55         }
56     }
57
58     public int computeSpecialProperties() {
59         return base.getSpecialProperties();
60     }
61
62     public ItemType getItemType() {
63         return base.getItemType();
64     }
65
66     public int computeCardinality() {
67         return base.getCardinality() | StaticProperty.ALLOWS_ZERO;
68     }
69
70     public Iterator iterateSubExpressions() {
71         return new MonoIterator(base);
72     }
73
74     public Expression getBaseExpression() {
75         return base;
76     }
77
78     public int getStart() {
79         return start;
80     }
81
82     public boolean equals(Object JavaDoc other) {
83         return other instanceof TailExpression &&
84                 base.equals(((TailExpression)other).base) &&
85                 start == ((TailExpression)other).start;
86     }
87
88     public int hashCode() {
89         return base.hashCode();
90     }
91
92     public SequenceIterator iterate(XPathContext context) throws XPathException {
93         SequenceIterator baseIter = base.iterate(context);
94         if (baseIter instanceof ArrayIterator) {
95             return ((ArrayIterator)baseIter).makeSliceIterator(start, Integer.MAX_VALUE);
96         } else {
97             return new TailIterator(baseIter, start);
98         }
99     }
100
101     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
102         out.println(ExpressionTool.indent(level) + "tail " + start);
103         base.display(level+1, pool, out);
104     }
105
106     public static class TailIterator implements SequenceIterator {
107
108         private SequenceIterator base;
109         private int start;
110
111         public TailIterator(SequenceIterator base, int start) throws XPathException {
112             this.base = base;
113             this.start = start;
114
115             // discard the first n-1 items from the underlying iterator
116
for (int i=0; i < start-1; i++) {
117                 base.next();
118             }
119         }
120
121         public Item next() throws XPathException {
122             return base.next();
123         }
124
125         public Item current() {
126             return base.current();
127         }
128
129         public int position() {
130             return base.position() - start + 1;
131         }
132
133         public SequenceIterator getAnother() throws XPathException {
134             return new TailIterator(base.getAnother(), start);
135         }
136
137         /**
138          * Get properties of this iterator, as a bit-significant integer.
139          *
140          * @return the properties of this iterator. This will be some combination of
141          * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
142          * and {@link LOOKAHEAD}. It is always
143          * acceptable to return the value zero, indicating that there are no known special properties.
144          * It is acceptable for the properties of the iterator to change depending on its state.
145          */

146
147         public int getProperties() {
148             return 0;
149         }
150     }
151 }
152
153 //
154
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
155
// you may not use this file except in compliance with the License. You may obtain a copy of the
156
// License at http://www.mozilla.org/MPL/
157
//
158
// Software distributed under the License is distributed on an "AS IS" basis,
159
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
160
// See the License for the specific language governing rights and limitations under the License.
161
//
162
// The Original Code is: all this file.
163
//
164
// The Initial Developer of the Original Code is Michael H. Kay.
165
//
166
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
167
//
168
// Contributor(s): none.
169
//
Popular Tags