• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  **********************************************************************
3  * Copyright (c) 2002-2010, International Business Machines
4  * Corporation and others.  All Rights Reserved.
5  **********************************************************************
6  * Author: John Emmons
7  **********************************************************************
8  */
9 package org.unicode.cldr.posix;
10 
11 import java.io.PrintWriter;
12 import java.util.Iterator;
13 import java.util.Set;
14 
15 import org.unicode.cldr.util.CLDRFile;
16 import org.unicode.cldr.util.SupplementalDataInfo;
17 
18 import com.ibm.icu.util.ULocale;
19 
20 public class POSIX_LCMonetary {
21     String int_curr_symbol;
22     String currency_symbol;
23     String mon_decimal_point;
24     String mon_thousands_sep;
25     String mon_grouping;
26     String positive_sign;
27     String negative_sign;
28     String frac_digits;
29     String p_cs_precedes;
30     String p_sep_by_space;
31     String p_sign_posn;
32     String n_cs_precedes;
33     String n_sep_by_space;
34     String n_sign_posn;
35     String int_frac_digits;
36     String int_p_cs_precedes;
37     String int_p_sep_by_space;
38     String int_p_sign_posn;
39     String int_n_cs_precedes;
40     String int_n_sep_by_space;
41     String int_n_sign_posn;
42     String numsys;
43     private static final int POSITIVE = 0;
44     private static final int NEGATIVE = 1;
45 
POSIX_LCMonetary(CLDRFile doc, SupplementalDataInfo supp, POSIXVariant variant)46     public POSIX_LCMonetary(CLDRFile doc, SupplementalDataInfo supp, POSIXVariant variant) {
47 
48         String grouping_pattern = doc
49             .getWinningValue("//ldml/numbers/currencyFormats/currencyFormatLength/currencyFormat[@type='standard']/pattern[@type='standard']");
50 
51         String[] monetary_formats = new String[2];
52         if (grouping_pattern.indexOf(";") > 0)
53             monetary_formats = grouping_pattern.split(";", 2);
54         else {
55             monetary_formats[POSITIVE] = grouping_pattern;
56             monetary_formats[NEGATIVE] = "-" + grouping_pattern;
57         }
58 
59         mon_grouping = POSIXUtilities.POSIXGrouping(monetary_formats[POSITIVE]);
60 
61         String territory = ULocale.getCountry(doc.getLocaleID());
62 
63         if (variant.currency.equals("default")) {
64             Set<SupplementalDataInfo.CurrencyDateInfo> ci = supp.getCurrencyDateInfo(territory);
65             Iterator<SupplementalDataInfo.CurrencyDateInfo> it = ci.iterator();
66             SupplementalDataInfo.CurrencyDateInfo currentCI = null;
67             while (it.hasNext()) {
68                 currentCI = it.next();
69                 if (currentCI.isLegalTender()
70                     && currentCI.getEnd().equals(SupplementalDataInfo.CurrencyDateInfo.END_OF_TIME)) {
71                     break;
72                 } else {
73                     currentCI = null;
74                 }
75             }
76 
77             if (currentCI == null) {
78                 int_curr_symbol = "XXX";
79             } else {
80                 int_curr_symbol = currentCI.getCurrency();
81             }
82         } else
83             int_curr_symbol = variant.currency;
84 
85         String tmp_currency_symbol;
86         String symbolPath = "//ldml/numbers/currencies/currency[@type=\"" + int_curr_symbol + "\"]/symbol";
87         tmp_currency_symbol = doc.getWinningValue(symbolPath);
88 
89         // Check to see if currency symbol has a choice pattern
90 
91         if (tmp_currency_symbol.indexOf("|") >= 0) {
92             String[] choices = tmp_currency_symbol.split("\\u007c");
93             for (int i = choices.length - 1; i >= 0; i--) {
94                 String[] numvalue = choices[i].split("[<\\u2264]", 2);
95                 Float num = Float.valueOf(numvalue[0]);
96                 Float ten = new Float(10);
97                 if (num.compareTo(ten) <= 0 || i == 0) {
98                     currency_symbol = POSIXUtilities.POSIXCharName(numvalue[1]);
99                     i = 0;
100                 }
101             }
102         } else
103             currency_symbol = POSIXUtilities.POSIXCharName(tmp_currency_symbol);
104 
105         numsys = doc.getWinningValue("//ldml/numbers/defaultNumberingSystem");
106         mon_decimal_point = doc.getWinningValue("//ldml/numbers/currencies/currency[@type='" + int_curr_symbol
107             + "']/decimal");
108         if (mon_decimal_point == null)
109             mon_decimal_point = doc.getWinningValue("//ldml/numbers/symbols[@numberSystem='" + numsys
110                 + "']/currencyDecimal");
111         if (mon_decimal_point == null)
112             mon_decimal_point = doc.getWinningValue("//ldml/numbers/symbols[@numberSystem='" + numsys + "']/decimal");
113 
114         mon_decimal_point = POSIXUtilities.POSIXCharName(mon_decimal_point);
115 
116         mon_thousands_sep = doc.getWinningValue("//ldml/numbers/currencies/currency[@type='" + int_curr_symbol
117             + "']/group");
118         if (mon_thousands_sep == null)
119             mon_thousands_sep = doc.getWinningValue("//ldml/numbers/symbols[@numberSystem='" + numsys
120                 + "']/currencyGroup");
121         if (mon_thousands_sep == null)
122             mon_thousands_sep = doc.getWinningValue("//ldml/numbers/symbols[@numberSystem='" + numsys + "']/group");
123 
124         mon_thousands_sep = POSIXUtilities.POSIXCharName(mon_thousands_sep);
125 
126         int fracDigits = supp.getCurrencyNumberInfo(int_curr_symbol).getDigits();
127         frac_digits = Integer.toString(fracDigits);
128 
129         int_frac_digits = frac_digits;
130 
131         if (monetary_formats[POSITIVE].indexOf('+') >= 0) {
132             positive_sign = POSIXUtilities.POSIXCharName(doc.getWinningValue("//ldml/numbers/symbols[@numberSystem='"
133                 + numsys + "']/plusSign"));
134         } else
135             positive_sign = "";
136 
137         if (monetary_formats[NEGATIVE].indexOf('-') >= 0) {
138             negative_sign = POSIXUtilities.POSIXCharName(doc.getWinningValue("//ldml/numbers/symbols[@numberSystem='"
139                 + numsys + "']/minusSign"));
140         } else
141             negative_sign = POSIXUtilities.POSIXCharName("-");
142 
143         // Parse Positive Monetary Formats
144         int currency_symbol_position = monetary_formats[POSITIVE].indexOf('\u00a4');
145         int decimal_symbol_position = monetary_formats[POSITIVE].indexOf('.');
146         int sign_position = monetary_formats[POSITIVE].indexOf('+');
147         int leftparen_position = monetary_formats[POSITIVE].lastIndexOf('(', decimal_symbol_position);
148         int rightparen_position = monetary_formats[POSITIVE].indexOf(')', decimal_symbol_position);
149         int space_position = monetary_formats[POSITIVE].indexOf(' ');
150 
151         if (space_position == -1)
152             space_position = monetary_formats[POSITIVE].indexOf('\u00a0');
153 
154         if (currency_symbol_position >= 0)
155             if (currency_symbol_position > decimal_symbol_position)
156                 p_cs_precedes = "0";
157             else
158                 p_cs_precedes = "1";
159         else
160             p_cs_precedes = "-1";
161 
162         int_p_cs_precedes = p_cs_precedes;
163 
164         if (positive_sign == "") {
165             if ((leftparen_position < decimal_symbol_position) &&
166                 (rightparen_position > decimal_symbol_position))
167                 p_sign_posn = "0";
168             else
169                 p_sign_posn = "1";
170         } else if (sign_position < currency_symbol_position) {
171             if (sign_position < decimal_symbol_position)
172                 p_sign_posn = "1";
173             else
174                 p_sign_posn = "3";
175         } else // sign_position >= currency_symbol_position
176         {
177             if (sign_position > decimal_symbol_position)
178                 p_sign_posn = "2";
179             else
180                 p_sign_posn = "4";
181         }
182 
183         int_p_sign_posn = p_sign_posn;
184 
185         p_sep_by_space = "0";
186         boolean currency_and_sign_are_adjacent = false;
187 
188         if (((currency_symbol_position < decimal_symbol_position) && (sign_position < decimal_symbol_position)) ||
189             ((currency_symbol_position > decimal_symbol_position) && (sign_position > decimal_symbol_position)))
190             currency_and_sign_are_adjacent = true;
191 
192         if (currency_and_sign_are_adjacent && (sign_position >= 0)) {
193             if (POSIXUtilities.isBetween(currency_symbol_position, space_position, decimal_symbol_position) &&
194                 POSIXUtilities.isBetween(sign_position, space_position, decimal_symbol_position))
195                 p_sep_by_space = "1";
196             if (POSIXUtilities.isBetween(currency_symbol_position, space_position, sign_position))
197                 p_sep_by_space = "2";
198         } else {
199             if ((currency_symbol_position > decimal_symbol_position && space_position == currency_symbol_position - 1)
200                 ||
201                 (currency_symbol_position < decimal_symbol_position && space_position == currency_symbol_position + 1))
202                 p_sep_by_space = "1";
203             if ((sign_position > decimal_symbol_position && space_position == sign_position - 1) ||
204                 (sign_position < decimal_symbol_position && space_position == sign_position + 1))
205                 p_sep_by_space = "2";
206         }
207 
208         int_p_sep_by_space = p_sep_by_space;
209 
210         // Parse Negative Monetary Formats
211         currency_symbol_position = monetary_formats[NEGATIVE].indexOf('\u00a4');
212         decimal_symbol_position = monetary_formats[NEGATIVE].indexOf('.');
213         sign_position = monetary_formats[NEGATIVE].indexOf('-');
214         leftparen_position = monetary_formats[NEGATIVE].lastIndexOf('(', decimal_symbol_position);
215         rightparen_position = monetary_formats[NEGATIVE].indexOf(')', decimal_symbol_position);
216         space_position = monetary_formats[NEGATIVE].indexOf(' ');
217 
218         if (space_position == -1)
219             space_position = monetary_formats[NEGATIVE].indexOf('\u00a0');
220 
221         if (currency_symbol_position >= 0)
222             if (currency_symbol_position > decimal_symbol_position)
223                 n_cs_precedes = "0";
224             else
225                 n_cs_precedes = "1";
226         else
227             n_cs_precedes = "-1";
228 
229         int_n_cs_precedes = n_cs_precedes;
230 
231         if (negative_sign == "") {
232             if ((leftparen_position < decimal_symbol_position) &&
233                 (rightparen_position > decimal_symbol_position))
234                 n_sign_posn = "0";
235             else
236                 n_sign_posn = "1";
237         } else if (sign_position < currency_symbol_position) {
238             if (sign_position < decimal_symbol_position)
239                 n_sign_posn = "1";
240             else
241                 n_sign_posn = "3";
242         } else // sign_position >= currency_symbol_position
243         {
244             if (sign_position > decimal_symbol_position)
245                 n_sign_posn = "2";
246             else
247                 n_sign_posn = "4";
248         }
249 
250         int_n_sign_posn = n_sign_posn;
251 
252         n_sep_by_space = "0";
253         currency_and_sign_are_adjacent = false;
254 
255         if (((currency_symbol_position < decimal_symbol_position) && (sign_position < decimal_symbol_position)) ||
256             ((currency_symbol_position > decimal_symbol_position) && (sign_position > decimal_symbol_position)))
257             currency_and_sign_are_adjacent = true;
258 
259         if (currency_and_sign_are_adjacent && (sign_position >= 0)) {
260             if (POSIXUtilities.isBetween(currency_symbol_position, space_position, decimal_symbol_position) &&
261                 POSIXUtilities.isBetween(sign_position, space_position, decimal_symbol_position))
262                 n_sep_by_space = "1";
263             if (POSIXUtilities.isBetween(currency_symbol_position, space_position, sign_position))
264                 n_sep_by_space = "2";
265         } else {
266             if ((currency_symbol_position > decimal_symbol_position && space_position == currency_symbol_position - 1)
267                 ||
268                 (currency_symbol_position < decimal_symbol_position && space_position == currency_symbol_position + 1))
269                 n_sep_by_space = "1";
270             if ((sign_position > decimal_symbol_position && space_position == sign_position - 1) ||
271                 (sign_position < decimal_symbol_position && space_position == sign_position + 1))
272                 n_sep_by_space = "2";
273         }
274 
275         int_n_sep_by_space = n_sep_by_space;
276 
277     } // end POSIX_LCMonetary( doc, supp );
278 
write(PrintWriter out)279     public void write(PrintWriter out) {
280 
281         out.println("*************");
282         out.println("LC_MONETARY");
283         out.println("*************");
284         out.println();
285 
286         out.print("int_curr_symbol       \"");
287         out.println(POSIXUtilities.POSIXCharName(int_curr_symbol + " ") + "\"");
288         out.println("currency_symbol       \"" + currency_symbol + "\"");
289         out.println("mon_decimal_point     \"" + mon_decimal_point + "\"");
290         out.println("mon_thousands_sep     \"" + mon_thousands_sep + "\"");
291         out.println("mon_grouping          " + mon_grouping);
292         out.println("positive_sign         \"" + positive_sign + "\"");
293         out.println("negative_sign         \"" + negative_sign + "\"");
294         out.println("int_frac_digits       " + int_frac_digits);
295         out.println("frac_digits           " + frac_digits);
296         out.println("p_cs_precedes         " + p_cs_precedes);
297         out.println("p_sep_by_space        " + p_sep_by_space);
298         out.println("n_cs_precedes         " + n_cs_precedes);
299         out.println("n_sep_by_space        " + n_sep_by_space);
300         out.println("p_sign_posn           " + p_sign_posn);
301         out.println("n_sign_posn           " + n_sign_posn);
302         out.println("int_p_cs_precedes     " + int_p_cs_precedes);
303         out.println("int_p_sep_by_space    " + int_p_sep_by_space);
304         out.println("int_n_cs_precedes     " + int_n_cs_precedes);
305         out.println("int_n_sep_by_space    " + int_n_sep_by_space);
306         out.println("int_p_sign_posn       " + int_p_sign_posn);
307         out.println("int_n_sign_posn       " + int_n_sign_posn);
308 
309         out.println();
310         out.println("END LC_MONETARY");
311         out.println();
312         out.println();
313 
314     }
315 }
316