Skip to content

Commit db7ae8f

Browse files
chris-evolvedbinaryadamretter
authored andcommitted
[feature] Implement the fn:collation-key function.
1 parent 2b0738c commit db7ae8f

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FnModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class FnModule extends AbstractInternalModule {
5252
new FunctionDef(FunCeiling.signature, FunCeiling.class),
5353
new FunctionDef(FunCodepointEqual.signature, FunCodepointEqual.class),
5454
new FunctionDef(FunCodepointsToString.signature, FunCodepointsToString.class),
55+
new FunctionDef(FunCollationKey.FS_COLLATION_KEY_SIGNATURES[0], FunCollationKey.class),
5556
new FunctionDef(FunCompare.signatures[0], FunCompare.class),
5657
new FunctionDef(FunCompare.signatures[1], FunCompare.class),
5758
new FunctionDef(FunConcat.signature, FunConcat.class),
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* eXist-db Open Source Native XML Database
3+
* Copyright (C) 2001 The eXist-db Authors
4+
*
5+
6+
* http://www.exist-db.org
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
23+
package org.exist.xquery.functions.fn;
24+
25+
import org.apache.commons.codec.binary.Base64;
26+
import org.exist.dom.QName;
27+
import org.exist.util.Collations;
28+
import org.exist.xquery.*;
29+
import org.exist.xquery.value.*;
30+
31+
import com.ibm.icu.text.Collator;
32+
33+
public class FunCollationKey extends BasicFunction {
34+
35+
private static final QName FN_NAME = new QName("collation-key", Function.BUILTIN_FUNCTION_NS, FnModule.PREFIX);
36+
private static final String FN_DESCRIPTION =
37+
"Given a $value-string value and a $collection-string " +
38+
"collation, generates an internal value called a collation key, with the " +
39+
"property that the matching and ordering of collation " +
40+
"keys reflects the matching and ordering of strings " +
41+
"under the specified collation.";
42+
private static final FunctionReturnSequenceType FN_RETURN = new FunctionReturnSequenceType(
43+
Type.BASE64_BINARY, Cardinality.ZERO_OR_ONE, "the collation key"
44+
);
45+
46+
public static final FunctionSignature[] FS_COLLATION_KEY_SIGNATURES = {
47+
new FunctionSignature(FunCollationKey.FN_NAME, FunCollationKey.FN_DESCRIPTION,
48+
new SequenceType[] {
49+
new FunctionParameterSequenceType("value-string", Type.STRING,
50+
Cardinality.ZERO_OR_ONE, "The value string"),
51+
new FunctionParameterSequenceType("collection-string", Type.STRING,
52+
Cardinality.ZERO_OR_ONE, "The collation string")
53+
}, FN_RETURN)
54+
55+
};
56+
57+
public FunCollationKey(final XQueryContext context, final FunctionSignature signature) {
58+
super(context, signature);
59+
}
60+
61+
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
62+
final BinaryValue result;
63+
final String source = (args.length >= 1) ? args[0].toString() : "";
64+
final Collator collator = (args.length >= 2) ? Collations.getCollationFromURI(args[1].toString()) : context.getDefaultCollator();
65+
result = new BinaryValueFromBinaryString(new Base64BinaryValueType(), Base64.encodeBase64String(collator.getCollationKey(source).toByteArray()));
66+
return result;
67+
}
68+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
(:
2+
: eXist-db Open Source Native XML Database
3+
: Copyright (C) 2001 The eXist-db Authors
4+
:
5+
6+
: http://www.exist-db.org
7+
:
8+
: This library is free software; you can redistribute it and/or
9+
: modify it under the terms of the GNU Lesser General Public
10+
: License as published by the Free Software Foundation; either
11+
: version 2.1 of the License, or (at your option) any later version.
12+
:
13+
: This library is distributed in the hope that it will be useful,
14+
: but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
: Lesser General Public License for more details.
17+
:
18+
: You should have received a copy of the GNU Lesser General Public
19+
: License along with this library; if not, write to the Free Software
20+
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
:)
22+
xquery version "3.1";
23+
24+
module namespace fnp="http://exist-db.org/xquery/test/function_codepoint_equal";
25+
26+
declare namespace test="http://exist-db.org/xquery/xqsuite";
27+
28+
declare
29+
%test:assertEmpty
30+
function fnp:empty-sequences() {
31+
fn:codepoint-equal((), ())
32+
};
33+
34+
declare
35+
%test:assertEmpty
36+
function fnp:empty-string-and-empty-sequence() {
37+
fn:codepoint-equal("", ())
38+
};
39+
40+
declare
41+
%test:assertTrue
42+
function fnp:empty-strings() {
43+
fn:codepoint-equal("", "")
44+
};
45+
46+
declare
47+
%test:assertTrue
48+
function fnp:equal() {
49+
fn:codepoint-equal("abcd", "abcd")
50+
};
51+
52+
declare
53+
%test:assertFalse
54+
function fnp:not-equal() {
55+
fn:codepoint-equal("abcd", "abcd ")
56+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(:
2+
: eXist-db Open Source Native XML Database
3+
: Copyright (C) 2001 The eXist-db Authors
4+
:
5+
6+
: http://www.exist-db.org
7+
:
8+
: This library is free software; you can redistribute it and/or
9+
: modify it under the terms of the GNU Lesser General Public
10+
: License as published by the Free Software Foundation; either
11+
: version 2.1 of the License, or (at your option) any later version.
12+
:
13+
: This library is distributed in the hope that it will be useful,
14+
: but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
: Lesser General Public License for more details.
17+
:
18+
: You should have received a copy of the GNU Lesser General Public
19+
: License along with this library; if not, write to the Free Software
20+
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
:)
22+
xquery version "3.1";
23+
24+
module namespace fnp="http://exist-db.org/xquery/test/function_collation_key";
25+
26+
declare namespace test="http://exist-db.org/xquery/xqsuite";
27+
28+
declare
29+
%test:assertTrue
30+
function fnp:equal() {
31+
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?strength=primary")
32+
let $second := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?strength=primary")
33+
return $first = $second
34+
};
35+
36+
declare
37+
%test:assertTrue
38+
function fnp:equal-ignore-case() {
39+
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?strength=primary")
40+
let $second := fn:collation-key("A", "http://www.w3.org/2013/collation/UCA?strength=primary")
41+
return $first = $second
42+
};
43+
44+
declare
45+
%test:assertTrue
46+
function fnp:not-equal() {
47+
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?strength=primary")
48+
let $second := fn:collation-key("b", "http://www.w3.org/2013/collation/UCA?strength=primary")
49+
return $first != $second
50+
};

0 commit comments

Comments
 (0)