KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > LocaleData


1 /*
2  *******************************************************************************
3  * Copyright (C) 2004-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6 */

7 package com.ibm.icu.util;
8
9 import java.util.MissingResourceException JavaDoc;
10
11 import com.ibm.icu.impl.ICUResourceBundle;
12 import com.ibm.icu.text.UnicodeSet;
13
14 /**
15  * A class for accessing miscelleneous data in the locale bundles
16  * @author ram
17  * @stable ICU 2.8
18  */

19 public final class LocaleData {
20     
21     private static final String JavaDoc EXEMPLAR_CHARS = "ExemplarCharacters";
22     private static final String JavaDoc MEASUREMENT_SYSTEM = "MeasurementSystem";
23     private static final String JavaDoc PAPER_SIZE = "PaperSize";
24     private boolean noSubstitute;
25     private ICUResourceBundle bundle;
26
27     /**
28      * EXType for {@link #getExemplarSet(int, int)}.
29      * @draft ICU 3.4
30      * @provisional This API might change or be removed in a future release.
31      */

32     public static final int ES_STANDARD = 0;
33
34     /**
35      * EXType for {@link #getExemplarSet(int, int)}.
36      * @draft ICU 3.4
37      * @provisional This API might change or be removed in a future release.
38      */

39     public static final int ES_AUXILIARY = 1;
40
41     /**
42      * Count of EXTypes for {@link #getExemplarSet(int, int)}.
43      * @draft ICU 3.4
44      * @provisional This API might change or be removed in a future release.
45      */

46     public static final int ES_COUNT = 2;
47     
48     /**
49      * Delimiter type for {@link #getDelimiter(int)}.
50      * @draft ICU 3.4
51      * @provisional This API might change or be removed in a future release.
52      */

53     public static final int QUOTATION_START = 0;
54
55     /**
56      * Delimiter type for {@link #getDelimiter(int)}.
57      * @draft ICU 3.4
58      * @provisional This API might change or be removed in a future release.
59      */

60     public static final int QUOTATION_END = 1;
61
62     /**
63      * Delimiter type for {@link #getDelimiter(int)}.
64      * @draft ICU 3.4
65      * @provisional This API might change or be removed in a future release.
66      */

67     public static final int ALT_QUOTATION_START = 2;
68
69     /**
70      * Delimiter type for {@link #getDelimiter(int)}.
71      * @draft ICU 3.4
72      * @provisional This API might change or be removed in a future release.
73      */

74     public static final int ALT_QUOTATION_END = 3;
75
76     /**
77      * Count of delimiter types for {@link #getDelimiter(int)}.
78      * @draft ICU 3.4
79      * @provisional This API might change or be removed in a future release.
80      */

81     public static final int DELIMITER_COUNT = 4;
82
83     // private constructor to prevent default construction
84
///CLOVER:OFF
85
private LocaleData(){}
86     ///CLOVER:ON
87

88     /**
89      * Returns the set of exemplar characters for a locale.
90      *
91      * @param locale Locale for which the exemplar character set
92      * is to be retrieved.
93      * @param options Bitmask for options to apply to the exemplar pattern.
94      * Specify zero to retrieve the exemplar set as it is
95      * defined in the locale data. Specify
96      * UnicodeSet.CASE to retrieve a case-folded exemplar
97      * set. See {@link UnicodeSet#applyPattern(String,
98      * int)} for a complete list of valid options. The
99      * IGNORE_SPACE bit is always set, regardless of the
100      * value of 'options'.
101      * @return The set of exemplar characters for the given locale.
102      * @stable ICU 3.0
103      */

104     public static UnicodeSet getExemplarSet(ULocale locale, int options) {
105         ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);
106         String JavaDoc pattern = bundle.getString(EXEMPLAR_CHARS);
107         return new UnicodeSet(pattern, UnicodeSet.IGNORE_SPACE | options);
108     }
109     
110     /**
111      * Returns the set of exemplar characters for a locale.
112      *
113      * @param options Bitmask for options to apply to the exemplar pattern.
114      * Specify zero to retrieve the exemplar set as it is
115      * defined in the locale data. Specify
116      * UnicodeSet.CASE to retrieve a case-folded exemplar
117      * set. See {@link UnicodeSet#applyPattern(String,
118      * int)} for a complete list of valid options. The
119      * IGNORE_SPACE bit is always set, regardless of the
120      * value of 'options'.
121      * @param extype The type of exemplar set to be retrieved,
122      * ES_STANDARD or ES_AUXILIARY
123      * @return The set of exemplar characters for the given locale.
124      * @draft ICU 3.4
125      * @provisional This API might change or be removed in a future release.
126      */

127     public UnicodeSet getExemplarSet(int options, int extype) {
128         String JavaDoc [] exemplarSetTypes = { "ExemplarCharacters", "AuxExemplarCharacters" };
129         try{
130             ICUResourceBundle stringBundle = bundle.get(exemplarSetTypes[extype]);
131     
132             if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )
133                return null;
134     
135             return new UnicodeSet(stringBundle.getString(), UnicodeSet.IGNORE_SPACE | options);
136         }catch(MissingResourceException JavaDoc ex){
137             if(extype==LocaleData.ES_AUXILIARY){
138                 return new UnicodeSet();
139             }
140             throw ex;
141         }
142     }
143
144     /**
145      * Gets the LocaleData object associated with the ULocale specified in locale
146      *
147      * @param locale Locale with thich the locale data object is associated.
148      * @return A locale data object.
149      * @draft ICU 3.4
150      * @provisional This API might change or be removed in a future release.
151      */

152     public static final LocaleData getInstance(ULocale locale) {
153        LocaleData ld = new LocaleData();
154        ld.bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale );
155        ld.noSubstitute = false;
156        return ld;
157     }
158
159     /**
160      * Gets the LocaleData object associated with the default locale
161      *
162      * @return A locale data object.
163      * @draft ICU 3.4
164      * @provisional This API might change or be removed in a future release.
165      */

166     public static final LocaleData getInstance() {
167        return LocaleData.getInstance(ULocale.getDefault());
168     }
169
170     /**
171      * Sets the "no substitute" behavior of this locale data object.
172      *
173      * @param setting Value for the no substitute behavior. If TRUE,
174      * methods of this locale data object will return
175      * an error when no data is available for that method,
176      * given the locale ID supplied to the constructor.
177      * @draft ICU 3.4
178      * @provisional This API might change or be removed in a future release.
179      */

180     public void setNoSubstitute(boolean setting) {
181        noSubstitute = setting;
182     }
183
184     /**
185      * Gets the "no substitute" behavior of this locale data object.
186      *
187      * @return Value for the no substitute behavior. If TRUE,
188      * methods of this locale data object will return
189      * an error when no data is available for that method,
190      * given the locale ID supplied to the constructor.
191      * @draft ICU 3.4
192      * @provisional This API might change or be removed in a future release.
193      */

194     public boolean getNoSubstitute() {
195        return noSubstitute;
196     }
197
198     /**
199      * Retrieves a delimiter string from the locale data.
200      *
201      * @param type The type of delimiter string desired. Currently,
202      * the valid choices are QUOTATION_START, QUOTATION_END,
203      * ALT_QUOTATION_START, or ALT_QUOTATION_END.
204      * @return The desired delimiter string.
205      * @draft ICU 3.4
206      * @provisional This API might change or be removed in a future release.
207      */

208     public String JavaDoc getDelimiter(int type) {
209         String JavaDoc [] delimiterTypes = { "quotationStart",
210                                      "quotationEnd",
211                                      "alternateQuotationStart",
212                                      "alternateQuotationEnd" };
213
214         ICUResourceBundle stringBundle = bundle.get("delimiters").get(delimiterTypes[type]);
215
216         if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) )
217            return null;
218
219         return new String JavaDoc (stringBundle.getString());
220     }
221
222     /**
223      * Enumeration for representing the measurement systems.
224      * @stable ICU 2.8
225      */

226     public static final class MeasurementSystem{
227         /**
228          * Measurement system specified by Le Système International d'Unités (SI)
229          * otherwise known as Metric system.
230          * @stable ICU 2.8
231          */

232         public static final MeasurementSystem SI = new MeasurementSystem(0);
233  
234         /**
235          * Measurement system followed in the United States of America.
236          * @stable ICU 2.8
237          */

238         public static final MeasurementSystem US = new MeasurementSystem(1);
239     
240         private int systemID;
241         private MeasurementSystem(int id){
242             systemID = id;
243         }
244
245         private boolean equals(int id){
246             return systemID == id;
247         }
248     }
249    
250     /**
251      * Returns the measurement system used in the locale specified by the locale.
252      *
253      * @param locale The locale for which the measurement system to be retrieved.
254      * @return MeasurementSystem the measurement system used in the locale.
255      * @stable ICU 3.0
256      */

257     public static final MeasurementSystem getMeasurementSystem(ULocale locale){
258         ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);
259         ICUResourceBundle sysBundle = bundle.get(MEASUREMENT_SYSTEM);
260         
261         int system = sysBundle.getInt();
262         if(MeasurementSystem.US.equals(system)){
263             return MeasurementSystem.US;
264         }
265         if(MeasurementSystem.SI.equals(system)){
266             return MeasurementSystem.SI;
267         }
268         // return null if the object is null or is not an instance
269
// of integer indicating an error
270
return null;
271     }
272     
273     /**
274      * A class that represents the size of letter head
275      * used in the country
276      * @stable ICU 2.8
277      */

278     public static final class PaperSize{
279         private int height;
280         private int width;
281         
282         private PaperSize(int h, int w){
283             height = h;
284             width = w;
285         }
286         /**
287          * Retruns the height of the paper
288          * @return the height
289          * @stable ICU 2.8
290          */

291         public int getHeight(){
292             return height;
293         }
294         /**
295          * Returns the width of hte paper
296          * @return the width
297          * @stable ICU 2.8
298          */

299         public int getWidth(){
300             return width;
301         }
302     }
303     
304     /**
305      * Returns the size of paper used in the locale. The paper sizes returned are always in
306      * <em> milli-meters<em>.
307      * @param locale The locale for which the measurement system to be retrieved.
308      * @return The paper size used in the locale
309      * @stable ICU 3.0
310      */

311     public static final PaperSize getPaperSize(ULocale locale){
312         ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);
313         ICUResourceBundle obj = bundle.get(PAPER_SIZE);
314         int[] size = obj.getIntVector();
315         return new PaperSize(size[0], size[1]);
316     }
317 }
318
Popular Tags