KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > CollatorServiceShim


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

7
8 package com.ibm.icu.text;
9
10 import java.util.Locale JavaDoc;
11 import java.util.MissingResourceException JavaDoc;
12 import java.util.Set JavaDoc;
13
14 import com.ibm.icu.impl.ICULocaleService;
15 import com.ibm.icu.impl.ICUResourceBundle;
16 import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory;
17 import com.ibm.icu.impl.ICUService;
18 import com.ibm.icu.impl.ICUService.Factory;
19 import com.ibm.icu.impl.LocaleUtility;
20 import com.ibm.icu.text.Collator.CollatorFactory;
21 import com.ibm.icu.util.ULocale;
22 import com.ibm.icu.impl.ICUResourceBundle;
23
24 final class CollatorServiceShim extends Collator.ServiceShim {
25
26     Collator getInstance(ULocale locale) {
27     // use service cache, it's faster than instantiation
28
// if (service.isDefault()) {
29
// return new RuleBasedCollator(locale);
30
// }
31

32         try {
33             ULocale[] actualLoc = new ULocale[1];
34             Collator coll = (Collator)service.get(locale, actualLoc);
35             if (coll == null) {
36                 throw new MissingResourceException JavaDoc("Could not locate Collator data", "", "");
37             }
38             coll = (Collator) coll.clone();
39             coll.setLocale(actualLoc[0], actualLoc[0]); // services make no distinction between actual & valid
40
return coll;
41         }
42         catch (CloneNotSupportedException JavaDoc e) {
43         ///CLOVER:OFF
44
throw new IllegalStateException JavaDoc(e.getMessage());
45         ///CLOVER:ON
46
}
47     }
48
49     Object JavaDoc registerInstance(Collator collator, ULocale locale) {
50         return service.registerObject(collator, locale);
51     }
52
53     Object JavaDoc registerFactory(CollatorFactory f) {
54         class CFactory extends LocaleKeyFactory {
55             CollatorFactory delegate;
56
57             CFactory(CollatorFactory f) {
58                 super(f.visible());
59                 this.delegate = f;
60             }
61
62             public Object JavaDoc handleCreate(ULocale loc, int kind, ICUService service) {
63                 Object JavaDoc coll = delegate.createCollator(loc);
64                 return coll;
65             }
66                 
67             public String JavaDoc getDisplayName(String JavaDoc id, ULocale displayLocale) {
68                 ULocale objectLocale = new ULocale(id);
69                 return delegate.getDisplayName(objectLocale, displayLocale);
70             }
71
72             public Set JavaDoc getSupportedIDs() {
73                 return delegate.getSupportedLocaleIDs();
74             }
75         }
76
77         return service.registerFactory(new CFactory(f));
78     }
79
80     boolean unregister(Object JavaDoc registryKey) {
81         return service.unregisterFactory((Factory)registryKey);
82     }
83
84     Locale JavaDoc[] getAvailableLocales() {
85         // TODO rewrite this to just wrap getAvailableULocales later
86
if (service.isDefault()) {
87             return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_COLLATION_BASE_NAME);
88         }
89         return service.getAvailableLocales();
90     }
91
92     ULocale[] getAvailableULocales() {
93         if (service.isDefault()) {
94             return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_COLLATION_BASE_NAME);
95         }
96         return service.getAvailableULocales();
97     }
98
99     String JavaDoc getDisplayName(ULocale objectLocale, ULocale displayLocale) {
100         String JavaDoc id = objectLocale.getName();
101         return service.getDisplayName(id, displayLocale);
102     }
103
104     private static class CService extends ICULocaleService {
105         CService() {
106             super("Collator");
107
108             class CollatorFactory extends ICUResourceBundleFactory {
109                 CollatorFactory() {
110                     super(ICUResourceBundle.ICU_COLLATION_BASE_NAME);
111                 }
112
113                 protected Object JavaDoc handleCreate(ULocale uloc, int kind, ICUService service) {
114                     return new RuleBasedCollator(uloc);
115                 }
116             }
117
118             this.registerFactory(new CollatorFactory());
119             markDefault();
120         }
121
122         protected Object JavaDoc handleDefault(Key key, String JavaDoc[] actualIDReturn) {
123             if (actualIDReturn != null) {
124                 actualIDReturn[0] = "root";
125             }
126             try {
127                 return new RuleBasedCollator(ULocale.ROOT);
128             }
129             catch (MissingResourceException JavaDoc e) {
130                 return null;
131             }
132         }
133     }
134     private static ICULocaleService service = new CService();
135 }
136
Popular Tags