1 /*
   2  * Copyright (c) 2012, 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.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 }