Debian Codegraph @

net/sf/saxon/expr/number/AbstractNumberer.java

raw symbols SCIP
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.expr.number;
9
10 import net.sf.saxon.lib.Numberer;
11 import net.sf.saxon.regex.EmptyString;
12 import net.sf.saxon.regex.UnicodeString;
13 import net.sf.saxon.tree.util.FastStringBuffer;
14
15 import java.util.Locale;
16
17 /**
18 * Class AbstractNumberer is a base implementation of Numberer that provides language-independent
19 * default numbering
20 * This supports the xsl:number element.
21 * Methods and data are declared as protected, and static is avoided, to allow easy subclassing.
22 *
23 * @author Michael H. Kay
24 */
25
26 public abstract class AbstractNumberer implements Numberer {
27
28 private String country;
29 private String language;
30
31 public static final int UPPER_CASE = 0;
32 public static final int LOWER_CASE = 1;
33 public static final int TITLE_CASE = 2;
34
35
36 /**
37 * Whether this numberer has had its locale defaulted, i.e. it's not using the language requested
38 * This can be used for situations, such as in fn:format-date(), where indication of use of defaults is required.
39 * Override for subclasses where this can happen
40 * @return the locale used, null if it wasn't defaulted
41 */
42 public Locale defaultedLocale() {
43 return null;
44 }
45
46 /**
47 * Set the country used by this numberer (currently used only for names of timezones)
48 */
49
50 public void setCountry(String country) {
51 this.country = country;
52 }
53
54 /**
55 * Set the language used by this numberer. Useful because it can potentially handle variants of English
56 * (and subclasses can handle other languages)
57 * @param language the requested language. Note that "en-x-hyphen" is recognized as a request to hyphenate
58 * numbers in the range 21-99.
59 */
60
61 public void setLanguage(String language) {
62 this.language = language;
63 }
64
65 /**
66 * Get the language used by this numberer
67 */
68
69 public String getLanguage() {
70 return language;
71 }
72
73 /**
74 * Get the country used by this numberer.
75 *
76 * @return the country used by this numberer, or null if no country has been set
77 */
78
79 public String getCountry() {
80 return country;
81 }
82
83 /**
84 * Format a number into a string. This method is provided for backwards compatibility. It merely
85 * calls the other format method after constructing a RegularGroupFormatter. The method is final;
86 * localization subclasses should implement the method
87 * {@link net.sf.saxon.lib.Numberer#format(long, UnicodeString, NumericGroupFormatter, String, String)} rather than this method.
88 *
89 * @param number The number to be formatted
90 * @param picture The format token. This is a single component of the format attribute
91 * of xsl:number, e.g. "1", "01", "i", or "a"
92 * @param groupSize number of digits per group (0 implies no grouping)
93 * @param groupSeparator string to appear between groups of digits
94 * @param letterValue The letter-value specified to xsl:number: "alphabetic" or
95 * "traditional". Can also be an empty string or null.
96 * @param ordinal The value of the ordinal attribute specified to xsl:number
97 * The value "yes" indicates that ordinal numbers should be used; "" or null indicates
98 * that cardinal numbers
99 * @return the formatted number. Note that no errors are reported; if the request
100 * is invalid, the number is formatted as if the string() function were used.
101 */
102
103 public final String format(long number,
104 UnicodeString picture,
105 int groupSize,
106 String groupSeparator,
107 String letterValue,
108 String ordinal) {
109
110 return format(number, picture,
111 new RegularGroupFormatter(groupSize, groupSeparator, EmptyString.THE_INSTANCE),
112 letterValue, ordinal);
113 }
114
115 /**
116 * Format a number into a string
117 *
118 * @param number The number to be formatted
119 * @param picture The format token. This is a single component of the format attribute
120 * of xsl:number, e.g. "1", "01", "i", or "a"
121 * @param numGroupFormatter object contains separators to appear between groups of digits
122 * @param letterValue The letter-value specified to xsl:number: "alphabetic" or
123 * "traditional". Can also be an empty string or null.
124 * @param ordinal The value of the ordinal attribute specified to xsl:number
125 * The value "yes" indicates that ordinal numbers should be used; "" or null indicates
126 * cardinal numbers. A value such as "-er" indicates ordinal with a particular gender.
127 * @return the formatted number. Note that no errors are reported; if the request
128 * is invalid, the number is formatted as if the string() function were used.
129 */
130 public String format(long number,
131 /*@Nullable*/ UnicodeString picture,
132 NumericGroupFormatter numGroupFormatter,
133 String letterValue,
134 String ordinal) {
135
136
137 if (number < 0) {
138 return "" + number;
139 }
140 if (picture == null || picture.uLength() == 0) {
141 return "" + number;
142 }
143
144 int pictureLength = picture.uLength();
145 FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C16);
146 int formchar = picture.uCharAt(0);
147
148 FastStringBuffer fsb = new FastStringBuffer(2);
149
150 switch (formchar) {
151
152 case '0':
153 case '1':
154 sb.append(toRadical(number, westernDigits, pictureLength, numGroupFormatter));
155 if (ordinal != null && !ordinal.isEmpty()) {
156 sb.append(ordinalSuffix(ordinal, number));
157 }
158 break;
159
160 case 'A':
161 if (number == 0) {
162 return "0";
163 }
164 return toAlphaSequence(number, latinUpper);
165
166 case 'a':
167 if (number == 0) {
168 return "0";
169 }
170 return toAlphaSequence(number, latinLower);
171
172 case 'w':
173 case 'W':
174 int wordCase;
175 if (picture.uLength() == 1) {
176 if (formchar == 'W') {
177 wordCase = UPPER_CASE;
178 } else /*if (formchar == 'w')*/ {
179 wordCase = LOWER_CASE;
180 }
181 } else {
182 // includes cases like "ww" or "Wz". The action here is conformant, but it's not clear what's best
183 wordCase = TITLE_CASE;
184 }
185 if (ordinal != null && !ordinal.isEmpty()) {
186 return toOrdinalWords(ordinal, number, wordCase);
187 } else {
188 return toWords(number, wordCase);
189 }
190
191 case 'i':
192 if (number == 0) {
193 return "0";
194 }
195 if (letterValue == null || letterValue.isEmpty() ||
196 letterValue.equals("traditional")) {
197 return toRoman(number);
198 } else {
199 alphaDefault(number, 'i', sb);
200 }
201 break;
202
203 case 'I':
204 if (number == 0) {
205 return "0";
206 }
207 if (letterValue == null || letterValue.isEmpty() ||
208 letterValue.equals("traditional")) {
209 return toRoman(number).toUpperCase();
210 } else {
211 alphaDefault(number, 'I', sb);
212 }
213 break;
214
215 case '\u2460':
216 // circled digits
217 if (number == 0) {
218 return "" + (char) 0x24EA;
219 }
220 if (number > 20 && number <= 35) {
221 return "" + (char) (0x3251 + number - 21);
222 }
223 if (number > 35 && number <= 50) {
224 return "" + (char) (0x32B1 + number - 36);
225 }
226 if (number > 50) {
227 return "" + number;
228 }
229 return "" + (char) (0x2460 + number - 1);
230
231 case '\u2474':
232 // parenthesized digits
233 if (number == 0 || number > 20) {
234 return "" + number;
235 }
236 return "" + (char) (0x2474 + number - 1);
237
238 case '\u2488':
239 // digit full stop
240 if (number == 0) {
241 return "" + (char) 0xD83C + (char) 0xDD00;
242 }
243 if (number > 20) {
244 return "" + number;
245 }
246 return "" + (char) (0x2488 + number - 1);
247
248 case '\u2776':
249 // dingbat negative circled digits
250 if (number == 0) {
251 return "" + (char) 0x24FF;
252 }
253 if (number > 10 && number <= 20) {
254 return "" + (char) (0x24EB + number - 11);
255 }
256 if (number > 20) {
257 return "" + number;
258 }
259 return "" + (char) (0x2776 + number - 1);
260
261 case '\u2780':
262 // double circled sans-serif digits
263 if (number == 0) {
264 return "" + (char) 0xD83C + (char) 0xDD0B;
265 }
266 if (number > 10) {
267 return "" + number;
268 }
269 return "" + (char) (0x2780 + number - 1);
270
271 case '\u24F5':
272 // double circled digits
273 if (number == 0 || number > 10) {
274 return "" + number;
275 }
276 return "" + (char) (0x24F5 + number - 1);
277
278 case '\u278A':
279 // dingbat negative circled sans-serif digits
280 if (number == 0) {
281 return "" + (char) 0xD83C + (char) 0xDD0C;
282 }
283 if (number > 10) {
284 return "" + number;
285 }
286 return "" + (char) (0x278A + number - 1);
287
288 case '\u3220':
289 // parenthesized ideograph
290 if (number == 0 || number > 10) {
291 return "" + number;
292 }
293 return "" + (char) (0x3220 + number - 1);
294
295 case '\u3280':
296 // circled ideograph
297 if (number == 0 || number > 10) {
298 return "" + number;
299 }
300 return "" + (char) (0x3280 + number - 1);
301
302 case 65799: //'\uD800\uDD07': \uD807\uDD27 hex 10107
303 // aegean number
304 if (number == 0 || number > 10) {
305 return "" + number;
306 }
307 fsb.appendWideChar(65799 + (int) number - 1);
308 return fsb.toString();
309
310 case 69216:
311 // rumi digit
312 if (number == 0 || number > 10) {
313 return "" + number;
314 }
315 fsb.appendWideChar(69216 + (int) number - 1);
316 return fsb.toString();
317
318 case 69714:
319 // brahmi digit
320 if (number == 0 || number > 10) {
321 return "" + number;
322 }
323 fsb.appendWideChar(69714 + (int) number - 1);
324 return fsb.toString();
325
326 case 119648:
327 // counting rod unit digit
328 if (number == 0 || number >= 10) {
329 return "" + number;
330 }
331 fsb.appendWideChar(119648 + (int) number - 1);
332 return fsb.toString();
333
334 case 127234:
335 // digit one comma
336 if (number == 0) {
337 fsb.appendWideChar(127233);
338 return fsb.toString();
339 }
340 if (number >= 10) {
341 return "" + number;
342 }
343 fsb.appendWideChar(127234 + (int) number - 1);
344 return fsb.toString();
345
346 case '\u0391':
347 if (number == 0) {
348 return "0";
349 }
350 return toAlphaSequence(number, greekUpper);
351
352 case '\u03b1':
353 if (number == 0) {
354 return "0";
355 }
356 return toAlphaSequence(number, greekLower);
357
358 case '\u0410':
359 if (number == 0) {
360 return "0";
361 }
362 return toAlphaSequence(number, cyrillicUpper);
363
364 case '\u0430':
365 if (number == 0) {
366 return "0";
367 }
368 return toAlphaSequence(number, cyrillicLower);
369
370 case '\u05d0':
371 if (number == 0) {
372 return "0";
373 }
374 return toAlphaSequence(number, hebrew);
375
376 case '\u3042':
377 if (number == 0) {
378 return "0";
379 }
380 return toAlphaSequence(number, hiraganaA);
381
382 case '\u30a2':
383 if (number == 0) {
384 return "0";
385 }
386 return toAlphaSequence(number, katakanaA);
387
388 case '\u3044':
389 if (number == 0) {
390 return "0";
391 }
392 return toAlphaSequence(number, hiraganaI);
393
394 case '\u30a4':
395 if (number == 0) {
396 return "0";
397 }
398 return toAlphaSequence(number, katakanaI);
399
400 case '\u4e00':
401 return toJapanese(number);
402
403 default:
404
405 int digitValue = Alphanumeric.getDigitValue(formchar);
406 if (digitValue >= 0) {
407
408 int zero = formchar - digitValue;
409 int[] digits = new int[10];
410 for (int z = 0; z <= 9; z++) {
411 digits[z] = zero + z;
412 }
413
414 return toRadical(number, digits, pictureLength, numGroupFormatter);
415
416 } else {
417 if (formchar < '\u1100' && Character.isLetter((char) formchar) && number > 0) {
418 alphaDefault(number, (char) formchar, sb);
419 } else {
420 // fallback to western numbering
421 sb.append(toRadical(number, westernDigits, pictureLength, numGroupFormatter));
422 if (ordinal != null && !ordinal.isEmpty()) {
423 sb.append(ordinalSuffix(ordinal, number));
424 }
425 //return toRadical(number, westernDigits, pictureLength, numGroupFormatter);
426 }
427 break;
428
429 }
430 }
431
432 return sb.toString();
433 }
434
435 /**
436 * Construct the ordinal suffix for a number, for example "st", "nd", "rd". The default
437 * (language-neutral) implementation returns a zero-length string
438 *
439 * @param ordinalParam the value of the ordinal attribute (used in non-English
440 * language implementations)
441 * @param number the number being formatted
442 * @return the ordinal suffix to be appended to the formatted number
443 */
444
445 protected String ordinalSuffix(String ordinalParam, long number) {
446 return "";
447 }
448
449 protected static final int[] westernDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
450
451 protected static final String latinUpper =
452 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
453
454 protected static final String latinLower =
455 "abcdefghijklmnopqrstuvwxyz";
456
457 protected static final String greekUpper =
458 "\u0391\u0392\u0393\u0394\u0395\u0396\u0397\u0398\u0399\u039a" +
459 "\u039b\u039c\u039d\u039e\u039f\u03a0\u03a1\u03a2\u03a3\u03a4" +
460 "\u03a5\u03a6\u03a7\u03a8\u03a9";
461
462 protected static final String greekLower =
463 "\u03b1\u03b2\u03b3\u03b4\u03b5\u03b6\u03b7\u03b8\u03b9\u03ba" +
464 "\u03bb\u03bc\u03bd\u03be\u03bf\u03c0\u03c1\u03c2\u03c3\u03c4" +
465 "\u03c5\u03c6\u03c7\u03c8\u03c9";
466
467 // Cyrillic information from Dmitry Kirsanov [[email protected]]
468 // (based on his personal knowledge of Russian texts, not any authoritative source)
469
470 protected static final String cyrillicUpper =
471 "\u0410\u0411\u0412\u0413\u0414\u0415\u0416\u0417\u0418" +
472 "\u041a\u041b\u041c\u041d\u041e\u041f\u0420\u0421\u0421\u0423" +
473 "\u0424\u0425\u0426\u0427\u0428\u0429\u042b\u042d\u042e\u042f";
474
475 protected static final String cyrillicLower =
476 "\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438" +
477 "\u043a\u043b\u043c\u043d\u043e\u043f\u0440\u0441\u0441\u0443" +
478 "\u0444\u0445\u0446\u0447\u0448\u0449\u044b\u044d\u044e\u044f";
479
480 protected static final String hebrew =
481 "\u05d0\u05d1\u05d2\u05d3\u05d4\u05d5\u05d6\u05d7\u05d8\u05d9\u05db\u05dc" +
482 "\u05de\u05e0\u05e1\u05e2\u05e4\u05e6\u05e7\u05e8\u05e9\u05ea";
483
484
485 // The following Japanese sequences were supplied by
486 // MURAKAMI Shinyu [[email protected]]
487
488 protected static final String hiraganaA =
489 "\u3042\u3044\u3046\u3048\u304a\u304b\u304d\u304f\u3051\u3053" +
490 "\u3055\u3057\u3059\u305b\u305d\u305f\u3061\u3064\u3066\u3068" +
491 "\u306a\u306b\u306c\u306d\u306e\u306f\u3072\u3075\u3078\u307b" +
492 "\u307e\u307f\u3080\u3081\u3082\u3084\u3086\u3088\u3089\u308a" +
493 "\u308b\u308c\u308d\u308f\u3092\u3093";
494
495 protected static final String katakanaA =
496
497 "\u30a2\u30a4\u30a6\u30a8\u30aa\u30ab\u30ad\u30af\u30b1\u30b3" +
498 "\u30b5\u30b7\u30b9\u30bb\u30bd\u30bf\u30c1\u30c4\u30c6\u30c8" +
499 "\u30ca\u30cb\u30cc\u30cd\u30ce\u30cf\u30d2\u30d5\u30d8\u30db" +
500 "\u30de\u30df\u30e0\u30e1\u30e2\u30e4\u30e6\u30e8\u30e9\u30ea" +
501 "\u30eb\u30ec\u30ed\u30ef\u30f2\u30f3";
502
503 protected static final String hiraganaI =
504
505 "\u3044\u308d\u306f\u306b\u307b\u3078\u3068\u3061\u308a\u306c" +
506 "\u308b\u3092\u308f\u304b\u3088\u305f\u308c\u305d\u3064\u306d" +
507 "\u306a\u3089\u3080\u3046\u3090\u306e\u304a\u304f\u3084\u307e" +
508 "\u3051\u3075\u3053\u3048\u3066\u3042\u3055\u304d\u3086\u3081" +
509 "\u307f\u3057\u3091\u3072\u3082\u305b\u3059";
510
511 protected static final String katakanaI =
512
513 "\u30a4\u30ed\u30cf\u30cb\u30db\u30d8\u30c8\u30c1\u30ea\u30cc" +
514 "\u30eb\u30f2\u30ef\u30ab\u30e8\u30bf\u30ec\u30bd\u30c4\u30cd" +
515 "\u30ca\u30e9\u30e0\u30a6\u30f0\u30ce\u30aa\u30af\u30e4\u30de" +
516 "\u30b1\u30d5\u30b3\u30a8\u30c6\u30a2\u30b5\u30ad\u30e6\u30e1" +
517 "\u30df\u30b7\u30f1\u30d2\u30e2\u30bb\u30b9";
518
519
520 /**
521 * Default processing with an alphabetic format token: use the contiguous
522 * range of Unicode letters starting with that token.
523 *
524 * @param number the number to be formatted
525 * @param formchar the format character, for example 'A' for the numbering sequence A,B,C
526 * @param sb buffer to hold the result of the formatting
527 */
528
529 protected void alphaDefault(long number, char formchar, FastStringBuffer sb) {
530 int min = (int) formchar;
531 int max = (int) formchar;
532 // use the contiguous range of letters starting with the specified one
533 while (Character.isLetterOrDigit((char) (max + 1))) {
534 max++;
535 }
536 sb.append(toAlpha(number, min, max));
537 }
538
539 /**
540 * Format the number as an alphabetic label using the alphabet consisting
541 * of consecutive Unicode characters from min to max
542 *
543 * @param number the number to be formatted
544 * @param min the start of the Unicode codepoint range
545 * @param max the end of the Unicode codepoint range
546 * @return the formatted number
547 */
548
549 protected String toAlpha(long number, int min, int max) {
550 if (number <= 0) {
551 return "" + number;
552 }
553 int range = max - min + 1;
554 char last = (char) (((number - 1) % range) + min);
555 if (number > range) {
556 return toAlpha((number - 1) / range, min, max) + last;
557 } else {
558 return "" + last;
559 }
560 }
561
562 /**
563 * Convert the number into an alphabetic label using a given alphabet.
564 * For example, if the alphabet is "xyz" the sequence is x, y, z, xx, xy, xz, ....
565 *
566 * @param number the number to be formatted
567 * @param alphabet a string containing the characters to be used, for example "abc...xyz"
568 * @return the formatted number
569 */
570
571 protected String toAlphaSequence(long number, String alphabet) {
572 if (number <= 0) {
573 return "" + number;
574 }
575 int range = alphabet.length();
576 char last = alphabet.charAt((int) ((number - 1) % range));
577 if (number > range) {
578 return toAlphaSequence((number - 1) / range, alphabet) + last;
579 } else {
580 return "" + last;
581 }
582 }
583
584 /**
585 * Convert the number into a decimal or other representation using the given set of
586 * digits.
587 * For example, if the digits are "01" the sequence is 1, 10, 11, 100, 101, 110, 111, ...
588 * More commonly, the digits will be "0123456789", giving the usual decimal numbering.
589 *
590 * @param number the number to be formatted
591 * @param digits the codepoints to be used for the digits
592 * @param pictureLength the length of the picture that is significant: for example "3" if the
593 * picture is "001"
594 * @param numGroupFormatter an object that encapsulates the rules for inserting grouping separators
595 * @return the formatted number
596 */
597
598 private String toRadical(long number, int[] digits, int pictureLength,
599 NumericGroupFormatter numGroupFormatter) {
600
601 FastStringBuffer temp = convertDigitSystem(number, digits, pictureLength);
602
603 if (numGroupFormatter == null) {
604 return temp.toString();
605 }
606
607 return numGroupFormatter.format(temp);
608 }
609
610 /**
611 * Convert a number to use a given set of digits, to a required length.
612 * For example, if the digits are "01" the sequence is 1, 10, 11, 100, 101, 110, 111, ...
613 * More commonly, the digits will be "0123456789", giving the usual decimal numbering.
614 *
615 * @param number the number to be formatted
616 * @param digits the codepoints to be used for the digits
617 * @param requiredLength the length of the picture that is significant: for example "3" if the
618 * picture is "001"
619 * @return the converted number
620 */
621
622 public static FastStringBuffer convertDigitSystem(long number, int[] digits, int requiredLength) {
623 FastStringBuffer temp = new FastStringBuffer(FastStringBuffer.C16);
624 int base = digits.length;
625 FastStringBuffer s = new FastStringBuffer(FastStringBuffer.C16);
626 long n = number;
627 int count = 0;
628 while (n > 0) {
629 int digit = digits[(int) (n % base)];
630 s.prependWideChar(digit);
631 count++;
632 n = n / base;
633 }
634
635 for (int i = 0; i < (requiredLength - count); i++) {
636 temp.appendWideChar(digits[0]);
637 }
638 temp.append(s);
639 return temp;
640 }
641
642 /**
643 * Generate a Roman numeral (in lower case)
644 *
645 * @param n the number to be formatted
646 * @return the Roman numeral representation of the number in lower case
647 */
648
649 public static String toRoman(long n) {
650 if (n <= 0 || n > 9999) {
651 return "" + n;
652 }
653 return romanThousands[(int) n / 1000] +
654 romanHundreds[((int) n / 100) % 10] +
655 romanTens[((int) n / 10) % 10] +
656 romanUnits[(int) n % 10];
657 }
658
659 // Roman numbers beyond 4000 use overlining and other conventions which we won't
660 // attempt to reproduce. We'll go high enough to handle present-day Gregorian years.
661
662 private static String[] romanThousands =
663 {"", "m", "mm", "mmm", "mmmm", "mmmmm", "mmmmmm", "mmmmmmm", "mmmmmmmm", "mmmmmmmmm"};
664 private static String[] romanHundreds =
665 {"", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"};
666 private static String[] romanTens =
667 {"", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"};
668 private static String[] romanUnits =
669 {"", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"};
670
671
672 /**
673 * Format the number in Japanese.
674 *
675 * @param number the number to be formatted: formatted in Western decimal style unless in the range 1 to 9999
676 * @return the Japanese Kanji representation of the number if in the range 1-9999
677 */
678
679 public String toJapanese(long number) {
680 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C16);
681 if (number == 0) {
682 fsb.appendWideChar(0x3007);
683 } else if (number <= 9999) {
684 toJapanese((int) number, fsb, false);
685 } else {
686 fsb.append("" + number);
687 }
688 return fsb.toString();
689 }
690
691 /**
692 * Format the number in Japanese.
693 *
694 * @param nr the number to be formatted: must be in the range 1 to 9999 (or 0 on a recursive call)
695 * @param fsb buffer to receive the formatted number as the Japanese Kanji representation of the number in lower case
696 * @param isInitial true except on a recursive call
697 */
698
699 private static void toJapanese(int nr, FastStringBuffer fsb, boolean isInitial) {
700 if (nr == 0) {
701 // no action (not used at top level)
702 } else if (nr <= 9) {
703 if (!(nr == 1 && isInitial)) {
704 fsb.appendWideChar(kanjiDigits[nr]);
705 }
706 } else if (nr == 10) {
707 fsb.appendWideChar(0x5341);
708 } else if (nr <= 99) {
709 toJapanese(nr / 10, fsb, true);
710 fsb.appendWideChar(0x5341);
711 toJapanese(nr % 10, fsb, false);
712 } else if (nr <= 999) {
713 toJapanese(nr / 100, fsb, true);
714 fsb.appendWideChar(0x767e);
715 toJapanese(nr % 100, fsb, false);
716 } else if (nr <= 9999) {
717 toJapanese(nr / 1000, fsb, true);
718 fsb.appendWideChar(0x5343);
719 toJapanese(nr % 1000, fsb, false);
720 }
721
722 }
723
724 private static final int[] kanjiDigits =
725 {0x3007, 0x4e00, 0x4e8c, 0x4e09, 0x56db, 0x4e94, 0x516d, 0x4e03, 0x516b, 0x4e5d};
726
727 /**
728 * Show the number as words in title case. (We choose title case because
729 * the result can then be converted algorithmically to lower case or upper case).
730 *
731 * @param number the number to be formatted
732 * @return the number formatted as English words
733 */
734
735 public abstract String toWords(long number);
736
737 /**
738 * Format a number as English words with specified case options
739 *
740 * @param number the number to be formatted
741 * @param wordCase the required case for example {@link #UPPER_CASE},
742 * {@link #LOWER_CASE}, {@link #TITLE_CASE}
743 * @return the formatted number
744 */
745
746 public String toWords(long number, int wordCase) {
747 String s;
748 if (number == 0) {
749 s = "Zero";
750 } else {
751 s = toWords(number);
752 }
753 switch (wordCase) {
754 case UPPER_CASE:
755 return s.toUpperCase();
756 case LOWER_CASE:
757 return s.toLowerCase();
758 default:
759 return s;
760 }
761 }
762
763 /**
764 * Show an ordinal number as English words in a requested case (for example, Twentyfirst)
765 *
766 * @param ordinalParam the value of the "ordinal" attribute as supplied by the user
767 * @param number the number to be formatted
768 * @param wordCase the required case for example {@link #UPPER_CASE},
769 * {@link #LOWER_CASE}, {@link #TITLE_CASE}
770 * @return the formatted number
771 */
772
773 public abstract String toOrdinalWords(String ordinalParam, long number, int wordCase);
774
775 /**
776 * Get a month name or abbreviation
777 *
778 * @param month The month number (1=January, 12=December)
779 * @param minWidth The minimum number of characters
780 * @param maxWidth The maximum number of characters
781 */
782
783 public abstract String monthName(int month, int minWidth, int maxWidth);
784
785 /**
786 * Get a day name or abbreviation
787 *
788 * @param day The day of the week (1=Monday, 7=Sunday)
789 * @param minWidth The minimum number of characters
790 * @param maxWidth The maximum number of characters
791 */
792
793 public abstract String dayName(int day, int minWidth, int maxWidth);
794
795 /**
796 * Get an am/pm indicator. Default implementation works for English, on the basis that some
797 * other languages might like to copy this (most non-English countries don't actually use the
798 * 12-hour clock, so it's irrelevant).
799 *
800 * @param minutes the minutes within the day
801 * @param minWidth minimum width of output
802 * @param maxWidth maximum width of output
803 * @return the AM or PM indicator
804 */
805
806 public String halfDayName(int minutes, int minWidth, int maxWidth) {
807 String s;
808 if (minutes == 0 && maxWidth >= 8 && "gb".equals(country)) {
809 s = "Midnight";
810 } else if (minutes < 12 * 60) {
811 switch (maxWidth) {
812 case 1:
813 s = "A";
814 break;
815 case 2:
816 case 3:
817 s = "Am";
818 break;
819 default:
820 s = "A.M.";
821 }
822 } else if (minutes == 12 * 60 && maxWidth >= 8 && "gb".equals(country)) {
823 s = "Noon";
824 } else {
825 switch (maxWidth) {
826 case 1:
827 s = "P";
828 break;
829 case 2:
830 case 3:
831 s = "Pm";
832 break;
833 default:
834 s = "P.M.";
835 }
836 }
837 return s;
838 }
839
840 /**
841 * Get an ordinal suffix for a particular component of a date/time.
842 *
843 * @param component the component specifier from a format-dateTime picture, for
844 * example "M" for the month or "D" for the day.
845 * @return a string that is acceptable in the ordinal attribute of xsl:number
846 * to achieve the required ordinal representation. For example, "-e" for the day component
847 * in German, to have the day represented as "dritte August".
848 */
849
850 public String getOrdinalSuffixForDateTime(String component) {
851 return "yes";
852 }
853
854 /**
855 * Get the name for an era (e.g. "BC" or "AD")
856 *
857 * @param year the proleptic gregorian year, using "0" for the year before 1AD
858 */
859
860 public String getEraName(int year) {
861 return year > 0 ? "AD" : "BC";
862 }
863
864 /**
865 * Get the name of a calendar
866 *
867 * @param code The code representing the calendar as in the XSLT 2.0 spec, e.g. AD for the Gregorian calendar
868 */
869
870 public String getCalendarName(String code) {
871 if (code.equals("AD")) {
872 return "Gregorian";
873 } else {
874 return code;
875 }
876 }
877
878
879 }
880