Debian Codegraph @

net/sf/saxon/functions/Sort_1.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * This class implements the function fn:sort#1, which is a standard function in XPath 3.1
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 * Evaluate the expression
38 *
39 * @param context the dynamic evaluation context
40 * @param arguments the values of the arguments, supplied as SequenceIterators
41 * @return the result of the evaluation, in the form of a SequenceIterator
42 * @throws XPathException if a dynamic error occurs during the evaluation of the expression
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 }