Debian Codegraph @

net/sf/saxon/tree/iter/HomogeneityCheckerIterator.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.tree.iter;
9
10 import net.sf.saxon.expr.parser.Location;
11 import net.sf.saxon.expr.sort.DocumentOrderIterator;
12 import net.sf.saxon.expr.sort.GlobalOrderComparer;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.om.NodeInfo;
15 import net.sf.saxon.om.SequenceIterator;
16 import net.sf.saxon.trans.XPathException;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 /**
22 * An iterator that returns the same items as its base iterator, checking to see that they are either
23 * all nodes, or all non-nodes; if they are all nodes, it delivers them in document order.
24 */
25
26 public class HomogeneityCheckerIterator implements SequenceIterator<Item<?>> {
27
28 SequenceIterator<?> base = null;
29 Location loc;
30 int state;
31 // state = 0: initial state, will accept either nodes or atomic values
32 // state = +1: have seen a node, all further items must be nodes
33 // state = -1: have seen an atomic value or function item, all further items must be the same
34
35 public HomogeneityCheckerIterator(SequenceIterator<?> base, Location loc) {
36 this.base = base;
37 this.loc = loc;
38 state = 0;
39 }
40
41 public void close() {
42 base.close();
43 }
44
45 /*@NotNull*/
46 private XPathException reportMixedItems() {
47 XPathException err = new XPathException("Cannot mix nodes and atomic values in the result of a path expression");
48 err.setErrorCode("XPTY0018");
49 err.setLocator(loc);
50 return err;
51 }
52
53 public int getProperties() {
54 return 0;
55 }
56
57 /*@Nullable*/
58 public Item next() throws XPathException {
59 Item item = base.next();
60 if (item == null) {
61 return null;
62 }
63 //first item in iterator
64 if (state == 0) {
65 if (item instanceof NodeInfo) {
66 List<Item<?>> nodes = new ArrayList<>(50);
67 nodes.add(item);
68 while ((item = base.next()) != null) {
69 if (!(item instanceof NodeInfo)) {
70 throw reportMixedItems();
71 } else {
72 nodes.add(item);
73 }
74 }
75 base = new DocumentOrderIterator(new ListIterator<>(nodes), GlobalOrderComparer.getInstance());
76 state = 1; // first item is a node
77 return base.next();
78 } else {
79 state = -1; // first item is an atomic value or function item
80 }
81 } else if (state == -1 && item instanceof NodeInfo) {
82 throw reportMixedItems();
83 }
84 return item;
85 }
86
87
88 }