Debian Codegraph @

net/sf/saxon/expr/CardinalityCheckingIterator.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;
9
10 import net.sf.saxon.expr.parser.Location;
11 import net.sf.saxon.expr.parser.RoleDiagnostic;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.SequenceIterator;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.iter.ArrayIterator;
16 import net.sf.saxon.value.Cardinality;
17
18 /**
19 * CardinalityCheckingIterator returns the items in an underlying sequence
20 * unchanged, but checks that the number of items conforms to the required
21 * cardinality. Because cardinality checks are required to take place even
22 * if the consumer of the sequence does not require all the items, we read
23 * the first two items at initialization time. This is sufficient to perform
24 * the checks; after that we can simply return the items from the base sequence.
25 */
26
27 public final class CardinalityCheckingIterator<T extends Item<?>> implements SequenceIterator<T> {
28
29 private SequenceIterator<T> base;
30 private Location locator;
31 /*@Nullable*/ private T first = null;
32 private T second = null;
33 private int position = 0;
34
35 /**
36 * Construct an CardinalityCheckingIterator that will return the same items as the base sequence,
37 * checking how many there are
38 *
39 * @param base the base iterator
40 * @param requiredCardinality the required Cardinality
41 */
42
43 public CardinalityCheckingIterator(SequenceIterator<T> base, int requiredCardinality,
44 RoleDiagnostic role, Location locator)
45 throws XPathException {
46 this.base = base;
47 this.locator = locator;
48 first = base.next();
49 if (first == null) {
50 if (!Cardinality.allowsZero(requiredCardinality)) {
51 typeError("An empty sequence is not allowed as the " +
52 role.getMessage(), role.getErrorCode());
53 }
54 } else {
55 if (requiredCardinality == StaticProperty.EMPTY) {
56 typeError("The only value allowed for the " +
57 role.getMessage() + " is an empty sequence", role.getErrorCode());
58 }
59 second = base.next();
60 if (second != null && !Cardinality.allowsMany(requiredCardinality)) {
61 T[] leaders = (T[])new Item[]{first, second};
62 typeError("A sequence of more than one item is not allowed as the " +
63 role.getMessage() + CardinalityChecker.depictSequenceStart(new ArrayIterator(leaders), 2),
64 role.getErrorCode());
65 }
66 }
67 }
68
69 public T next() throws XPathException {
70 if (position < 2) {
71 if (position == 0) {
72 T current = first;
73 position = first == null ? -1 : 1;
74 return current;
75 } else if (position == 1) {
76 T current = second;
77 position = second == null ? -1 : 2;
78 return current;
79 } else {
80 // position == -1
81 return null;
82 }
83 }
84 T current = base.next();
85 if (current == null) {
86 position = -1;
87 } else {
88 position++;
89 }
90 return current;
91 }
92
93 public void close() {
94 base.close();
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 net.sf.saxon.om.SequenceIterator#GROUNDED},
102 * {@link net.sf.saxon.om.SequenceIterator#LAST_POSITION_FINDER},
103 * and {@link net.sf.saxon.om.SequenceIterator#LOOKAHEAD}. It is always
104 * acceptable to return the value zero, indicating that there are no known special properties.
105 * It is acceptable for the properties of the iterator to change depending on its state.
106 */
107
108 public int getProperties() {
109 return 0;
110 }
111
112 private void typeError(String message, String errorCode) throws XPathException {
113 XPathException e = new XPathException(message, errorCode, locator);
114 e.setIsTypeError(!errorCode.startsWith("FORG"));
115 throw e;
116 }
117
118
119 }
120