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) { 55 this.type = type; 56 this.langtags = langtags; 57 } 58 59 /** 60 * Returns an array of all locales for which this locale service provider 61 * can provide localized objects or names. 62 * 63 * @return An array of all locales for which this locale service provider 64 * can provide localized objects or names. 65 */ 66 @Override 67 public Locale[] getAvailableLocales() { 68 return LocaleProviderAdapter.toLocaleArray(langtags); 69 } 70 71 /** 72 * Returns a new <code>BreakIterator</code> instance 73 * for <a href="../BreakIterator.html#word">word breaks</a> 74 * for the given locale. 75 * @param locale the desired locale 76 * @return A break iterator for word breaks 77 * @exception NullPointerException if <code>locale</code> is null 78 * @exception IllegalArgumentException if <code>locale</code> isn't 79 * one of the locales returned from 80 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() 81 * getAvailableLocales()}. 82 * @see java.text.BreakIterator#getWordInstance(java.util.Locale) 83 */ 84 @Override 85 public BreakIterator getWordInstance(Locale locale) { 86 return getBreakInstance(locale, 87 WORD_INDEX, 88 "WordData", 89 "WordDictionary"); 90 } 91 92 /** 93 * Returns a new <code>BreakIterator</code> instance 94 * for <a href="../BreakIterator.html#line">line breaks</a> 95 * for the given locale. 96 * @param locale the desired locale 97 * @return A break iterator for line breaks 98 * @exception NullPointerException if <code>locale</code> is null 99 * @exception IllegalArgumentException if <code>locale</code> isn't 100 * one of the locales returned from 101 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() 102 * getAvailableLocales()}. 103 * @see java.text.BreakIterator#getLineInstance(java.util.Locale) 104 */ 105 @Override 106 public BreakIterator getLineInstance(Locale locale) { 107 return getBreakInstance(locale, 108 LINE_INDEX, 109 "LineData", 110 "LineDictionary"); 111 } 112 113 /** 114 * Returns a new <code>BreakIterator</code> instance 115 * for <a href="../BreakIterator.html#character">character breaks</a> 116 * for the given locale. 117 * @param locale the desired locale 118 * @return A break iterator for character breaks 119 * @exception NullPointerException if <code>locale</code> is null 120 * @exception IllegalArgumentException if <code>locale</code> isn't 121 * one of the locales returned from 122 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() 123 * getAvailableLocales()}. 124 * @see java.text.BreakIterator#getCharacterInstance(java.util.Locale) 125 */ 126 @Override 127 public BreakIterator getCharacterInstance(Locale locale) { 128 return getBreakInstance(locale, 129 CHARACTER_INDEX, 130 "CharacterData", 131 "CharacterDictionary"); 132 } 133 134 /** 135 * Returns a new <code>BreakIterator</code> instance 136 * for <a href="../BreakIterator.html#sentence">sentence breaks</a> 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 }