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" + File.separator
 147                 + "cldr" + File.separator;
 148         File dir = new File(dirName);
 149         if (!dir.exists()) {
 150             dir.mkdirs();
 151         }
 152         File file = new File(dir, METAINFO_CLASS + ".java");
 153         if (!file.exists()) {
 154             file.createNewFile();
 155         }
 156         CLDRConverter.info("Generating file " + file);
 157 
 158         try (PrintWriter out = new PrintWriter(file, "us-ascii")) {
 159             out.println(CopyrightHeaders.getOpenJDKCopyright());
 160 
 161             out.println("package sun.util.cldr;\n\n"
 162                       + "import java.util.ListResourceBundle;\n");
 163             out.printf("public class %s extends ListResourceBundle {\n", METAINFO_CLASS);
 164             out.println("    @Override\n" +
 165                         "    protected final Object[][] getContents() {\n" +
 166                         "        final Object[][] data = new Object[][] {");
 167             for (String key : metaInfo.keySet()) {
 168                 out.printf("            { \"%s\",\n", key);
 169                 out.printf("              \"%s\" },\n", toLocaleList(metaInfo.get(key)));
 170             }
 171             out.println("        };\n        return data;\n    }\n}");
 172         }
 173     }
 174 
 175     private static String toLocaleList(SortedSet<String> set) {
 176         StringBuilder sb = new StringBuilder(set.size() * 6);
 177         for (String id : set) {
 178             if (!"root".equals(id)) {
 179                 if (sb.length() > 0) {
 180                     sb.append(' ');
 181                 }
 182                 sb.append(id);
 183             }
 184         }
 185         return sb.toString();
 186     }
 187 }