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 /* 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 Set<String> langtags; 58 59 public CollatorProviderImpl(LocaleProviderAdapter.Type type) { 60 this.type = type; 61 } 62 63 /** 64 * Returns an array of all locales for which this locale service provider 65 * can provide localized objects or names. 66 * 67 * @return An array of all locales for which this locale service provider 68 * can provide localized objects or names. 69 */ 70 @Override 71 public Locale[] getAvailableLocales() { 72 return LocaleProviderAdapter.toLocaleArray(getLangTags()); 73 } 74 75 @Override 76 public boolean isSupportedLocale(Locale locale) { 77 return LocaleProviderAdapter.isSupportedLocale(locale, type, getLangTags()); 78 } 79 80 /** 81 * Returns a new <code>Collator</code> instance for the specified locale. 82 * @param locale the desired locale. 83 * @return the <code>Collator</code> for the desired locale. 84 * @exception NullPointerException if 85 * <code>locale</code> is null 86 * @exception IllegalArgumentException if <code>locale</code> isn't 87 * one of the locales returned from 88 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() 89 * getAvailableLocales()}. 90 * @see java.text.Collator#getInstance(java.util.Locale) 91 */ 92 @Override 93 public Collator getInstance(Locale locale) { 94 if (locale == null) { 95 throw new NullPointerException(); 96 } 97 98 Collator result = null; 99 100 // Load the resource of the desired locale from resource 101 // manager. 102 String colString = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCollationData(); 103 try 104 { 105 result = new RuleBasedCollator(CollationRules.DEFAULTRULES + 106 colString); 107 } 108 catch(ParseException foo) 109 { 110 // predefined tables should contain correct grammar 111 try { 112 result = new RuleBasedCollator(CollationRules.DEFAULTRULES); 113 } catch (ParseException bar) { 114 // the default rules should always be parsable. 115 throw new InternalError(bar); 116 } 117 } 118 // Now that RuleBasedCollator adds expansions for pre-composed characters 119 // into their decomposed equivalents, the default collators don't need 120 // to have decomposition turned on. Laura, 5/5/98, bug 4114077 121 result.setDecomposition(Collator.NO_DECOMPOSITION); 122 123 return (Collator)result.clone(); 124 } 125 126 @Override 127 public Set<String> getAvailableLanguageTags() { 128 return getLangTags(); 129 } 130 131 private Set<String> getLangTags() { 132 if (langtags == null) { 133 langtags = ((JRELocaleProviderAdapter)LocaleProviderAdapter.forType(type)).getLanguageTagSet("CollationData"); 134 } 135 return langtags; 136 } 137 }