1 7 package com.ibm.icu.text; 8 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 12 import java.util.Locale ; 13 import java.util.MissingResourceException ; 14 import java.util.ResourceBundle ; 15 16 import com.ibm.icu.impl.ICUData; 17 import com.ibm.icu.impl.ICULocaleService; 18 import com.ibm.icu.impl.ICUResourceBundle; 19 import com.ibm.icu.impl.ICUService; 20 import com.ibm.icu.impl.ICUService.Factory; 21 import com.ibm.icu.util.ULocale; 22 import com.ibm.icu.util.UResourceBundle; 23 import com.ibm.icu.impl.Assert; 24 25 33 final class BreakIteratorFactory extends BreakIterator.BreakIteratorServiceShim { 34 35 public Object registerInstance(BreakIterator iter, ULocale locale, int kind) { 36 iter.setText(new java.text.StringCharacterIterator ("")); 37 return service.registerObject(iter, locale, kind); 38 } 39 40 public boolean unregister(Object key) { 41 if (service.isDefault()) { 42 return false; 43 } 44 return service.unregisterFactory((Factory)key); 45 } 46 47 public Locale [] getAvailableLocales() { 48 if (service == null) { 49 return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME); 50 } else { 51 return service.getAvailableLocales(); 52 } 53 } 54 55 public ULocale[] getAvailableULocales() { 56 if (service == null) { 57 return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME); 58 } else { 59 return service.getAvailableULocales(); 60 } 61 } 62 63 public BreakIterator createBreakIterator(ULocale locale, int kind) { 64 if (service.isDefault()) { 66 return createBreakInstance(locale, kind); 67 } 68 ULocale[] actualLoc = new ULocale[1]; 69 BreakIterator iter = (BreakIterator)service.get(locale, kind, actualLoc); 70 iter.setLocale(actualLoc[0], actualLoc[0]); return iter; 72 } 73 74 private static class BFService extends ICULocaleService { 75 BFService() { 76 super("BreakIterator"); 77 78 class RBBreakIteratorFactory extends ICUResourceBundleFactory { 79 protected Object handleCreate(ULocale loc, int kind, ICUService service) { 80 return createBreakInstance(loc, kind); 81 } 82 } 83 registerFactory(new RBBreakIteratorFactory()); 84 85 markDefault(); 86 } 87 } 88 static final ICULocaleService service = new BFService(); 89 90 91 101 private static final String [] KIND_NAMES = { 102 "grapheme", "word", "line", "sentence", "title" 103 }; 104 private static final boolean[] DICTIONARY_POSSIBLE = { 105 false, true, true, false, false 106 }; 107 108 109 private static BreakIterator createBreakInstance(ULocale locale, int kind) { 110 111 BreakIterator iter = null; 112 ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BRKITR_BASE_NAME, locale); 113 114 InputStream ruleStream = null; 119 try { 120 String typeKey = KIND_NAMES[kind]; 121 String brkfname = rb.getStringWithFallback("boundaries/" + typeKey); 122 String rulesFileName = ICUResourceBundle.ICU_BUNDLE +ICUResourceBundle.ICU_BRKITR_NAME+ "/" + brkfname; 123 ruleStream = ICUData.getStream(rulesFileName); 124 } 125 catch (Exception e) { 126 throw new MissingResourceException (e.toString(),"",""); 127 } 128 129 if (DICTIONARY_POSSIBLE[kind]) { 134 try { 137 if(locale.equals("th")){ 142 String fileName = "data/th.brk"; 143 InputStream is = ICUData.getStream(fileName); 144 iter = new DictionaryBasedBreakIterator(ruleStream, is); 145 } 146 } catch (MissingResourceException e) { 147 } catch (IOException e) { 153 Assert.fail(e); 154 } 155 } 156 157 if (iter == null) { 158 try { 163 iter = RuleBasedBreakIterator.getInstanceFromCompiledRules(ruleStream); 164 } 165 catch (IOException e) { 166 Assert.fail(e); 169 } 170 } 171 ULocale uloc = ULocale.forLocale(rb.getLocale()); 173 iter.setLocale(uloc, uloc); 174 175 return iter; 176 177 } 178 179 } 180 | Popular Tags |