1 7 8 package com.ibm.icu.text; 9 10 import java.util.Locale ; 11 import java.util.MissingResourceException ; 12 import java.util.Set ; 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 32 try { 33 ULocale[] actualLoc = new ULocale[1]; 34 Collator coll = (Collator)service.get(locale, actualLoc); 35 if (coll == null) { 36 throw new MissingResourceException ("Could not locate Collator data", "", ""); 37 } 38 coll = (Collator) coll.clone(); 39 coll.setLocale(actualLoc[0], actualLoc[0]); return coll; 41 } 42 catch (CloneNotSupportedException e) { 43 throw new IllegalStateException (e.getMessage()); 45 } 47 } 48 49 Object registerInstance(Collator collator, ULocale locale) { 50 return service.registerObject(collator, locale); 51 } 52 53 Object 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 handleCreate(ULocale loc, int kind, ICUService service) { 63 Object coll = delegate.createCollator(loc); 64 return coll; 65 } 66 67 public String getDisplayName(String id, ULocale displayLocale) { 68 ULocale objectLocale = new ULocale(id); 69 return delegate.getDisplayName(objectLocale, displayLocale); 70 } 71 72 public Set getSupportedIDs() { 73 return delegate.getSupportedLocaleIDs(); 74 } 75 } 76 77 return service.registerFactory(new CFactory(f)); 78 } 79 80 boolean unregister(Object registryKey) { 81 return service.unregisterFactory((Factory)registryKey); 82 } 83 84 Locale [] getAvailableLocales() { 85 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 getDisplayName(ULocale objectLocale, ULocale displayLocale) { 100 String 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 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 handleDefault(Key key, String [] actualIDReturn) { 123 if (actualIDReturn != null) { 124 actualIDReturn[0] = "root"; 125 } 126 try { 127 return new RuleBasedCollator(ULocale.ROOT); 128 } 129 catch (MissingResourceException e) { 130 return null; 131 } 132 } 133 } 134 private static ICULocaleService service = new CService(); 135 } 136 | Popular Tags |