|
32 | 32 |
|
33 | 33 | import java.nio.charset.StandardCharsets;
|
34 | 34 |
|
| 35 | +import static org.exist.xquery.FunctionDSL.*; |
| 36 | + |
35 | 37 | public class FunCollationKey extends BasicFunction {
|
36 | 38 |
|
37 |
| - private static final QName FN_NAME = new QName("collation-key", Function.BUILTIN_FUNCTION_NS, FnModule.PREFIX); |
| 39 | + private static final String FN_NAME = "collation-key"; // , Function.BUILTIN_FUNCTION_NS, FnModule.PREFIX); |
38 | 40 | private static final String FN_DESCRIPTION =
|
39 |
| - "Given a $value-string value and a $collection-string " + |
| 41 | + "Given a $value-string value and a $collation-string " + |
40 | 42 | "collation, generates an internal value called a collation key, with the " +
|
41 | 43 | "property that the matching and ordering of collation " +
|
42 | 44 | "keys reflects the matching and ordering of strings " +
|
43 | 45 | "under the specified collation.";
|
44 |
| - private static final FunctionReturnSequenceType FN_RETURN = new FunctionReturnSequenceType( |
45 |
| - Type.BASE64_BINARY, Cardinality.ZERO_OR_ONE, "the collation key" |
46 |
| - ); |
47 |
| - |
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), |
54 |
| - new FunctionSignature(FunCollationKey.FN_NAME, FunCollationKey.FN_DESCRIPTION, |
55 |
| - new SequenceType[] { |
56 |
| - new FunctionParameterSequenceType("value-string", Type.STRING, |
57 |
| - Cardinality.ZERO_OR_ONE, "The value string"), |
58 |
| - new FunctionParameterSequenceType("collection-string", Type.STRING, |
59 |
| - Cardinality.ZERO_OR_ONE, "The collation string") |
60 |
| - }, FN_RETURN) |
61 |
| - }; |
| 46 | + private static final FunctionReturnSequenceType FN_RETURN = returnsOpt(Type.BASE64_BINARY, "the collation key"); |
| 47 | + private static final FunctionParameterSequenceType PARAM_VALUE_STRING = param("value-string", Type.STRING, "The value string"); |
| 48 | + private static final FunctionParameterSequenceType PARAM_COLLATION_STRING = param("collation-string", Type.STRING, "The collation string"); |
| 49 | + public static final FunctionSignature[] FS_COLLATION_KEY_SIGNATURES = functionSignatures( |
| 50 | + FN_NAME, |
| 51 | + FN_DESCRIPTION, |
| 52 | + FN_RETURN, |
| 53 | + arities( |
| 54 | + arity(PARAM_VALUE_STRING), |
| 55 | + arity(PARAM_VALUE_STRING, PARAM_COLLATION_STRING) |
| 56 | + ) |
| 57 | + ); |
62 | 58 |
|
63 | 59 | public FunCollationKey(final XQueryContext context, final FunctionSignature signature) {
|
64 | 60 | super(context, signature);
|
65 | 61 | }
|
66 | 62 |
|
67 |
| - public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { |
| 63 | + public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException { |
68 | 64 | final String source = (args.length >= 1) ? args[0].toString() : "";
|
69 | 65 | final Collator collator = (args.length >= 2) ? Collations.getCollationFromURI(args[1].toString()) : null;
|
70 | 66 |
|
71 | 67 | return new BinaryValueFromBinaryString(new Base64BinaryValueType(), Base64.encodeBase64String(
|
72 | 68 | (collator == null) ? source.getBytes(StandardCharsets.UTF_8) : new String(collator.getCollationKey(source).toByteArray()).getBytes(StandardCharsets.UTF_8)));
|
73 | 69 | }
|
| 70 | + |
| 71 | + private static FunctionSignature[] functionSignatures(final String name, final String description, final FunctionReturnSequenceType returnType, final FunctionParameterSequenceType[][] variableParamTypes) { |
| 72 | + return FunctionDSL.functionSignatures(new QName(name, Function.BUILTIN_FUNCTION_NS, FnModule.PREFIX), description, returnType, variableParamTypes); |
| 73 | + } |
74 | 74 | }
|
0 commit comments