1 /* 2 * Copyright (c) 2012, 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.Map; 32 import java.util.SortedSet; 33 34 class ResourceBundleGenerator implements BundleGenerator { 35 36 @Override 37 public void generateBundle(String packageName, String baseName, String localeID, boolean useJava, 38 Map<String, ?> map, boolean open) throws IOException { 39 String suffix = useJava ? ".java" : ".properties"; 40 String lang = CLDRConverter.getLanguageCode(localeID); 41 String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator 42 + packageName + File.separator + "resources" + File.separator + "cldr"; 43 if (lang.length() > 0) { 44 dirName = dirName + File.separator + lang; 45 packageName = packageName + ".resources.cldr." + lang; 46 } else { 47 packageName = packageName + ".resources.cldr"; 48 } 49 File dir = new File(dirName); 50 if (!dir.exists()) { 51 dir.mkdirs(); 52 } 53 File file = new File(dir, baseName + ("root".equals(localeID) ? "" : "_" + localeID) + suffix); 54 if (!file.exists()) { 55 file.createNewFile(); 56 } 57 CLDRConverter.info("\tWriting file " + file); 58 59 String encoding; 60 if (useJava) { 61 if (CLDRConverter.USE_UTF8) { 62 encoding = "utf-8"; 63 } else { 64 encoding = "us-ascii"; 65 } 66 } else { 67 encoding = "iso-8859-1"; 68 } 69 70 try (PrintWriter out = new PrintWriter(file, encoding)) { 71 // Output copyright headers 72 out.println(CopyrightHeaders.getOpenJDKCopyright()); 73 out.println(CopyrightHeaders.getUnicodeCopyright()); 74 75 if (useJava) { 76 out.println("package sun." + packageName + ";\n"); 77 if (open) { 78 out.println("import sun.util.resources.OpenListResourceBundle;\n"); 79 out.println("public class " + baseName + ("root".equals(localeID) ? "" : "_" + localeID) + " extends OpenListResourceBundle {"); 80 } else { 81 out.println("import java.util.ListResourceBundle;\n"); 82 out.println("public class " + baseName + ("root".equals(localeID) ? "" : "_" + localeID) + " extends ListResourceBundle {"); 83 } 84 out.println(" @Override\n" + 85 " protected final Object[][] getContents() {\n" + 86 " final Object[][] data = new Object[][] {"); 87 } 88 for (String key : map.keySet()) { 89 if (useJava) { 90 Object value = map.get(key); 91 if (value == null) { 92 CLDRConverter.warning("null value for " + key); 93 } else if (value instanceof String) { 94 out.println(" { \"" + key + "\", \"" + CLDRConverter.saveConvert((String) value, useJava) + "\" },"); 95 } else if (value instanceof String[]) { 96 String[] values = (String[]) value; 97 out.println(" { \"" + key + "\",\n new String[] {"); 98 for (String s : values) { 99 out.println(" \"" + CLDRConverter.saveConvert(s, useJava) + "\","); 100 } 101 out.println(" }\n },"); 102 } else { 103 throw new RuntimeException("unknown value type: " + value.getClass().getName()); 104 } 105 } else { 106 out.println(key + "=" + CLDRConverter.saveConvert((String) map.get(key), useJava)); 107 } 108 } 109 if (useJava) { 110 out.println(" };\n return data;\n }\n}"); 111 } 112 } 113 } 114 115 private static final String METAINFO_CLASS = "CLDRLocaleDataMetaInfo"; 116 117 @Override 118 public void generateMetaInfo(Map<String, SortedSet<String>> metaInfo) throws IOException { 119 String dirName = CLDRConverter.DESTINATION_DIR + File.separator + "sun" + File.separator + "util" + File.separator 120 + "cldr" + File.separator; 121 File dir = new File(dirName); 122 if (!dir.exists()) { 123 dir.mkdirs(); 124 } 125 File file = new File(dir, METAINFO_CLASS + ".java"); 126 if (!file.exists()) { 127 file.createNewFile(); 128 } 129 CLDRConverter.info("Generating file " + file); 130 131 try (PrintWriter out = new PrintWriter(file, "us-ascii")) { 132 out.println(CopyrightHeaders.getOpenJDKCopyright()); 133 134 out.println("package sun.util.cldr;\n\n" 135 + "import java.util.ListResourceBundle;\n"); 136 out.printf("public class %s extends ListResourceBundle {\n", METAINFO_CLASS); 137 out.println(" @Override\n" + 138 " protected final Object[][] getContents() {\n" + 139 " final Object[][] data = new Object[][] {"); 140 for (String key : metaInfo.keySet()) { 141 out.printf(" { \"%s\",\n", key); 142 out.printf(" \"%s\" },\n", toLocaleList(metaInfo.get(key))); 143 } 144 out.println(" };\n return data;\n }\n}"); 145 } 146 } 147 148 private static String toLocaleList(SortedSet<String> set) { 149 StringBuilder sb = new StringBuilder(set.size() * 6); 150 for (String id : set) { 151 if (!"root".equals(id)) { 152 if (sb.length() > 0) { 153 sb.append(' '); 154 } 155 sb.append(id); 156 } 157 } 158 return sb.toString(); 159 } 160 }