| 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.lib.StringCollator; |
| 12 | import net.sf.saxon.om.Sequence; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.value.AtomicValue; |
| 15 | import net.sf.saxon.value.Base64BinaryValue; |
| 16 | |
| 17 | /** |
| 18 | * Implements the collation-key function defined in the XSLT 3.0 and XPath 3.1 specifications |
| 19 | */ |
| 20 | public class CollationKeyFn extends CollatingFunctionFixed { |
| 21 | |
| 22 | |
| 23 | private static Base64BinaryValue getCollationKey(String s, StringCollator collator) { |
| 24 | AtomicValue val = collator.getCollationKey(s).asAtomic(); |
| 25 | if (val instanceof Base64BinaryValue) { |
| 26 | return (Base64BinaryValue) val; |
| 27 | } else { |
| 28 | throw new IllegalStateException("Collation key must be Base64Binary"); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Call the Callable. |
| 34 | * |
| 35 | * @param context the dynamic evaluation context |
| 36 | * @param arguments the values of the arguments, supplied as Sequences. |
| 37 | * <p>Generally it is advisable, if calling iterate() to process a supplied sequence, to |
| 38 | * call it only once; if the value is required more than once, it should first be converted |
| 39 | * to a {@link net.sf.saxon.om.GroundedValue} by calling the utility methd |
| 40 | * SequenceTool.toGroundedValue().</p> |
| 41 | * <p>If the expected value is a single item, the item should be obtained by calling |
| 42 | * Sequence.head(): it cannot be assumed that the item will be passed as an instance of |
| 43 | * {@link net.sf.saxon.om.Item} or {@link net.sf.saxon.value.AtomicValue}.</p> |
| 44 | * <p>It is the caller's responsibility to perform any type conversions required |
| 45 | * to convert arguments to the type expected by the callee. An exception is where |
| 46 | * this Callable is explicitly an argument-converting wrapper around the original |
| 47 | * Callable.</p> |
| 48 | * @return the result of the evaluation, in the form of a Sequence. It is the responsibility |
| 49 | * of the callee to ensure that the type of result conforms to the expected result type. |
| 50 | * @throws net.sf.saxon.trans.XPathException |
| 51 | * if a dynamic error occurs during the evaluation of the expression |
| 52 | */ |
| 53 | public Base64BinaryValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 54 | String in = arguments[0].head().getStringValue(); |
| 55 | StringCollator collator = getStringCollator(); |
| 56 | return getCollationKey(in, collator); |
| 57 | } |
| 58 | } |
| 59 |