KickJava   Java API By Example, From Geeks To Geeks.

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


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 GroupEndingIterator iterates over a sequence of groups defined by
17  * xsl:for-each-group group-ending-with="x". The groups are returned in
18  * order of first appearance.
19  */

20
21 public class GroupEndingIterator implements GroupIterator {
22
23     private SequenceIterator population;
24     private Pattern endPattern;
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 GroupEndingIterator(SequenceIterator population, Pattern endPattern,
33                                  XPathContext context)
34     throws XPathException {
35         this.population = population;
36         this.endPattern = endPattern;
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(20);
46          currentMembers.add(current);
47
48          next = current;
49          while (next != null) {
50              if (endPattern.matches((NodeInfo)next, runningContext)) {
51                  next = population.next();
52                  if (next != null) {
53                      break;
54                  }
55              } else {
56                  next = population.next();
57                  if (next != null) {
58                      currentMembers.add(next);
59                  }
60              }
61          }
62      }
63
64      public AtomicValue getCurrentGroupingKey() {
65          return null;
66      }
67
68      public SequenceIterator iterateCurrentGroup() {
69          return new ListIterator(currentMembers);
70      }
71
72      public Item next() throws XPathException {
73          if (next != null) {
74              current = next;
75              position++;
76              advance();
77              return current;
78          } else {
79              current = null;
80              position = -1;
81              return null;
82          }
83      }
84
85      public Item current() {
86          return current;
87      }
88
89      public int position() {
90          return position;
91      }
92
93     public SequenceIterator getAnother() throws XPathException {
94         return new GroupEndingIterator(population.getAnother(), endPattern, baseContext);
95     }
96
97     /**
98      * Get properties of this iterator, as a bit-significant integer.
99      *
100      * @return the properties of this iterator. This will be some combination of
101      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
102      * and {@link LOOKAHEAD}. It is always
103      * acceptable to return the value zero, indicating that there are no known special properties.
104      * It is acceptable for the properties of the iterator to change depending on its state.
105      */

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