8306597: Improve string formatting in EquivMapsGenerator.java
Reviewed-by: naoto
This commit is contained in:
parent
7b0b9b570b
commit
316837226e
@ -60,7 +60,9 @@ public class EquivMapsGenerator {
|
||||
}
|
||||
copyrightYear = Integer.parseInt(args[2]);
|
||||
readLSRfile(args[0]);
|
||||
// Builds the maps from the IANA data
|
||||
generateEquivalentMap();
|
||||
// Writes the maps out to LocaleEquivalentMaps.java
|
||||
generateSourceCode(args[1]);
|
||||
}
|
||||
|
||||
@ -213,63 +215,6 @@ public class EquivMapsGenerator {
|
||||
return list.toArray(new String[list.size()]);
|
||||
}
|
||||
|
||||
private static String generateValuesString(String[] values) {
|
||||
String outputStr = "";
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (i != values.length - 1) {
|
||||
outputStr = outputStr + "\"" + values[i] + "\", ";
|
||||
} else {
|
||||
outputStr = outputStr + "\"" + values[i] + "\"";
|
||||
}
|
||||
|
||||
}
|
||||
return outputStr;
|
||||
}
|
||||
|
||||
private static final String COPYRIGHT = "/*\n"
|
||||
+ " * Copyright (c) 2012, %d, Oracle and/or its affiliates. All rights reserved.\n"
|
||||
+ " * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\n"
|
||||
+ " *\n"
|
||||
+ " * This code is free software; you can redistribute it and/or modify it\n"
|
||||
+ " * under the terms of the GNU General Public License version 2 only, as\n"
|
||||
+ " * published by the Free Software Foundation. Oracle designates this\n"
|
||||
+ " * particular file as subject to the \"Classpath\" exception as provided\n"
|
||||
+ " * by Oracle in the LICENSE file that accompanied this code.\n"
|
||||
+ " *\n"
|
||||
+ " * This code is distributed in the hope that it will be useful, but WITHOUT\n"
|
||||
+ " * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n"
|
||||
+ " * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n"
|
||||
+ " * version 2 for more details (a copy is included in the LICENSE file that\n"
|
||||
+ " * accompanied this code).\n"
|
||||
+ " *\n"
|
||||
+ " * You should have received a copy of the GNU General Public License version\n"
|
||||
+ " * 2 along with this work; if not, write to the Free Software Foundation,\n"
|
||||
+ " * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\n"
|
||||
+ " *\n"
|
||||
+ " * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\n"
|
||||
+ " * or visit www.oracle.com if you need additional information or have any\n"
|
||||
+ " * questions.\n"
|
||||
+ "*/\n\n";
|
||||
|
||||
private static final String headerText =
|
||||
"package sun.util.locale;\n\n"
|
||||
+ "import java.util.HashMap;\n"
|
||||
+ "import java.util.Map;\n\n"
|
||||
+ "final class LocaleEquivalentMaps {\n\n"
|
||||
+ " static final Map<String, String> singleEquivMap;\n"
|
||||
+ " static final Map<String, String[]> multiEquivsMap;\n"
|
||||
+ " static final Map<String, String> regionVariantEquivMap;\n\n"
|
||||
+ " static {\n"
|
||||
+ " singleEquivMap = HashMap.newHashMap(";
|
||||
|
||||
private static final String footerText =
|
||||
" }\n\n"
|
||||
+ "}";
|
||||
|
||||
private static String getOpenJDKCopyright() {
|
||||
return String.format(Locale.US, COPYRIGHT, copyrightYear);
|
||||
}
|
||||
|
||||
/**
|
||||
* The input lsr data file is in UTF-8, so theoretically for the characters
|
||||
* beyond US-ASCII, the generated Java String literals need to be Unicode
|
||||
@ -277,53 +222,132 @@ public class EquivMapsGenerator {
|
||||
* the case since we don't use "description", "comment" or alike.
|
||||
*/
|
||||
private static void generateSourceCode(String fileName) {
|
||||
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(
|
||||
Paths.get(fileName))) {
|
||||
writer.write(getOpenJDKCopyright());
|
||||
writer.write(headerText
|
||||
+ sortedLanguageMap1.size() + ");\n"
|
||||
+ " multiEquivsMap = HashMap.newHashMap("
|
||||
+ sortedLanguageMap2.size() + ");\n"
|
||||
+ " regionVariantEquivMap = HashMap.newHashMap("
|
||||
+ sortedRegionVariantMap.size() + ");\n\n"
|
||||
+ " // This is an auto-generated file and should not be manually edited.\n"
|
||||
+ " // LSR Revision: " + LSRrevisionDate);
|
||||
writer.write(HEADER_TEXT);
|
||||
writer.write(getMapsText());
|
||||
writer.write(getLSRText());
|
||||
writeEquiv(writer, "singleEquivMap", sortedLanguageMap1);
|
||||
writer.newLine();
|
||||
|
||||
for (String key : sortedLanguageMap1.keySet()) {
|
||||
String value = sortedLanguageMap1.get(key);
|
||||
writer.write(" singleEquivMap.put(\""
|
||||
+ key + "\", \"" + value + "\");");
|
||||
writer.newLine();
|
||||
}
|
||||
|
||||
writeMultiEquiv(writer);
|
||||
writer.newLine();
|
||||
for (String key : sortedLanguageMap2.keySet()) {
|
||||
String[] values = sortedLanguageMap2.get(key);
|
||||
|
||||
if (values.length >= 2) {
|
||||
writer.write(" multiEquivsMap.put(\""
|
||||
+ key + "\", new String[] {"
|
||||
+ generateValuesString(values) + "});");
|
||||
writer.newLine();
|
||||
}
|
||||
}
|
||||
|
||||
writer.newLine();
|
||||
for (String key : sortedRegionVariantMap.keySet()) {
|
||||
String value = sortedRegionVariantMap.get(key);
|
||||
writer.write(" regionVariantEquivMap.put(\""
|
||||
+ key + "\", \"" + value + "\");");
|
||||
writer.newLine();
|
||||
}
|
||||
|
||||
writer.write(footerText);
|
||||
writeEquiv(writer, "regionVariantEquivMap", sortedRegionVariantMap);
|
||||
writer.write(FOOTER_TEXT);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace(System.err);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static String getOpenJDKCopyright() {
|
||||
return String.format(Locale.US, COPYRIGHT, copyrightYear);
|
||||
}
|
||||
|
||||
private static final String COPYRIGHT =
|
||||
"""
|
||||
/*
|
||||
* Copyright (c) 2012, %d, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the \"Classpath\" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
""";
|
||||
|
||||
private static final String HEADER_TEXT =
|
||||
"""
|
||||
package sun.util.locale;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
final class LocaleEquivalentMaps {
|
||||
|
||||
static final Map<String, String> singleEquivMap;
|
||||
static final Map<String, String[]> multiEquivsMap;
|
||||
static final Map<String, String> regionVariantEquivMap;
|
||||
|
||||
""";
|
||||
|
||||
private static final String FOOTER_TEXT =
|
||||
"""
|
||||
}
|
||||
|
||||
}
|
||||
""";
|
||||
|
||||
private static String getMapsText() {
|
||||
return """
|
||||
static {
|
||||
singleEquivMap = HashMap.newHashMap(%s);
|
||||
multiEquivsMap = HashMap.newHashMap(%s);
|
||||
regionVariantEquivMap = HashMap.newHashMap(%s);
|
||||
|
||||
""".formatted(
|
||||
sortedLanguageMap1.size(),
|
||||
sortedLanguageMap2.size(),
|
||||
sortedRegionVariantMap.size());
|
||||
}
|
||||
|
||||
private static final String getLSRText(){
|
||||
return """
|
||||
// This is an auto-generated file and should not be manually edited.
|
||||
// LSR Revision: %s
|
||||
""".formatted(LSRrevisionDate);
|
||||
}
|
||||
|
||||
private static void writeMultiEquiv(BufferedWriter writer) throws IOException {
|
||||
for (String key : sortedLanguageMap2.keySet()) {
|
||||
String[] values = sortedLanguageMap2.get(key);
|
||||
if (values.length >= 2) {
|
||||
writer.write(String.format(
|
||||
" multiEquivsMap.put(\"%s\", new String[] {%s});"
|
||||
, key, generateValuesString(values)));
|
||||
writer.newLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Use for writing an equivlancy map with one value
|
||||
private static void writeEquiv(BufferedWriter writer, String name, Map<String, String> map) throws IOException {
|
||||
for (String key : map.keySet()) {
|
||||
String value = map.get(key);
|
||||
writer.write(String.format(
|
||||
" %s.put(\"%s\", \"%s\");"
|
||||
, name, key, value));
|
||||
writer.newLine();
|
||||
}
|
||||
}
|
||||
|
||||
private static String generateValuesString(String[] values) {
|
||||
String outputStr = "";
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (i != values.length - 1) {
|
||||
outputStr = String.format("%s\"%s\", ", outputStr, values[i]);
|
||||
} else {
|
||||
outputStr = String.format("%s\"%s\"", outputStr, values[i]);
|
||||
}
|
||||
|
||||
}
|
||||
return outputStr;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user