KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sort > GroupStartingIterator


1 package net.sf.saxon.sort;
2
3 import net.sf.saxon.expr.XPathContext;
4 import net.sf.saxon.om.Item;
5 import net.sf.saxon.om.ListIterator;
6 import net.sf.saxon.om.NodeInfo;
7 import net.sf.saxon.om.SequenceIterator;
8 import net.sf.saxon.pattern.Pattern;
9 import net.sf.saxon.trans.XPathException;
10 import net.sf.saxon.value.AtomicValue;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 /**
16  * A GroupStartingIterator iterates over a sequence of groups defined by
17  * xsl:for-each-group group-starting-with="x". The groups are returned in
18  * order of first appearance.
19  */

20
21 public class GroupStartingIterator implements GroupIterator {
22
23     private SequenceIterator population;
24     private Pattern startPattern;
25     private XPathContext baseContext;
26     private XPathContext runningContext;
27     private List JavaDoc currentMembers;
28     private Item next;
29     private Item current = null;
30     private int position = 0;
31
32     public GroupStartingIterator(SequenceIterator population, Pattern startPattern,
33                                  XPathContext context)
34     throws XPathException {
35         this.population = population;
36         this.startPattern = startPattern;
37         baseContext = context;
38         runningContext = context.newMinorContext();
39         runningContext.setCurrentIterator(population);
40         // the first item in the population always starts a new group
41
next = population.next();
42      }
43
44      private void advance() throws XPathException {
45          currentMembers = new ArrayList JavaDoc(10);
46          currentMembers.add(current);
47          while (true) {
48              NodeInfo nextCandidate = (NodeInfo)population.next();
49              if (nextCandidate == null) {
50                  break;
51              }
52              if (startPattern.matches(nextCandidate, runningContext)) {
53                  next = nextCandidate;
54                  return;
55              } else {
56                  currentMembers.add(nextCandidate);
57              }
58          }
59          next = null;
60      }
61
62      public AtomicValue getCurrentGroupingKey() {
63          return null;
64      }
65
66      public SequenceIterator iterateCurrentGroup() {
67          return new ListIterator(currentMembers);
68      }
69
70      public Item next() throws XPathException {
71          if (next != null) {
72              current = next;
73              position++;
74              advance();
75              return current;
76          } else {
77              current = null;
78              position = -1;
79              return null;
80          }
81      }
82
83      public Item current() {
84          return current;
85      }
86
87      public int position() {
88          return position;
89      }
90
91     public SequenceIterator getAnother() throws XPathException {
92         return new GroupStartingIterator(population.getAnother(), startPattern, baseContext);
93     }
94
95     /**
96      * Get properties of this iterator, as a bit-significant integer.
97      *
98      * @return the properties of this iterator. This will be some combination of
99      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
100      * and {@link LOOKAHEAD}. It is always
101      * acceptable to return the value zero, indicating that there are no known special properties.
102      * It is acceptable for the properties of the iterator to change depending on its state.
103      */

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