KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.AtomizableIterator;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.trans.XPathException;
6
7 /**
8 * MappingIterator merges a sequence of sequences into a single flat
9 * sequence. It takes as inputs an iteration, and a mapping function to be
10 * applied to each Item returned by that iteration. The mapping function itself
11 * returns another iteration. The result is an iteration of the concatenation of all
12 * the iterations returned by the mapping function.<p>
13 *
14 * This is a powerful class. It is used, with different mapping functions,
15 * in a great variety of ways. It underpins the way that "for" expressions and
16 * path expressions are evaluated, as well as sequence expressions. It is also
17 * used in the implementation of the document(), key(), and id() functions.
18 */

19
20 public final class MappingIterator implements SequenceIterator, AtomizableIterator {
21
22     private SequenceIterator base;
23     private MappingFunction action;
24     private XPathContext context;
25     //private Object info;
26
private SequenceIterator results = null;
27     private boolean atomizing = false;
28     private Item current = null;
29     private int position = 0;
30
31     /**
32     * Construct a MappingIterator that will apply a specified MappingFunction to
33     * each Item returned by the base iterator.
34     * @param base the base iterator
35      * @param action the mapping function to be applied
36      * @param context the processing context. This should be supplied only if each item to be processed
37     * is to become the context item. In this case "base" should be the same as context.getCurrentIterator().
38      */

39
40     public MappingIterator(SequenceIterator base, MappingFunction action, XPathContext context) {
41         // System.err.println("New Mapping iterator " + this + " with base " + base);
42
this.base = base;
43         this.action = action;
44         this.context = context;
45     }
46
47     public Item next() throws XPathException {
48         Item nextItem;
49         while (true) {
50             if (results != null) {
51                 nextItem = results.next();
52                 if (nextItem != null) {
53                     break;
54                 } else {
55                     results = null;
56                 }
57             }
58             Item nextSource = base.next();
59             if (nextSource != null) {
60                 // Call the supplied mapping function
61
Object JavaDoc obj = action.map(nextSource, context);
62
63                 // The result may be null (representing an empty sequence), an item
64
// (representing a singleton sequence), or a SequenceIterator (any sequence)
65

66                 if (obj != null) {
67                     if (obj instanceof Item) {
68                         nextItem = (Item)obj;
69                         results = null;
70                         break;
71                     }
72                     results = (SequenceIterator)obj;
73                     if (atomizing && results instanceof AtomizableIterator) {
74                         ((AtomizableIterator)results).setIsAtomizing(atomizing);
75                     }
76                     nextItem = results.next();
77                     if (nextItem == null) {
78                         results = null;
79                     } else {
80                         break;
81                     }
82                 }
83                 // now go round the loop to get the next item from the base sequence
84
} else {
85                 results = null;
86                 current = null;
87                 position = -1;
88                 return null;
89             }
90         }
91
92         current = nextItem;
93         // System.err.println("MappingIterator.next(), this = " + this + " returning " + ((net.sf.saxon.om.NodeInfo)current).generateId() );
94
position++;
95         return nextItem;
96     }
97
98     public Item current() {
99         return current;
100     }
101
102     public int position() {
103         return position;
104     }
105
106
107     public SequenceIterator getAnother() throws XPathException {
108         // System.err.println(this + " getAnother() ");
109
SequenceIterator newBase = base.getAnother();
110         XPathContext c = context;
111         if (c!=null) {
112             c = c.newMinorContext();
113             c.setCurrentIterator(newBase);
114             c.setOrigin(context.getOrigin());
115         }
116         MappingIterator m = new MappingIterator(newBase, action, c);
117         m.setIsAtomizing(atomizing);
118         return m;
119     }
120
121     /**
122      * Get properties of this iterator, as a bit-significant integer.
123      *
124      * @return the properties of this iterator. This will be some combination of
125      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
126      * and {@link LOOKAHEAD}. It is always
127      * acceptable to return the value zero, indicating that there are no known special properties.
128      * It is acceptable for the properties of the iterator to change depending on its state.
129      */

130
131     public int getProperties() {
132         return 0;
133     }
134
135     /**
136      * Indicate that any nodes returned in the sequence will be atomized. This
137      * means that if it wishes to do so, the implementation can return the typed
138      * values of the nodes rather than the nodes themselves. The implementation
139      * is free to ignore this hint.
140      * @param atomizing true if the caller of this iterator will atomize any
141      * nodes that are returned, and is therefore willing to accept the typed
142      * value of the nodes instead of the nodes themselves.
143      */

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