Debian Codegraph @

net/sf/saxon/expr/sort/GroupEndingIterator.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr.sort;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.functions.Count;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.pattern.Pattern;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.iter.LookaheadIterator;
17
18 import java.util.ArrayList;
19
20 /**
21 * A GroupEndingIterator iterates over a sequence of groups defined by
22 * xsl:for-each-group group-ending-with="x". The groups are returned in
23 * order of first appearance.
24 */
25
26 public class GroupEndingIterator extends GroupMatchingIterator implements GroupIterator, LookaheadIterator<Item<?>> {
27
28 public GroupEndingIterator(Expression select, Pattern endPattern,
29 XPathContext context)
30 throws XPathException {
31 this.select = select;
32 this.pattern = endPattern;
33 baseContext = context;
34 runningContext = context.newMinorContext();
35 this.population = runningContext.trackFocus(select.iterate(context));
36 // the first item in the population always starts a new group
37 next = population.next();
38 }
39
40 @Override
41 public int getLength() throws XPathException {
42 GroupEndingIterator another = new GroupEndingIterator(select, pattern, baseContext);
43 return Count.steppingCount(another);
44 }
45
46 protected void advance() throws XPathException {
47 currentMembers = new ArrayList<>(20);
48 currentMembers.add(current);
49
50 next = current;
51 while (next != null) {
52 if (pattern.matches(next, runningContext)) {
53 next = population.next();
54 if (next != null) {
55 break;
56 }
57 } else {
58 next = population.next();
59 if (next != null) {
60 currentMembers.add(next);
61 }
62 }
63 }
64 }
65
66
67 }
68