]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/text/RuleBasedNumberFormat.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / text / RuleBasedNumberFormat.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 \r
8 package com.ibm.icu.text;\r
9 \r
10 import java.math.BigInteger;\r
11 import java.text.FieldPosition;\r
12 import java.text.ParsePosition;\r
13 import java.util.Arrays;\r
14 import java.util.HashMap;\r
15 import java.util.Locale;\r
16 import java.util.Map;\r
17 import java.util.MissingResourceException;\r
18 import java.util.Set;\r
19 \r
20 import com.ibm.icu.impl.ICUDebug;\r
21 import com.ibm.icu.impl.ICUResourceBundle;\r
22 import com.ibm.icu.impl.UCharacterProperty;\r
23 import com.ibm.icu.util.ULocale;\r
24 import com.ibm.icu.util.UResourceBundle;\r
25 import com.ibm.icu.util.UResourceBundleIterator;\r
26 \r
27 \r
28 /**\r
29  * <p>A class that formats numbers according to a set of rules. This number formatter is\r
30  * typically used for spelling out numeric values in words (e.g., 25,3476 as\r
31  * &quot;twenty-five thousand three hundred seventy-six&quot; or &quot;vingt-cinq mille trois\r
32  * cents soixante-seize&quot; or\r
33  * &quot;funfundzwanzigtausenddreihundertsechsundsiebzig&quot;), but can also be used for\r
34  * other complicated formatting tasks, such as formatting a number of seconds as hours,\r
35  * minutes and seconds (e.g., 3,730 as &quot;1:02:10&quot;).</p>\r
36  *\r
37  * <p>The resources contain three predefined formatters for each locale: spellout, which\r
38  * spells out a value in words (123 is &quot;one hundred twenty-three&quot;); ordinal, which\r
39  * appends an ordinal suffix to the end of a numeral (123 is &quot;123rd&quot;); and\r
40  * duration, which shows a duration in seconds as hours, minutes, and seconds (123 is\r
41  * &quot;2:03&quot;).&nbsp; The client can also define more specialized <tt>RuleBasedNumberFormat</tt>s\r
42  * by supplying programmer-defined rule sets.</p>\r
43  *\r
44  * <p>The behavior of a <tt>RuleBasedNumberFormat</tt> is specified by a textual description\r
45  * that is either passed to the constructor as a <tt>String</tt> or loaded from a resource\r
46  * bundle. In its simplest form, the description consists of a semicolon-delimited list of <em>rules.</em>\r
47  * Each rule has a string of output text and a value or range of values it is applicable to.\r
48  * In a typical spellout rule set, the first twenty rules are the words for the numbers from\r
49  * 0 to 19:</p>\r
50  *\r
51  * <pre>zero; one; two; three; four; five; six; seven; eight; nine;\r
52  * ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen; seventeen; eighteen; nineteen;</pre>\r
53  *\r
54  * <p>For larger numbers, we can use the preceding set of rules to format the ones place, and\r
55  * we only have to supply the words for the multiples of 10:</p>\r
56  *\r
57  * <pre>20: twenty[-&gt;&gt;];\r
58  * 30: thirty{-&gt;&gt;];\r
59  * 40: forty[-&gt;&gt;];\r
60  * 50: fifty[-&gt;&gt;];\r
61  * 60: sixty[-&gt;&gt;];\r
62  * 70: seventy[-&gt;&gt;];\r
63  * 80: eighty[-&gt;&gt;];\r
64  * 90: ninety[-&gt;&gt;];</pre>\r
65  *\r
66  * <p>In these rules, the <em>base value</em> is spelled out explicitly and set off from the\r
67  * rule's output text with a colon. The rules are in a sorted list, and a rule is applicable\r
68  * to all numbers from its own base value to one less than the next rule's base value. The\r
69  * &quot;&gt;&gt;&quot; token is called a <em>substitution</em> and tells the fomatter to\r
70  * isolate the number's ones digit, format it using this same set of rules, and place the\r
71  * result at the position of the &quot;&gt;&gt;&quot; token. Text in brackets is omitted if\r
72  * the number being formatted is an even multiple of 10 (the hyphen is a literal hyphen; 24\r
73  * is &quot;twenty-four,&quot; not &quot;twenty four&quot;).</p>\r
74  *\r
75  * <p>For even larger numbers, we can actually look up several parts of the number in the\r
76  * list:</p>\r
77  *\r
78  * <pre>100: &lt;&lt; hundred[ &gt;&gt;];</pre>\r
79  *\r
80  * <p>The &quot;&lt;&lt;&quot; represents a new kind of substitution. The &lt;&lt; isolates\r
81  * the hundreds digit (and any digits to its left), formats it using this same rule set, and\r
82  * places the result where the &quot;&lt;&lt;&quot; was. Notice also that the meaning of\r
83  * &gt;&gt; has changed: it now refers to both the tens and the ones digits. The meaning of\r
84  * both substitutions depends on the rule's base value. The base value determines the rule's <em>divisor,</em>\r
85  * which is the highest power of 10 that is less than or equal to the base value (the user\r
86  * can change this). To fill in the substitutions, the formatter divides the number being\r
87  * formatted by the divisor. The integral quotient is used to fill in the &lt;&lt;\r
88  * substitution, and the remainder is used to fill in the &gt;&gt; substitution. The meaning\r
89  * of the brackets changes similarly: text in brackets is omitted if the value being\r
90  * formatted is an even multiple of the rule's divisor. The rules are applied recursively, so\r
91  * if a substitution is filled in with text that includes another substitution, that\r
92  * substitution is also filled in.</p>\r
93  *\r
94  * <p>This rule covers values up to 999, at which point we add another rule:</p>\r
95  *\r
96  * <pre>1000: &lt;&lt; thousand[ &gt;&gt;];</pre>\r
97  *\r
98  * <p>Again, the meanings of the brackets and substitution tokens shift because the rule's\r
99  * base value is a higher power of 10, changing the rule's divisor. This rule can actually be\r
100  * used all the way up to 999,999. This allows us to finish out the rules as follows:</p>\r
101  *\r
102  * <pre>1,000,000: &lt;&lt; million[ &gt;&gt;];\r
103  * 1,000,000,000: &lt;&lt; billion[ &gt;&gt;];\r
104  * 1,000,000,000,000: &lt;&lt; trillion[ &gt;&gt;];\r
105  * 1,000,000,000,000,000: OUT OF RANGE!;</pre>\r
106  *\r
107  * <p>Commas, periods, and spaces can be used in the base values to improve legibility and\r
108  * are ignored by the rule parser. The last rule in the list is customarily treated as an\r
109  * &quot;overflow rule,&quot; applying to everything from its base value on up, and often (as\r
110  * in this example) being used to print out an error message or default representation.\r
111  * Notice also that the size of the major groupings in large numbers is controlled by the\r
112  * spacing of the rules: because in English we group numbers by thousand, the higher rules\r
113  * are separated from each other by a factor of 1,000.</p>\r
114  *\r
115  * <p>To see how these rules actually work in practice, consider the following example:\r
116  * Formatting 25,430 with this rule set would work like this:</p>\r
117  *\r
118  * <table border="0" width="630">\r
119  *   <tr>\r
120  *     <td width="21"></td>\r
121  *     <td width="257" valign="top"><strong>&lt;&lt; thousand &gt;&gt;</strong></td>\r
122  *     <td width="340" valign="top">[the rule whose base value is 1,000 is applicable to 25,340]</td>\r
123  *   </tr>\r
124  *   <tr>\r
125  *     <td width="21"></td>\r
126  *     <td width="257" valign="top"><strong>twenty-&gt;&gt;</strong> thousand &gt;&gt;</td>\r
127  *     <td width="340" valign="top">[25,340 over 1,000 is 25. The rule for 20 applies.]</td>\r
128  *   </tr>\r
129  *   <tr>\r
130  *     <td width="21"></td>\r
131  *     <td width="257" valign="top">twenty-<strong>five</strong> thousand &gt;&gt;</td>\r
132  *     <td width="340" valign="top">[25 mod 10 is 5. The rule for 5 is &quot;five.&quot;</td>\r
133  *   </tr>\r
134  *   <tr>\r
135  *     <td width="21"></td>\r
136  *     <td width="257" valign="top">twenty-five thousand <strong>&lt;&lt; hundred &gt;&gt;</strong></td>\r
137  *     <td width="340" valign="top">[25,340 mod 1,000 is 340. The rule for 100 applies.]</td>\r
138  *   </tr>\r
139  *   <tr>\r
140  *     <td width="21"></td>\r
141  *     <td width="257" valign="top">twenty-five thousand <strong>three</strong> hundred &gt;&gt;</td>\r
142  *     <td width="340" valign="top">[340 over 100 is 3. The rule for 3 is &quot;three.&quot;]</td>\r
143  *   </tr>\r
144  *   <tr>\r
145  *     <td width="21"></td>\r
146  *     <td width="257" valign="top">twenty-five thousand three hundred <strong>forty</strong></td>\r
147  *     <td width="340" valign="top">[340 mod 100 is 40. The rule for 40 applies. Since 40 divides\r
148  *     evenly by 10, the hyphen and substitution in the brackets are omitted.]</td>\r
149  *   </tr>\r
150  * </table>\r
151  *\r
152  * <p>The above syntax suffices only to format positive integers. To format negative numbers,\r
153  * we add a special rule:</p>\r
154  *\r
155  * <pre>-x: minus &gt;&gt;;</pre>\r
156  *\r
157  * <p>This is called a <em>negative-number rule,</em> and is identified by &quot;-x&quot;\r
158  * where the base value would be. This rule is used to format all negative numbers. the\r
159  * &gt;&gt; token here means &quot;find the number's absolute value, format it with these\r
160  * rules, and put the result here.&quot;</p>\r
161  *\r
162  * <p>We also add a special rule called a <em>fraction rule </em>for numbers with fractional\r
163  * parts:</p>\r
164  *\r
165  * <pre>x.x: &lt;&lt; point &gt;&gt;;</pre>\r
166  *\r
167  * <p>This rule is used for all positive non-integers (negative non-integers pass through the\r
168  * negative-number rule first and then through this rule). Here, the &lt;&lt; token refers to\r
169  * the number's integral part, and the &gt;&gt; to the number's fractional part. The\r
170  * fractional part is formatted as a series of single-digit numbers (e.g., 123.456 would be\r
171  * formatted as &quot;one hundred twenty-three point four five six&quot;).</p>\r
172  *\r
173  * <p>To see how this rule syntax is applied to various languages, examine the resource data.</p>\r
174  *\r
175  * <p>There is actually much more flexibility built into the rule language than the\r
176  * description above shows. A formatter may own multiple rule sets, which can be selected by\r
177  * the caller, and which can use each other to fill in their substitutions. Substitutions can\r
178  * also be filled in with digits, using a DecimalFormat object. There is syntax that can be\r
179  * used to alter a rule's divisor in various ways. And there is provision for much more\r
180  * flexible fraction handling. A complete description of the rule syntax follows:</p>\r
181  *\r
182  * <hr>\r
183  *\r
184  * <p>The description of a <tt>RuleBasedNumberFormat</tt>'s behavior consists of one or more <em>rule\r
185  * sets.</em> Each rule set consists of a name, a colon, and a list of <em>rules.</em> A rule\r
186  * set name must begin with a % sign. Rule sets with names that begin with a single % sign\r
187  * are <em>public:</em> the caller can specify that they be used to format and parse numbers.\r
188  * Rule sets with names that begin with %% are <em>private:</em> they exist only for the use\r
189  * of other rule sets. If a formatter only has one rule set, the name may be omitted.</p>\r
190  *\r
191  * <p>The user can also specify a special &quot;rule set&quot; named <tt>%%lenient-parse</tt>.\r
192  * The body of <tt>%%lenient-parse</tt> isn't a set of number-formatting rules, but a <tt>RuleBasedCollator</tt>\r
193  * description which is used to define equivalences for lenient parsing. For more information\r
194  * on the syntax, see <tt>RuleBasedCollator</tt>. For more information on lenient parsing,\r
195  * see <tt>setLenientParse()</tt>. <em>Note:</em> symbols that have syntactic meaning\r
196  * in collation rules, such as '&amp;', have no particular meaning when appearing outside\r
197  * of the <tt>lenient-parse</tt> rule set.</p>\r
198  *\r
199  * <p>The body of a rule set consists of an ordered, semicolon-delimited list of <em>rules.</em>\r
200  * Internally, every rule has a base value, a divisor, rule text, and zero, one, or two <em>substitutions.</em>\r
201  * These parameters are controlled by the description syntax, which consists of a <em>rule\r
202  * descriptor,</em> a colon, and a <em>rule body.</em></p>\r
203  *\r
204  * <p>A rule descriptor can take one of the following forms (text in <em>italics</em> is the\r
205  * name of a token):</p>\r
206  *\r
207  * <table border="0" width="100%">\r
208  *   <tr>\r
209  *     <td width="5%" valign="top"></td>\r
210  *     <td width="8%" valign="top"><em>bv</em>:</td>\r
211  *     <td valign="top"><em>bv</em> specifies the rule's base value. <em>bv</em> is a decimal\r
212  *     number expressed using ASCII digits. <em>bv</em> may contain spaces, period, and commas,\r
213  *     which are irgnored. The rule's divisor is the highest power of 10 less than or equal to\r
214  *     the base value.</td>\r
215  *   </tr>\r
216  *   <tr>\r
217  *     <td width="5%" valign="top"></td>\r
218  *     <td width="8%" valign="top"><em>bv</em>/<em>rad</em>:</td>\r
219  *     <td valign="top"><em>bv</em> specifies the rule's base value. The rule's divisor is the\r
220  *     highest power of <em>rad</em> less than or equal to the base value.</td>\r
221  *   </tr>\r
222  *   <tr>\r
223  *     <td width="5%" valign="top"></td>\r
224  *     <td width="8%" valign="top"><em>bv</em>&gt;:</td>\r
225  *     <td valign="top"><em>bv</em> specifies the rule's base value. To calculate the divisor,\r
226  *     let the radix be 10, and the exponent be the highest exponent of the radix that yields a\r
227  *     result less than or equal to the base value. Every &gt; character after the base value\r
228  *     decreases the exponent by 1. If the exponent is positive or 0, the divisor is the radix\r
229  *     raised to the power of the exponent; otherwise, the divisor is 1.</td>\r
230  *   </tr>\r
231  *   <tr>\r
232  *     <td width="5%" valign="top"></td>\r
233  *     <td width="8%" valign="top"><em>bv</em>/<em>rad</em>&gt;:</td>\r
234  *     <td valign="top"><em>bv</em> specifies the rule's base value. To calculate the divisor,\r
235  *     let the radix be <em>rad</em>, and the exponent be the highest exponent of the radix that\r
236  *     yields a result less than or equal to the base value. Every &gt; character after the radix\r
237  *     decreases the exponent by 1. If the exponent is positive or 0, the divisor is the radix\r
238  *     raised to the power of the exponent; otherwise, the divisor is 1.</td>\r
239  *   </tr>\r
240  *   <tr>\r
241  *     <td width="5%" valign="top"></td>\r
242  *     <td width="8%" valign="top">-x:</td>\r
243  *     <td valign="top">The rule is a negative-number rule.</td>\r
244  *   </tr>\r
245  *   <tr>\r
246  *     <td width="5%" valign="top"></td>\r
247  *     <td width="8%" valign="top">x.x:</td>\r
248  *     <td valign="top">The rule is an <em>improper fraction rule.</em></td>\r
249  *   </tr>\r
250  *   <tr>\r
251  *     <td width="5%" valign="top"></td>\r
252  *     <td width="8%" valign="top">0.x:</td>\r
253  *     <td valign="top">The rule is a <em>proper fraction rule.</em></td>\r
254  *   </tr>\r
255  *   <tr>\r
256  *     <td width="5%" valign="top"></td>\r
257  *     <td width="8%" valign="top">x.0:</td>\r
258  *     <td valign="top">The rule is a <em>master rule.</em></td>\r
259  *   </tr>\r
260  *   <tr>\r
261  *     <td width="5%" valign="top"></td>\r
262  *     <td width="8%" valign="top"><em>nothing</em></td>\r
263  *     <td valign="top">If the rule's rule descriptor is left out, the base value is one plus the\r
264  *     preceding rule's base value (or zero if this is the first rule in the list) in a normal\r
265  *     rule set.&nbsp; In a fraction rule set, the base value is the same as the preceding rule's\r
266  *     base value.</td>\r
267  *   </tr>\r
268  * </table>\r
269  *\r
270  * <p>A rule set may be either a regular rule set or a <em>fraction rule set,</em> depending\r
271  * on whether it is used to format a number's integral part (or the whole number) or a\r
272  * number's fractional part. Using a rule set to format a rule's fractional part makes it a\r
273  * fraction rule set.</p>\r
274  *\r
275  * <p>Which rule is used to format a number is defined according to one of the following\r
276  * algorithms: If the rule set is a regular rule set, do the following:\r
277  *\r
278  * <ul>\r
279  *   <li>If the rule set includes a master rule (and the number was passed in as a <tt>double</tt>),\r
280  *     use the master rule.&nbsp; (If the number being formatted was passed in as a <tt>long</tt>,\r
281  *     the master rule is ignored.)</li>\r
282  *   <li>If the number is negative, use the negative-number rule.</li>\r
283  *   <li>If the number has a fractional part and is greater than 1, use the improper fraction\r
284  *     rule.</li>\r
285  *   <li>If the number has a fractional part and is between 0 and 1, use the proper fraction\r
286  *     rule.</li>\r
287  *   <li>Binary-search the rule list for the rule with the highest base value less than or equal\r
288  *     to the number. If that rule has two substitutions, its base value is not an even multiple\r
289  *     of its divisor, and the number <em>is</em> an even multiple of the rule's divisor, use the\r
290  *     rule that precedes it in the rule list. Otherwise, use the rule itself.</li>\r
291  * </ul>\r
292  *\r
293  * <p>If the rule set is a fraction rule set, do the following:\r
294  *\r
295  * <ul>\r
296  *   <li>Ignore negative-number and fraction rules.</li>\r
297  *   <li>For each rule in the list, multiply the number being formatted (which will always be\r
298  *     between 0 and 1) by the rule's base value. Keep track of the distance between the result\r
299  *     the nearest integer.</li>\r
300  *   <li>Use the rule that produced the result closest to zero in the above calculation. In the\r
301  *     event of a tie or a direct hit, use the first matching rule encountered. (The idea here is\r
302  *     to try each rule's base value as a possible denominator of a fraction. Whichever\r
303  *     denominator produces the fraction closest in value to the number being formatted wins.) If\r
304  *     the rule following the matching rule has the same base value, use it if the numerator of\r
305  *     the fraction is anything other than 1; if the numerator is 1, use the original matching\r
306  *     rule. (This is to allow singular and plural forms of the rule text without a lot of extra\r
307  *     hassle.)</li>\r
308  * </ul>\r
309  *\r
310  * <p>A rule's body consists of a string of characters terminated by a semicolon. The rule\r
311  * may include zero, one, or two <em>substitution tokens,</em> and a range of text in\r
312  * brackets. The brackets denote optional text (and may also include one or both\r
313  * substitutions). The exact meanings of the substitution tokens, and under what conditions\r
314  * optional text is omitted, depend on the syntax of the substitution token and the context.\r
315  * The rest of the text in a rule body is literal text that is output when the rule matches\r
316  * the number being formatted.</p>\r
317  *\r
318  * <p>A substitution token begins and ends with a <em>token character.</em> The token\r
319  * character and the context together specify a mathematical operation to be performed on the\r
320  * number being formatted. An optional <em>substitution descriptor </em>specifies how the\r
321  * value resulting from that operation is used to fill in the substitution. The position of\r
322  * the substitution token in the rule body specifies the location of the resultant text in\r
323  * the original rule text.</p>\r
324  *\r
325  * <p>The meanings of the substitution token characters are as follows:</p>\r
326  *\r
327  * <table border="0" width="100%">\r
328  *   <tr>\r
329  *     <td width="37"></td>\r
330  *     <td width="23">&gt;&gt;</td>\r
331  *     <td width="165" valign="top">in normal rule</td>\r
332  *     <td>Divide the number by the rule's divisor and format the remainder</td>\r
333  *   </tr>\r
334  *   <tr>\r
335  *     <td width="37"></td>\r
336  *     <td width="23"></td>\r
337  *     <td width="165" valign="top">in negative-number rule</td>\r
338  *     <td>Find the absolute value of the number and format the result</td>\r
339  *   </tr>\r
340  *   <tr>\r
341  *     <td width="37"></td>\r
342  *     <td width="23"></td>\r
343  *     <td width="165" valign="top">in fraction or master rule</td>\r
344  *     <td>Isolate the number's fractional part and format it.</td>\r
345  *   </tr>\r
346  *   <tr>\r
347  *     <td width="37"></td>\r
348  *     <td width="23"></td>\r
349  *     <td width="165" valign="top">in rule in fraction rule set</td>\r
350  *     <td>Not allowed.</td>\r
351  *   </tr>\r
352  *   <tr>\r
353  *     <td width="37"></td>\r
354  *     <td width="23">&gt;&gt;&gt;</td>\r
355  *     <td width="165" valign="top">in normal rule</td>\r
356  *     <td>Divide the number by the rule's divisor and format the remainder,\r
357  *       but bypass the normal rule-selection process and just use the\r
358  *       rule that precedes this one in this rule list.</td>\r
359  *   </tr>\r
360  *   <tr>\r
361  *     <td width="37"></td>\r
362  *     <td width="23"></td>\r
363  *     <td width="165" valign="top">in all other rules</td>\r
364  *     <td>Not allowed.</td>\r
365  *   </tr>\r
366  *   <tr>\r
367  *     <td width="37"></td>\r
368  *     <td width="23">&lt;&lt;</td>\r
369  *     <td width="165" valign="top">in normal rule</td>\r
370  *     <td>Divide the number by the rule's divisor and format the quotient</td>\r
371  *   </tr>\r
372  *   <tr>\r
373  *     <td width="37"></td>\r
374  *     <td width="23"></td>\r
375  *     <td width="165" valign="top">in negative-number rule</td>\r
376  *     <td>Not allowed.</td>\r
377  *   </tr>\r
378  *   <tr>\r
379  *     <td width="37"></td>\r
380  *     <td width="23"></td>\r
381  *     <td width="165" valign="top">in fraction or master rule</td>\r
382  *     <td>Isolate the number's integral part and format it.</td>\r
383  *   </tr>\r
384  *   <tr>\r
385  *     <td width="37"></td>\r
386  *     <td width="23"></td>\r
387  *     <td width="165" valign="top">in rule in fraction rule set</td>\r
388  *     <td>Multiply the number by the rule's base value and format the result.</td>\r
389  *   </tr>\r
390  *   <tr>\r
391  *     <td width="37"></td>\r
392  *     <td width="23">==</td>\r
393  *     <td width="165" valign="top">in all rule sets</td>\r
394  *     <td>Format the number unchanged</td>\r
395  *   </tr>\r
396  *   <tr>\r
397  *     <td width="37"></td>\r
398  *     <td width="23">[]</td>\r
399  *     <td width="165" valign="top">in normal rule</td>\r
400  *     <td>Omit the optional text if the number is an even multiple of the rule's divisor</td>\r
401  *   </tr>\r
402  *   <tr>\r
403  *     <td width="37"></td>\r
404  *     <td width="23"></td>\r
405  *     <td width="165" valign="top">in negative-number rule</td>\r
406  *     <td>Not allowed.</td>\r
407  *   </tr>\r
408  *   <tr>\r
409  *     <td width="37"></td>\r
410  *     <td width="23"></td>\r
411  *     <td width="165" valign="top">in improper-fraction rule</td>\r
412  *     <td>Omit the optional text if the number is between 0 and 1 (same as specifying both an\r
413  *     x.x rule and a 0.x rule)</td>\r
414  *   </tr>\r
415  *   <tr>\r
416  *     <td width="37"></td>\r
417  *     <td width="23"></td>\r
418  *     <td width="165" valign="top">in master rule</td>\r
419  *     <td>Omit the optional text if the number is an integer (same as specifying both an x.x\r
420  *     rule and an x.0 rule)</td>\r
421  *   </tr>\r
422  *   <tr>\r
423  *     <td width="37"></td>\r
424  *     <td width="23"></td>\r
425  *     <td width="165" valign="top">in proper-fraction rule</td>\r
426  *     <td>Not allowed.</td>\r
427  *   </tr>\r
428  *   <tr>\r
429  *     <td width="37"></td>\r
430  *     <td width="23"></td>\r
431  *     <td width="165" valign="top">in rule in fraction rule set</td>\r
432  *     <td>Omit the optional text if multiplying the number by the rule's base value yields 1.</td>\r
433  *   </tr>\r
434  * </table>\r
435  *\r
436  * <p>The substitution descriptor (i.e., the text between the token characters) may take one\r
437  * of three forms:</p>\r
438  *\r
439  * <table border="0" width="100%">\r
440  *   <tr>\r
441  *     <td width="42"></td>\r
442  *     <td width="166" valign="top">a rule set name</td>\r
443  *     <td>Perform the mathematical operation on the number, and format the result using the\r
444  *     named rule set.</td>\r
445  *   </tr>\r
446  *   <tr>\r
447  *     <td width="42"></td>\r
448  *     <td width="166" valign="top">a DecimalFormat pattern</td>\r
449  *     <td>Perform the mathematical operation on the number, and format the result using a\r
450  *     DecimalFormat with the specified pattern.&nbsp; The pattern must begin with 0 or #.</td>\r
451  *   </tr>\r
452  *   <tr>\r
453  *     <td width="42"></td>\r
454  *     <td width="166" valign="top">nothing</td>\r
455  *     <td>Perform the mathematical operation on the number, and format the result using the rule\r
456  *     set containing the current rule, except:<ul>\r
457  *       <li>You can't have an empty substitution descriptor with a == substitution.</li>\r
458  *       <li>If you omit the substitution descriptor in a &gt;&gt; substitution in a fraction rule,\r
459  *         format the result one digit at a time using the rule set containing the current rule.</li>\r
460  *       <li>If you omit the substitution descriptor in a &lt;&lt; substitution in a rule in a\r
461  *         fraction rule set, format the result using the default rule set for this formatter.</li>\r
462  *     </ul>\r
463  *     </td>\r
464  *   </tr>\r
465  * </table>\r
466  *\r
467  * <p>Whitespace is ignored between a rule set name and a rule set body, between a rule\r
468  * descriptor and a rule body, or between rules. If a rule body begins with an apostrophe,\r
469  * the apostrophe is ignored, but all text after it becomes significant (this is how you can\r
470  * have a rule's rule text begin with whitespace). There is no escape function: the semicolon\r
471  * is not allowed in rule set names or in rule text, and the colon is not allowed in rule set\r
472  * names. The characters beginning a substitution token are always treated as the beginning\r
473  * of a substitution token.</p>\r
474  *\r
475  * <p>See the resource data and the demo program for annotated examples of real rule sets\r
476  * using these features.</p>\r
477  *\r
478  * @author Richard Gillam\r
479  * @see NumberFormat\r
480  * @see DecimalFormat\r
481  * @stable ICU 2.0\r
482  */\r
483 public class RuleBasedNumberFormat extends NumberFormat {\r
484 \r
485     //-----------------------------------------------------------------------\r
486     // constants\r
487     //-----------------------------------------------------------------------\r
488 \r
489     // Generated by serialver from JDK 1.4.1_01\r
490     static final long serialVersionUID = -7664252765575395068L;\r
491 \r
492     /**\r
493      * Selector code that tells the constructor to create a spellout formatter\r
494      * @stable ICU 2.0\r
495      */\r
496     public static final int SPELLOUT = 1;\r
497 \r
498     /**\r
499      * Selector code that tells the constructor to create an ordinal formatter\r
500      * @stable ICU 2.0\r
501      */\r
502     public static final int ORDINAL = 2;\r
503 \r
504     /**\r
505      * Selector code that tells the constructor to create a duration formatter\r
506      * @stable ICU 2.0\r
507      */\r
508     public static final int DURATION = 3;\r
509 \r
510     /**\r
511      * Selector code that tells the constructor to create a numbering system formatter\r
512      * @stable ICU 4.2\r
513      */\r
514     public static final int NUMBERING_SYSTEM = 4;\r
515 \r
516     //-----------------------------------------------------------------------\r
517     // data members\r
518     //-----------------------------------------------------------------------\r
519 \r
520     /**\r
521      * The formatter's rule sets.\r
522      */\r
523     private transient NFRuleSet[] ruleSets = null;\r
524 \r
525     /**\r
526      * A pointer to the formatter's default rule set.  This is always included\r
527      * in ruleSets.\r
528      */\r
529     private transient NFRuleSet defaultRuleSet = null;\r
530 \r
531     /**\r
532      * The formatter's locale.  This is used to create DecimalFormatSymbols and\r
533      * Collator objects.\r
534      * @serial\r
535      */\r
536     private ULocale locale = null;\r
537 \r
538     /**\r
539      * Collator to be used in lenient parsing.  This variable is lazy-evaluated:\r
540      * the collator is actually created the first time the client does a parse\r
541      * with lenient-parse mode turned on.\r
542      */\r
543     private transient RbnfLenientScannerProvider scannerProvider = null;\r
544 \r
545     // flag to mark whether we've previously looked for a scanner and failed\r
546     private transient boolean lookedForScanner;\r
547 \r
548     /**\r
549      * The DecimalFormatSymbols object that any DecimalFormat objects this\r
550      * formatter uses should use.  This variable is lazy-evaluated: it isn't\r
551      * filled in if the rule set never uses a DecimalFormat pattern.\r
552      */\r
553     private transient DecimalFormatSymbols decimalFormatSymbols = null;\r
554 \r
555     /**\r
556      * The NumberFormat used when lenient parsing numbers.  This needs to reflect\r
557      * the locale.  This is lazy-evaluated, like decimalFormatSymbols.  It is\r
558      * here so it can be shared by different NFSubstitutions.\r
559      */\r
560     private transient DecimalFormat decimalFormat = null;\r
561 \r
562     /**\r
563      * Flag specifying whether lenient parse mode is on or off.  Off by default.\r
564      * @serial\r
565      */\r
566     private boolean lenientParse = false;\r
567 \r
568     /**\r
569      * If the description specifies lenient-parse rules, they're stored here until\r
570      * the collator is created.\r
571      */\r
572     private transient String lenientParseRules;\r
573 \r
574     /**\r
575      * If the description specifies post-process rules, they're stored here until\r
576      * post-processing is required.\r
577      */\r
578     private transient String postProcessRules;\r
579 \r
580     /**\r
581      * Post processor lazily constructed from the postProcessRules.\r
582      */\r
583     private transient RBNFPostProcessor postProcessor;\r
584 \r
585     /**\r
586      * Localizations for rule set names.\r
587      * @serial\r
588      */\r
589     private Map<String, String[]> ruleSetDisplayNames;\r
590 \r
591     /**\r
592      * The public rule set names;\r
593      * @serial\r
594      */\r
595     private String[] publicRuleSetNames;\r
596 \r
597     private static final boolean DEBUG  =  ICUDebug.enabled("rbnf");\r
598 \r
599     // Temporary workaround - when noParse is true, do noting in parse.\r
600     // TODO: We need a real fix - see #6895/#6896\r
601     private boolean noParse;\r
602     private static final String[] NO_SPELLOUT_PARSE_LANGUAGES = { "ga" };\r
603 \r
604     //-----------------------------------------------------------------------\r
605     // constructors\r
606     //-----------------------------------------------------------------------\r
607 \r
608     /**\r
609      * Creates a RuleBasedNumberFormat that behaves according to the description\r
610      * passed in.  The formatter uses the default locale.\r
611      * @param description A description of the formatter's desired behavior.\r
612      * See the class documentation for a complete explanation of the description\r
613      * syntax.\r
614      * @stable ICU 2.0\r
615      */\r
616     public RuleBasedNumberFormat(String description) {\r
617         locale = ULocale.getDefault();\r
618         init(description, null);\r
619     }\r
620 \r
621     /**\r
622      * Creates a RuleBasedNumberFormat that behaves according to the description\r
623      * passed in.  The formatter uses the default locale.\r
624      * <p>\r
625      * The localizations data provides information about the public\r
626      * rule sets and their localized display names for different\r
627      * locales. The first element in the list is an array of the names\r
628      * of the public rule sets.  The first element in this array is\r
629      * the initial default ruleset.  The remaining elements in the\r
630      * list are arrays of localizations of the names of the public\r
631      * rule sets.  Each of these is one longer than the initial array,\r
632      * with the first String being the ULocale ID, and the remaining\r
633      * Strings being the localizations of the rule set names, in the\r
634      * same order as the initial array.\r
635      * @param description A description of the formatter's desired behavior.\r
636      * See the class documentation for a complete explanation of the description\r
637      * syntax.\r
638      * @param localizations a list of localizations for the rule set\r
639      * names in the description.\r
640      * @stable ICU 3.2\r
641      */\r
642     public RuleBasedNumberFormat(String description, String[][] localizations) {\r
643         locale = ULocale.getDefault();\r
644         init(description, localizations);\r
645     }\r
646 \r
647     /**\r
648      * Creates a RuleBasedNumberFormat that behaves according to the description\r
649      * passed in.  The formatter uses the specified locale to determine the\r
650      * characters to use when formatting in numerals, and to define equivalences\r
651      * for lenient parsing.\r
652      * @param description A description of the formatter's desired behavior.\r
653      * See the class documentation for a complete explanation of the description\r
654      * syntax.\r
655      * @param locale A locale, which governs which characters are used for\r
656      * formatting values in numerals, and which characters are equivalent in\r
657      * lenient parsing.\r
658      * @stable ICU 2.0\r
659      */\r
660     public RuleBasedNumberFormat(String description, Locale locale) {\r
661         this(description, ULocale.forLocale(locale));\r
662     }\r
663 \r
664     /**\r
665      * Creates a RuleBasedNumberFormat that behaves according to the description\r
666      * passed in.  The formatter uses the specified locale to determine the\r
667      * characters to use when formatting in numerals, and to define equivalences\r
668      * for lenient parsing.\r
669      * @param description A description of the formatter's desired behavior.\r
670      * See the class documentation for a complete explanation of the description\r
671      * syntax.\r
672      * @param locale A locale, which governs which characters are used for\r
673      * formatting values in numerals, and which characters are equivalent in\r
674      * lenient parsing.\r
675      * @stable ICU 3.2\r
676      */\r
677     public RuleBasedNumberFormat(String description, ULocale locale) {\r
678         this.locale = locale;\r
679         init(description, null);\r
680     }\r
681 \r
682     /**\r
683      * Creates a RuleBasedNumberFormat that behaves according to the description\r
684      * passed in.  The formatter uses the specified locale to determine the\r
685      * characters to use when formatting in numerals, and to define equivalences\r
686      * for lenient parsing.\r
687      * <p>\r
688      * The localizations data provides information about the public\r
689      * rule sets and their localized display names for different\r
690      * locales. The first element in the list is an array of the names\r
691      * of the public rule sets.  The first element in this array is\r
692      * the initial default ruleset.  The remaining elements in the\r
693      * list are arrays of localizations of the names of the public\r
694      * rule sets.  Each of these is one longer than the initial array,\r
695      * with the first String being the ULocale ID, and the remaining\r
696      * Strings being the localizations of the rule set names, in the\r
697      * same order as the initial array.\r
698      * @param description A description of the formatter's desired behavior.\r
699      * See the class documentation for a complete explanation of the description\r
700      * syntax.\r
701      * @param localizations a list of localizations for the rule set names in the description.\r
702      * @param locale A ulocale that governs which characters are used for\r
703      * formatting values in numerals, and determines which characters are equivalent in\r
704      * lenient parsing.\r
705      * @stable ICU 3.2\r
706      */\r
707     public RuleBasedNumberFormat(String description, String[][] localizations, ULocale locale) {\r
708         this.locale = locale;\r
709         init(description, localizations);\r
710     }\r
711 \r
712     /**\r
713      * Creates a RuleBasedNumberFormat from a predefined description.  The selector\r
714      * code choosed among three possible predefined formats: spellout, ordinal,\r
715      * and duration.\r
716      * @param locale The locale for the formatter.\r
717      * @param format A selector code specifying which kind of formatter to create for that\r
718      * locale.  There are three legal values: SPELLOUT, which creates a formatter that\r
719      * spells out a value in words in the desired language, ORDINAL, which attaches\r
720      * an ordinal suffix from the desired language to the end of a number (e.g. "123rd"),\r
721      * and DURATION, which formats a duration in seconds as hours, minutes, and seconds.\r
722      * @stable ICU 2.0\r
723      */\r
724     public RuleBasedNumberFormat(Locale locale, int format) {\r
725         this(ULocale.forLocale(locale), format);\r
726     }\r
727 \r
728     /**\r
729      * Creates a RuleBasedNumberFormat from a predefined description.  The selector\r
730      * code choosed among three possible predefined formats: spellout, ordinal,\r
731      * and duration.\r
732      * @param locale The locale for the formatter.\r
733      * @param format A selector code specifying which kind of formatter to create for that\r
734      * locale.  There are four legal values: SPELLOUT, which creates a formatter that\r
735      * spells out a value in words in the desired language, ORDINAL, which attaches\r
736      * an ordinal suffix from the desired language to the end of a number (e.g. "123rd"),\r
737      * DURATION, which formats a duration in seconds as hours, minutes, and seconds, and\r
738      * NUMBERING_SYSTEM, which is used to invoke rules for alternate numbering\r
739      * systems such as the Hebrew numbering system, or for Roman numerals, etc..\r
740      * @stable ICU 3.2\r
741      */\r
742     public RuleBasedNumberFormat(ULocale locale, int format) {\r
743         this.locale = locale;\r
744 \r
745         ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.\r
746             getBundleInstance(ICUResourceBundle.ICU_RBNF_BASE_NAME, locale);\r
747 \r
748         // TODO: determine correct actual/valid locale.  Note ambiguity\r
749         // here -- do actual/valid refer to pattern, DecimalFormatSymbols,\r
750         // or Collator?\r
751         ULocale uloc = bundle.getULocale();\r
752         setLocale(uloc, uloc);\r
753 \r
754         String description = "";\r
755         String[][] localizations = null;\r
756 \r
757         try {\r
758             // For backwards compatability - If we have a pre-4.2 style RBNF resource, attempt to read it.\r
759             description = bundle.getString(rulenames[format-1]);\r
760         }\r
761         catch (MissingResourceException e) {\r
762             try {\r
763                 ICUResourceBundle rules = bundle.getWithFallback("RBNFRules/"+rulenames[format-1]);\r
764                 UResourceBundleIterator it = rules.getIterator();\r
765                 while (it.hasNext()) {\r
766                    description = description.concat(it.nextString());\r
767                 }\r
768             }\r
769             catch (MissingResourceException e1) {\r
770             }\r
771         }\r
772 \r
773         try {\r
774             UResourceBundle locb = bundle.get(locnames[format-1]);\r
775             localizations = new String[locb.getSize()][];\r
776             for (int i = 0; i < localizations.length; ++i) {\r
777                 localizations[i] = locb.get(i).getStringArray();\r
778             }\r
779         }\r
780         catch (MissingResourceException e) {\r
781             // might have description and no localizations, or no description...\r
782         }\r
783 \r
784         init(description, localizations);\r
785 \r
786         //TODO: we need a real fix - see #6895 / #6896\r
787         noParse = false;\r
788         if (locnames[format-1].equals("SpelloutLocalizations")) {\r
789             String lang = locale.getLanguage();\r
790             for (int i = 0; i < NO_SPELLOUT_PARSE_LANGUAGES.length; i++) {\r
791                 if (NO_SPELLOUT_PARSE_LANGUAGES[i].equals(lang)) {\r
792                     noParse = true;\r
793                     break;\r
794                 }\r
795             }\r
796         }\r
797     }\r
798 \r
799     private static final String[] rulenames = {\r
800         "SpelloutRules", "OrdinalRules", "DurationRules", "NumberingSystemRules",\r
801     };\r
802     private static final String[] locnames = {\r
803         "SpelloutLocalizations", "OrdinalLocalizations", "DurationLocalizations", "NumberingSystemLocalizations",\r
804     };\r
805 \r
806     /**\r
807      * Creates a RuleBasedNumberFormat from a predefined description.  Uses the\r
808      * default locale.\r
809      * @param format A selector code specifying which kind of formatter to create.\r
810      * There are three legal values: SPELLOUT, which creates a formatter that spells\r
811      * out a value in words in the default locale's langyage, ORDINAL, which attaches\r
812      * an ordinal suffix from the default locale's language to a numeral, and\r
813      * DURATION, which formats a duration in seconds as hours, minutes, and seconds.\r
814      * or NUMBERING_SYSTEM, which is used for alternate numbering systems such as Hebrew.\r
815      * @stable ICU 2.0\r
816      */\r
817     public RuleBasedNumberFormat(int format) {\r
818         this(ULocale.getDefault(), format);\r
819     }\r
820 \r
821     //-----------------------------------------------------------------------\r
822     // boilerplate\r
823     //-----------------------------------------------------------------------\r
824 \r
825     /**\r
826      * Duplicates this formatter.\r
827      * @return A RuleBasedNumberFormat that is equal to this one.\r
828      * @stable ICU 2.0\r
829      */\r
830     public Object clone() {\r
831         return super.clone();\r
832     }\r
833 \r
834     /**\r
835      * Tests two RuleBasedNumberFormats for equality.\r
836      * @param that The formatter to compare against this one.\r
837      * @return true if the two formatters have identical behavior.\r
838      * @stable ICU 2.0\r
839      */\r
840     public boolean equals(Object that) {\r
841         // if the other object isn't a RuleBasedNumberFormat, that's\r
842         // all we need to know\r
843         if (!(that instanceof RuleBasedNumberFormat)) {\r
844             return false;\r
845         } else {\r
846             // cast the other object's pointer to a pointer to a\r
847             // RuleBasedNumberFormat\r
848             RuleBasedNumberFormat that2 = (RuleBasedNumberFormat)that;\r
849 \r
850             // compare their locales and lenient-parse modes\r
851             if (!locale.equals(that2.locale) || lenientParse != that2.lenientParse) {\r
852                 return false;\r
853             }\r
854 \r
855             // if that succeeds, then compare their rule set lists\r
856             if (ruleSets.length != that2.ruleSets.length) {\r
857                 return false;\r
858             }\r
859             for (int i = 0; i < ruleSets.length; i++) {\r
860                 if (!ruleSets[i].equals(that2.ruleSets[i])) {\r
861                     return false;\r
862                 }\r
863             }\r
864 \r
865             return true;\r
866         }\r
867     }\r
868 \r
869     /**\r
870      * Generates a textual description of this formatter.\r
871      * @return a String containing a rule set that will produce a RuleBasedNumberFormat\r
872      * with identical behavior to this one.  This won't necessarily be identical\r
873      * to the rule set description that was originally passed in, but will produce\r
874      * the same result.\r
875      * @stable ICU 2.0\r
876      */\r
877     public String toString() {\r
878 \r
879         // accumulate the descriptions of all the rule sets in a\r
880         // StringBuffer, then cast it to a String and return it\r
881         StringBuilder result = new StringBuilder();\r
882         for (int i = 0; i < ruleSets.length; i++) {\r
883             result.append(ruleSets[i].toString());\r
884         }\r
885         return result.toString();\r
886     }\r
887 \r
888     /**\r
889      * Writes this object to a stream.\r
890      * @param out The stream to write to.\r
891      */\r
892     private void writeObject(java.io.ObjectOutputStream out)\r
893         throws java.io.IOException {\r
894         // we just write the textual description to the stream, so we\r
895         // have an implementation-independent streaming format\r
896         out.writeUTF(this.toString());\r
897         out.writeObject(this.locale);\r
898     }\r
899 \r
900     /**\r
901      * Reads this object in from a stream.\r
902      * @param in The stream to read from.\r
903      */\r
904     private void readObject(java.io.ObjectInputStream in)\r
905         throws java.io.IOException {\r
906 \r
907         // read the description in from the stream\r
908         String description = in.readUTF();\r
909         ULocale loc;\r
910 \r
911         try {\r
912             loc = (ULocale) in.readObject();\r
913         } catch (Exception e) {\r
914             loc = ULocale.getDefault();\r
915         }\r
916 \r
917         // build a brand-new RuleBasedNumberFormat from the description,\r
918         // then steal its substructure.  This object's substructure and\r
919         // the temporary RuleBasedNumberFormat drop on the floor and\r
920         // get swept up by the garbage collector\r
921         RuleBasedNumberFormat temp = new RuleBasedNumberFormat(description, loc);\r
922         ruleSets = temp.ruleSets;\r
923         defaultRuleSet = temp.defaultRuleSet;\r
924         publicRuleSetNames = temp.publicRuleSetNames;\r
925         decimalFormatSymbols = temp.decimalFormatSymbols;\r
926         decimalFormat = temp.decimalFormat;\r
927         locale = temp.locale;\r
928     }\r
929 \r
930 \r
931     //-----------------------------------------------------------------------\r
932     // public API functions\r
933     //-----------------------------------------------------------------------\r
934 \r
935     /**\r
936      * Returns a list of the names of all of this formatter's public rule sets.\r
937      * @return A list of the names of all of this formatter's public rule sets.\r
938      * @stable ICU 2.0\r
939      */\r
940     public String[] getRuleSetNames() {\r
941         return publicRuleSetNames.clone();\r
942     }\r
943 \r
944     /**\r
945      * Return a list of locales for which there are locale-specific display names\r
946      * for the rule sets in this formatter.  If there are no localized display names, return null.\r
947      * @return an array of the ulocales for which there is rule set display name information\r
948      * @stable ICU 3.2\r
949      */\r
950     public ULocale[] getRuleSetDisplayNameLocales() {\r
951         if (ruleSetDisplayNames != null) {\r
952             Set<String> s = ruleSetDisplayNames.keySet();\r
953             String[] locales = s.toArray(new String[s.size()]);\r
954             Arrays.sort(locales, String.CASE_INSENSITIVE_ORDER);\r
955             ULocale[] result = new ULocale[locales.length];\r
956             for (int i = 0; i < locales.length; ++i) {\r
957                 result[i] = new ULocale(locales[i]);\r
958             }\r
959             return result;\r
960         }\r
961         return null;\r
962     }\r
963 \r
964     private String[] getNameListForLocale(ULocale loc) {\r
965         if (loc != null && ruleSetDisplayNames != null) {\r
966             String[] localeNames = { loc.getBaseName(), ULocale.getDefault().getBaseName() };\r
967             for (int i = 0; i < localeNames.length; ++i) {\r
968                 String lname = localeNames[i];\r
969                 while (lname.length() > 0) {\r
970                     String[] names = ruleSetDisplayNames.get(lname);\r
971                     if (names != null) {\r
972                         return names;\r
973                     }\r
974                     lname = ULocale.getFallback(lname);\r
975                 }\r
976             }\r
977         }\r
978         return null;\r
979     }\r
980 \r
981     /**\r
982      * Return the rule set display names for the provided locale.  These are in the same order\r
983      * as those returned by getRuleSetNames.  The locale is matched against the locales for\r
984      * which there is display name data, using normal fallback rules.  If no locale matches,\r
985      * the default display names are returned.  (These are the internal rule set names minus\r
986      * the leading '%'.)\r
987      * @return an array of the locales that have display name information\r
988      * @see #getRuleSetNames\r
989      * @stable ICU 3.2\r
990      */\r
991     public String[] getRuleSetDisplayNames(ULocale loc) {\r
992         String[] names = getNameListForLocale(loc);\r
993         if (names != null) {\r
994             return names.clone();\r
995         }\r
996         names = getRuleSetNames();\r
997         for (int i = 0; i < names.length; ++i) {\r
998             names[i] = names[i].substring(1);\r
999         }\r
1000         return names;\r
1001     }\r
1002 \r
1003     /**\r
1004      * Return the rule set display names for the current default locale.\r
1005      * @return an array of the display names\r
1006      * @see #getRuleSetDisplayNames(ULocale)\r
1007      * @stable ICU 3.2\r
1008      */\r
1009     public String[] getRuleSetDisplayNames() {\r
1010         return getRuleSetDisplayNames(ULocale.getDefault());\r
1011     }\r
1012 \r
1013     /**\r
1014      * Return the rule set display name for the provided rule set and locale.\r
1015      * The locale is matched against the locales for which there is display name data, using\r
1016      * normal fallback rules.  If no locale matches, the default display name is returned.\r
1017      * @return the display name for the rule set\r
1018      * @see #getRuleSetDisplayNames\r
1019      * @throws IllegalArgumentException if ruleSetName is not a valid rule set name for this format\r
1020      * @stable ICU 3.2\r
1021      */\r
1022     public String getRuleSetDisplayName(String ruleSetName, ULocale loc) {\r
1023         String[] rsnames = publicRuleSetNames;\r
1024         for (int ix = 0; ix < rsnames.length; ++ix) {\r
1025             if (rsnames[ix].equals(ruleSetName)) {\r
1026                 String[] names = getNameListForLocale(loc);\r
1027                 if (names != null) {\r
1028                     return names[ix];\r
1029                 }\r
1030                 return rsnames[ix].substring(1);\r
1031             }\r
1032         }\r
1033         throw new IllegalArgumentException("unrecognized rule set name: " + ruleSetName);\r
1034     }\r
1035 \r
1036     /**\r
1037      * Return the rule set display name for the provided rule set in the current default locale.\r
1038      * @return the display name for the rule set\r
1039      * @see #getRuleSetDisplayName(String,ULocale)\r
1040      * @stable ICU 3.2\r
1041      */\r
1042     public String getRuleSetDisplayName(String ruleSetName) {\r
1043         return getRuleSetDisplayName(ruleSetName, ULocale.getDefault());\r
1044     }\r
1045 \r
1046     /**\r
1047      * Formats the specified number according to the specified rule set.\r
1048      * @param number The number to format.\r
1049      * @param ruleSet The name of the rule set to format the number with.\r
1050      * This must be the name of a valid public rule set for this formatter.\r
1051      * @return A textual representation of the number.\r
1052      * @stable ICU 2.0\r
1053      */\r
1054     public String format(double number, String ruleSet) throws IllegalArgumentException {\r
1055         if (ruleSet.startsWith("%%")) {\r
1056             throw new IllegalArgumentException("Can't use internal rule set");\r
1057         }\r
1058         return format(number, findRuleSet(ruleSet));\r
1059     }\r
1060 \r
1061     /**\r
1062      * Formats the specified number according to the specified rule set.\r
1063      * (If the specified rule set specifies a master ["x.0"] rule, this function\r
1064      * ignores it.  Convert the number to a double first if you ned it.)  This\r
1065      * function preserves all the precision in the long-- it doesn't convert it\r
1066      * to a double.\r
1067      * @param number The number to format.\r
1068      * @param ruleSet The name of the rule set to format the number with.\r
1069      * This must be the name of a valid public rule set for this formatter.\r
1070      * @return A textual representation of the number.\r
1071      * @stable ICU 2.0\r
1072      */\r
1073     public String format(long number, String ruleSet) throws IllegalArgumentException {\r
1074         if (ruleSet.startsWith("%%")) {\r
1075             throw new IllegalArgumentException("Can't use internal rule set");\r
1076         }\r
1077         return format(number, findRuleSet(ruleSet));\r
1078     }\r
1079 \r
1080     /**\r
1081      * Formats the specified number using the formatter's default rule set.\r
1082      * (The default rule set is the last public rule set defined in the description.)\r
1083      * @param number The number to format.\r
1084      * @param toAppendTo A StringBuffer that the result should be appended to.\r
1085      * @param ignore This function doesn't examine or update the field position.\r
1086      * @return toAppendTo\r
1087      * @stable ICU 2.0\r
1088      */\r
1089     public StringBuffer format(double number,\r
1090                                StringBuffer toAppendTo,\r
1091                                FieldPosition ignore) {\r
1092         // this is one of the inherited format() methods.  Since it doesn't\r
1093         // have a way to select the rule set to use, it just uses the\r
1094         // default one\r
1095         toAppendTo.append(format(number, defaultRuleSet));\r
1096         return toAppendTo;\r
1097     }\r
1098 \r
1099     /**\r
1100      * Formats the specified number using the formatter's default rule set.\r
1101      * (The default rule set is the last public rule set defined in the description.)\r
1102      * (If the specified rule set specifies a master ["x.0"] rule, this function\r
1103      * ignores it.  Convert the number to a double first if you ned it.)  This\r
1104      * function preserves all the precision in the long-- it doesn't convert it\r
1105      * to a double.\r
1106      * @param number The number to format.\r
1107      * @param toAppendTo A StringBuffer that the result should be appended to.\r
1108      * @param ignore This function doesn't examine or update the field position.\r
1109      * @return toAppendTo\r
1110      * @stable ICU 2.0\r
1111      */\r
1112     public StringBuffer format(long number,\r
1113                                StringBuffer toAppendTo,\r
1114                                FieldPosition ignore) {\r
1115         // this is one of the inherited format() methods.  Since it doesn't\r
1116         // have a way to select the rule set to use, it just uses the\r
1117         // default one\r
1118         toAppendTo.append(format(number, defaultRuleSet));\r
1119         return toAppendTo;\r
1120     }\r
1121 \r
1122     /**\r
1123      * <strong><font face=helvetica color=red>NEW</font></strong>\r
1124      * Implement com.ibm.icu.text.NumberFormat:\r
1125      * Format a BigInteger.\r
1126      * @stable ICU 2.0\r
1127      */\r
1128     public StringBuffer format(BigInteger number,\r
1129                                StringBuffer toAppendTo,\r
1130                                FieldPosition pos) {\r
1131         return format(new com.ibm.icu.math.BigDecimal(number), toAppendTo, pos);\r
1132     }\r
1133 \r
1134     /**\r
1135      * <strong><font face=helvetica color=red>NEW</font></strong>\r
1136      * Implement com.ibm.icu.text.NumberFormat:\r
1137      * Format a BigDecimal.\r
1138      * @stable ICU 2.0\r
1139      */\r
1140     public StringBuffer format(java.math.BigDecimal number,\r
1141                                StringBuffer toAppendTo,\r
1142                                FieldPosition pos) {\r
1143         return format(new com.ibm.icu.math.BigDecimal(number), toAppendTo, pos);\r
1144     }\r
1145 \r
1146     /**\r
1147      * <strong><font face=helvetica color=red>NEW</font></strong>\r
1148      * Implement com.ibm.icu.text.NumberFormat:\r
1149      * Format a BigDecimal.\r
1150      * @stable ICU 2.0\r
1151      */\r
1152     public StringBuffer format(com.ibm.icu.math.BigDecimal number,\r
1153                                StringBuffer toAppendTo,\r
1154                                FieldPosition pos) {\r
1155         // TEMPORARY:\r
1156         return format(number.doubleValue(), toAppendTo, pos);\r
1157     }\r
1158 \r
1159     /**\r
1160      * Parses the specfied string, beginning at the specified position, according\r
1161      * to this formatter's rules.  This will match the string against all of the\r
1162      * formatter's public rule sets and return the value corresponding to the longest\r
1163      * parseable substring.  This function's behavior is affected by the lenient\r
1164      * parse mode.\r
1165      * @param text The string to parse\r
1166      * @param parsePosition On entry, contains the position of the first character\r
1167      * in "text" to examine.  On exit, has been updated to contain the position\r
1168      * of the first character in "text" that wasn't consumed by the parse.\r
1169      * @return The number that corresponds to the parsed text.  This will be an\r
1170      * instance of either Long or Double, depending on whether the result has a\r
1171      * fractional part.\r
1172      * @see #setLenientParseMode\r
1173      * @stable ICU 2.0\r
1174      */\r
1175     public Number parse(String text, ParsePosition parsePosition) {\r
1176 \r
1177         //TODO: We need a real fix.  See #6895 / #6896\r
1178         if (noParse) {\r
1179             // skip parsing\r
1180             return new Long(0);\r
1181         }\r
1182 \r
1183         // parsePosition tells us where to start parsing.  We copy the\r
1184         // text in the string from here to the end inro a new string,\r
1185         // and create a new ParsePosition and result variable to use\r
1186         // for the duration of the parse operation\r
1187         String workingText = text.substring(parsePosition.getIndex());\r
1188         ParsePosition workingPos = new ParsePosition(0);\r
1189         Number tempResult = null;\r
1190 \r
1191         // keep track of the largest number of characters consumed in\r
1192         // the various trials, and the result that corresponds to it\r
1193         Number result = new Long(0);\r
1194         ParsePosition highWaterMark = new ParsePosition(workingPos.getIndex());\r
1195 \r
1196         // iterate over the public rule sets (beginning with the default one)\r
1197         // and try parsing the text with each of them.  Keep track of which\r
1198         // one consumes the most characters: that's the one that determines\r
1199         // the result we return\r
1200         for (int i = ruleSets.length - 1; i >= 0; i--) {\r
1201             // skip private rule sets\r
1202             if (!ruleSets[i].isPublic() || !ruleSets[i].isParseable()) {\r
1203                 continue;\r
1204             }\r
1205 \r
1206             // try parsing the string with the rule set.  If it gets past the\r
1207             // high-water mark, update the high-water mark and the result\r
1208             tempResult = ruleSets[i].parse(workingText, workingPos, Double.MAX_VALUE);\r
1209             if (workingPos.getIndex() > highWaterMark.getIndex()) {\r
1210                 result = tempResult;\r
1211                 highWaterMark.setIndex(workingPos.getIndex());\r
1212             }\r
1213             // commented out because this API on ParsePosition doesn't exist in 1.1.x\r
1214             //            if (workingPos.getErrorIndex() > highWaterMark.getErrorIndex()) {\r
1215             //                highWaterMark.setErrorIndex(workingPos.getErrorIndex());\r
1216             //            }\r
1217 \r
1218             // if we manage to use up all the characters in the string,\r
1219             // we don't have to try any more rule sets\r
1220             if (highWaterMark.getIndex() == workingText.length()) {\r
1221                 break;\r
1222             }\r
1223 \r
1224             // otherwise, reset our internal parse position to the\r
1225             // beginning and try again with the next rule set\r
1226             workingPos.setIndex(0);\r
1227         }\r
1228 \r
1229         // add the high water mark to our original parse position and\r
1230         // return the result\r
1231         parsePosition.setIndex(parsePosition.getIndex() + highWaterMark.getIndex());\r
1232         // commented out because this API on ParsePosition doesn't exist in 1.1.x\r
1233         //        if (highWaterMark.getIndex() == 0) {\r
1234         //            parsePosition.setErrorIndex(parsePosition.getIndex() + highWaterMark.getErrorIndex());\r
1235         //        }\r
1236         return result;\r
1237     }\r
1238 \r
1239     /**\r
1240      * Turns lenient parse mode on and off.\r
1241      *\r
1242      * When in lenient parse mode, the formatter uses an RbnfLenientScanner\r
1243      * for parsing the text.  Lenient parsing is only in effect if a scanner\r
1244      * is set.  If a provider is not set, and this is used for parsing,\r
1245      * a default scanner <code>RbnfLenientScannerProviderImpl</code> will be set if\r
1246      * it is available on the classpath.  Otherwise this will have no effect.\r
1247      *\r
1248      * @param enabled If true, turns lenient-parse mode on; if false, turns it off.\r
1249      * @see RbnfLenientScanner\r
1250      * @see RbnfLenientScannerProvider\r
1251      * @stable ICU 2.0\r
1252      */\r
1253     public void setLenientParseMode(boolean enabled) {\r
1254         lenientParse = enabled;\r
1255     }\r
1256 \r
1257     /**\r
1258      * Returns true if lenient-parse mode is turned on.  Lenient parsing is off\r
1259      * by default.\r
1260      * @return true if lenient-parse mode is turned on.\r
1261      * @see #setLenientParseMode\r
1262      * @stable ICU 2.0\r
1263      */\r
1264     public boolean lenientParseEnabled() {\r
1265         return lenientParse;\r
1266     }\r
1267 \r
1268     /**\r
1269      * Sets the provider for the lenient scanner.  If this has not been set, \r
1270      * {@link #setLenientParseMode}\r
1271      * has no effect.  This is necessary to decouple collation from format code.\r
1272      * @param scannerProvider the provider\r
1273      * @see #setLenientParseMode\r
1274      * @see #getLenientScannerProvider\r
1275      * @draft ICU 4.4\r
1276      * @provisional This API might change or be removed in a future release.\r
1277      */\r
1278     public void setLenientScannerProvider(RbnfLenientScannerProvider scannerProvider) {\r
1279         this.scannerProvider = scannerProvider;\r
1280     }\r
1281 \r
1282     /**\r
1283      * Returns the lenient scanner provider.  If none was set, and lenient parse is\r
1284      * enabled, this will attempt to instantiate a default scanner, setting it if\r
1285      * it was successful.  Otherwise this returns false.\r
1286      *\r
1287      * @see #setLenientScannerProvider\r
1288      * @draft ICU 4.4\r
1289      * @provisional This API might change or be removed in a future release.\r
1290      */\r
1291     public RbnfLenientScannerProvider getLenientScannerProvider() {\r
1292         // there's a potential race condition if two threads try to set/get the scanner at\r
1293         // the same time, but you get what you get, and you shouldn't be using this from\r
1294         // multiple threads anyway.\r
1295         if (scannerProvider == null && lenientParse && !lookedForScanner) {\r
1296             ///CLOVER:OFF\r
1297             try {\r
1298                 lookedForScanner = true;\r
1299                 Class<?> cls = Class.forName("com.ibm.icu.text.RbnfScannerProviderImpl");\r
1300                 RbnfLenientScannerProvider provider = (RbnfLenientScannerProvider)cls.newInstance();\r
1301                 setLenientScannerProvider(provider);\r
1302             }\r
1303             catch (Exception e) {\r
1304                 // any failure, we just ignore and return null\r
1305             }\r
1306             ///CLOVER:ON\r
1307         }\r
1308 \r
1309         return scannerProvider;\r
1310     }\r
1311 \r
1312     /**\r
1313      * Override the default rule set to use.  If ruleSetName is null, reset\r
1314      * to the initial default rule set.\r
1315      * @param ruleSetName the name of the rule set, or null to reset the initial default.\r
1316      * @throws IllegalArgumentException if ruleSetName is not the name of a public ruleset.\r
1317      * @stable ICU 2.0\r
1318      */\r
1319     public void setDefaultRuleSet(String ruleSetName) {\r
1320         if (ruleSetName == null) {\r
1321             if (publicRuleSetNames.length > 0) {\r
1322                 defaultRuleSet = findRuleSet(publicRuleSetNames[0]);\r
1323             } else {\r
1324                 defaultRuleSet = null;\r
1325                 int n = ruleSets.length;\r
1326                 while (--n >= 0) {\r
1327                    String currentName = ruleSets[n].getName();\r
1328                    if (currentName.equals("%spellout-numbering") ||\r
1329                        currentName.equals("%digits-ordinal") ||\r
1330                        currentName.equals("%duration")) {\r
1331 \r
1332                        defaultRuleSet = ruleSets[n];\r
1333                        return;\r
1334                    }\r
1335                 }\r
1336 \r
1337                 n = ruleSets.length;\r
1338                 while (--n >= 0) {\r
1339                     if (ruleSets[n].isPublic()) {\r
1340                         defaultRuleSet = ruleSets[n];\r
1341                         break;\r
1342                     }\r
1343                 }\r
1344             }\r
1345         } else if (ruleSetName.startsWith("%%")) {\r
1346             throw new IllegalArgumentException("cannot use private rule set: " + ruleSetName);\r
1347         } else {\r
1348             defaultRuleSet = findRuleSet(ruleSetName);\r
1349         }\r
1350     }\r
1351 \r
1352     /**\r
1353      * Return the name of the current default rule set.\r
1354      * @return the name of the current default rule set, if it is public, else the empty string.\r
1355      * @stable ICU 3.0\r
1356      */\r
1357     public String getDefaultRuleSetName() {\r
1358         if (defaultRuleSet != null && defaultRuleSet.isPublic()) {\r
1359             return defaultRuleSet.getName();\r
1360         }\r
1361         return "";\r
1362     }\r
1363 \r
1364     //-----------------------------------------------------------------------\r
1365     // package-internal API\r
1366     //-----------------------------------------------------------------------\r
1367 \r
1368     /**\r
1369      * Returns a reference to the formatter's default rule set.  The default\r
1370      * rule set is the last public rule set in the description, or the one\r
1371      * most recently set by setDefaultRuleSet.\r
1372      * @return The formatter's default rule set.\r
1373      */\r
1374     NFRuleSet getDefaultRuleSet() {\r
1375         return defaultRuleSet;\r
1376     }\r
1377 \r
1378     /**\r
1379      * Returns the scanner to use for lenient parsing.  The scanner is\r
1380      * provided by the provider.\r
1381      * @return The collator to use for lenient parsing, or null if lenient parsing\r
1382      * is turned off.\r
1383      */\r
1384     RbnfLenientScanner getLenientScanner() {\r
1385         if (lenientParse) {\r
1386             RbnfLenientScannerProvider provider = getLenientScannerProvider();\r
1387             if (provider != null) {\r
1388                 return provider.get(locale, lenientParseRules);\r
1389             }\r
1390         }\r
1391         return null;\r
1392     }\r
1393 \r
1394     /**\r
1395      * Returns the DecimalFormatSymbols object that should be used by all DecimalFormat\r
1396      * instances owned by this formatter.  This object is lazily created: this function\r
1397      * creates it the first time it's called.\r
1398      * @return The DecimalFormatSymbols object that should be used by all DecimalFormat\r
1399      * instances owned by this formatter.\r
1400      */\r
1401     DecimalFormatSymbols getDecimalFormatSymbols() {\r
1402         // lazy-evaluate the DecimalFormatSymbols object.  This object\r
1403         // is shared by all DecimalFormat instances belonging to this\r
1404         // formatter\r
1405         if (decimalFormatSymbols == null) {\r
1406             decimalFormatSymbols = new DecimalFormatSymbols(locale);\r
1407         }\r
1408         return decimalFormatSymbols;\r
1409     }\r
1410 \r
1411     DecimalFormat getDecimalFormat() {\r
1412         if (decimalFormat == null) {\r
1413             decimalFormat = (DecimalFormat)NumberFormat.getInstance(locale);\r
1414         }\r
1415         return decimalFormat;\r
1416     }\r
1417 \r
1418     //-----------------------------------------------------------------------\r
1419     // construction implementation\r
1420     //-----------------------------------------------------------------------\r
1421 \r
1422     /**\r
1423      * This extracts the special information from the rule sets before the\r
1424      * main parsing starts.  Extra whitespace must have already been removed\r
1425      * from the description.  If found, the special information is removed from the\r
1426      * description and returned, otherwise the description is unchanged and null\r
1427      * is returned.  Note: the trailing semicolon at the end of the special\r
1428      * rules is stripped.\r
1429      * @param description the rbnf description with extra whitespace removed\r
1430      * @param specialName the name of the special rule text to extract\r
1431      * @return the special rule text, or null if the rule was not found\r
1432      */\r
1433     private String extractSpecial(StringBuilder description, String specialName) {\r
1434         String result = null;\r
1435         int lp = description.indexOf(specialName);\r
1436         if (lp != -1) {\r
1437             // we've got to make sure we're not in the middle of a rule\r
1438             // (where specialName would actually get treated as\r
1439             // rule text)\r
1440             if (lp == 0 || description.charAt(lp - 1) == ';') {\r
1441                 // locate the beginning and end of the actual special\r
1442                 // rules (there may be whitespace between the name and\r
1443                 // the first token in the description)\r
1444                 int lpEnd = description.indexOf(";%", lp);\r
1445 \r
1446                 if (lpEnd == -1) {\r
1447                     lpEnd = description.length() - 1; // later we add 1 back to get the '%'\r
1448                 }\r
1449                 int lpStart = lp + specialName.length();\r
1450                 while (lpStart < lpEnd &&\r
1451                        UCharacterProperty.isRuleWhiteSpace(description.charAt(lpStart))) {\r
1452                     ++lpStart;\r
1453                 }\r
1454 \r
1455                 // copy out the special rules\r
1456                 result = description.substring(lpStart, lpEnd);\r
1457 \r
1458                 // remove the special rule from the description\r
1459                 description.delete(lp, lpEnd+1); // delete the semicolon but not the '%'\r
1460             }\r
1461         }\r
1462         return result;\r
1463     }\r
1464 \r
1465     /**\r
1466      * This function parses the description and uses it to build all of\r
1467      * internal data structures that the formatter uses to do formatting\r
1468      * @param description The description of the formatter's desired behavior.\r
1469      * This is either passed in by the caller or loaded out of a resource\r
1470      * by one of the constructors, and is in the description format specified\r
1471      * in the class docs.\r
1472      */\r
1473     private void init(String description, String[][] localizations) {\r
1474         initLocalizations(localizations);\r
1475 \r
1476         // start by stripping the trailing whitespace from all the rules\r
1477         // (this is all the whitespace follwing each semicolon in the\r
1478         // description).  This allows us to look for rule-set boundaries\r
1479         // by searching for ";%" without having to worry about whitespace\r
1480         // between the ; and the %\r
1481         StringBuilder descBuf = stripWhitespace(description);\r
1482 \r
1483         // check to see if there's a set of lenient-parse rules.  If there\r
1484         // is, pull them out into our temporary holding place for them,\r
1485         // and delete them from the description before the real desciption-\r
1486         // parsing code sees them\r
1487 \r
1488         lenientParseRules = extractSpecial(descBuf, "%%lenient-parse:");\r
1489         postProcessRules = extractSpecial(descBuf, "%%post-process:");\r
1490 \r
1491         // pre-flight parsing the description and count the number of\r
1492         // rule sets (";%" marks the end of one rule set and the beginning\r
1493         // of the next)\r
1494         int numRuleSets = 0;\r
1495         for (int p = descBuf.indexOf(";%"); p != -1; p = descBuf.indexOf(";%", p)) {\r
1496             ++numRuleSets;\r
1497             ++p;\r
1498         }\r
1499         ++numRuleSets;\r
1500 \r
1501         // our rule list is an array of the apprpriate size\r
1502         ruleSets = new NFRuleSet[numRuleSets];\r
1503 \r
1504         // divide up the descriptions into individual rule-set descriptions\r
1505         // and store them in a temporary array.  At each step, we also\r
1506         // new up a rule set, but all this does is initialize its name\r
1507         // and remove it from its description.  We can't actually parse\r
1508         // the rest of the descriptions and finish initializing everything\r
1509         // because we have to know the names and locations of all the rule\r
1510         // sets before we can actually set everything up\r
1511         String[] ruleSetDescriptions = new String[numRuleSets];\r
1512 \r
1513         int curRuleSet = 0;\r
1514         int start = 0;\r
1515         for (int p = descBuf.indexOf(";%"); p != -1; p = descBuf.indexOf(";%", start)) {\r
1516             ruleSetDescriptions[curRuleSet] = descBuf.substring(start, p + 1);\r
1517             ruleSets[curRuleSet] = new NFRuleSet(ruleSetDescriptions, curRuleSet);\r
1518             ++curRuleSet;\r
1519             start = p + 1;\r
1520         }\r
1521         ruleSetDescriptions[curRuleSet] = descBuf.substring(start);\r
1522         ruleSets[curRuleSet] = new NFRuleSet(ruleSetDescriptions, curRuleSet);\r
1523 \r
1524         // now we can take note of the formatter's default rule set, which\r
1525         // is the last public rule set in the description (it's the last\r
1526         // rather than the first so that a user can create a new formatter\r
1527         // from an existing formatter and change its default bevhaior just\r
1528         // by appending more rule sets to the end)\r
1529 \r
1530         // {dlf} Initialization of a fraction rule set requires the default rule\r
1531         // set to be known.  For purposes of initialization, this is always the\r
1532         // last public rule set, no matter what the localization data says.\r
1533 \r
1534         // Set the default ruleset to the last public ruleset, unless one of the predefined\r
1535         // ruleset names %spellout-numbering, %digits-ordinal, or %duration is found\r
1536 \r
1537         boolean defaultNameFound = false;\r
1538         int n = ruleSets.length;\r
1539         defaultRuleSet = ruleSets[ruleSets.length - 1];\r
1540 \r
1541         while (--n >= 0) {\r
1542             String currentName = ruleSets[n].getName();\r
1543             if (currentName.equals("%spellout-numbering") || currentName.equals("%digits-ordinal") || currentName.equals("%duration")) {\r
1544                 defaultRuleSet = ruleSets[n];\r
1545                 defaultNameFound = true;\r
1546                 break;\r
1547             }\r
1548         }\r
1549 \r
1550         if ( !defaultNameFound ) {\r
1551             for (int i = ruleSets.length - 1; i >= 0; --i) {\r
1552                 if (!ruleSets[i].getName().startsWith("%%")) {\r
1553                     defaultRuleSet = ruleSets[i];\r
1554                     break;\r
1555                 }\r
1556             }\r
1557         }\r
1558 \r
1559         // finally, we can go back through the temporary descriptions\r
1560         // list and finish seting up the substructure (and we throw\r
1561         // away the temporary descriptions as we go)\r
1562         for (int i = 0; i < ruleSets.length; i++) {\r
1563             ruleSets[i].parseRules(ruleSetDescriptions[i], this);\r
1564             ruleSetDescriptions[i] = null;\r
1565         }\r
1566 \r
1567         // Now that the rules are initialized, the 'real' default rule\r
1568         // set can be adjusted by the localization data.\r
1569 \r
1570         // count the number of public rule sets\r
1571         // (public rule sets have names that begin with % instead of %%)\r
1572         int publicRuleSetCount = 0;\r
1573         for (int i = 0; i < ruleSets.length; i++) {\r
1574             if (!ruleSets[i].getName().startsWith("%%")) {\r
1575                 ++publicRuleSetCount;\r
1576             }\r
1577         }\r
1578 \r
1579         // prepare an array of the proper size and copy the names into it\r
1580         String[] publicRuleSetTemp = new String[publicRuleSetCount];\r
1581         publicRuleSetCount = 0;\r
1582         for (int i = ruleSets.length - 1; i >= 0; i--) {\r
1583             if (!ruleSets[i].getName().startsWith("%%")) {\r
1584                 publicRuleSetTemp[publicRuleSetCount++] = ruleSets[i].getName();\r
1585             }\r
1586         }\r
1587 \r
1588         if (publicRuleSetNames != null) {\r
1589             // confirm the names, if any aren't in the rules, that's an error\r
1590             // it is ok if the rules contain public rule sets that are not in this list\r
1591             loop: for (int i = 0; i < publicRuleSetNames.length; ++i) {\r
1592                 String name = publicRuleSetNames[i];\r
1593                 for (int j = 0; j < publicRuleSetTemp.length; ++j) {\r
1594                     if (name.equals(publicRuleSetTemp[j])) {\r
1595                         continue loop;\r
1596                     }\r
1597                 }\r
1598                 throw new IllegalArgumentException("did not find public rule set: " + name);\r
1599             }\r
1600 \r
1601             defaultRuleSet = findRuleSet(publicRuleSetNames[0]); // might be different\r
1602         } else {\r
1603             publicRuleSetNames = publicRuleSetTemp;\r
1604         }\r
1605     }\r
1606 \r
1607     /**\r
1608      * Take the localizations array and create a Map from the locale strings to\r
1609      * the localization arrays.\r
1610      */\r
1611     private void initLocalizations(String[][] localizations) {\r
1612         if (localizations != null) {\r
1613             publicRuleSetNames = localizations[0].clone();\r
1614 \r
1615             Map<String, String[]> m = new HashMap<String, String[]>();\r
1616             for (int i = 1; i < localizations.length; ++i) {\r
1617                 String[] data = localizations[i];\r
1618                 String loc = data[0];\r
1619                 String[] names = new String[data.length-1];\r
1620                 if (names.length != publicRuleSetNames.length) {\r
1621                     throw new IllegalArgumentException("public name length: " + publicRuleSetNames.length +\r
1622                                                        " != localized names[" + i + "] length: " + names.length);\r
1623                 }\r
1624                 System.arraycopy(data, 1, names, 0, names.length);\r
1625                 m.put(loc, names);\r
1626             }\r
1627 \r
1628             if (!m.isEmpty()) {\r
1629                 ruleSetDisplayNames = m;\r
1630             }\r
1631         }\r
1632     }\r
1633 \r
1634     /**\r
1635      * This function is used by init() to strip whitespace between rules (i.e.,\r
1636      * after semicolons).\r
1637      * @param description The formatter description\r
1638      * @return The description with all the whitespace that follows semicolons\r
1639      * taken out.\r
1640      */\r
1641     private StringBuilder stripWhitespace(String description) {\r
1642         // since we don't have a method that deletes characters (why?!!)\r
1643         // create a new StringBuffer to copy the text into\r
1644         StringBuilder result = new StringBuilder();\r
1645 \r
1646         // iterate through the characters...\r
1647         int start = 0;\r
1648         while (start != -1 && start < description.length()) {\r
1649             // seek to the first non-whitespace character...\r
1650             while (start < description.length()\r
1651                    && UCharacterProperty.isRuleWhiteSpace(description.charAt(start))) {\r
1652                 ++start;\r
1653             }\r
1654 \r
1655             //if the first non-whitespace character is semicolon, skip it and continue\r
1656             if (start < description.length() && description.charAt(start) == ';') {\r
1657                 start += 1;\r
1658                 continue;\r
1659             }\r
1660 \r
1661             // locate the next semicolon in the text and copy the text from\r
1662             // our current position up to that semicolon into the result\r
1663             int p;\r
1664             p = description.indexOf(';', start);\r
1665             if (p == -1) {\r
1666                 // or if we don't find a semicolon, just copy the rest of\r
1667                 // the string into the result\r
1668                 result.append(description.substring(start));\r
1669                 start = -1;\r
1670             }\r
1671             else if (p < description.length()) {\r
1672                 result.append(description.substring(start, p + 1));\r
1673                 start = p + 1;\r
1674             }\r
1675 \r
1676             // when we get here, we've seeked off the end of the sring, and\r
1677             // we terminate the loop (we continue until *start* is -1 rather\r
1678             // than until *p* is -1, because otherwise we'd miss the last\r
1679             // rule in the description)\r
1680             else {\r
1681                 start = -1;\r
1682             }\r
1683         }\r
1684         return result;\r
1685     }\r
1686 \r
1687 //    /**\r
1688 //     * This function is called ONLY DURING CONSTRUCTION to fill in the\r
1689 //     * defaultRuleSet variable once we've set up all the rule sets.\r
1690 //     * The default rule set is the last public rule set in the description.\r
1691 //     * (It's the last rather than the first so that a caller can append\r
1692 //     * text to the end of an existing formatter description to change its\r
1693 //     * behavior.)\r
1694 //     */\r
1695 //    private void initDefaultRuleSet() {\r
1696 //        // seek backward from the end of the list until we reach a rule set\r
1697 //        // whose name DOESN'T begin with %%.  That's the default rule set\r
1698 //        for (int i = ruleSets.length - 1; i >= 0; --i) {\r
1699 //            if (!ruleSets[i].getName().startsWith("%%")) {\r
1700 //                defaultRuleSet = ruleSets[i];\r
1701 //                return;\r
1702 //            }\r
1703 //        }\r
1704 //        defaultRuleSet = ruleSets[ruleSets.length - 1];\r
1705 //    }\r
1706 \r
1707     //-----------------------------------------------------------------------\r
1708     // formatting implementation\r
1709     //-----------------------------------------------------------------------\r
1710 \r
1711     /**\r
1712      * Bottleneck through which all the public format() methods\r
1713      * that take a double pass. By the time we get here, we know\r
1714      * which rule set we're using to do the formatting.\r
1715      * @param number The number to format\r
1716      * @param ruleSet The rule set to use to format the number\r
1717      * @return The text that resulted from formatting the number\r
1718      */\r
1719     private String format(double number, NFRuleSet ruleSet) {\r
1720         // all API format() routines that take a double vector through\r
1721         // here.  Create an empty string buffer where the result will\r
1722         // be built, and pass it to the rule set (along with an insertion\r
1723         // position of 0 and the number being formatted) to the rule set\r
1724         // for formatting\r
1725         StringBuffer result = new StringBuffer();\r
1726         ruleSet.format(number, result, 0);\r
1727         postProcess(result, ruleSet);\r
1728         return result.toString();\r
1729     }\r
1730 \r
1731     /**\r
1732      * Bottleneck through which all the public format() methods\r
1733      * that take a long pass. By the time we get here, we know\r
1734      * which rule set we're using to do the formatting.\r
1735      * @param number The number to format\r
1736      * @param ruleSet The rule set to use to format the number\r
1737      * @return The text that resulted from formatting the number\r
1738      */\r
1739     private String format(long number, NFRuleSet ruleSet) {\r
1740         // all API format() routines that take a double vector through\r
1741         // here.  We have these two identical functions-- one taking a\r
1742         // double and one taking a long-- the couple digits of precision\r
1743         // that long has but double doesn't (both types are 8 bytes long,\r
1744         // but double has to borrow some of the mantissa bits to hold\r
1745         // the exponent).\r
1746         // Create an empty string buffer where the result will\r
1747         // be built, and pass it to the rule set (along with an insertion\r
1748         // position of 0 and the number being formatted) to the rule set\r
1749         // for formatting\r
1750         StringBuffer result = new StringBuffer();\r
1751         ruleSet.format(number, result, 0);\r
1752         postProcess(result, ruleSet);\r
1753         return result.toString();\r
1754     }\r
1755 \r
1756     /**\r
1757      * Post-process the rules if we have a post-processor.\r
1758      */\r
1759     private void postProcess(StringBuffer result, NFRuleSet ruleSet) {\r
1760         if (postProcessRules != null) {\r
1761             if (postProcessor == null) {\r
1762                 int ix = postProcessRules.indexOf(";");\r
1763                 if (ix == -1) {\r
1764                     ix = postProcessRules.length();\r
1765                 }\r
1766                 String ppClassName = postProcessRules.substring(0, ix).trim();\r
1767                 try {\r
1768                     Class<?> cls = Class.forName(ppClassName);\r
1769                     postProcessor = (RBNFPostProcessor)cls.newInstance();\r
1770                     postProcessor.init(this, postProcessRules);\r
1771                 }\r
1772                 catch (Exception e) {\r
1773                     // if debug, print it out\r
1774                     if (DEBUG) System.out.println("could not locate " + ppClassName + ", error " +\r
1775                                        e.getClass().getName() + ", " + e.getMessage());\r
1776                     postProcessor = null;\r
1777                     postProcessRules = null; // don't try again\r
1778                     return;\r
1779                 }\r
1780             }\r
1781 \r
1782             postProcessor.process(result, ruleSet);\r
1783         }\r
1784     }\r
1785 \r
1786     /**\r
1787      * Returns the named rule set.  Throws an IllegalArgumentException\r
1788      * if this formatter doesn't have a rule set with that name.\r
1789      * @param name The name of the desired rule set\r
1790      * @return The rule set with that name\r
1791      */\r
1792     NFRuleSet findRuleSet(String name) throws IllegalArgumentException {\r
1793         for (int i = 0; i < ruleSets.length; i++) {\r
1794             if (ruleSets[i].getName().equals(name)) {\r
1795                 return ruleSets[i];\r
1796             }\r
1797         }\r
1798         throw new IllegalArgumentException("No rule set named " + name);\r
1799     }\r
1800 }\r