001: /**
002: *******************************************************************************
003: * Copyright (C) 2003-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.text;
007:
008: import java.util.Locale;
009: import java.util.MissingResourceException;
010: import java.util.Set;
011:
012: import com.ibm.icu.impl.ICULocaleService;
013: import com.ibm.icu.impl.ICUResourceBundle;
014: import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory;
015: import com.ibm.icu.impl.ICUService;
016: import com.ibm.icu.impl.ICUService.Factory;
017: import com.ibm.icu.impl.LocaleUtility;
018: import com.ibm.icu.text.Collator.CollatorFactory;
019: import com.ibm.icu.util.ULocale;
020: import com.ibm.icu.impl.ICUResourceBundle;
021:
022: final class CollatorServiceShim extends Collator.ServiceShim {
023:
024: Collator getInstance(ULocale locale) {
025: // use service cache, it's faster than instantiation
026: // if (service.isDefault()) {
027: // return new RuleBasedCollator(locale);
028: // }
029:
030: try {
031: ULocale[] actualLoc = new ULocale[1];
032: Collator coll = (Collator) service.get(locale, actualLoc);
033: if (coll == null) {
034: throw new MissingResourceException(
035: "Could not locate Collator data", "", "");
036: }
037: coll = (Collator) coll.clone();
038: coll.setLocale(actualLoc[0], actualLoc[0]); // services make no distinction between actual & valid
039: return coll;
040: } catch (CloneNotSupportedException e) {
041: ///CLOVER:OFF
042: throw new IllegalStateException(e.getMessage());
043: ///CLOVER:ON
044: }
045: }
046:
047: Object registerInstance(Collator collator, ULocale locale) {
048: return service.registerObject(collator, locale);
049: }
050:
051: Object registerFactory(CollatorFactory f) {
052: class CFactory extends LocaleKeyFactory {
053: CollatorFactory delegate;
054:
055: CFactory(CollatorFactory f) {
056: super (f.visible());
057: this .delegate = f;
058: }
059:
060: public Object handleCreate(ULocale loc, int kind,
061: ICUService service) {
062: Object coll = delegate.createCollator(loc);
063: return coll;
064: }
065:
066: public String getDisplayName(String id,
067: ULocale displayLocale) {
068: ULocale objectLocale = new ULocale(id);
069: return delegate.getDisplayName(objectLocale,
070: displayLocale);
071: }
072:
073: public Set getSupportedIDs() {
074: return delegate.getSupportedLocaleIDs();
075: }
076: }
077:
078: return service.registerFactory(new CFactory(f));
079: }
080:
081: boolean unregister(Object registryKey) {
082: return service.unregisterFactory((Factory) registryKey);
083: }
084:
085: Locale[] getAvailableLocales() {
086: // TODO rewrite this to just wrap getAvailableULocales later
087: if (service.isDefault()) {
088: return ICUResourceBundle
089: .getAvailableLocales(ICUResourceBundle.ICU_COLLATION_BASE_NAME);
090: }
091: return service.getAvailableLocales();
092: }
093:
094: ULocale[] getAvailableULocales() {
095: if (service.isDefault()) {
096: return ICUResourceBundle
097: .getAvailableULocales(ICUResourceBundle.ICU_COLLATION_BASE_NAME);
098: }
099: return service.getAvailableULocales();
100: }
101:
102: String getDisplayName(ULocale objectLocale, ULocale displayLocale) {
103: String id = objectLocale.getName();
104: return service.getDisplayName(id, displayLocale);
105: }
106:
107: private static class CService extends ICULocaleService {
108: CService() {
109: super ("Collator");
110:
111: class CollatorFactory extends ICUResourceBundleFactory {
112: CollatorFactory() {
113: super (ICUResourceBundle.ICU_COLLATION_BASE_NAME);
114: }
115:
116: protected Object handleCreate(ULocale uloc, int kind,
117: ICUService service) {
118: return new RuleBasedCollator(uloc);
119: }
120: }
121:
122: this .registerFactory(new CollatorFactory());
123: markDefault();
124: }
125:
126: protected Object handleDefault(Key key, String[] actualIDReturn) {
127: if (actualIDReturn != null) {
128: actualIDReturn[0] = "root";
129: }
130: try {
131: return new RuleBasedCollator(ULocale.ROOT);
132: } catch (MissingResourceException e) {
133: return null;
134: }
135: }
136: }
137:
138: private static ICULocaleService service = new CService();
139: }
|