|
| 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 | +} |
0 commit comments