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 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) { 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", 88 "WordDictionary"); 89 } 90 91 /** 92 * Returns a new <code>BreakIterator</code> instance 93 * for <a href="../BreakIterator.html#line">line breaks</a> 94 * for the given locale. 95 * @param locale the desired locale 96 * @return A break iterator for line breaks 97 * @exception NullPointerException if <code>locale</code> is null 98 * @exception IllegalArgumentException if <code>locale</code> isn't 99 * one of the locales returned from 100 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() 101 * getAvailableLocales()}. 102 * @see java.text.BreakIterator#getLineInstance(java.util.Locale) 103 */ 104 @Override 105 public BreakIterator getLineInstance(Locale locale) { 106 return getBreakInstance(locale, 107 LINE_INDEX, 108 "LineData", 109 "LineDictionary"); 110 } 111 112 /** 113 * Returns a new <code>BreakIterator</code> instance 114 * for <a href="../BreakIterator.html#character">character breaks</a> 115 * for the given locale. 116 * @param locale the desired locale 117 * @return A break iterator for character breaks 118 * @exception NullPointerException if <code>locale</code> is null 119 * @exception IllegalArgumentException if <code>locale</code> isn't 120 * one of the locales returned from 121 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() 122 * getAvailableLocales()}. 123 * @see java.text.BreakIterator#getCharacterInstance(java.util.Locale) 124 */ 125 @Override 126 public BreakIterator getCharacterInstance(Locale locale) { 127 return getBreakInstance(locale, 128 CHARACTER_INDEX, 129 "CharacterData", 130 "CharacterDictionary"); 131 } 132 133 /** 134 * Returns a new <code>BreakIterator</code> instance 135 * for <a href="../BreakIterator.html#sentence">sentence breaks</a> 136 * for the given locale. 137 * @param locale the desired locale 138 * @return A break iterator for sentence breaks 139 * @exception NullPointerException if <code>locale</code> is null 140 * @exception IllegalArgumentException if <code>locale</code> isn't 141 * one of the locales returned from 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.forType(type).isSupportedProviderLocale(locale, langtags); 190 } 191 }