2 **************************************************************************************
\r
3 * Copyright (C) 2009-2010, Google, Inc.; International Business Machines Corporation *
\r
4 * and others. All Rights Reserved. *
\r
5 **************************************************************************************
\r
7 package com.ibm.icu.util;
\r
9 import java.util.MissingResourceException;
\r
11 import com.ibm.icu.impl.ICUResourceBundle;
\r
12 import com.ibm.icu.text.UnicodeSet;
\r
15 * A class for accessing miscelleneous data in the locale bundles
\r
19 public final class LocaleData {
\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
32 * EXType for {@link #getExemplarSet(int, int)}.
\r
35 public static final int ES_STANDARD = 0;
\r
38 * EXType for {@link #getExemplarSet(int, int)}.
\r
41 public static final int ES_AUXILIARY = 1;
\r
44 * Count of EXTypes for {@link #getExemplarSet(int, int)}.
\r
47 public static final int ES_COUNT = 2;
\r
50 * Delimiter type for {@link #getDelimiter(int)}.
\r
53 public static final int QUOTATION_START = 0;
\r
56 * Delimiter type for {@link #getDelimiter(int)}.
\r
59 public static final int QUOTATION_END = 1;
\r
62 * Delimiter type for {@link #getDelimiter(int)}.
\r
65 public static final int ALT_QUOTATION_START = 2;
\r
68 * Delimiter type for {@link #getDelimiter(int)}.
\r
71 public static final int ALT_QUOTATION_END = 3;
\r
74 * Count of delimiter types for {@link #getDelimiter(int)}.
\r
77 public static final int DELIMITER_COUNT = 4;
\r
79 // private constructor to prevent default construction
\r
81 private LocaleData(){}
\r
85 * Returns the set of exemplar characters for a locale.
\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
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
107 * Returns the set of exemplar characters for a locale.
\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
122 public UnicodeSet getExemplarSet(int options, int extype) {
\r
123 String [] exemplarSetTypes = { "ExemplarCharacters", "AuxExemplarCharacters" };
\r
125 ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get(exemplarSetTypes[extype]);
\r
127 if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )
\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
140 * Gets the LocaleData object associated with the ULocale specified in locale
\r
142 * @param locale Locale with thich the locale data object is associated.
\r
143 * @return A locale data object.
\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
155 * Gets the LocaleData object associated with the default locale
\r
157 * @return A locale data object.
\r
160 public static final LocaleData getInstance() {
\r
161 return LocaleData.getInstance(ULocale.getDefault());
\r
165 * Sets the "no substitute" behavior of this locale data object.
\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
173 public void setNoSubstitute(boolean setting) {
\r
174 noSubstitute = setting;
\r
178 * Gets the "no substitute" behavior of this locale data object.
\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
186 public boolean getNoSubstitute() {
\r
187 return noSubstitute;
\r
191 * Retrieves a delimiter string from the locale data.
\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
199 public String getDelimiter(int type) {
\r
200 String [] delimiterTypes = { "quotationStart",
\r
202 "alternateQuotationStart",
\r
203 "alternateQuotationEnd" };
\r
205 ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get("delimiters").get(delimiterTypes[type]);
\r
207 if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )
\r
210 return new String (stringBundle.getString());
\r
214 * Enumeration for representing the measurement systems.
\r
217 public static final class MeasurementSystem{
\r
219 * Measurement system specified by Le Système International d'Unités (SI)
\r
220 * otherwise known as Metric system.
\r
223 public static final MeasurementSystem SI = new MeasurementSystem(0);
\r
226 * Measurement system followed in the United States of America.
\r
229 public static final MeasurementSystem US = new MeasurementSystem(1);
\r
231 private int systemID;
\r
232 private MeasurementSystem(int id){
\r
236 private boolean equals(int id){
\r
237 return systemID == id;
\r
242 * Returns the measurement system used in the locale specified by the locale.
\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
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
252 int system = sysBundle.getInt();
\r
253 if(MeasurementSystem.US.equals(system)){
\r
254 return MeasurementSystem.US;
\r
256 if(MeasurementSystem.SI.equals(system)){
\r
257 return MeasurementSystem.SI;
\r
259 // return null if the object is null or is not an instance
\r
260 // of integer indicating an error
\r
265 * A class that represents the size of letter head
\r
266 * used in the country
\r
269 public static final class PaperSize{
\r
270 private int height;
\r
273 private PaperSize(int h, int w){
\r
278 * Retruns the height of the paper
\r
279 * @return the height
\r
282 public int getHeight(){
\r
286 * Returns the width of the paper
\r
287 * @return the width
\r
290 public int getWidth(){
\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
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
310 * Returns LocaleDisplayPattern for this locale, e.g., {0}({1})
\r
311 * @return locale display pattern as a String.
\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
321 * Returns LocaleDisplaySeparator for this locale.
\r
322 * @return locale display separator as a char.
\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
331 private static VersionInfo gCLDRVersion = null;
\r
334 * Returns the current CLDR version
\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
345 return gCLDRVersion;
\r