| 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.expr.sort; |
| 9 | |
| 10 | import net.sf.saxon.expr.XPathContext; |
| 11 | import net.sf.saxon.lib.StringCollator; |
| 12 | import net.sf.saxon.trans.NoDynamicContextException; |
| 13 | import net.sf.saxon.value.AtomicValue; |
| 14 | import net.sf.saxon.value.StringValue; |
| 15 | |
| 16 | /** |
| 17 | * A Comparer used for comparing sort keys when data-type="text". The items to be |
| 18 | * compared are converted to strings, and the strings are then compared using an |
| 19 | * underlying collator |
| 20 | * |
| 21 | * @author Michael H. Kay |
| 22 | */ |
| 23 | |
| 24 | public class TextComparer implements AtomicComparer { |
| 25 | |
| 26 | private AtomicComparer baseComparer; |
| 27 | |
| 28 | public TextComparer(AtomicComparer baseComparer) { |
| 29 | this.baseComparer = baseComparer; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Get the underlying comparer (which doesn't do conversion to string) |
| 34 | */ |
| 35 | |
| 36 | public AtomicComparer getBaseComparer() { |
| 37 | return baseComparer; |
| 38 | } |
| 39 | |
| 40 | public StringCollator getCollator() { |
| 41 | return baseComparer.getCollator(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Supply the dynamic context in case this is needed for the comparison |
| 46 | * |
| 47 | * @param context the dynamic evaluation context |
| 48 | * @return either the original AtomicComparer, or a new AtomicComparer in which the context |
| 49 | * is known. The original AtomicComparer is not modified |
| 50 | * @throws net.sf.saxon.trans.NoDynamicContextException |
| 51 | * if the context is an "early evaluation" (compile-time) context |
| 52 | */ |
| 53 | |
| 54 | public AtomicComparer provideContext(XPathContext context) { |
| 55 | AtomicComparer newBase = baseComparer.provideContext(context); |
| 56 | if (newBase != baseComparer) { |
| 57 | return new TextComparer(newBase); |
| 58 | } else { |
| 59 | return this; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Compare two Items by converting them to strings and comparing the string values. |
| 66 | * |
| 67 | * @param a the first Item to be compared. |
| 68 | * @param b the second Item to be compared. |
| 69 | * @return <0 if a<b, 0 if a=b, >0 if a>b |
| 70 | * @throws ClassCastException if the objects are not Items, or are items that cannot be convered |
| 71 | * to strings (e.g. QNames) |
| 72 | */ |
| 73 | |
| 74 | public int compareAtomicValues(AtomicValue a, AtomicValue b) throws ClassCastException, NoDynamicContextException { |
| 75 | return baseComparer.compareAtomicValues(toStringValue(a), toStringValue(b)); |
| 76 | } |
| 77 | |
| 78 | private StringValue toStringValue(AtomicValue a) { |
| 79 | if (a instanceof StringValue) { |
| 80 | return (StringValue) a; |
| 81 | } else { |
| 82 | return new StringValue(a == null ? "" : a.getStringValue()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Compare two AtomicValue objects for equality according to the rules for their data type. UntypedAtomic |
| 88 | * values are compared by converting to the type of the other operand. |
| 89 | * |
| 90 | * @param a the first object to be compared. |
| 91 | * @param b the second object to be compared. |
| 92 | * @return true if the values are equal, false if not |
| 93 | * @throws ClassCastException if the objects are not comparable |
| 94 | */ |
| 95 | |
| 96 | public boolean comparesEqual(AtomicValue a, /*@NotNull*/ AtomicValue b) throws NoDynamicContextException { |
| 97 | return compareAtomicValues(a, b) == 0; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Create a string representation of this AtomicComparer that can be saved in a compiled |
| 102 | * package and used to reconstitute the AtomicComparer when the package is reloaded |
| 103 | * |
| 104 | * @return a string representation of the AtomicComparer |
| 105 | */ |
| 106 | public String save() { |
| 107 | return "TEXT|" + baseComparer.save(); |
| 108 | } |
| 109 | } |
| 110 |