001: /*
002: *******************************************************************************
003: * Copyright (C) 2003-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.text;
009:
010: import java.util.Locale;
011: import java.util.Set;
012: import java.util.MissingResourceException;
013:
014: //import com.ibm.icu.impl.ICULocaleData;
015: import com.ibm.icu.impl.ICUResourceBundle;
016: import com.ibm.icu.impl.ICUService;
017: import com.ibm.icu.impl.ICUService.Factory;
018: import com.ibm.icu.impl.ICUService.Key;
019: import com.ibm.icu.impl.ICULocaleService;
020: import com.ibm.icu.impl.ICULocaleService.LocaleKey;
021: import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory;
022: import com.ibm.icu.text.NumberFormat;
023: import com.ibm.icu.text.NumberFormat.NumberFormatFactory;
024: import com.ibm.icu.util.ULocale;
025: import com.ibm.icu.util.UResourceBundle;
026:
027: class NumberFormatServiceShim extends NumberFormat.NumberFormatShim {
028:
029: Locale[] getAvailableLocales() {
030: if (service.isDefault()) {
031: return ICUResourceBundle
032: .getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME);
033: }
034: return service.getAvailableLocales();
035: }
036:
037: ULocale[] getAvailableULocales() {
038: if (service.isDefault()) {
039: return ICUResourceBundle
040: .getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME);
041: }
042: return service.getAvailableULocales();
043: }
044:
045: private static final class NFFactory extends LocaleKeyFactory {
046: private NumberFormatFactory delegate;
047:
048: NFFactory(NumberFormatFactory delegate) {
049: super (delegate.visible() ? VISIBLE : INVISIBLE);
050:
051: this .delegate = delegate;
052: }
053:
054: public Object create(Key key, ICUService service) {
055: if (handlesKey(key)) {
056: LocaleKey lkey = (LocaleKey) key;
057: ULocale loc = lkey.canonicalLocale();
058: int kind = lkey.kind();
059:
060: Object result = delegate.createFormat(loc, kind);
061: if (result == null) {
062: result = service.getKey(key, null, this );
063: }
064: return result;
065: }
066: return null;
067: }
068:
069: protected Set getSupportedIDs() {
070: return delegate.getSupportedLocaleNames();
071: }
072: }
073:
074: Object registerFactory(NumberFormatFactory factory) {
075: return service.registerFactory(new NFFactory(factory));
076: }
077:
078: boolean unregister(Object registryKey) {
079: return service.unregisterFactory((Factory) registryKey);
080: }
081:
082: NumberFormat createInstance(ULocale desiredLocale, int choice) {
083:
084: // use service cache
085: // if (service.isDefault()) {
086: // return NumberFormat.createInstance(desiredLocale, choice);
087: // }
088:
089: ULocale[] actualLoc = new ULocale[1];
090: if (desiredLocale.equals(ULocale.ROOT)) {
091: desiredLocale = ULocale.ROOT;
092: }
093: NumberFormat fmt = (NumberFormat) service.get(desiredLocale,
094: choice, actualLoc);
095: if (fmt == null) {
096: throw new MissingResourceException(
097: "Unable to construct NumberFormat", "", "");
098: }
099: fmt = (NumberFormat) fmt.clone();
100:
101: ULocale uloc = actualLoc[0];
102: fmt.setLocale(uloc, uloc); // services make no distinction between actual & valid
103: return fmt;
104: }
105:
106: private static class NFService extends ICULocaleService {
107: NFService() {
108: super ("NumberFormat");
109:
110: class RBNumberFormatFactory extends
111: ICUResourceBundleFactory {
112: protected Object handleCreate(ULocale loc, int kind,
113: ICUService service) {
114: return NumberFormat.createInstance(loc, kind);
115: }
116: }
117:
118: this .registerFactory(new RBNumberFormatFactory());
119: markDefault();
120: }
121: }
122:
123: private static ICULocaleService service = new NFService();
124: }
|