2 *******************************************************************************
\r
3 * Copyright (C) 2004-2009, International Business Machines Corporation and *
\r
4 * 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
31 * EXType for {@link #getExemplarSet(int, int)}.
\r
34 public static final int ES_STANDARD = 0;
\r
37 * EXType for {@link #getExemplarSet(int, int)}.
\r
40 public static final int ES_AUXILIARY = 1;
\r
43 * Count of EXTypes for {@link #getExemplarSet(int, int)}.
\r
46 public static final int ES_COUNT = 2;
\r
49 * Delimiter type for {@link #getDelimiter(int)}.
\r
52 public static final int QUOTATION_START = 0;
\r
55 * Delimiter type for {@link #getDelimiter(int)}.
\r
58 public static final int QUOTATION_END = 1;
\r
61 * Delimiter type for {@link #getDelimiter(int)}.
\r
64 public static final int ALT_QUOTATION_START = 2;
\r
67 * Delimiter type for {@link #getDelimiter(int)}.
\r
70 public static final int ALT_QUOTATION_END = 3;
\r
73 * Count of delimiter types for {@link #getDelimiter(int)}.
\r
76 public static final int DELIMITER_COUNT = 4;
\r
78 // private constructor to prevent default construction
\r
80 private LocaleData(){}
\r
84 * Returns the set of exemplar characters for a locale.
\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
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
106 * Returns the set of exemplar characters for a locale.
\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
121 public UnicodeSet getExemplarSet(int options, int extype) {
\r
122 String [] exemplarSetTypes = { "ExemplarCharacters", "AuxExemplarCharacters" };
\r
124 ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get(exemplarSetTypes[extype]);
\r
126 if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )
\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
139 * Gets the LocaleData object associated with the ULocale specified in locale
\r
141 * @param locale Locale with thich the locale data object is associated.
\r
142 * @return A locale data object.
\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
153 * Gets the LocaleData object associated with the default locale
\r
155 * @return A locale data object.
\r
158 public static final LocaleData getInstance() {
\r
159 return LocaleData.getInstance(ULocale.getDefault());
\r
163 * Sets the "no substitute" behavior of this locale data object.
\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
171 public void setNoSubstitute(boolean setting) {
\r
172 noSubstitute = setting;
\r
176 * Gets the "no substitute" behavior of this locale data object.
\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
184 public boolean getNoSubstitute() {
\r
185 return noSubstitute;
\r
189 * Retrieves a delimiter string from the locale data.
\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
197 public String getDelimiter(int type) {
\r
198 String [] delimiterTypes = { "quotationStart",
\r
200 "alternateQuotationStart",
\r
201 "alternateQuotationEnd" };
\r
203 ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get("delimiters").get(delimiterTypes[type]);
\r
205 if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )
\r
208 return new String (stringBundle.getString());
\r
212 * Enumeration for representing the measurement systems.
\r
215 public static final class MeasurementSystem{
\r
217 * Measurement system specified by Le Système International d'Unités (SI)
\r
218 * otherwise known as Metric system.
\r
221 public static final MeasurementSystem SI = new MeasurementSystem(0);
\r
224 * Measurement system followed in the United States of America.
\r
227 public static final MeasurementSystem US = new MeasurementSystem(1);
\r
229 private int systemID;
\r
230 private MeasurementSystem(int id){
\r
234 private boolean equals(int id){
\r
235 return systemID == id;
\r
240 * Returns the measurement system used in the locale specified by the locale.
\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
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
250 int system = sysBundle.getInt();
\r
251 if(MeasurementSystem.US.equals(system)){
\r
252 return MeasurementSystem.US;
\r
254 if(MeasurementSystem.SI.equals(system)){
\r
255 return MeasurementSystem.SI;
\r
257 // return null if the object is null or is not an instance
\r
258 // of integer indicating an error
\r
263 * A class that represents the size of letter head
\r
264 * used in the country
\r
267 public static final class PaperSize{
\r
268 private int height;
\r
271 private PaperSize(int h, int w){
\r
276 * Retruns the height of the paper
\r
277 * @return the height
\r
280 public int getHeight(){
\r
284 * Returns the width of hte paper
\r
285 * @return the width
\r
288 public int getWidth(){
\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
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
308 * Returns LocaleDisplayPattern for this locale, e.g., {0}({1})
\r
309 * @return locale display pattern as a String.
\r
311 * @provisional This API might change or be removed in a future release.
\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
318 ICUResourceBundle locDispBundle = (ICUResourceBundle) bundle.get(LOCALE_DISPLAY_PATTERN);
\r
319 String localeDisplayPattern = locDispBundle.getStringWithFallback(PATTERN);
\r
320 return localeDisplayPattern;
\r
324 * Returns LocaleDisplaySeparator for this locale.
\r
325 * @return locale display separator as a char.
\r
327 * @provisional This API might change or be removed in a future release.
\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
334 ICUResourceBundle locDispBundle = (ICUResourceBundle) bundle.get(LOCALE_DISPLAY_PATTERN);
\r
335 String localeSeparator = locDispBundle.getStringWithFallback(SEPARATOR);
\r
336 return localeSeparator;
\r