| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.flwor; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.expr.sort.*; |
| 12 |
import net.sf.saxon.trans.NoDynamicContextException; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
|
| 15 |
import java.util.ArrayList; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public class OrderByClausePull extends TuplePull implements Sortable { |
| 22 |
|
| 23 |
private TuplePull base; |
| 24 |
private OrderByClause orderByClause; |
| 25 |
private TupleExpression tupleExpr; |
| 26 |
private int currentPosition = -1; |
| 27 |
protected AtomicComparer[] comparers; |
| 28 |
private ArrayList<ItemToBeSorted> tupleArray = new ArrayList<ItemToBeSorted>(100); |
| 29 |
|
| 30 |
public OrderByClausePull(TuplePull base, TupleExpression tupleExpr, OrderByClause orderBy, XPathContext context) { |
| 31 |
this.base = base; |
| 32 |
this.tupleExpr = tupleExpr; |
| 33 |
this.orderByClause = orderBy; |
| 34 |
|
| 35 |
AtomicComparer[] suppliedComparers = orderBy.getAtomicComparers(); |
| 36 |
comparers = new AtomicComparer[suppliedComparers.length]; |
| 37 |
for (int n = 0; n < comparers.length; n++) { |
| 38 |
this.comparers[n] = suppliedComparers[n].provideContext(context); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
@Override |
| 52 |
public boolean nextTuple(XPathContext context) throws XPathException { |
| 53 |
if (currentPosition < 0) { |
| 54 |
currentPosition = 0; |
| 55 |
int position = 0; |
| 56 |
|
| 57 |
while (base.nextTuple(context)) { |
| 58 |
Tuple tuple = tupleExpr.evaluateItem(context); |
| 59 |
SortKeyDefinitionList sortKeyDefinitions = orderByClause.getSortKeyDefinitions(); |
| 60 |
ItemToBeSorted itbs = new ItemToBeSorted(sortKeyDefinitions.size()); |
| 61 |
itbs.value = tuple; |
| 62 |
for (int i = 0; i < sortKeyDefinitions.size(); i++) { |
| 63 |
itbs.sortKeyValues[i] = orderByClause.evaluateSortKey(i, context); |
| 64 |
} |
| 65 |
itbs.originalPosition = ++position; |
| 66 |
tupleArray.add(itbs); |
| 67 |
} |
| 68 |
|
| 69 |
try { |
| 70 |
GenericSorter.quickSort(0, position, this); |
| 71 |
} catch (ClassCastException e) { |
| 72 |
XPathException err = new XPathException("Non-comparable types found while sorting: " + e.getMessage()); |
| 73 |
err.setErrorCode("XPTY0004"); |
| 74 |
throw err; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if (currentPosition < tupleArray.size()) { |
| 79 |
tupleExpr.setCurrentTuple(context, (Tuple) tupleArray.get(currentPosition++).value); |
| 80 |
return true; |
| 81 |
} else { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public int compare(int a, int b) { |
| 93 |
try { |
| 94 |
for (int i = 0; i < comparers.length; i++) { |
| 95 |
int comp = comparers[i].compareAtomicValues( |
| 96 |
tupleArray.get(a).sortKeyValues[i], tupleArray.get(b).sortKeyValues[i]); |
| 97 |
if (comp != 0) { |
| 98 |
|
| 99 |
return comp; |
| 100 |
} |
| 101 |
} |
| 102 |
} catch (NoDynamicContextException e) { |
| 103 |
throw new AssertionError("Sorting without dynamic context: " + e.getMessage()); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
return tupleArray.get(a).originalPosition - tupleArray.get(b).originalPosition; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public void swap(int a, int b) { |
| 115 |
ItemToBeSorted temp = tupleArray.get(a); |
| 116 |
tupleArray.set(a, tupleArray.get(b)); |
| 117 |
tupleArray.set(b, temp); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@Override |
| 125 |
public void close() { |
| 126 |
base.close(); |
| 127 |
} |
| 128 |
} |
| 129 |
|