|
| 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.exist.xquery.BasicFunction; |
| 26 | +import org.exist.xquery.FunctionSignature; |
| 27 | +import org.exist.xquery.XPathException; |
| 28 | +import org.exist.xquery.XQueryContext; |
| 29 | +import org.exist.xquery.functions.integer.IntegerPicture; |
| 30 | +import org.exist.xquery.value.*; |
| 31 | + |
| 32 | +import java.math.BigInteger; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +import static org.exist.xquery.FunctionDSL.*; |
| 37 | +import static org.exist.xquery.functions.fn.FnModule.functionSignatures; |
| 38 | + |
| 39 | +/** |
| 40 | + * Implements fn:format-integer as per W3C XPath and XQuery Functions and Operators 3.1 |
| 41 | + * <p> |
| 42 | + * fn:format-number($value as integer?, $picture as xs:string) as xs:string |
| 43 | + * fn:format-number($value as integer?, $picture as xs:string, $lang as xs:string) as xs:string |
| 44 | + * |
| 45 | + * @author <a href="mailto:[email protected]">Alan Paxton</a> |
| 46 | + */ |
| 47 | +public class FnFormatIntegers extends BasicFunction { |
| 48 | + |
| 49 | + private static final FunctionParameterSequenceType FS_PARAM_VALUE = optParam("value", Type.NUMBER, "The number to format"); |
| 50 | + private static final FunctionParameterSequenceType FS_PARAM_PICTURE = param("picture", Type.STRING, "The picture string to use for formatting. To understand the picture string syntax, see: https://www.w3.org/TR/xpath-functions-31/#func-format-number"); |
| 51 | + |
| 52 | + private static final String FS_FORMAT_INTEGER_NAME = "format-integer"; |
| 53 | + static final FunctionSignature[] FS_FORMAT_INTEGER = functionSignatures( |
| 54 | + FS_FORMAT_INTEGER_NAME, |
| 55 | + "Returns a string containing an integer formatted according to a given picture string.", |
| 56 | + returns(Type.STRING, "The formatted string representation of the supplied integer"), |
| 57 | + arities( |
| 58 | + arity( |
| 59 | + FS_PARAM_VALUE, |
| 60 | + FS_PARAM_PICTURE |
| 61 | + ), |
| 62 | + arity( |
| 63 | + FS_PARAM_VALUE, |
| 64 | + FS_PARAM_PICTURE, |
| 65 | + optParam("lang", Type.STRING, "The language in which to format the integers.") |
| 66 | + ) |
| 67 | + ) |
| 68 | + ); |
| 69 | + |
| 70 | + public FnFormatIntegers(final XQueryContext context, final FunctionSignature signature) { |
| 71 | + super(context, signature); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public Sequence eval(final Sequence[] args, final Sequence contextSequence) |
| 76 | + throws XPathException { |
| 77 | + // If $value is an empty sequence, the function returns a zero-length string |
| 78 | + // https://www.w3.org/TR/xpath-functions-31/#func-format-integer |
| 79 | + if (args[0].isEmpty()) { |
| 80 | + return Sequence.EMPTY_SEQUENCE; |
| 81 | + } |
| 82 | + |
| 83 | + // If the value of $value is negative, the rules below are applied to the absolute value of $value, |
| 84 | + // and a minus sign is prepended to the result. |
| 85 | + final IntegerValue integerValue = (IntegerValue) args[0].itemAt(0); |
| 86 | + final BigInteger bigInteger = integerValue.toJavaObject(BigInteger.class); |
| 87 | + |
| 88 | + final IntegerPicture picture = IntegerPicture.fromString(args[1].getStringValue()); |
| 89 | + |
| 90 | + // Build a list of languages to try |
| 91 | + // the called picture will use the first one with a valid locale |
| 92 | + final List<String> languages = new ArrayList<>(2); |
| 93 | + if (args.length == 3 && !args[2].isEmpty()) { |
| 94 | + languages.add(args[2].getStringValue()); |
| 95 | + } |
| 96 | + languages.add(context.getDefaultLanguage()); |
| 97 | + |
| 98 | + return new StringValue(picture.formatInteger(bigInteger, languages)); |
| 99 | + } |
| 100 | +} |
0 commit comments