| 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 OrderByClausePush extends TuplePush implements Sortable { |
| 22 |
|
| 23 |
private TuplePush destination; |
| 24 |
private OrderByClause orderByClause; |
| 25 |
private TupleExpression tupleExpr; |
| 26 |
protected AtomicComparer[] comparers; |
| 27 |
XPathContext context; |
| 28 |
int position = 0; |
| 29 |
private ArrayList<ItemToBeSorted> tupleArray = new ArrayList<ItemToBeSorted>(100); |
| 30 |
|
| 31 |
public OrderByClausePush(TuplePush destination, TupleExpression tupleExpr, OrderByClause orderBy, XPathContext context) { |
| 32 |
this.destination = destination; |
| 33 |
this.tupleExpr = tupleExpr; |
| 34 |
this.orderByClause = orderBy; |
| 35 |
this.context = context; |
| 36 |
|
| 37 |
AtomicComparer[] suppliedComparers = orderBy.getAtomicComparers(); |
| 38 |
comparers = new AtomicComparer[suppliedComparers.length]; |
| 39 |
for (int n = 0; n < comparers.length; n++) { |
| 40 |
this.comparers[n] = suppliedComparers[n].provideContext(context); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
@Override |
| 48 |
public void processTuple(XPathContext context) throws XPathException { |
| 49 |
|
| 50 |
Tuple tuple = tupleExpr.evaluateItem(context); |
| 51 |
SortKeyDefinitionList sortKeyDefinitions = orderByClause.getSortKeyDefinitions(); |
| 52 |
ItemToBeSorted itbs = new ItemToBeSorted(sortKeyDefinitions.size()); |
| 53 |
itbs.value = tuple; |
| 54 |
for (int i = 0; i < sortKeyDefinitions.size(); i++) { |
| 55 |
itbs.sortKeyValues[i] = orderByClause.evaluateSortKey(i, context); |
| 56 |
} |
| 57 |
itbs.originalPosition = ++position; |
| 58 |
tupleArray.add(itbs); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public int compare(int a, int b) { |
| 68 |
try { |
| 69 |
for (int i = 0; i < comparers.length; i++) { |
| 70 |
int comp = comparers[i].compareAtomicValues( |
| 71 |
tupleArray.get(a).sortKeyValues[i], tupleArray.get(b).sortKeyValues[i]); |
| 72 |
if (comp != 0) { |
| 73 |
|
| 74 |
return comp; |
| 75 |
} |
| 76 |
} |
| 77 |
} catch (NoDynamicContextException e) { |
| 78 |
throw new AssertionError("Sorting without dynamic context: " + e.getMessage()); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
return tupleArray.get(a).originalPosition - tupleArray.get(b).originalPosition; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public void swap(int a, int b) { |
| 90 |
ItemToBeSorted temp = tupleArray.get(a); |
| 91 |
tupleArray.set(a, tupleArray.get(b)); |
| 92 |
tupleArray.set(b, temp); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
@Override |
| 99 |
public void close() throws XPathException { |
| 100 |
try { |
| 101 |
GenericSorter.quickSort(0, position, this); |
| 102 |
} catch (ClassCastException e) { |
| 103 |
XPathException err = new XPathException("Non-comparable types found while sorting: " + e.getMessage()); |
| 104 |
err.setErrorCode("XPTY0004"); |
| 105 |
throw err; |
| 106 |
} |
| 107 |
|
| 108 |
for (ItemToBeSorted itbs : tupleArray) { |
| 109 |
tupleExpr.setCurrentTuple(context, (Tuple) itbs.value); |
| 110 |
destination.processTuple(context); |
| 111 |
} |
| 112 |
destination.close(); |
| 113 |
} |
| 114 |
} |
| 115 |
|