| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.lib; |
| 9 | |
| 10 | import net.sf.saxon.expr.number.NumericGroupFormatter; |
| 11 | import net.sf.saxon.regex.UnicodeString; |
| 12 | |
| 13 | import java.util.Locale; |
| 14 | |
| 15 | /** |
| 16 | * Interface Numberer supports number formatting. There is a separate |
| 17 | * implementation for each language, e.g. Numberer_en for English. |
| 18 | * This supports the xsl:number element |
| 19 | * |
| 20 | * @author Michael H. Kay |
| 21 | */ |
| 22 | |
| 23 | public interface Numberer { |
| 24 | |
| 25 | /** |
| 26 | * Set the country used by this numberer (currently used only for names of timezones). |
| 27 | * <p>Note: this method is called by the system when allocating a numberer for |
| 28 | * a specific language and country. Since numberers are normally shared across threads, |
| 29 | * it should not be changed after the initial creation of the Numberer.</p> |
| 30 | * |
| 31 | * @param country The ISO two-letter country code. |
| 32 | */ |
| 33 | |
| 34 | void setCountry(String country); |
| 35 | |
| 36 | /** |
| 37 | * Get the country used by this numberer |
| 38 | * |
| 39 | * @return The ISO code of the country |
| 40 | */ |
| 41 | |
| 42 | String getCountry(); |
| 43 | |
| 44 | /** |
| 45 | * Whether this numberer has had its locale defaulted, i.e. it's not using the language requested |
| 46 | * This can be used for situations, such as in fn:format-date(), where indication of use of defaults is required. |
| 47 | * Override for subclasses where this can happen |
| 48 | * @return the locale used, null if it wasn't defaulted |
| 49 | */ |
| 50 | Locale defaultedLocale() ; |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Format a number into a string |
| 55 | * |
| 56 | * @param number The number to be formatted |
| 57 | * @param picture The format token. This is a single component of the format attribute |
| 58 | * of xsl:number, e.g. "1", "01", "i", or "a" |
| 59 | * @param groupSize number of digits per group (0 implies no grouping) |
| 60 | * @param groupSeparator string to appear between groups of digits |
| 61 | * @param letterValue The letter-value specified to xsl:number: "alphabetic" or |
| 62 | * "traditional". Can also be an empty string or null. |
| 63 | * @param ordinal The value of the ordinal attribute specified to xsl:number |
| 64 | * The value "yes" indicates that ordinal numbers should be used; "" or null indicates |
| 65 | * that cardinal numbers |
| 66 | * @return the formatted number. Note that no errors are reported; if the request |
| 67 | * is invalid, the number is formatted as if the string() function were used. |
| 68 | */ |
| 69 | |
| 70 | String format(long number, |
| 71 | UnicodeString picture, |
| 72 | int groupSize, |
| 73 | String groupSeparator, |
| 74 | String letterValue, |
| 75 | String ordinal); |
| 76 | |
| 77 | /** |
| 78 | * Format a number into a string |
| 79 | * |
| 80 | * @param number The number to be formatted |
| 81 | * @param picture The format token. This is a single component of the format attribute |
| 82 | * of xsl:number, e.g. "1", "01", "i", or "a" |
| 83 | * @param numGrpFormatter an object that handles insertion of grouping separators into the formatted number |
| 84 | * @param letterValue The letter-value specified to xsl:number: "alphabetic" or |
| 85 | * "traditional". Can also be an empty string or null. |
| 86 | * @param ordinal The value of the ordinal attribute specified to xsl:number |
| 87 | * The value "yes" indicates that ordinal numbers should be used; "" or null indicates |
| 88 | * that cardinal numbers |
| 89 | * @return the formatted number. Note that no errors are reported; if the request |
| 90 | * is invalid, the number is formatted as if the string() function were used. |
| 91 | */ |
| 92 | |
| 93 | String format(long number, |
| 94 | UnicodeString picture, |
| 95 | NumericGroupFormatter numGrpFormatter, |
| 96 | String letterValue, |
| 97 | String ordinal); |
| 98 | |
| 99 | /** |
| 100 | * Get a month name or abbreviation |
| 101 | * |
| 102 | * @param month The month number (1=January, 12=December) |
| 103 | * @param minWidth The minimum number of characters |
| 104 | * @param maxWidth The maximum number of characters |
| 105 | * @return the month name or abbreviation as a string (for example, "September" or "Sep") |
| 106 | */ |
| 107 | |
| 108 | String monthName(int month, int minWidth, int maxWidth); |
| 109 | |
| 110 | /** |
| 111 | * Get a day name or abbreviation |
| 112 | * |
| 113 | * @param day The month number (1=Monday, 7=Sunday) |
| 114 | * @param minWidth The minimum number of characters |
| 115 | * @param maxWidth The maximum number of characters |
| 116 | * @return the day name or abbreviation as a string (for example, "Monday" or "Mon") |
| 117 | */ |
| 118 | |
| 119 | String dayName(int day, int minWidth, int maxWidth); |
| 120 | |
| 121 | /** |
| 122 | * Get an am/pm indicator |
| 123 | * |
| 124 | * @param minutes the minutes within the day |
| 125 | * @param minWidth minimum width of output |
| 126 | * @param maxWidth maximum width of output |
| 127 | * @return the AM or PM indicator |
| 128 | */ |
| 129 | |
| 130 | String halfDayName(int minutes, |
| 131 | int minWidth, int maxWidth); |
| 132 | |
| 133 | /** |
| 134 | * Get an ordinal suffix for a particular component of a date/time. |
| 135 | * |
| 136 | * @param component the component specifier from a format-dateTime picture, for |
| 137 | * example "M" for the month or "D" for the day. |
| 138 | * @return a string that is acceptable in the ordinal attribute of xsl:number |
| 139 | * to achieve the required ordinal representation. For example, "-e" for the day component |
| 140 | * in German, to have the day represented as "dritte August". |
| 141 | */ |
| 142 | |
| 143 | String getOrdinalSuffixForDateTime(String component); |
| 144 | |
| 145 | /** |
| 146 | * Get the name for an era (e.g. "BC" or "AD") |
| 147 | * |
| 148 | * @param year the proleptic gregorian year, using "0" for the year before 1AD |
| 149 | * @return the name of the era, for example "AD" |
| 150 | */ |
| 151 | |
| 152 | String getEraName(int year); |
| 153 | |
| 154 | /** |
| 155 | * Get the name of a calendar |
| 156 | * |
| 157 | * @param code The code representing the calendar as in the XSLT 2.0 spec, e.g. AD for the Gregorian calendar |
| 158 | * @return the name of the calendar, for example "AD" |
| 159 | */ |
| 160 | |
| 161 | String getCalendarName(String code); |
| 162 | |
| 163 | |
| 164 | } |
| 165 |