| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.expr.sort.AtomicComparer; |
| 12 |
import net.sf.saxon.expr.sort.AtomicSortComparer; |
| 13 |
import net.sf.saxon.expr.sort.GenericSorter; |
| 14 |
import net.sf.saxon.expr.sort.Sortable; |
| 15 |
import net.sf.saxon.lib.StringCollator; |
| 16 |
import net.sf.saxon.ma.arrays.ArraySort; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.value.SequenceExtent; |
| 20 |
|
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.List; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class Sort_1 extends SystemFunction { |
| 29 |
|
| 30 |
public static class ItemToBeSorted { |
| 31 |
public Item value; |
| 32 |
public GroundedValue sortKey; |
| 33 |
public int originalPosition; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 45 |
final List<ItemToBeSorted> inputList = getItemsToBeSorted(arguments[0]); |
| 46 |
StringCollator collation = context.getConfiguration().getCollation(getRetainedStaticContext().getDefaultCollationName()); |
| 47 |
return doSort(inputList, collation, context); |
| 48 |
} |
| 49 |
|
| 50 |
protected List<ItemToBeSorted> getItemsToBeSorted(Sequence<?> input) throws XPathException { |
| 51 |
final List<ItemToBeSorted> inputList = new ArrayList<>(); |
| 52 |
int i = 0; |
| 53 |
SequenceIterator<?> iterator = input.iterate(); |
| 54 |
Item item; |
| 55 |
while ((item = iterator.next()) != null) { |
| 56 |
ItemToBeSorted member = new ItemToBeSorted(); |
| 57 |
member.value = item; |
| 58 |
member.originalPosition = i++; |
| 59 |
member.sortKey = item.atomize(); |
| 60 |
inputList.add(member); |
| 61 |
} |
| 62 |
return inputList; |
| 63 |
} |
| 64 |
|
| 65 |
protected Sequence<?> doSort(final List<ItemToBeSorted> inputList, StringCollator collation, XPathContext context) throws XPathException { |
| 66 |
final AtomicComparer atomicComparer = AtomicSortComparer.makeSortComparer( |
| 67 |
collation, StandardNames.XS_ANY_ATOMIC_TYPE, context); |
| 68 |
Sortable sortable = new Sortable() { |
| 69 |
public int compare(int a, int b) { |
| 70 |
int result = ArraySort.compareSortKeys(inputList.get(a).sortKey, inputList.get(b).sortKey, atomicComparer); |
| 71 |
if (result == 0) { |
| 72 |
return inputList.get(a).originalPosition - inputList.get(b).originalPosition; |
| 73 |
} else { |
| 74 |
return result; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public void swap(int a, int b) { |
| 79 |
ItemToBeSorted temp = inputList.get(a); |
| 80 |
inputList.set(a, inputList.get(b)); |
| 81 |
inputList.set(b, temp); |
| 82 |
} |
| 83 |
}; |
| 84 |
try { |
| 85 |
GenericSorter.quickSort(0, inputList.size(), sortable); |
| 86 |
} catch (ClassCastException e) { |
| 87 |
XPathException err = new XPathException("Non-comparable types found while sorting: " + e.getMessage()); |
| 88 |
err.setErrorCode("XPTY0004"); |
| 89 |
throw err; |
| 90 |
} |
| 91 |
List<Item> outputList = new ArrayList<>(inputList.size()); |
| 92 |
for (ItemToBeSorted member : inputList) { |
| 93 |
outputList.add(member.value); |
| 94 |
} |
| 95 |
return new SequenceExtent(outputList); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
} |