001: /*
002: *******************************************************************************
003: * Copyright (C) 2004-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.util;
008:
009: import java.util.MissingResourceException;
010:
011: import com.ibm.icu.impl.ICUResourceBundle;
012: import com.ibm.icu.text.UnicodeSet;
013:
014: /**
015: * A class for accessing miscelleneous data in the locale bundles
016: * @author ram
017: * @stable ICU 2.8
018: */
019: public final class LocaleData {
020:
021: private static final String EXEMPLAR_CHARS = "ExemplarCharacters";
022: private static final String MEASUREMENT_SYSTEM = "MeasurementSystem";
023: private static final String PAPER_SIZE = "PaperSize";
024: private boolean noSubstitute;
025: private ICUResourceBundle bundle;
026:
027: /**
028: * EXType for {@link #getExemplarSet(int, int)}.
029: * @draft ICU 3.4
030: * @provisional This API might change or be removed in a future release.
031: */
032: public static final int ES_STANDARD = 0;
033:
034: /**
035: * EXType for {@link #getExemplarSet(int, int)}.
036: * @draft ICU 3.4
037: * @provisional This API might change or be removed in a future release.
038: */
039: public static final int ES_AUXILIARY = 1;
040:
041: /**
042: * Count of EXTypes for {@link #getExemplarSet(int, int)}.
043: * @draft ICU 3.4
044: * @provisional This API might change or be removed in a future release.
045: */
046: public static final int ES_COUNT = 2;
047:
048: /**
049: * Delimiter type for {@link #getDelimiter(int)}.
050: * @draft ICU 3.4
051: * @provisional This API might change or be removed in a future release.
052: */
053: public static final int QUOTATION_START = 0;
054:
055: /**
056: * Delimiter type for {@link #getDelimiter(int)}.
057: * @draft ICU 3.4
058: * @provisional This API might change or be removed in a future release.
059: */
060: public static final int QUOTATION_END = 1;
061:
062: /**
063: * Delimiter type for {@link #getDelimiter(int)}.
064: * @draft ICU 3.4
065: * @provisional This API might change or be removed in a future release.
066: */
067: public static final int ALT_QUOTATION_START = 2;
068:
069: /**
070: * Delimiter type for {@link #getDelimiter(int)}.
071: * @draft ICU 3.4
072: * @provisional This API might change or be removed in a future release.
073: */
074: public static final int ALT_QUOTATION_END = 3;
075:
076: /**
077: * Count of delimiter types for {@link #getDelimiter(int)}.
078: * @draft ICU 3.4
079: * @provisional This API might change or be removed in a future release.
080: */
081: public static final int DELIMITER_COUNT = 4;
082:
083: // private constructor to prevent default construction
084: ///CLOVER:OFF
085: private LocaleData() {
086: }
087:
088: ///CLOVER:ON
089:
090: /**
091: * Returns the set of exemplar characters for a locale.
092: *
093: * @param locale Locale for which the exemplar character set
094: * is to be retrieved.
095: * @param options Bitmask for options to apply to the exemplar pattern.
096: * Specify zero to retrieve the exemplar set as it is
097: * defined in the locale data. Specify
098: * UnicodeSet.CASE to retrieve a case-folded exemplar
099: * set. See {@link UnicodeSet#applyPattern(String,
100: * int)} for a complete list of valid options. The
101: * IGNORE_SPACE bit is always set, regardless of the
102: * value of 'options'.
103: * @return The set of exemplar characters for the given locale.
104: * @stable ICU 3.0
105: */
106: public static UnicodeSet getExemplarSet(ULocale locale, int options) {
107: ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle
108: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,
109: locale);
110: String pattern = bundle.getString(EXEMPLAR_CHARS);
111: return new UnicodeSet(pattern, UnicodeSet.IGNORE_SPACE
112: | options);
113: }
114:
115: /**
116: * Returns the set of exemplar characters for a locale.
117: *
118: * @param options Bitmask for options to apply to the exemplar pattern.
119: * Specify zero to retrieve the exemplar set as it is
120: * defined in the locale data. Specify
121: * UnicodeSet.CASE to retrieve a case-folded exemplar
122: * set. See {@link UnicodeSet#applyPattern(String,
123: * int)} for a complete list of valid options. The
124: * IGNORE_SPACE bit is always set, regardless of the
125: * value of 'options'.
126: * @param extype The type of exemplar set to be retrieved,
127: * ES_STANDARD or ES_AUXILIARY
128: * @return The set of exemplar characters for the given locale.
129: * @draft ICU 3.4
130: * @provisional This API might change or be removed in a future release.
131: */
132: public UnicodeSet getExemplarSet(int options, int extype) {
133: String[] exemplarSetTypes = { "ExemplarCharacters",
134: "AuxExemplarCharacters" };
135: try {
136: ICUResourceBundle stringBundle = bundle
137: .get(exemplarSetTypes[extype]);
138:
139: if (noSubstitute
140: && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT))
141: return null;
142:
143: return new UnicodeSet(stringBundle.getString(),
144: UnicodeSet.IGNORE_SPACE | options);
145: } catch (MissingResourceException ex) {
146: if (extype == LocaleData.ES_AUXILIARY) {
147: return new UnicodeSet();
148: }
149: throw ex;
150: }
151: }
152:
153: /**
154: * Gets the LocaleData object associated with the ULocale specified in locale
155: *
156: * @param locale Locale with thich the locale data object is associated.
157: * @return A locale data object.
158: * @draft ICU 3.4
159: * @provisional This API might change or be removed in a future release.
160: */
161: public static final LocaleData getInstance(ULocale locale) {
162: LocaleData ld = new LocaleData();
163: ld.bundle = (ICUResourceBundle) UResourceBundle
164: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,
165: locale);
166: ld.noSubstitute = false;
167: return ld;
168: }
169:
170: /**
171: * Gets the LocaleData object associated with the default locale
172: *
173: * @return A locale data object.
174: * @draft ICU 3.4
175: * @provisional This API might change or be removed in a future release.
176: */
177: public static final LocaleData getInstance() {
178: return LocaleData.getInstance(ULocale.getDefault());
179: }
180:
181: /**
182: * Sets the "no substitute" behavior of this locale data object.
183: *
184: * @param setting Value for the no substitute behavior. If TRUE,
185: * methods of this locale data object will return
186: * an error when no data is available for that method,
187: * given the locale ID supplied to the constructor.
188: * @draft ICU 3.4
189: * @provisional This API might change or be removed in a future release.
190: */
191: public void setNoSubstitute(boolean setting) {
192: noSubstitute = setting;
193: }
194:
195: /**
196: * Gets the "no substitute" behavior of this locale data object.
197: *
198: * @return Value for the no substitute behavior. If TRUE,
199: * methods of this locale data object will return
200: * an error when no data is available for that method,
201: * given the locale ID supplied to the constructor.
202: * @draft ICU 3.4
203: * @provisional This API might change or be removed in a future release.
204: */
205: public boolean getNoSubstitute() {
206: return noSubstitute;
207: }
208:
209: /**
210: * Retrieves a delimiter string from the locale data.
211: *
212: * @param type The type of delimiter string desired. Currently,
213: * the valid choices are QUOTATION_START, QUOTATION_END,
214: * ALT_QUOTATION_START, or ALT_QUOTATION_END.
215: * @return The desired delimiter string.
216: * @draft ICU 3.4
217: * @provisional This API might change or be removed in a future release.
218: */
219: public String getDelimiter(int type) {
220: String[] delimiterTypes = { "quotationStart", "quotationEnd",
221: "alternateQuotationStart", "alternateQuotationEnd" };
222:
223: ICUResourceBundle stringBundle = bundle.get("delimiters").get(
224: delimiterTypes[type]);
225:
226: if (noSubstitute
227: && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT))
228: return null;
229:
230: return new String(stringBundle.getString());
231: }
232:
233: /**
234: * Enumeration for representing the measurement systems.
235: * @stable ICU 2.8
236: */
237: public static final class MeasurementSystem {
238: /**
239: * Measurement system specified by Le Système International d'Unités (SI)
240: * otherwise known as Metric system.
241: * @stable ICU 2.8
242: */
243: public static final MeasurementSystem SI = new MeasurementSystem(
244: 0);
245:
246: /**
247: * Measurement system followed in the United States of America.
248: * @stable ICU 2.8
249: */
250: public static final MeasurementSystem US = new MeasurementSystem(
251: 1);
252:
253: private int systemID;
254:
255: private MeasurementSystem(int id) {
256: systemID = id;
257: }
258:
259: private boolean equals(int id) {
260: return systemID == id;
261: }
262: }
263:
264: /**
265: * Returns the measurement system used in the locale specified by the locale.
266: *
267: * @param locale The locale for which the measurement system to be retrieved.
268: * @return MeasurementSystem the measurement system used in the locale.
269: * @stable ICU 3.0
270: */
271: public static final MeasurementSystem getMeasurementSystem(
272: ULocale locale) {
273: ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle
274: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,
275: locale);
276: ICUResourceBundle sysBundle = bundle.get(MEASUREMENT_SYSTEM);
277:
278: int system = sysBundle.getInt();
279: if (MeasurementSystem.US.equals(system)) {
280: return MeasurementSystem.US;
281: }
282: if (MeasurementSystem.SI.equals(system)) {
283: return MeasurementSystem.SI;
284: }
285: // return null if the object is null or is not an instance
286: // of integer indicating an error
287: return null;
288: }
289:
290: /**
291: * A class that represents the size of letter head
292: * used in the country
293: * @stable ICU 2.8
294: */
295: public static final class PaperSize {
296: private int height;
297: private int width;
298:
299: private PaperSize(int h, int w) {
300: height = h;
301: width = w;
302: }
303:
304: /**
305: * Retruns the height of the paper
306: * @return the height
307: * @stable ICU 2.8
308: */
309: public int getHeight() {
310: return height;
311: }
312:
313: /**
314: * Returns the width of hte paper
315: * @return the width
316: * @stable ICU 2.8
317: */
318: public int getWidth() {
319: return width;
320: }
321: }
322:
323: /**
324: * Returns the size of paper used in the locale. The paper sizes returned are always in
325: * <em> milli-meters<em>.
326: * @param locale The locale for which the measurement system to be retrieved.
327: * @return The paper size used in the locale
328: * @stable ICU 3.0
329: */
330: public static final PaperSize getPaperSize(ULocale locale) {
331: ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle
332: .getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,
333: locale);
334: ICUResourceBundle obj = bundle.get(PAPER_SIZE);
335: int[] size = obj.getIntVector();
336: return new PaperSize(size[0], size[1]);
337: }
338: }
|