| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class HomogeneityCheckerIterator implements SequenceIterator<Item<?>> { |
| 27 |
|
| 28 |
SequenceIterator<?> base = null; |
| 29 |
Location loc; |
| 30 |
int state; |
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 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 |
|
| 58 |
public Item next() throws XPathException { |
| 59 |
Item item = base.next(); |
| 60 |
if (item == null) { |
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 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; |
| 77 |
return base.next(); |
| 78 |
} else { |
| 79 |
state = -1; |
| 80 |
} |
| 81 |
} else if (state == -1 && item instanceof NodeInfo) { |
| 82 |
throw reportMixedItems(); |
| 83 |
} |
| 84 |
return item; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
} |