]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/util/LocaleData.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / util / LocaleData.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2004-2009, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6 */\r
7 package com.ibm.icu.util;\r
8 \r
9 import java.util.MissingResourceException;\r
10 \r
11 import com.ibm.icu.impl.ICUResourceBundle;\r
12 import com.ibm.icu.text.UnicodeSet;\r
13 \r
14 /**\r
15  * A class for accessing miscelleneous data in the locale bundles\r
16  * @author ram\r
17  * @stable ICU 2.8\r
18  */\r
19 public final class LocaleData {\r
20     \r
21     private static final String EXEMPLAR_CHARS      = "ExemplarCharacters";\r
22     private static final String MEASUREMENT_SYSTEM  = "MeasurementSystem";\r
23     private static final String PAPER_SIZE          = "PaperSize";\r
24     private static final String LOCALE_DISPLAY_PATTERN  = "localeDisplayPattern";\r
25     private static final String PATTERN             = "pattern";\r
26     private static final String SEPARATOR           = "separator";\r
27     private boolean noSubstitute;\r
28     private ICUResourceBundle bundle;\r
29 \r
30     /**\r
31      * EXType for {@link #getExemplarSet(int, int)}.\r
32      * @stable ICU 3.4\r
33      */\r
34     public static final int ES_STANDARD = 0;\r
35 \r
36     /**\r
37      * EXType for {@link #getExemplarSet(int, int)}.\r
38      * @stable ICU 3.4\r
39      */\r
40     public static final int ES_AUXILIARY = 1;\r
41 \r
42     /**\r
43      * Count of EXTypes for {@link #getExemplarSet(int, int)}.\r
44      * @stable ICU 3.4\r
45      */\r
46     public static final int ES_COUNT = 2;\r
47     \r
48     /**\r
49      * Delimiter type for {@link #getDelimiter(int)}.\r
50      * @stable ICU 3.4\r
51      */\r
52     public static final int QUOTATION_START = 0;\r
53 \r
54     /**\r
55      * Delimiter type for {@link #getDelimiter(int)}.\r
56      * @stable ICU 3.4\r
57      */\r
58     public static final int QUOTATION_END = 1;\r
59 \r
60     /**\r
61      * Delimiter type for {@link #getDelimiter(int)}.\r
62      * @stable ICU 3.4\r
63      */\r
64     public static final int ALT_QUOTATION_START = 2;\r
65 \r
66     /**\r
67      * Delimiter type for {@link #getDelimiter(int)}.\r
68      * @stable ICU 3.4\r
69      */\r
70     public static final int ALT_QUOTATION_END = 3;\r
71 \r
72     /**\r
73      * Count of delimiter types for {@link #getDelimiter(int)}.\r
74      * @stable ICU 3.4\r
75      */\r
76     public static final int DELIMITER_COUNT = 4;\r
77 \r
78     // private constructor to prevent default construction\r
79     ///CLOVER:OFF\r
80     private LocaleData(){}\r
81     ///CLOVER:ON\r
82 \r
83     /**\r
84      * Returns the set of exemplar characters for a locale.\r
85      *\r
86      * @param locale    Locale for which the exemplar character set\r
87      *                  is to be retrieved.\r
88      * @param options   Bitmask for options to apply to the exemplar pattern.\r
89      *                  Specify zero to retrieve the exemplar set as it is\r
90      *                  defined in the locale data.  Specify\r
91      *                  UnicodeSet.CASE to retrieve a case-folded exemplar\r
92      *                  set.  See {@link UnicodeSet#applyPattern(String,\r
93      *                  int)} for a complete list of valid options.  The\r
94      *                  IGNORE_SPACE bit is always set, regardless of the\r
95      *                  value of 'options'.\r
96      * @return          The set of exemplar characters for the given locale.\r
97      * @stable ICU 3.0\r
98      */\r
99     public static UnicodeSet getExemplarSet(ULocale locale, int options) {\r
100         ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);\r
101         String pattern = bundle.getString(EXEMPLAR_CHARS);\r
102         return new UnicodeSet(pattern, UnicodeSet.IGNORE_SPACE | options);\r
103     }\r
104     \r
105     /**\r
106      * Returns the set of exemplar characters for a locale.\r
107      *\r
108      * @param options   Bitmask for options to apply to the exemplar pattern.\r
109      *                  Specify zero to retrieve the exemplar set as it is\r
110      *                  defined in the locale data.  Specify\r
111      *                  UnicodeSet.CASE to retrieve a case-folded exemplar\r
112      *                  set.  See {@link UnicodeSet#applyPattern(String,\r
113      *                  int)} for a complete list of valid options.  The\r
114      *                  IGNORE_SPACE bit is always set, regardless of the\r
115      *                  value of 'options'.\r
116      * @param extype      The type of exemplar set to be retrieved,\r
117      *                  ES_STANDARD or ES_AUXILIARY\r
118      * @return          The set of exemplar characters for the given locale.\r
119      * @stable ICU 3.4\r
120      */\r
121     public UnicodeSet getExemplarSet(int options, int extype) {\r
122         String [] exemplarSetTypes = { "ExemplarCharacters", "AuxExemplarCharacters" };\r
123         try{\r
124             ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get(exemplarSetTypes[extype]);\r
125     \r
126             if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )\r
127                return null;\r
128     \r
129             return new UnicodeSet(stringBundle.getString(), UnicodeSet.IGNORE_SPACE | options);\r
130         }catch(MissingResourceException ex){\r
131             if(extype==LocaleData.ES_AUXILIARY){\r
132                 return new UnicodeSet();\r
133             }\r
134             throw ex;\r
135         }\r
136     }\r
137 \r
138     /**\r
139      * Gets the LocaleData object associated with the ULocale specified in locale\r
140      *\r
141      * @param locale    Locale with thich the locale data object is associated.\r
142      * @return          A locale data object.\r
143      * @stable ICU 3.4\r
144      */\r
145     public static final LocaleData getInstance(ULocale locale) {\r
146        LocaleData ld = new LocaleData();\r
147        ld.bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale );\r
148        ld.noSubstitute = false;\r
149        return ld;\r
150     }\r
151 \r
152     /**\r
153      * Gets the LocaleData object associated with the default locale\r
154      *\r
155      * @return          A locale data object.\r
156      * @stable ICU 3.4\r
157      */\r
158     public static final LocaleData getInstance() {\r
159        return LocaleData.getInstance(ULocale.getDefault());\r
160     }\r
161 \r
162     /**\r
163      * Sets the "no substitute" behavior of this locale data object.\r
164      *\r
165      * @param setting   Value for the no substitute behavior.  If TRUE,\r
166      *                  methods of this locale data object will return\r
167      *                  an error when no data is available for that method,\r
168      *                  given the locale ID supplied to the constructor.\r
169      * @stable ICU 3.4\r
170      */\r
171     public void setNoSubstitute(boolean setting) {\r
172        noSubstitute = setting;\r
173     }\r
174 \r
175     /**\r
176      * Gets the "no substitute" behavior of this locale data object.\r
177      *\r
178      * @return          Value for the no substitute behavior.  If TRUE,\r
179      *                  methods of this locale data object will return\r
180      *                  an error when no data is available for that method,\r
181      *                  given the locale ID supplied to the constructor.\r
182      * @stable ICU 3.4\r
183      */\r
184     public boolean getNoSubstitute() {\r
185        return noSubstitute;\r
186     }\r
187 \r
188     /**\r
189      * Retrieves a delimiter string from the locale data.\r
190      *\r
191      * @param type      The type of delimiter string desired.  Currently,\r
192      *                  the valid choices are QUOTATION_START, QUOTATION_END,\r
193      *                  ALT_QUOTATION_START, or ALT_QUOTATION_END.\r
194      * @return          The desired delimiter string.\r
195      * @stable ICU 3.4\r
196      */\r
197     public String getDelimiter(int type) {\r
198         String [] delimiterTypes = { "quotationStart", \r
199                                      "quotationEnd", \r
200                                      "alternateQuotationStart", \r
201                                      "alternateQuotationEnd" };\r
202 \r
203         ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get("delimiters").get(delimiterTypes[type]);\r
204 \r
205         if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )\r
206            return null;\r
207 \r
208         return new String (stringBundle.getString());\r
209     }\r
210 \r
211     /**\r
212      * Enumeration for representing the measurement systems.\r
213      * @stable ICU 2.8\r
214      */\r
215     public static final class MeasurementSystem{\r
216         /** \r
217          * Measurement system specified by Le Système International d'Unités (SI)\r
218          * otherwise known as Metric system. \r
219          * @stable ICU 2.8\r
220          */\r
221         public static final MeasurementSystem SI = new MeasurementSystem(0);\r
222  \r
223         /** \r
224          * Measurement system followed in the United States of America. \r
225          * @stable ICU 2.8\r
226          */ \r
227         public static final MeasurementSystem US = new MeasurementSystem(1);\r
228     \r
229         private int systemID;\r
230         private MeasurementSystem(int id){\r
231             systemID = id;\r
232         }\r
233 \r
234         private boolean equals(int id){\r
235             return systemID == id;\r
236         }\r
237     }\r
238    \r
239     /**\r
240      * Returns the measurement system used in the locale specified by the locale.\r
241      *\r
242      * @param locale      The locale for which the measurement system to be retrieved.\r
243      * @return MeasurementSystem the measurement system used in the locale.\r
244      * @stable ICU 3.0\r
245      */\r
246     public static final MeasurementSystem getMeasurementSystem(ULocale locale){\r
247         UResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);\r
248         UResourceBundle sysBundle = bundle.get(MEASUREMENT_SYSTEM);\r
249         \r
250         int system = sysBundle.getInt();\r
251         if(MeasurementSystem.US.equals(system)){\r
252             return MeasurementSystem.US;\r
253         }\r
254         if(MeasurementSystem.SI.equals(system)){\r
255             return MeasurementSystem.SI;\r
256         }\r
257         // return null if the object is null or is not an instance\r
258         // of integer indicating an error\r
259         return null;\r
260     }\r
261     \r
262     /**\r
263      * A class that represents the size of letter head \r
264      * used in the country\r
265      * @stable ICU 2.8\r
266      */\r
267     public static final class PaperSize{\r
268         private int height;\r
269         private int width;\r
270         \r
271         private PaperSize(int h, int w){\r
272             height = h;\r
273             width = w;\r
274         }\r
275         /** \r
276          * Retruns the height of the paper\r
277          * @return the height \r
278          * @stable ICU 2.8\r
279          */\r
280         public int getHeight(){\r
281             return height;\r
282         }\r
283         /**\r
284          * Returns the width of hte paper\r
285          * @return the width\r
286          * @stable ICU 2.8\r
287          */\r
288         public int getWidth(){\r
289             return width;\r
290         }\r
291     }\r
292     \r
293     /**\r
294      * Returns the size of paper used in the locale. The paper sizes returned are always in \r
295      * <em> milli-meters<em>.\r
296      * @param locale The locale for which the measurement system to be retrieved. \r
297      * @return The paper size used in the locale\r
298      * @stable ICU 3.0\r
299      */\r
300     public static final PaperSize getPaperSize(ULocale locale){\r
301         UResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);\r
302         UResourceBundle obj = bundle.get(PAPER_SIZE);\r
303         int[] size = obj.getIntVector();\r
304         return new PaperSize(size[0], size[1]);\r
305     }\r
306     \r
307     /**\r
308      * Returns LocaleDisplayPattern for this locale, e.g., {0}({1})\r
309      * @return locale display pattern as a String.\r
310      * @draft ICU 4.2\r
311      * @provisional This API might change or be removed in a future release.\r
312      */ \r
313     public String getLocaleDisplayPattern() {\r
314       if (bundle == null) {\r
315         bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(\r
316                         ICUResourceBundle.ICU_BASE_NAME, ULocale.getDefault());\r
317       }\r
318       ICUResourceBundle locDispBundle = (ICUResourceBundle) bundle.get(LOCALE_DISPLAY_PATTERN);\r
319       String localeDisplayPattern = locDispBundle.getStringWithFallback(PATTERN);\r
320       return localeDisplayPattern;\r
321     }\r
322     \r
323     /**\r
324      * Returns LocaleDisplaySeparator for this locale.\r
325      * @return locale display separator as a char.\r
326      * @draft ICU 4.2\r
327      * @provisional This API might change or be removed in a future release.\r
328      */ \r
329     public String getLocaleSeparator() {\r
330       if (bundle == null) {\r
331         bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(\r
332                         ICUResourceBundle.ICU_BASE_NAME, ULocale.getDefault());\r
333       }\r
334       ICUResourceBundle locDispBundle = (ICUResourceBundle) bundle.get(LOCALE_DISPLAY_PATTERN);\r
335       String  localeSeparator = locDispBundle.getStringWithFallback(SEPARATOR);\r
336       return localeSeparator;\r
337     }\r
338 }\r