1 /*
2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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
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) {
54 this.type = type;
55 this.langtags = langtags;
56 }
57
58 /**
59 * Returns an array of all locales for which this locale service provider
60 * can provide localized objects or names.
61 *
62 * @return An array of all locales for which this locale service provider
63 * can provide localized objects or names.
64 */
65 @Override
66 public Locale[] getAvailableLocales() {
67 return LocaleProviderAdapter.toLocaleArray(langtags);
68 }
69
70 /**
71 * Returns a new <code>BreakIterator</code> instance
72 * for <a href="../BreakIterator.html#word">word breaks</a>
73 * for the given locale.
74 * @param locale the desired locale
75 * @return A break iterator for word breaks
76 * @exception NullPointerException if <code>locale</code> is null
77 * @exception IllegalArgumentException if <code>locale</code> isn't
78 * one of the locales returned from
79 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
80 * getAvailableLocales()}.
81 * @see java.text.BreakIterator#getWordInstance(java.util.Locale)
82 */
83 @Override
84 public BreakIterator getWordInstance(Locale locale) {
85 return getBreakInstance(locale,
86 WORD_INDEX,
87 "WordData",
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 }
|
1 /*
2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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
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
52 public BreakIteratorProviderImpl(LocaleProviderAdapter.Type type) {
53 this.type = type;
54 }
55
56 /**
57 * Returns an array of all locales for which this locale service provider
58 * can provide localized objects or names.
59 *
60 * @return An array of all locales for which this locale service provider
61 * can provide localized objects or names.
62 */
63 @Override
64 public Locale[] getAvailableLocales() {
65 return LocaleProviderAdapter.toLocaleArray(DateFormatProviderImpl.getLangTags(type));
66 }
67
68 /**
69 * Returns a new <code>BreakIterator</code> instance
70 * for <a href="../BreakIterator.html#word">word breaks</a>
71 * for the given locale.
72 * @param locale the desired locale
73 * @return A break iterator for word breaks
74 * @exception NullPointerException if <code>locale</code> is null
75 * @exception IllegalArgumentException if <code>locale</code> isn't
76 * one of the locales returned from
77 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
78 * getAvailableLocales()}.
79 * @see java.text.BreakIterator#getWordInstance(java.util.Locale)
80 */
81 @Override
82 public BreakIterator getWordInstance(Locale locale) {
83 return getBreakInstance(locale,
84 WORD_INDEX,
85 "WordData",
162 String dataFile = (String) lr.getBreakIteratorInfo(dataName);
163
164 try {
165 switch (classNames[type]) {
166 case "RuleBasedBreakIterator":
167 return new RuleBasedBreakIterator(dataFile);
168 case "DictionaryBasedBreakIterator":
169 String dictionaryFile = (String) lr.getBreakIteratorInfo(dictionaryName);
170 return new DictionaryBasedBreakIterator(dataFile, dictionaryFile);
171 default:
172 throw new IllegalArgumentException("Invalid break iterator class \"" +
173 classNames[type] + "\"");
174 }
175 } catch (IOException | MissingResourceException | IllegalArgumentException e) {
176 throw new InternalError(e.toString(), e);
177 }
178 }
179
180 @Override
181 public Set<String> getAvailableLanguageTags() {
182 return DateFormatProviderImpl.getLangTags(type);
183 }
184
185 @Override
186 public boolean isSupportedLocale(Locale locale) {
187 return LocaleProviderAdapter.isSupportedLocale(locale, type, DateFormatProviderImpl.getLangTags(type));
188 }
189 }
|