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
  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 
  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",
  86                                 "WordDictionary");
  87     }
  88 
  89     /**
  90      * Returns a new <code>BreakIterator</code> instance
  91      * for <a href="../BreakIterator.html#line">line breaks</a>
  92      * for the given locale.
  93      * @param locale the desired locale
  94      * @return A break iterator for line breaks
  95      * @exception NullPointerException if <code>locale</code> is null
  96      * @exception IllegalArgumentException if <code>locale</code> isn't
  97      *     one of the locales returned from
  98      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
  99      *     getAvailableLocales()}.
 100      * @see java.text.BreakIterator#getLineInstance(java.util.Locale)
 101      */
 102     @Override
 103     public BreakIterator getLineInstance(Locale locale) {
 104         return getBreakInstance(locale,
 105                                 LINE_INDEX,
 106                                 "LineData",
 107                                 "LineDictionary");
 108     }
 109 
 110     /**
 111      * Returns a new <code>BreakIterator</code> instance
 112      * for <a href="../BreakIterator.html#character">character breaks</a>
 113      * for the given locale.
 114      * @param locale the desired locale
 115      * @return A break iterator for character breaks
 116      * @exception NullPointerException if <code>locale</code> is null
 117      * @exception IllegalArgumentException if <code>locale</code> isn't
 118      *     one of the locales returned from
 119      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 120      *     getAvailableLocales()}.
 121      * @see java.text.BreakIterator#getCharacterInstance(java.util.Locale)
 122      */
 123     @Override
 124     public BreakIterator getCharacterInstance(Locale locale) {
 125         return getBreakInstance(locale,
 126                                 CHARACTER_INDEX,
 127                                 "CharacterData",
 128                                 "CharacterDictionary");
 129     }
 130 
 131     /**
 132      * Returns a new <code>BreakIterator</code> instance
 133      * for <a href="../BreakIterator.html#sentence">sentence breaks</a>
 134      * for the given locale.
 135      * @param locale the desired locale
 136      * @return A break iterator for sentence breaks
 137      * @exception NullPointerException if <code>locale</code> is null
 138      * @exception IllegalArgumentException if <code>locale</code> isn't
 139      *     one of the locales returned from
 140      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 141      *     getAvailableLocales()}.
 142      * @see java.text.BreakIterator#getSentenceInstance(java.util.Locale)
 143      */
 144     @Override
 145     public BreakIterator getSentenceInstance(Locale locale) {
 146         return getBreakInstance(locale,
 147                                 SENTENCE_INDEX,
 148                                 "SentenceData",
 149                                 "SentenceDictionary");
 150     }
 151 
 152     private BreakIterator getBreakInstance(Locale locale,
 153                                                   int type,
 154                                                   String dataName,
 155                                                   String dictionaryName) {
 156         if (locale == null) {
 157             throw new NullPointerException();
 158         }
 159 
 160         LocaleResources lr = LocaleProviderAdapter.forJRE().getLocaleResources(locale);
 161         String[] classNames = (String[]) lr.getBreakIteratorInfo("BreakIteratorClasses");
 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 }