2929
3030import com .ibm .icu .text .Collator ;
3131
32+ import java .io .IOException ;
3233import java .nio .charset .StandardCharsets ;
3334
3435import static org .exist .xquery .FunctionDSL .*;
3536import static org .exist .xquery .functions .fn .FnModule .functionSignatures ;
3637
3738public class FunCollationKey extends BasicFunction {
3839
39- private static final String FN_NAME = "collation-key" ; // , Function.BUILTIN_FUNCTION_NS, FnModule.PREFIX);
40+ private static final String FN_NAME = "collation-key" ;
4041 private static final String FN_DESCRIPTION =
4142 "Given a $value-string value and a $collation-string " +
4243 "collation, generates an internal value called a collation key, with the " +
@@ -63,8 +64,17 @@ public FunCollationKey(final XQueryContext context, final FunctionSignature sign
6364 public Sequence eval (final Sequence [] args , final Sequence contextSequence ) throws XPathException {
6465 final String source = (args .length >= 1 ) ? args [0 ].itemAt (0 ).toString () : "" ;
6566 final Collator collator = (args .length >= 2 ) ? Collations .getCollationFromURI (args [1 ].itemAt (0 ).toString ()) : null ;
66-
67- return new BinaryValueFromBinaryString (new Base64BinaryValueType (), Base64 .encodeBase64String (
68- (collator == null ) ? source .getBytes (StandardCharsets .UTF_8 ) : new String (collator .getCollationKey (source ).toByteArray ()).getBytes (StandardCharsets .UTF_8 ))).convertTo (new Base64BinaryValueType ());
67+ final Sequence sequence ;
68+ try (BinaryValueFromBinaryString binaryValue = new BinaryValueFromBinaryString (
69+ new Base64BinaryValueType (),
70+ Base64 .encodeBase64String (
71+ (collator == null ) ?
72+ source .getBytes (StandardCharsets .UTF_8 ) :
73+ new String (collator .getCollationKey (source ).toByteArray ()).getBytes (StandardCharsets .UTF_8 )))) {
74+ sequence = binaryValue .convertTo (new Base64BinaryValueType ());
75+ } catch (IOException e ) {
76+ throw new XPathException (e );
77+ }
78+ return sequence ;
6979 }
7080}
0 commit comments