1 /*
2 * Copyright (c) 1999, 2015, 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
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.Objects;
34 import java.util.Set;
35
36 /**
37 * Concrete implementation of the {@link java.text.spi.BreakIteratorProvider
38 * BreakIteratorProvider} class for the JRE LocaleProviderAdapter.
39 *
40 * @author Naoto Sato
41 * @author Masayoshi Okutsu
42 */
43 public class BreakIteratorProviderImpl extends BreakIteratorProvider
44 implements AvailableLanguageTags {
45
46 private static final int CHARACTER_INDEX = 0;
47 private static final int WORD_INDEX = 1;
48 private static final int LINE_INDEX = 2;
49 private static final int SENTENCE_INDEX = 3;
50
51 private final LocaleProviderAdapter.Type type;
52 private final Set<String> langtags;
53
54 public BreakIteratorProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
137 * for the given locale.
138 * @param locale the desired locale
139 * @return A break iterator for sentence breaks
140 * @exception NullPointerException if <code>locale</code> is null
141 * @exception IllegalArgumentException if <code>locale</code> isn't
142 * one of the locales returned from
143 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
144 * getAvailableLocales()}.
145 * @see java.text.BreakIterator#getSentenceInstance(java.util.Locale)
146 */
147 @Override
148 public BreakIterator getSentenceInstance(Locale locale) {
149 return getBreakInstance(locale,
150 SENTENCE_INDEX,
151 "SentenceData",
152 "SentenceDictionary");
153 }
154
155 private BreakIterator getBreakInstance(Locale locale,
156 int type,
157 String dataName,
158 String dictionaryName) {
159 Objects.requireNonNull(locale);
160
161 LocaleResources lr = LocaleProviderAdapter.forJRE().getLocaleResources(locale);
162 String[] classNames = (String[]) lr.getBreakIteratorInfo("BreakIteratorClasses");
163 String dataFile = (String) lr.getBreakIteratorInfo(dataName);
164
165 try {
166 switch (classNames[type]) {
167 case "RuleBasedBreakIterator":
168 return new RuleBasedBreakIterator(
169 lr.getBreakIteratorDataModule(), dataFile);
170 case "DictionaryBasedBreakIterator":
171 String dictionaryFile = (String) lr.getBreakIteratorInfo(dictionaryName);
172 return new DictionaryBasedBreakIterator(
173 lr.getBreakIteratorDataModule(), dataFile, dictionaryFile);
174 default:
175 throw new IllegalArgumentException("Invalid break iterator class \"" +
176 classNames[type] + "\"");
177 }
178 } catch (IOException | MissingResourceException | IllegalArgumentException e) {
179 throw new InternalError(e.toString(), e);
180 }
181 }
182
183 @Override
184 public Set<String> getAvailableLanguageTags() {
185 return langtags;
186 }
187
188 @Override
189 public boolean isSupportedLocale(Locale locale) {
190 return LocaleProviderAdapter.forType(type).isSupportedProviderLocale(locale, langtags);
191 }
192 }
|
1 /*
2 * Copyright (c) 1999, 2016, 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
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.Objects;
34 import java.util.Set;
35 import sun.text.DictionaryBasedBreakIterator;
36 import sun.text.RuleBasedBreakIterator;
37
38 /**
39 * Concrete implementation of the {@link java.text.spi.BreakIteratorProvider
40 * BreakIteratorProvider} class for the JRE LocaleProviderAdapter.
41 *
42 * @author Naoto Sato
43 * @author Masayoshi Okutsu
44 */
45 public class BreakIteratorProviderImpl extends BreakIteratorProvider
46 implements AvailableLanguageTags {
47
48 private static final int CHARACTER_INDEX = 0;
49 private static final int WORD_INDEX = 1;
50 private static final int LINE_INDEX = 2;
51 private static final int SENTENCE_INDEX = 3;
52
53 private final LocaleProviderAdapter.Type type;
54 private final Set<String> langtags;
55
56 public BreakIteratorProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
139 * for the given locale.
140 * @param locale the desired locale
141 * @return A break iterator for sentence breaks
142 * @exception NullPointerException if <code>locale</code> is null
143 * @exception IllegalArgumentException if <code>locale</code> isn't
144 * one of the locales returned from
145 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
146 * getAvailableLocales()}.
147 * @see java.text.BreakIterator#getSentenceInstance(java.util.Locale)
148 */
149 @Override
150 public BreakIterator getSentenceInstance(Locale locale) {
151 return getBreakInstance(locale,
152 SENTENCE_INDEX,
153 "SentenceData",
154 "SentenceDictionary");
155 }
156
157 private BreakIterator getBreakInstance(Locale locale,
158 int type,
159 String ruleName,
160 String dictionaryName) {
161 Objects.requireNonNull(locale);
162
163 LocaleResources lr = LocaleProviderAdapter.forJRE().getLocaleResources(locale);
164 String[] classNames = (String[]) lr.getBreakIteratorInfo("BreakIteratorClasses");
165 String ruleFile = (String) lr.getBreakIteratorInfo(ruleName);
166 byte[] ruleData = lr.getBreakIteratorResources(ruleName);
167
168 try {
169 switch (classNames[type]) {
170 case "RuleBasedBreakIterator":
171 return new RuleBasedBreakIterator(ruleFile, ruleData);
172
173 case "DictionaryBasedBreakIterator":
174 String dictionaryFile = (String) lr.getBreakIteratorInfo(dictionaryName);
175 byte[] dictionaryData = lr.getBreakIteratorResources(dictionaryName);
176 return new DictionaryBasedBreakIterator(ruleFile, ruleData,
177 dictionaryFile, dictionaryData);
178 default:
179 throw new IllegalArgumentException("Invalid break iterator class \"" +
180 classNames[type] + "\"");
181 }
182 } catch (MissingResourceException | IllegalArgumentException e) {
183 throw new InternalError(e.toString(), e);
184 }
185 }
186
187 @Override
188 public Set<String> getAvailableLanguageTags() {
189 return langtags;
190 }
191
192 @Override
193 public boolean isSupportedLocale(Locale locale) {
194 return LocaleProviderAdapter.forType(type).isSupportedProviderLocale(locale, langtags);
195 }
196 }
|