KickJava   Java API By Example, From Geeks To Geeks.

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


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

7 package com.ibm.icu.text;
8
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11
12 import java.util.Locale JavaDoc;
13 import java.util.MissingResourceException JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
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 /**
26  * @author Ram
27  *
28  * To change this generated comment edit the template variable "typecomment":
29  * Window>Preferences>Java>Templates.
30  * To enable and disable the creation of type comments go to
31  * Window>Preferences>Java>Code Generation.
32  */

33 final class BreakIteratorFactory extends BreakIterator.BreakIteratorServiceShim {
34
35     public Object JavaDoc registerInstance(BreakIterator iter, ULocale locale, int kind) {
36         iter.setText(new java.text.StringCharacterIterator JavaDoc(""));
37         return service.registerObject(iter, locale, kind);
38     }
39
40     public boolean unregister(Object JavaDoc key) {
41         if (service.isDefault()) {
42             return false;
43         }
44         return service.unregisterFactory((Factory)key);
45     }
46
47     public Locale JavaDoc[] 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     // TODO: convert to ULocale when service switches over
65
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]); // services make no distinction between actual & valid
71
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 JavaDoc 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     /** KIND_NAMES are the resource key to be used to fetch the name of the
92      * pre-compiled break rules. The resource bundle name is "boundaries".
93      * The value for each key will be the rules to be used for the
94      * specified locale - "word" -> "word_th" for Thai, for example.
95      * DICTIONARY_POSSIBLE indexes in the same way, and indicates whether a
96      * dictionary is a possibility for that type of break. This is just
97      * an optimization to avoid a resource lookup where no dictionary is
98      * ever possible.
99      * @internal
100      */

101     private static final String JavaDoc[] 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         //
115
// Get the binary rules. These are needed for both normal RulesBasedBreakIterators
116
// and for Dictionary iterators.
117
//
118
InputStream JavaDoc ruleStream = null;
119         try {
120             String JavaDoc typeKey = KIND_NAMES[kind];
121             String JavaDoc brkfname = rb.getStringWithFallback("boundaries/" + typeKey);
122             String JavaDoc rulesFileName = ICUResourceBundle.ICU_BUNDLE +ICUResourceBundle.ICU_BRKITR_NAME+ "/" + brkfname;
123                            ruleStream = ICUData.getStream(rulesFileName);
124         }
125         catch (Exception JavaDoc e) {
126             throw new MissingResourceException JavaDoc(e.toString(),"","");
127         }
128  
129         //
130
// Check whether a dictionary exists, and create a DBBI iterator is
131
// one does.
132
//
133
if (DICTIONARY_POSSIBLE[kind]) {
134             // This type of break iterator could potentially use a dictionary.
135
//
136
try {
137                 //ICUResourceBundle dictRes = (ICUResourceBundle)rb.getObject("BreakDictionaryData");
138
//byte[] dictBytes = null;
139
//dictBytes = dictRes.getBinary(dictBytes);
140
//TODO: Hard code this for now! fix it once CompactTrieDictionary is ported
141
if(locale.equals("th")){
142                     String JavaDoc fileName = "data/th.brk";
143                     InputStream JavaDoc is = ICUData.getStream(fileName);
144                     iter = new DictionaryBasedBreakIterator(ruleStream, is);
145                 }
146             } catch (MissingResourceException JavaDoc e) {
147                 // Couldn't find a dictionary.
148
// This is normal, and will occur whenever creating a word or line
149
// break iterator for a locale that does not have a BreakDictionaryData
150
// resource - meaning for all but Thai.
151
// Fall through to creating a normal RulebasedBreakIterator.
152
} catch (IOException JavaDoc e) {
153                 Assert.fail(e);
154             }
155          }
156
157         if (iter == null) {
158             //
159
// Create a normal RuleBasedBreakIterator.
160
// We have determined that this is not supposed to be a dictionary iterator.
161
//
162
try {
163                 iter = RuleBasedBreakIterator.getInstanceFromCompiledRules(ruleStream);
164             }
165             catch (IOException JavaDoc e) {
166                 // Shouldn't be possible to get here.
167
// If it happens, the compiled rules are probably corrupted in some way.
168
Assert.fail(e);
169            }
170         }
171         // TODO: Determine valid and actual locale correctly.
172
ULocale uloc = ULocale.forLocale(rb.getLocale());
173         iter.setLocale(uloc, uloc);
174         
175         return iter;
176
177     }
178
179 }
180
Popular Tags