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