< prev index next >

make/jdk/src/classes/build/tools/cldrconverter/ResourceBundleGenerator.java

Print this page




  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.Arrays;
  32 import java.util.Formatter;
  33 import java.util.HashSet;
  34 import java.util.HashMap;
  35 import java.util.LinkedHashMap;

  36 import java.util.Map;
  37 import java.util.Locale;
  38 import java.util.Objects;
  39 import java.util.Set;
  40 import java.util.SortedSet;
  41 
  42 class ResourceBundleGenerator implements BundleGenerator {
  43     // preferred timezones - keeping compatibility with JDK1.1 3 letter abbreviations
  44     private static final String[] preferredTZIDs = {
  45         "America/Los_Angeles",
  46         "America/Denver",
  47         "America/Phoenix",
  48         "America/Chicago",
  49         "America/New_York",
  50         "America/Indianapolis",
  51         "Pacific/Honolulu",
  52         "America/Anchorage",
  53         "America/Halifax",
  54         "America/Sitka",
  55         "America/St_Johns",


  89         File dir = new File(dirName);
  90         if (!dir.exists()) {
  91             dir.mkdirs();
  92         }
  93         File file = new File(dir, baseName + ("root".equals(localeID) ? "" : "_" + localeID) + suffix);
  94         if (!file.exists()) {
  95             file.createNewFile();
  96         }
  97         CLDRConverter.info("\tWriting file " + file);
  98 
  99         String encoding;
 100         if (useJava) {
 101             if (CLDRConverter.USE_UTF8) {
 102                 encoding = "utf-8";
 103             } else {
 104                 encoding = "us-ascii";
 105             }
 106         } else {
 107             encoding = "iso-8859-1";
 108         }
 109 
 110         Formatter fmt = null;
 111         if (type == BundleType.TIMEZONE) {
 112             fmt = new Formatter();
 113             Set<String> metaKeys = new HashSet<>();
 114             for (String key : map.keySet()) {
 115                 if (key.startsWith(CLDRConverter.METAZONE_ID_PREFIX)) {
 116                     String meta = key.substring(CLDRConverter.METAZONE_ID_PREFIX.length());
 117                     String[] value;
 118                     value = (String[]) map.get(key);
 119                     fmt.format("        final String[] %s = new String[] {\n", meta);
 120                     for (String s : value) {
 121                         fmt.format("               \"%s\",\n", CLDRConverter.saveConvert(s, useJava));
 122                     }
 123                     fmt.format("            };\n");
 124                     metaKeys.add(key);
 125                 }
 126             }
 127             for (String key : metaKeys) {
 128                 map.remove(key);
 129             }


 149 
 150                 for (Map.Entry<String, ?> entry : map.entrySet()) {
 151                     String k = entry.getKey();
 152                     if (!k.equals(key) &&
 153                         Objects.deepEquals(val, entry.getValue()) &&
 154                         !(Objects.nonNull(newMap) && newMap.containsKey(k))) {
 155                         if (Objects.isNull(newMap)) {
 156                             newMap = new HashMap<>();
 157                             fmt = new Formatter();
 158                         }
 159 
 160                         if (Objects.isNull(metaVal)) {
 161                             metaVal = META_VALUE_PREFIX + key.replaceAll("\\.", "_");
 162 
 163                             if (val instanceof String[]) {
 164                                 fmt.format("        final String[] %s = new String[] {\n", metaVal);
 165                                 for (String s : (String[])val) {
 166                                     fmt.format("               \"%s\",\n", CLDRConverter.saveConvert(s, useJava));
 167                                 }
 168                                 fmt.format("            };\n");








 169                             } else {
 170                                 fmt.format("        final String %s = \"%s\";\n", metaVal, CLDRConverter.saveConvert((String)val, useJava));
 171                             }
 172                         }
 173 
 174                         newMap.put(k, metaVal);
 175                     }
 176                 }
 177 
 178                 if (Objects.nonNull(metaVal)) {
 179                     newMap.put(key, metaVal);
 180                 }
 181             }
 182 
 183             if (Objects.nonNull(newMap)) {
 184                 for (String key : map.keySet()) {
 185                     newMap.putIfAbsent(key, map.get(key));
 186                 }
 187                 map = newMap;
 188             }
 189         }
 190 
 191         try (PrintWriter out = new PrintWriter(file, encoding)) {
 192             // Output copyright headers
 193             out.println(CopyrightHeaders.getOpenJDKCopyright());
 194             out.println(CopyrightHeaders.getUnicodeCopyright());
 195 
 196             if (useJava) {
 197                 out.println("package sun." + packageName + ";\n");






 198                 out.printf("import %s;\n\n", type.getPathName());
 199                 out.printf("public class %s%s extends %s {\n", baseName, "root".equals(localeID) ? "" : "_" + localeID, type.getClassName());
 200 
 201                 out.println("    @Override\n" +
 202                             "    protected final Object[][] getContents() {");
 203                 if (fmt != null) {
 204                     out.print(fmt.toString());
 205                 }
 206                 out.println("        final Object[][] data = new Object[][] {");
 207             }
 208             for (String key : map.keySet()) {
 209                 if (useJava) {
 210                     Object value = map.get(key);
 211                     if (value == null) {
 212                         CLDRConverter.warning("null value for " + key);
 213                     } else if (value instanceof String) {
 214                         String valStr = (String)value;
 215                         if (type == BundleType.TIMEZONE &&
 216                             !key.startsWith(CLDRConverter.EXEMPLAR_CITY_PREFIX) ||
 217                             valStr.startsWith(META_VALUE_PREFIX)) {
 218                             out.printf("            { \"%s\", %s },\n", key, CLDRConverter.saveConvert(valStr, useJava));
 219                         } else {
 220                             out.printf("            { \"%s\", \"%s\" },\n", key, CLDRConverter.saveConvert(valStr, useJava));
 221                         }
 222                     } else if (value instanceof String[]) {
 223                         String[] values = (String[]) value;
 224                         out.println("            { \"" + key + "\",\n                new String[] {");
 225                         for (String s : values) {
 226                             out.println("                    \"" + CLDRConverter.saveConvert(s, useJava) + "\",");
 227                         }
 228                         out.println("                }\n            },");








 229                     } else {
 230                         throw new RuntimeException("unknown value type: " + value.getClass().getName());
 231                     }
 232                 } else {
 233                     out.println(key + "=" + CLDRConverter.saveConvert((String) map.get(key), useJava));
 234                 }
 235             }
 236             if (useJava) {
 237                 out.println("        };\n        return data;\n    }\n}");
 238             }
 239         }
 240     }
 241 
 242     @Override
 243     public void generateMetaInfo(Map<String, SortedSet<String>> metaInfo) throws IOException {
 244         String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator + "util" +
 245             File.separator +
 246             (CLDRConverter.isBaseModule ? "cldr" + File.separator + File.separator :
 247                       "resources" + File.separator + "cldr" + File.separator + "provider" + File.separator);
 248         File dir = new File(dirName);




  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.Arrays;
  32 import java.util.Formatter;
  33 import java.util.HashSet;
  34 import java.util.HashMap;
  35 import java.util.LinkedHashMap;
  36 import java.util.List;
  37 import java.util.Map;
  38 import java.util.Locale;
  39 import java.util.Objects;
  40 import java.util.Set;
  41 import java.util.SortedSet;
  42 
  43 class ResourceBundleGenerator implements BundleGenerator {
  44     // preferred timezones - keeping compatibility with JDK1.1 3 letter abbreviations
  45     private static final String[] preferredTZIDs = {
  46         "America/Los_Angeles",
  47         "America/Denver",
  48         "America/Phoenix",
  49         "America/Chicago",
  50         "America/New_York",
  51         "America/Indianapolis",
  52         "Pacific/Honolulu",
  53         "America/Anchorage",
  54         "America/Halifax",
  55         "America/Sitka",
  56         "America/St_Johns",


  90         File dir = new File(dirName);
  91         if (!dir.exists()) {
  92             dir.mkdirs();
  93         }
  94         File file = new File(dir, baseName + ("root".equals(localeID) ? "" : "_" + localeID) + suffix);
  95         if (!file.exists()) {
  96             file.createNewFile();
  97         }
  98         CLDRConverter.info("\tWriting file " + file);
  99 
 100         String encoding;
 101         if (useJava) {
 102             if (CLDRConverter.USE_UTF8) {
 103                 encoding = "utf-8";
 104             } else {
 105                 encoding = "us-ascii";
 106             }
 107         } else {
 108             encoding = "iso-8859-1";
 109         }
 110         boolean containsList = false;
 111         Formatter fmt = null;
 112         if (type == BundleType.TIMEZONE) {
 113             fmt = new Formatter();
 114             Set<String> metaKeys = new HashSet<>();
 115             for (String key : map.keySet()) {
 116                 if (key.startsWith(CLDRConverter.METAZONE_ID_PREFIX)) {
 117                     String meta = key.substring(CLDRConverter.METAZONE_ID_PREFIX.length());
 118                     String[] value;
 119                     value = (String[]) map.get(key);
 120                     fmt.format("        final String[] %s = new String[] {\n", meta);
 121                     for (String s : value) {
 122                         fmt.format("               \"%s\",\n", CLDRConverter.saveConvert(s, useJava));
 123                     }
 124                     fmt.format("            };\n");
 125                     metaKeys.add(key);
 126                 }
 127             }
 128             for (String key : metaKeys) {
 129                 map.remove(key);
 130             }


 150 
 151                 for (Map.Entry<String, ?> entry : map.entrySet()) {
 152                     String k = entry.getKey();
 153                     if (!k.equals(key) &&
 154                         Objects.deepEquals(val, entry.getValue()) &&
 155                         !(Objects.nonNull(newMap) && newMap.containsKey(k))) {
 156                         if (Objects.isNull(newMap)) {
 157                             newMap = new HashMap<>();
 158                             fmt = new Formatter();
 159                         }
 160 
 161                         if (Objects.isNull(metaVal)) {
 162                             metaVal = META_VALUE_PREFIX + key.replaceAll("\\.", "_");
 163 
 164                             if (val instanceof String[]) {
 165                                 fmt.format("        final String[] %s = new String[] {\n", metaVal);
 166                                 for (String s : (String[])val) {
 167                                     fmt.format("               \"%s\",\n", CLDRConverter.saveConvert(s, useJava));
 168                                 }
 169                                 fmt.format("            };\n");
 170                             } else if (val instanceof List) {
 171                                 containsList = true;
 172                                 List<String> values = (List) val;
 173                                 fmt.format("        final List<String> %s = List.of (\n", metaVal);
 174                                 for (int index = 0; index < values.size() - 1; index++) {
 175                                     fmt.format("               \"%s\",\n", CLDRConverter.saveConvert(values.get(index), useJava));
 176                                 }
 177                                 fmt.format("               \"%s\");\n", CLDRConverter.saveConvert(values.get(values.size() - 1), useJava));
 178                             } else {
 179                                 fmt.format("        final String %s = \"%s\";\n", metaVal, CLDRConverter.saveConvert((String)val, useJava));
 180                             }
 181                         }
 182 
 183                         newMap.put(k, metaVal);
 184                     }
 185                 }
 186 
 187                 if (Objects.nonNull(metaVal)) {
 188                     newMap.put(key, metaVal);
 189                 }
 190             }
 191 
 192             if (Objects.nonNull(newMap)) {
 193                 for (String key : map.keySet()) {
 194                     newMap.putIfAbsent(key, map.get(key));
 195                 }
 196                 map = newMap;
 197             }
 198         }
 199 
 200         try (PrintWriter out = new PrintWriter(file, encoding)) {
 201             // Output copyright headers
 202             out.println(CopyrightHeaders.getOpenJDKCopyright());
 203             out.println(CopyrightHeaders.getUnicodeCopyright());
 204 
 205             if (useJava) {
 206                 out.println("package sun." + packageName + ";\n");
 207                 // only in the FormatData resource bundles which contains
 208                 // list in map
 209                 if (baseName.equals("FormatData") && (containsList
 210                         || map.values().stream().anyMatch(v -> v instanceof List))) {
 211                     out.printf("import java.util.List;\n\n");
 212                 }
 213                 out.printf("import %s;\n\n", type.getPathName());
 214                 out.printf("public class %s%s extends %s {\n", baseName, "root".equals(localeID) ? "" : "_" + localeID, type.getClassName());
 215 
 216                 out.println("    @Override\n" +
 217                             "    protected final Object[][] getContents() {");
 218                 if (fmt != null) {
 219                     out.print(fmt.toString());
 220                 }
 221                 out.println("        final Object[][] data = new Object[][] {");
 222             }
 223             for (String key : map.keySet()) {
 224                 if (useJava) {
 225                     Object value = map.get(key);
 226                     if (value == null) {
 227                         CLDRConverter.warning("null value for " + key);
 228                     } else if (value instanceof String) {
 229                         String valStr = (String)value;
 230                         if (type == BundleType.TIMEZONE &&
 231                             !key.startsWith(CLDRConverter.EXEMPLAR_CITY_PREFIX) ||
 232                             valStr.startsWith(META_VALUE_PREFIX)) {
 233                             out.printf("            { \"%s\", %s },\n", key, CLDRConverter.saveConvert(valStr, useJava));
 234                         } else {
 235                             out.printf("            { \"%s\", \"%s\" },\n", key, CLDRConverter.saveConvert(valStr, useJava));
 236                         }
 237                     } else if (value instanceof String[]) {
 238                         String[] values = (String[]) value;
 239                         out.println("            { \"" + key + "\",\n                new String[] {");
 240                         for (String s : values) {
 241                             out.println("                    \"" + CLDRConverter.saveConvert(s, useJava) + "\",");
 242                         }
 243                         out.println("                }\n            },");
 244                     } else if (value instanceof List) {
 245                         List<String> values = (List) value;
 246                         out.println("            { \"" + key + "\",\n                List.of(");
 247                         for (int index = 0; index < values.size() - 1; index++) {
 248                             out.println("                    \"" + CLDRConverter.saveConvert(values.get(index), useJava) + "\",");
 249                         }
 250                         out.println("                    \"" + CLDRConverter.saveConvert(values.get(values.size() - 1), useJava) + "\"");
 251                         out.println("                )\n            },");
 252                     } else {
 253                         throw new RuntimeException("unknown value type: " + value.getClass().getName());
 254                     }
 255                 } else {
 256                     out.println(key + "=" + CLDRConverter.saveConvert((String) map.get(key), useJava));
 257                 }
 258             }
 259             if (useJava) {
 260                 out.println("        };\n        return data;\n    }\n}");
 261             }
 262         }
 263     }
 264 
 265     @Override
 266     public void generateMetaInfo(Map<String, SortedSet<String>> metaInfo) throws IOException {
 267         String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator + "util" +
 268             File.separator +
 269             (CLDRConverter.isBaseModule ? "cldr" + File.separator + File.separator :
 270                       "resources" + File.separator + "cldr" + File.separator + "provider" + File.separator);
 271         File dir = new File(dirName);


< prev index next >