2 *******************************************************************************
3 * Copyright (C) 2002-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.text;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.util.Locale;
12 import java.util.MissingResourceException;
14 import com.ibm.icu.impl.Assert;
15 import com.ibm.icu.impl.ICUData;
16 import com.ibm.icu.impl.ICULocaleService;
17 import com.ibm.icu.impl.ICUResourceBundle;
18 import com.ibm.icu.impl.ICUService;
19 import com.ibm.icu.impl.ICUService.Factory;
20 import com.ibm.icu.util.ULocale;
21 import com.ibm.icu.util.UResourceBundle;
26 * To change this generated comment edit the template variable "typecomment":
27 * Window>Preferences>Java>Templates.
28 * To enable and disable the creation of type comments go to
29 * Window>Preferences>Java>Code Generation.
31 final class BreakIteratorFactory extends BreakIterator.BreakIteratorServiceShim {
33 public Object registerInstance(BreakIterator iter, ULocale locale, int kind) {
34 iter.setText(new java.text.StringCharacterIterator(""));
35 return service.registerObject(iter, locale, kind);
38 public boolean unregister(Object key) {
39 if (service.isDefault()) {
42 return service.unregisterFactory((Factory)key);
45 public Locale[] getAvailableLocales() {
46 if (service == null) {
47 return ICUResourceBundle.getAvailableLocales();
49 return service.getAvailableLocales();
53 public ULocale[] getAvailableULocales() {
54 if (service == null) {
55 return ICUResourceBundle.getAvailableULocales();
57 return service.getAvailableULocales();
61 public BreakIterator createBreakIterator(ULocale locale, int kind) {
62 // TODO: convert to ULocale when service switches over
63 if (service.isDefault()) {
64 return createBreakInstance(locale, kind);
66 ULocale[] actualLoc = new ULocale[1];
67 BreakIterator iter = (BreakIterator)service.get(locale, kind, actualLoc);
68 iter.setLocale(actualLoc[0], actualLoc[0]); // services make no distinction between actual & valid
72 private static class BFService extends ICULocaleService {
74 super("BreakIterator");
76 class RBBreakIteratorFactory extends ICUResourceBundleFactory {
77 protected Object handleCreate(ULocale loc, int kind, ICUService srvc) {
78 return createBreakInstance(loc, kind);
81 registerFactory(new RBBreakIteratorFactory());
86 static final ICULocaleService service = new BFService();
89 /** KIND_NAMES are the resource key to be used to fetch the name of the
90 * pre-compiled break rules. The resource bundle name is "boundaries".
91 * The value for each key will be the rules to be used for the
92 * specified locale - "word" -> "word_th" for Thai, for example.
94 private static final String[] KIND_NAMES = {
95 "grapheme", "word", "line", "sentence", "title"
99 private static BreakIterator createBreakInstance(ULocale locale, int kind) {
101 RuleBasedBreakIterator iter = null;
102 ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BRKITR_BASE_NAME, locale);
105 // Get the binary rules.
107 InputStream ruleStream = null;
109 String typeKey = KIND_NAMES[kind];
110 String brkfname = rb.getStringWithFallback("boundaries/" + typeKey);
111 String rulesFileName = ICUResourceBundle.ICU_BUNDLE +ICUResourceBundle.ICU_BRKITR_NAME+ "/" + brkfname;
112 ruleStream = ICUData.getStream(rulesFileName);
114 catch (Exception e) {
115 throw new MissingResourceException(e.toString(),"","");
119 // Create a normal RuleBasedBreakIterator.
122 iter = RuleBasedBreakIterator.getInstanceFromCompiledRules(ruleStream);
124 catch (IOException e) {
125 // Shouldn't be possible to get here.
126 // If it happens, the compiled rules are probably corrupted in some way.
129 // TODO: Determine valid and actual locale correctly.
130 ULocale uloc = ULocale.forLocale(rb.getLocale());
131 iter.setLocale(uloc, uloc);
132 iter.setBreakType(kind);