8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.util.locale.provider;
27
28 import java.text.BreakIterator;
29 import java.text.spi.BreakIteratorProvider;
30 import java.util.Locale;
31 import java.util.ResourceBundle;
32 import java.util.Set;
33 import sun.util.resources.LocaleData;
34
35 /**
36 * Concrete implementation of the {@link java.text.spi.BreakIteratorProvider
37 * BreakIteratorProvider} class for the JRE LocaleProviderAdapter.
38 *
39 * @author Naoto Sato
40 * @author Masayoshi Okutsu
41 */
42 public class BreakIteratorProviderImpl extends BreakIteratorProvider
43 implements AvailableLanguageTags {
44
45 private static final int CHARACTER_INDEX = 0;
46 private static final int WORD_INDEX = 1;
47 private static final int LINE_INDEX = 2;
48 private static final int SENTENCE_INDEX = 3;
49
50 private final LocaleProviderAdapter.Type type;
51 private final Set<String> langtags;
52
53 public BreakIteratorProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
142 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
143 * getAvailableLocales()}.
144 * @see java.text.BreakIterator#getSentenceInstance(java.util.Locale)
145 */
146 @Override
147 public BreakIterator getSentenceInstance(Locale locale) {
148 return getBreakInstance(locale,
149 SENTENCE_INDEX,
150 "SentenceData",
151 "SentenceDictionary");
152 }
153
154 private BreakIterator getBreakInstance(Locale locale,
155 int type,
156 String dataName,
157 String dictionaryName) {
158 if (locale == null) {
159 throw new NullPointerException();
160 }
161
162 ResourceBundle bundle = LocaleData.getBundle(
163 LocaleProviderAdapter.Type.JRE.getTextResourcesPackage() + ".BreakIteratorInfo", locale);
164 String[] classNames = bundle.getStringArray("BreakIteratorClasses");
165
166 String dataFile = bundle.getString(dataName);
167
168 try {
169 switch (classNames[type]) {
170 case "RuleBasedBreakIterator":
171 return new RuleBasedBreakIterator(dataFile);
172 case "DictionaryBasedBreakIterator":
173 String dictionaryFile = bundle.getString(dictionaryName);
174 return new DictionaryBasedBreakIterator(dataFile, dictionaryFile);
175 default:
176 throw new IllegalArgumentException("Invalid break iterator class \"" +
177 classNames[type] + "\"");
178 }
179 } catch (Exception e) {
180 throw new InternalError(e.toString(), e);
181 }
182 }
183
184 @Override
185 public Set<String> getAvailableLanguageTags() {
186 return langtags;
187 }
188
189 @Override
190 public boolean isSupportedLocale(Locale locale) {
191 return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
192 }
193 }
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.util.locale.provider;
27
28 import java.io.IOException;
29 import java.text.BreakIterator;
30 import java.text.spi.BreakIteratorProvider;
31 import java.util.Locale;
32 import java.util.MissingResourceException;
33 import java.util.Set;
34
35 /**
36 * Concrete implementation of the {@link java.text.spi.BreakIteratorProvider
37 * BreakIteratorProvider} class for the JRE LocaleProviderAdapter.
38 *
39 * @author Naoto Sato
40 * @author Masayoshi Okutsu
41 */
42 public class BreakIteratorProviderImpl extends BreakIteratorProvider
43 implements AvailableLanguageTags {
44
45 private static final int CHARACTER_INDEX = 0;
46 private static final int WORD_INDEX = 1;
47 private static final int LINE_INDEX = 2;
48 private static final int SENTENCE_INDEX = 3;
49
50 private final LocaleProviderAdapter.Type type;
51 private final Set<String> langtags;
52
53 public BreakIteratorProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
142 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
143 * getAvailableLocales()}.
144 * @see java.text.BreakIterator#getSentenceInstance(java.util.Locale)
145 */
146 @Override
147 public BreakIterator getSentenceInstance(Locale locale) {
148 return getBreakInstance(locale,
149 SENTENCE_INDEX,
150 "SentenceData",
151 "SentenceDictionary");
152 }
153
154 private BreakIterator getBreakInstance(Locale locale,
155 int type,
156 String dataName,
157 String dictionaryName) {
158 if (locale == null) {
159 throw new NullPointerException();
160 }
161
162 LocaleResources lr = LocaleProviderAdapter.forJRE().getLocaleResources(locale);
163 String[] classNames = (String[]) lr.getBreakIteratorInfo("BreakIteratorClasses");
164 String dataFile = (String) lr.getBreakIteratorInfo(dataName);
165
166 try {
167 switch (classNames[type]) {
168 case "RuleBasedBreakIterator":
169 return new RuleBasedBreakIterator(dataFile);
170 case "DictionaryBasedBreakIterator":
171 String dictionaryFile = (String) lr.getBreakIteratorInfo(dictionaryName);
172 return new DictionaryBasedBreakIterator(dataFile, dictionaryFile);
173 default:
174 throw new IllegalArgumentException("Invalid break iterator class \"" +
175 classNames[type] + "\"");
176 }
177 } catch (IOException | MissingResourceException | IllegalArgumentException e) {
178 throw new InternalError(e.toString(), e);
179 }
180 }
181
182 @Override
183 public Set<String> getAvailableLanguageTags() {
184 return langtags;
185 }
186
187 @Override
188 public boolean isSupportedLocale(Locale locale) {
189 return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
190 }
191 }
|