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 /*
  27  *
  28  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  29  * (C) Copyright IBM Corp. 1996 - 2002 - All Rights Reserved
  30  *
  31  * The original version of this source code and documentation
  32  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  33  * subsidiary of IBM. These materials are provided under terms
  34  * of a License Agreement between Taligent and Sun. This technology
  35  * is protected by multiple US and International patents.
  36  *
  37  * This notice and attribution to Taligent may not be removed.
  38  * Taligent is a registered trademark of Taligent, Inc.
  39  */
  40 
  41 package sun.util.locale.provider;
  42 
  43 import java.text.Collator;
  44 import java.text.ParseException;
  45 import java.text.RuleBasedCollator;
  46 import java.text.spi.CollatorProvider;
  47 import java.util.Locale;
  48 import java.util.Set;
  49 
  50 /**
  51  * Concrete implementation of the
  52  * {@link java.text.spi.CollatorProvider CollatorProvider} class
  53  * for the JRE LocaleProviderAdapter.
  54  */
  55 public class CollatorProviderImpl extends CollatorProvider implements AvailableLanguageTags {
  56     private final LocaleProviderAdapter.Type type;
  57     private final Set<String> langtags;
  58 
  59     public CollatorProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  60         this.type = type;
  61         this.langtags = langtags;
  62     }
  63 
  64     /**
  65      * Returns an array of all locales for which this locale service provider
  66      * can provide localized objects or names.
  67      *
  68      * @return An array of all locales for which this locale service provider
  69      * can provide localized objects or names.
  70      */
  71     @Override
  72     public Locale[] getAvailableLocales() {
  73         return LocaleProviderAdapter.toLocaleArray(langtags);
  74     }
  75 
  76     @Override
  77     public boolean isSupportedLocale(Locale locale) {
  78         return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
  79     }
  80 
  81     /**
  82      * Returns a new <code>Collator</code> instance for the specified locale.
  83      * @param locale the desired locale.
  84      * @return the <code>Collator</code> for the desired locale.
  85      * @exception NullPointerException if
  86      * <code>locale</code> is null
  87      * @exception IllegalArgumentException if <code>locale</code> isn't
  88      *     one of the locales returned from
  89      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
  90      *     getAvailableLocales()}.
  91      * @see java.text.Collator#getInstance(java.util.Locale)
  92      */
  93     @Override
  94     public Collator getInstance(Locale locale) {
  95         if (locale == null) {
  96             throw new NullPointerException();
  97         }
  98 
  99         Collator result = null;
 100 
 101         // Load the resource of the desired locale from resource
 102         // manager.
 103         String colString = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCollationData();
 104         try
 105         {
 106             result = new RuleBasedCollator(CollationRules.DEFAULTRULES +
 107                                            colString);
 108         }
 109         catch(ParseException foo)
 110         {
 111             // predefined tables should contain correct grammar
 112             try {
 113                 result = new RuleBasedCollator(CollationRules.DEFAULTRULES);
 114             } catch (ParseException bar) {
 115                 // the default rules should always be parsable.
 116                 throw new InternalError(bar);
 117             }
 118         }
 119         // Now that RuleBasedCollator adds expansions for pre-composed characters
 120         // into their decomposed equivalents, the default collators don't need
 121         // to have decomposition turned on.  Laura, 5/5/98, bug 4114077
 122         result.setDecomposition(Collator.NO_DECOMPOSITION);
 123 
 124         return (Collator)result.clone();
 125     }
 126 
 127     @Override
 128     public Set<String> getAvailableLanguageTags() {
 129         return langtags;
 130     }
 131 }