KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.XPathContext;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.sort.AtomicComparer;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.value.AtomicValue;
8 import net.sf.saxon.value.IntegerValue;
9
10
11 /**
12 * The XPath 2.0 index-of() function
13 */

14
15
16 public class IndexOf extends CollatingFunction {
17
18     /**
19     * Evaluate the function to return an iteration of selected nodes.
20     */

21
22     public SequenceIterator iterate(XPathContext context) throws XPathException {
23         AtomicComparer comparer = getAtomicComparer(2, context);
24         SequenceIterator seq = argument[0].iterate(context);
25         AtomicValue val = (AtomicValue)argument[1].evaluateItem(context);
26         return new IndexIterator(seq, val, comparer);
27     }
28
29     private class IndexIterator implements SequenceIterator {
30
31         SequenceIterator base;
32         AtomicValue value;
33         AtomicComparer comparer;
34         int index = 0;
35         int position = 0;
36         Item current = null;
37
38         public IndexIterator(SequenceIterator base, AtomicValue value, AtomicComparer comparer)
39         {
40             this.base = base;
41             this.value = value;
42             this.comparer = comparer;
43         }
44
45         public Item next() throws XPathException {
46             while (true) {
47                 AtomicValue i = (AtomicValue)base.next();
48                 if (i==null) break;
49                 index++;
50                 if (comparer.comparesEqual(i, value)) {
51                     current = new IntegerValue(index);
52                     position++;
53                     return current;
54                 }
55             }
56             current = null;
57             position = -1;
58             return null;
59         }
60
61         public Item current() {
62             return current;
63         }
64
65         public int position() {
66             return position;
67         }
68
69         public SequenceIterator getAnother() throws XPathException {
70             return new IndexIterator(base.getAnother(), value, comparer);
71         }
72
73         /**
74          * Get properties of this iterator, as a bit-significant integer.
75          *
76          * @return the properties of this iterator. This will be some combination of
77          * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
78          * and {@link LOOKAHEAD}. It is always
79          * acceptable to return the value zero, indicating that there are no known special properties.
80          * It is acceptable for the properties of the iterator to change depending on its state.
81          */

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