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.cldr; 27 28 import java.security.AccessController; 29 import java.security.PrivilegedActionException; 30 import java.security.PrivilegedExceptionAction; 31 import java.text.spi.BreakIteratorProvider; 32 import java.text.spi.CollatorProvider; 33 import java.util.Collections; 34 import java.util.HashSet; 35 import java.util.Locale; 36 import java.util.ServiceLoader; 37 import java.util.Set; 38 import java.util.StringTokenizer; 39 import sun.util.locale.provider.JRELocaleProviderAdapter; 40 import sun.util.locale.provider.LocaleProviderAdapter; 41 import sun.util.locale.provider.LocaleDataMetaInfo; 42 43 /** 44 * LocaleProviderAdapter implementation for the CLDR locale data. 45 * 46 * @author Masayoshi Okutsu 47 * @author Naoto Sato 48 */ 49 public class CLDRLocaleProviderAdapter extends JRELocaleProviderAdapter { 50 51 private final LocaleDataMetaInfo metaInfo; 52 53 public CLDRLocaleProviderAdapter() { 54 try { 55 metaInfo = AccessController.doPrivileged(new PrivilegedExceptionAction<LocaleDataMetaInfo>() { 56 @Override 57 public LocaleDataMetaInfo run() { 58 for (LocaleDataMetaInfo ldmi : ServiceLoader.loadInstalled(LocaleDataMetaInfo.class)) { 59 if (ldmi.getType() == LocaleProviderAdapter.Type.CLDR) { 60 return ldmi; 61 } 62 } 63 return null; 64 } 65 }); 66 } catch (Exception e) { 67 // Catch any exception, and fail gracefully as if CLDR locales do not exist. 68 // It's ok ignore it if something wrong happens because there always is the 69 // JRE or FALLBACK LocaleProviderAdapter that will do the right thing. 70 throw new UnsupportedOperationException(e); 71 } 72 73 if (metaInfo == null) { 74 throw new UnsupportedOperationException("CLDR locale data could not be found."); 75 } 76 } 77 78 /** 79 * Returns the type of this LocaleProviderAdapter 80 * @return the type of this 81 */ 82 @Override 83 public LocaleProviderAdapter.Type getAdapterType() { 84 return LocaleProviderAdapter.Type.CLDR; 85 } 86 87 @Override 88 public BreakIteratorProvider getBreakIteratorProvider() { 89 return null; 90 } 91 92 @Override 93 public CollatorProvider getCollatorProvider() { 94 return null; 95 } 96 97 @Override 98 public Locale[] getAvailableLocales() { 99 Set<String> all = createLanguageTagSet("AvailableLocales"); 100 Locale[] locs = new Locale[all.size()]; 101 int index = 0; 102 for (String tag : all) { 103 locs[index++] = Locale.forLanguageTag(tag); 104 } 105 return locs; 106 } 107 108 @Override 109 protected Set<String> createLanguageTagSet(String category) { 110 String supportedLocaleString = metaInfo.availableLanguageTags(category); 111 if (supportedLocaleString == null) { 112 return Collections.emptySet(); 113 } 114 Set<String> tagset = new HashSet<>(); 115 StringTokenizer tokens = new StringTokenizer(supportedLocaleString); 116 while (tokens.hasMoreTokens()) { 117 tagset.add(tokens.nextToken()); 118 } 119 return tagset; 120 } 121 } | 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.cldr; 27 28 import java.security.AccessController; 29 import java.security.PrivilegedActionException; 30 import java.security.PrivilegedExceptionAction; 31 import java.text.spi.BreakIteratorProvider; 32 import java.text.spi.CollatorProvider; 33 import java.util.Arrays; 34 import java.util.Collections; 35 import java.util.HashMap; 36 import java.util.HashSet; 37 import java.util.List; 38 import java.util.Locale; 39 import java.util.Map; 40 import java.util.Objects; 41 import java.util.ServiceLoader; 42 import java.util.Set; 43 import java.util.StringTokenizer; 44 import sun.util.locale.provider.JRELocaleProviderAdapter; 45 import sun.util.locale.provider.LocaleProviderAdapter; 46 import sun.util.locale.provider.LocaleDataMetaInfo; 47 48 /** 49 * LocaleProviderAdapter implementation for the CLDR locale data. 50 * 51 * @author Masayoshi Okutsu 52 * @author Naoto Sato 53 */ 54 public class CLDRLocaleProviderAdapter extends JRELocaleProviderAdapter { 55 56 private static final CLDRBaseLocaleDataMetaInfo baseMetaInfo = new CLDRBaseLocaleDataMetaInfo(); 57 // Assumption: CLDR has only one non-Base module. 58 private final LocaleDataMetaInfo nonBaseMetaInfo; 59 60 // parent locales map 61 private static Map<Locale, Locale> parentLocalesMap = null; 62 63 public CLDRLocaleProviderAdapter() { 64 try { 65 nonBaseMetaInfo = AccessController.doPrivileged(new PrivilegedExceptionAction<LocaleDataMetaInfo>() { 66 @Override 67 public LocaleDataMetaInfo run() { 68 for (LocaleDataMetaInfo ldmi : ServiceLoader.loadInstalled(LocaleDataMetaInfo.class)) { 69 if (ldmi.getType() == LocaleProviderAdapter.Type.CLDR) { 70 return ldmi; 71 } 72 } 73 return null; 74 } 75 }); 76 } catch (Exception e) { 77 // Catch any exception, and fail gracefully as if CLDR locales do not exist. 78 // It's ok ignore it if something wrong happens because there always is the 79 // JRE or FALLBACK LocaleProviderAdapter that will do the right thing. 80 throw new UnsupportedOperationException(e); 81 } 82 83 if (nonBaseMetaInfo == null) { 84 throw new UnsupportedOperationException("CLDR locale data could not be found."); 85 } 86 } 87 88 /** 89 * Returns the type of this LocaleProviderAdapter 90 * @return the type of this 91 */ 92 @Override 93 public LocaleProviderAdapter.Type getAdapterType() { 94 return LocaleProviderAdapter.Type.CLDR; 95 } 96 97 @Override 98 public BreakIteratorProvider getBreakIteratorProvider() { 99 return null; 100 } 101 102 @Override 103 public CollatorProvider getCollatorProvider() { 104 return null; 105 } 106 107 @Override 108 public Locale[] getAvailableLocales() { 109 Set<String> all = createLanguageTagSet("AvailableLocales"); 110 Locale[] locs = new Locale[all.size()]; 111 int index = 0; 112 for (String tag : all) { 113 locs[index++] = Locale.forLanguageTag(tag); 114 } 115 return locs; 116 } 117 118 @Override 119 protected Set<String> createLanguageTagSet(String category) { 120 // Directly call Base tags, as we know it's in the base module. 121 String supportedLocaleString = baseMetaInfo.availableLanguageTags(category); 122 String nonBaseTags = nonBaseMetaInfo.availableLanguageTags(category); 123 if (nonBaseTags != null) { 124 if (supportedLocaleString != null) { 125 supportedLocaleString += " " + nonBaseTags; 126 } else { 127 supportedLocaleString = nonBaseTags; 128 } 129 } 130 if (supportedLocaleString == null) { 131 return Collections.emptySet(); 132 } 133 Set<String> tagset = new HashSet<>(); 134 StringTokenizer tokens = new StringTokenizer(supportedLocaleString); 135 while (tokens.hasMoreTokens()) { 136 tagset.add(tokens.nextToken()); 137 } 138 return tagset; 139 } 140 141 // Implementation of ResourceBundleBasedAdapter 142 @Override 143 public List<Locale> getCandidateLocales(String baseName, Locale locale) { 144 List<Locale> candidates = super.getCandidateLocales(baseName, locale); 145 return applyParentLocales(baseName, candidates); 146 } 147 148 private List<Locale> applyParentLocales(String baseName, List<Locale> candidates) { 149 if (Objects.isNull(parentLocalesMap)) { 150 parentLocalesMap = new HashMap<>(); 151 Map<String, String> parentLocales = baseMetaInfo.parentLocales(); 152 parentLocales.keySet().forEach(parent -> { 153 Arrays.asList(parentLocales.get(parent).split(" ")).stream().forEach(child -> { 154 parentLocalesMap.put(Locale.forLanguageTag(child), 155 "root".equals(parent) ? Locale.ROOT : Locale.forLanguageTag(parent)); 156 }); 157 }); 158 } 159 160 // check irregular parents 161 for (int i = 0; i < candidates.size(); i++) { 162 Locale l = candidates.get(i); 163 Locale p = parentLocalesMap.get(l); 164 if (!l.equals(Locale.ROOT) && 165 Objects.nonNull(p) && 166 !candidates.get(i+1).equals(p)) { 167 List<Locale> applied = candidates.subList(0, i+1); 168 applied.addAll(applyParentLocales(baseName, super.getCandidateLocales(baseName, p))); 169 return applied; 170 } 171 } 172 173 return candidates; 174 } 175 176 @Override 177 public boolean isSupportedProviderLocale(Locale locale, Set<String> langtags) { 178 return Locale.ROOT.equals(locale) || 179 langtags.contains(locale.stripExtensions().toLanguageTag()); 180 } 181 } |