30
30
31
31
import com .ibm .icu .text .Collator ;
32
32
33
+ import java .nio .charset .StandardCharsets ;
34
+
33
35
public class FunCollationKey extends BasicFunction {
34
36
35
37
private static final QName FN_NAME = new QName ("collation-key" , Function .BUILTIN_FUNCTION_NS , FnModule .PREFIX );
@@ -44,14 +46,18 @@ public class FunCollationKey extends BasicFunction {
44
46
);
45
47
46
48
public static final FunctionSignature [] FS_COLLATION_KEY_SIGNATURES = {
49
+ new FunctionSignature (FunCollationKey .FN_NAME , FunCollationKey .FN_DESCRIPTION ,
50
+ new SequenceType [] {
51
+ new FunctionParameterSequenceType ("value-string" , Type .STRING ,
52
+ Cardinality .ZERO_OR_ONE , "The value string" )
53
+ }, FN_RETURN ),
47
54
new FunctionSignature (FunCollationKey .FN_NAME , FunCollationKey .FN_DESCRIPTION ,
48
55
new SequenceType [] {
49
56
new FunctionParameterSequenceType ("value-string" , Type .STRING ,
50
57
Cardinality .ZERO_OR_ONE , "The value string" ),
51
58
new FunctionParameterSequenceType ("collection-string" , Type .STRING ,
52
59
Cardinality .ZERO_OR_ONE , "The collation string" )
53
60
}, FN_RETURN )
54
-
55
61
};
56
62
57
63
public FunCollationKey (final XQueryContext context , final FunctionSignature signature ) {
@@ -61,7 +67,18 @@ public FunCollationKey(final XQueryContext context, final FunctionSignature sign
61
67
public Sequence eval (Sequence [] args , Sequence contextSequence ) throws XPathException {
62
68
final BinaryValue result ;
63
69
final String source = (args .length >= 1 ) ? args [0 ].toString () : "" ;
64
- final Collator collator = (args .length >= 2 ) ? Collations .getCollationFromURI (args [1 ].toString ()) : context .getDefaultCollator ();
70
+ final Collator collator ;
71
+ if (args .length >= 2 ) {
72
+ collator = Collations .getCollationFromURI (args [1 ].toString ());
73
+ if (collator == null ) {
74
+ throw new XPathException (ErrorCodes .FOCH0002 , "Unsupported collation: " + args [1 ]);
75
+ }
76
+ } else {
77
+ collator = context .getDefaultCollator ();
78
+ if (collator == null ) {
79
+ throw new XPathException (ErrorCodes .FOCH0002 , "Could not get default collator." );
80
+ }
81
+ }
65
82
result = new BinaryValueFromBinaryString (new Base64BinaryValueType (), Base64 .encodeBase64String (collator .getCollationKey (source ).toByteArray ()));
66
83
return result ;
67
84
}
0 commit comments