29
29
30
30
import com .ibm .icu .text .Collator ;
31
31
32
+ import java .io .IOException ;
32
33
import java .nio .charset .StandardCharsets ;
33
34
34
35
import static org .exist .xquery .FunctionDSL .*;
35
36
import static org .exist .xquery .functions .fn .FnModule .functionSignatures ;
36
37
37
38
public class FunCollationKey extends BasicFunction {
38
39
39
- private static final String FN_NAME = "collation-key" ; // , Function.BUILTIN_FUNCTION_NS, FnModule.PREFIX);
40
+ private static final String FN_NAME = "collation-key" ;
40
41
private static final String FN_DESCRIPTION =
41
42
"Given a $value-string value and a $collation-string " +
42
43
"collation, generates an internal value called a collation key, with the " +
@@ -63,8 +64,17 @@ public FunCollationKey(final XQueryContext context, final FunctionSignature sign
63
64
public Sequence eval (final Sequence [] args , final Sequence contextSequence ) throws XPathException {
64
65
final String source = (args .length >= 1 ) ? args [0 ].itemAt (0 ).toString () : "" ;
65
66
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 ;
69
79
}
70
80
}
0 commit comments