| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.sort; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.LastPositionFinder; |
| 11 |
import net.sf.saxon.om.Item; |
| 12 |
import net.sf.saxon.om.NodeInfo; |
| 13 |
import net.sf.saxon.om.SequenceIterator; |
| 14 |
import net.sf.saxon.trans.Err; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.tree.iter.ListIterator; |
| 17 |
|
| 18 |
import java.util.ArrayList; |
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public final class DocumentOrderIterator implements SequenceIterator<NodeInfo>, Sortable { |
| 29 |
|
| 30 |
private SequenceIterator<NodeInfo> iterator; |
| 31 |
private List<NodeInfo> sequence; |
| 32 |
private ItemOrderComparer comparer; |
| 33 |
private NodeInfo current = null; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public DocumentOrderIterator(SequenceIterator<?> base, ItemOrderComparer comparer) throws XPathException { |
| 44 |
|
| 45 |
this.comparer = comparer; |
| 46 |
|
| 47 |
int len = (base.getProperties() & SequenceIterator.LAST_POSITION_FINDER) == 0 |
| 48 |
? 50 |
| 49 |
: ((LastPositionFinder) base).getLength(); |
| 50 |
sequence = new ArrayList<>(len); |
| 51 |
base.forEachOrFail(item -> { |
| 52 |
if (item instanceof NodeInfo) { |
| 53 |
sequence.add((NodeInfo) item); |
| 54 |
} else { |
| 55 |
throw new XPathException("Item in input for sorting is not a node: " + Err.depict(item), "XPTY0004"); |
| 56 |
} |
| 57 |
}); |
| 58 |
|
| 59 |
|
| 60 |
if (sequence.size() > 1) { |
| 61 |
GenericSorter.quickSort(0, sequence.size(), this); |
| 62 |
} |
| 63 |
iterator = new ListIterator<>(sequence); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private DocumentOrderIterator() { |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public int compare(int a, int b) { |
| 79 |
|
| 80 |
return comparer.compare(sequence.get(a), sequence.get(b)); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public void swap(int a, int b) { |
| 88 |
NodeInfo temp = sequence.get(a); |
| 89 |
sequence.set(a, sequence.get(b)); |
| 90 |
sequence.set(b, temp); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public NodeInfo next() throws XPathException { |
| 97 |
while (true) { |
| 98 |
NodeInfo next = iterator.next(); |
| 99 |
if (next == null) { |
| 100 |
current = null; |
| 101 |
return null; |
| 102 |
} |
| 103 |
if (!next.equals(current)) { |
| 104 |
current = next; |
| 105 |
return current; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public int getProperties() { |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
public void close() { |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|