src/java.base/share/classes/sun/util/cldr/CLDRLocaleProviderAdapter.java

Print this page
rev 10528 : 8038436: Re-examine the mechanism to determine available localedata and cldrdata
Reviewed-by:


   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.io.File;
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;

  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.ResourceBundle;
  37 import java.util.Set;
  38 import java.util.StringTokenizer;
  39 import java.util.spi.TimeZoneNameProvider;
  40 import sun.util.locale.provider.JRELocaleProviderAdapter;
  41 import sun.util.locale.provider.LocaleProviderAdapter;

  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     private static final String LOCALE_DATA_JAR_NAME = "cldrdata.jar";

  51 
  52     public CLDRLocaleProviderAdapter() {
  53         final String sep = File.separator;
  54         String localeDataJar = java.security.AccessController.doPrivileged(
  55                     new sun.security.action.GetPropertyAction("java.home"))
  56                 + sep + "lib" + sep + "ext" + sep + LOCALE_DATA_JAR_NAME;
  57 
  58         // Peek at the installed extension directory to see if the jar file for
  59         // CLDR resources is installed or not.
  60         final File f = new File(localeDataJar);
  61         boolean result = AccessController.doPrivileged(
  62                 new PrivilegedAction<Boolean>() {
  63                     @Override
  64                     public Boolean run() {
  65                         return f.exists();





  66                     }
  67                 });
  68         if (!result) {




  69             throw new UnsupportedOperationException();
  70         }
  71     }
  72 
  73     /**
  74      * Returns the type of this LocaleProviderAdapter
  75      * @return the type of this
  76      */
  77     @Override
  78     public LocaleProviderAdapter.Type getAdapterType() {
  79         return LocaleProviderAdapter.Type.CLDR;
  80     }
  81 
  82     @Override
  83     public BreakIteratorProvider getBreakIteratorProvider() {
  84         return null;
  85     }
  86 
  87     @Override
  88     public CollatorProvider getCollatorProvider() {
  89         return null;
  90     }
  91 
  92     @Override
  93     public Locale[] getAvailableLocales() {
  94         Set<String> all = createLanguageTagSet("All");
  95         Locale[] locs = new Locale[all.size()];
  96         int index = 0;
  97         for (String tag : all) {
  98             locs[index++] = Locale.forLanguageTag(tag);
  99         }
 100         return locs;
 101     }
 102 
 103     @Override
 104     protected Set<String> createLanguageTagSet(String category) {
 105         ResourceBundle rb = ResourceBundle.getBundle("sun.util.cldr.CLDRLocaleDataMetaInfo", Locale.ROOT);
 106         if (rb.containsKey(category)) {
 107             return Collections.emptySet();
 108         }
 109         String supportedLocaleString = rb.getString(category);
 110         Set<String> tagset = new HashSet<>();
 111         StringTokenizer tokens = new StringTokenizer(supportedLocaleString);
 112         while (tokens.hasMoreTokens()) {
 113             tagset.add(tokens.nextToken());
 114         }
 115         return tagset;
 116     }
 117 }


   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 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.
  68         }
  69 
  70         if (metaInfo == null) {
  71             throw new UnsupportedOperationException();
  72         }
  73     }
  74 
  75     /**
  76      * Returns the type of this LocaleProviderAdapter
  77      * @return the type of this
  78      */
  79     @Override
  80     public LocaleProviderAdapter.Type getAdapterType() {
  81         return LocaleProviderAdapter.Type.CLDR;
  82     }
  83 
  84     @Override
  85     public BreakIteratorProvider getBreakIteratorProvider() {
  86         return null;
  87     }
  88 
  89     @Override
  90     public CollatorProvider getCollatorProvider() {
  91         return null;
  92     }
  93 
  94     @Override
  95     public Locale[] getAvailableLocales() {
  96         Set<String> all = createLanguageTagSet("AvailableLocales");
  97         Locale[] locs = new Locale[all.size()];
  98         int index = 0;
  99         for (String tag : all) {
 100             locs[index++] = Locale.forLanguageTag(tag);
 101         }
 102         return locs;
 103     }
 104 
 105     @Override
 106     protected Set<String> createLanguageTagSet(String category) {
 107         String supportedLocaleString = metaInfo.availableLanguageTags(category);
 108         if (supportedLocaleString == null) {
 109             return Collections.emptySet();
 110         }

 111         Set<String> tagset = new HashSet<>();
 112         StringTokenizer tokens = new StringTokenizer(supportedLocaleString);
 113         while (tokens.hasMoreTokens()) {
 114             tagset.add(tokens.nextToken());
 115         }
 116         return tagset;
 117     }
 118 }