KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > Remove


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.StaticContext;
4 import net.sf.saxon.expr.TailExpression;
5 import net.sf.saxon.expr.XPathContext;
6 import net.sf.saxon.om.Item;
7 import net.sf.saxon.om.SequenceIterator;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.type.ItemType;
10 import net.sf.saxon.value.AtomicValue;
11 import net.sf.saxon.value.IntegerValue;
12 import net.sf.saxon.value.NumericValue;
13
14 /**
15 * The XPath 2.0 remove() function
16 */

17
18
19 public class Remove extends SystemFunction {
20
21     /**
22      * Simplify. Recognize remove(seq, 1) as a TailExpression.
23      */

24
25      public Expression simplify(StaticContext env) throws XPathException {
26         Expression exp = super.simplify(env);
27         if (exp instanceof Remove) {
28             return ((Remove)exp).simplifyAsTailExpression();
29         } else {
30             return exp;
31         }
32     }
33
34     /**
35      * Simplify. Recognize remove(seq, 1) as a TailExpression. This
36      * is worth doing because tail expressions used in a recursive call
37      * are handled specially.
38      */

39
40     private Expression simplifyAsTailExpression() {
41         if (argument[1] instanceof IntegerValue &&
42                 ((IntegerValue)argument[1]).longValue() == 1) {
43             return new TailExpression(argument[0], 2);
44         } else {
45             return this;
46         }
47     }
48
49     /**
50     * Determine the data type of the items in the sequence
51     * @return the type of the input sequence
52     */

53
54     public ItemType getItemType() {
55         return argument[0].getItemType();
56     }
57
58     /**
59     * Evaluate the function to return an iteration of selected nodes.
60     */

61
62     public SequenceIterator iterate(XPathContext context) throws XPathException {
63         SequenceIterator seq = argument[0].iterate(context);
64         AtomicValue n0 = (AtomicValue)argument[1].evaluateItem(context);
65         NumericValue n = (NumericValue)n0.getPrimitiveValue();
66         int pos = (int)n.longValue();
67         if (pos < 1) {
68             return seq;
69         }
70         return new RemoveIterator(seq, pos);
71     }
72
73     private class RemoveIterator implements SequenceIterator {
74
75         SequenceIterator base;
76         int removePosition;
77         int position = 0;
78         Item current = null;
79
80         public RemoveIterator(SequenceIterator base, int removePosition) {
81             this.base = base;
82             this.removePosition = removePosition;
83         }
84
85         public Item next() throws XPathException {
86             current = base.next();
87             if (current != null && base.position() == removePosition) {
88                 current = base.next();
89             }
90             if (current == null) {
91                 position = -1;
92             } else {
93                 position++;
94             }
95             return current;
96         }
97
98         public Item current() {
99             return current;
100         }
101
102         public int position() {
103             return position;
104         }
105
106         public SequenceIterator getAnother() throws XPathException {
107             return new RemoveIterator( base.getAnother(),
108                                         removePosition);
109         }
110
111         /**
112          * Get properties of this iterator, as a bit-significant integer.
113          *
114          * @return the properties of this iterator. This will be some combination of
115          * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
116          * and {@link LOOKAHEAD}. It is always
117          * acceptable to return the value zero, indicating that there are no known special properties.
118          * It is acceptable for the properties of the iterator to change depending on its state.
119          */

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