rev 10673 : 8058509: CLDRLocaleDataMetaInfo should be in jdk.localedata
Reviewed-by:

   1 /*
   2  * Copyright (c) 2012, 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 package build.tools.cldrconverter;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.PrintWriter;
  31 import java.util.Formatter;
  32 import java.util.HashSet;
  33 import java.util.Map;
  34 import java.util.Set;
  35 import java.util.SortedSet;
  36 
  37 class ResourceBundleGenerator implements BundleGenerator {
  38     @Override
  39     public void generateBundle(String packageName, String baseName, String localeID, boolean useJava,
  40                                Map<String, ?> map, BundleType type) throws IOException {
  41         String suffix = useJava ? ".java" : ".properties";
  42         String lang = CLDRConverter.getLanguageCode(localeID);
  43         String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator
  44                 + packageName + File.separator + "resources" + File.separator + "cldr";
  45         if (lang.length() > 0) {
  46             dirName = dirName + File.separator + lang;
  47             packageName = packageName + ".resources.cldr." + lang;
  48         } else {
  49             packageName = packageName + ".resources.cldr";
  50         }
  51         File dir = new File(dirName);
  52         if (!dir.exists()) {
  53             dir.mkdirs();
  54         }
  55         File file = new File(dir, baseName + ("root".equals(localeID) ? "" : "_" + localeID) + suffix);
  56         if (!file.exists()) {
  57             file.createNewFile();
  58         }
  59         CLDRConverter.info("\tWriting file " + file);
  60 
  61         String encoding;
  62         if (useJava) {
  63             if (CLDRConverter.USE_UTF8) {
  64                 encoding = "utf-8";
  65             } else {
  66                 encoding = "us-ascii";
  67             }
  68         } else {
  69             encoding = "iso-8859-1";
  70         }
  71 
  72         Formatter fmt = null;
  73         if (type == BundleType.TIMEZONE) {
  74             fmt = new Formatter();
  75             Set<String> metaKeys = new HashSet<>();
  76             for (String key : map.keySet()) {
  77                 if (key.startsWith(CLDRConverter.METAZONE_ID_PREFIX)) {
  78                     String meta = key.substring(CLDRConverter.METAZONE_ID_PREFIX.length());
  79                     String[] value;
  80                     value = (String[]) map.get(key);
  81                     fmt.format("        final String[] %s = new String[] {\n", meta);
  82                     for (String s : value) {
  83                         fmt.format("               \"%s\",\n", CLDRConverter.saveConvert(s, useJava));
  84                     }
  85                     fmt.format("            };\n");
  86                     metaKeys.add(key);
  87                 }
  88             }
  89             for (String key : metaKeys) {
  90                 map.remove(key);
  91             }
  92         }
  93 
  94         try (PrintWriter out = new PrintWriter(file, encoding)) {
  95             // Output copyright headers
  96             out.println(CopyrightHeaders.getOpenJDKCopyright());
  97             out.println(CopyrightHeaders.getUnicodeCopyright());
  98 
  99             if (useJava) {
 100                 out.println("package sun." + packageName + ";\n");
 101                 out.printf("import %s;\n\n", type.getPathName());
 102                 out.printf("public class %s%s extends %s {\n", baseName, "root".equals(localeID) ? "" : "_" + localeID, type.getClassName());
 103 
 104                 out.println("    @Override\n" +
 105                             "    protected final Object[][] getContents() {");
 106                 if (fmt != null) {
 107                     out.print(fmt.toString());
 108                 }
 109                 out.println("        final Object[][] data = new Object[][] {");
 110             }
 111             for (String key : map.keySet()) {
 112                 if (useJava) {
 113                     Object value = map.get(key);
 114                     if (value == null) {
 115                         CLDRConverter.warning("null value for " + key);
 116                     } else if (value instanceof String) {
 117                         if (type == BundleType.TIMEZONE) {
 118                             out.printf("            { \"%s\", %s },\n", key, CLDRConverter.saveConvert((String) value, useJava));
 119                         } else {
 120                             out.printf("            { \"%s\", \"%s\" },\n", key, CLDRConverter.saveConvert((String) value, useJava));
 121                         }
 122                     } else if (value instanceof String[]) {
 123                         String[] values = (String[]) value;
 124                         out.println("            { \"" + key + "\",\n                new String[] {");
 125                         for (String s : values) {
 126                             out.println("                    \"" + CLDRConverter.saveConvert(s, useJava) + "\",");
 127                         }
 128                         out.println("                }\n            },");
 129                     } else {
 130                         throw new RuntimeException("unknown value type: " + value.getClass().getName());
 131                     }
 132                 } else {
 133                     out.println(key + "=" + CLDRConverter.saveConvert((String) map.get(key), useJava));
 134                 }
 135             }
 136             if (useJava) {
 137                 out.println("        };\n        return data;\n    }\n}");
 138             }
 139         }
 140     }
 141 
 142     private static final String METAINFO_CLASS = "CLDRLocaleDataMetaInfo";
 143 
 144     @Override
 145     public void generateMetaInfo(Map<String, SortedSet<String>> metaInfo) throws IOException {
 146         String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator + "util" +
 147             File.separator + "resources" + File.separator + "cldr" + File.separator +
 148             "provider" + File.separator ;
 149         File dir = new File(dirName);
 150         if (!dir.exists()) {
 151             dir.mkdirs();
 152         }
 153         File file = new File(dir, METAINFO_CLASS + ".java");
 154         if (!file.exists()) {
 155             file.createNewFile();
 156         }
 157         CLDRConverter.info("Generating file " + file);
 158 
 159         try (PrintWriter out = new PrintWriter(file, "us-ascii")) {
 160             out.println(CopyrightHeaders.getOpenJDKCopyright());
 161 
 162             out.println("package sun.util.resources.cldr.provider;\n\n"
 163                       + "import java.util.ListResourceBundle;\n"
 164                       + "import sun.util.locale.provider.LocaleProviderAdapter;\n"
 165                       + "import sun.util.locale.provider.LocaleDataMetaInfo;\n");
 166             out.printf("public class %s extends ListResourceBundle implements LocaleDataMetaInfo {\n", METAINFO_CLASS);
 167             out.println("    @Override\n" +
 168                         "    protected final Object[][] getContents() {\n" +
 169                         "        final Object[][] data = new Object[][] {");
 170             for (String key : metaInfo.keySet()) {
 171                 out.printf("            { \"%s\",\n", key);
 172                 out.printf("              \"%s\" },\n", toLocaleList(metaInfo.get(key)));
 173             }
 174             out.println("        };\n        return data;\n    }\n\n");
 175 
 176             out.println("    public LocaleProviderAdapter.Type getType() {\n" +
 177                         "        return LocaleProviderAdapter.Type.CLDR;\n" +
 178                         "    }\n\n");
 179 
 180             out.println("    public String availableLanguageTags(String category) {\n" +
 181                         "        return getString(category);\n" +
 182                         "    };\n}");
 183         }
 184     }
 185 
 186     private static String toLocaleList(SortedSet<String> set) {
 187         StringBuilder sb = new StringBuilder(set.size() * 6);
 188         for (String id : set) {
 189             if (!"root".equals(id)) {
 190                 if (sb.length() > 0) {
 191                     sb.append(' ');
 192                 }
 193                 sb.append(id);
 194             }
 195         }
 196         return sb.toString();
 197     }
 198 }
--- EOF ---