| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public final class CardinalityCheckingIterator<T extends Item<?>> implements SequenceIterator<T> { |
| 28 |
|
| 29 |
private SequenceIterator<T> base; |
| 30 |
private Location locator; |
| 31 |
private T first = null; |
| 32 |
private T second = null; |
| 33 |
private int position = 0; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 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 |
|