xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/tool/FixDelimiters.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.tool;
2 
3 import com.ibm.icu.impl.Row;
4 import com.ibm.icu.impl.Row.R2;
5 import java.io.IOException;
6 import java.io.PrintWriter;
7 import java.util.LinkedHashMap;
8 import java.util.LinkedHashSet;
9 import java.util.Map;
10 import java.util.Map.Entry;
11 import java.util.Set;
12 import java.util.regex.Matcher;
13 import org.unicode.cldr.draft.FileUtilities;
14 import org.unicode.cldr.util.CLDRFile;
15 import org.unicode.cldr.util.CLDRPaths;
16 import org.unicode.cldr.util.Factory;
17 import org.unicode.cldr.util.PatternCache;
18 import org.unicode.cldr.util.SimpleFactory;
19 import org.unicode.cldr.util.StringIterables;
20 import org.unicode.cldr.util.SupplementalDataInfo;
21 
22 /**
23  * One-time class to fix delimiters
24  *
25  * @author markdavis
26  */
27 public class FixDelimiters {
28 
main(String[] args)29     public static void main(String[] args) throws IOException {
30         SupplementalDataInfo info = SupplementalDataInfo.getInstance();
31         Set<String> defaultContentLocales = info.getDefaultContentLocales();
32 
33         for (Entry<String, R2<Quotes, Quotes>> entry : Data.locales2delimiters.entrySet()) {
34             System.out.println(entry);
35         }
36         Factory factory = SimpleFactory.make(CLDRPaths.MAIN_DIRECTORY, ".*");
37         Set<String> remainder = new LinkedHashSet<>(Data.locales2delimiters.keySet());
38 
39         String[] paths = {
40             "//ldml/delimiters/quotationStart",
41             "//ldml/delimiters/quotationEnd",
42             "//ldml/delimiters/alternateQuotationStart",
43             "//ldml/delimiters/alternateQuotationEnd"
44         };
45         String[] oldValue = new String[4];
46         String[] newValue = new String[4];
47 
48         System.out.println("Writing data");
49         for (String locale : factory.getAvailable()) {
50             if (defaultContentLocales.contains(locale)) continue;
51 
52             R2<Quotes, Quotes> data = Data.locales2delimiters.get(locale);
53             if (data == null) {
54                 continue;
55             }
56             if (!remainder.contains(locale)) {
57                 System.out.println("Superflous: " + locale);
58             } else {
59                 remainder.remove(locale);
60             }
61             CLDRFile cldrFile = factory.make(locale, false).cloneAsThawed();
62 
63             newValue[0] = data.get0().start;
64             newValue[1] = data.get0().end;
65             newValue[2] = data.get1().start;
66             newValue[3] = data.get1().end;
67 
68             for (int i = 0; i < paths.length; ++i) {
69                 String value = cldrFile.getStringValue(paths[i]);
70                 oldValue[i] = value;
71                 if (newValue[i].equals(oldValue[i])) {
72                     continue;
73                 }
74                 cldrFile.add(paths[i], newValue[i]);
75                 String revalue = cldrFile.getStringValue(paths[i]);
76                 System.out.println(
77                         locale + "\t" + paths[i] + "\t" + oldValue[i] + "\t=>\t" + revalue);
78             }
79             PrintWriter pw =
80                     FileUtilities.openUTF8Writer(
81                             CLDRPaths.GEN_DIRECTORY + "temp/", locale + ".xml");
82             cldrFile.write(pw);
83             pw.close();
84         }
85         System.out.println("Missing: " + remainder);
86     }
87 
88     static class Quotes {
89         static Matcher quotes = PatternCache.get("(.*)…(.*)").matcher("");
90         final String start;
91         final String end;
92 
Quotes(String input)93         Quotes(String input) {
94             if (!quotes.reset(input).matches()) {
95                 throw new IllegalArgumentException(input);
96             }
97             start = quotes.group(1);
98             end = quotes.group(2);
99         }
100 
101         @Override
toString()102         public String toString() {
103             return start + "...." + end;
104         }
105     }
106 
107     static class Data {
108         static Map<String, Row.R2<Quotes, Quotes>> locales2delimiters = new LinkedHashMap<>();
109         static Matcher localeString = PatternCache.get(".*\\((.*)\\)").matcher("");
110 
111         static {
112             final String instructionFile = "delimiterFixes.txt";
113             System.out.println("Instruction file: " + instructionFile);
114             for (String line : StringIterables.in(FixDelimiters.class, instructionFile)) {
115                 int first = line.indexOf(' ');
116                 int second = line.indexOf(' ', first + 1);
117                 Quotes qmain = new Quotes(line.substring(0, first));
118                 Quotes qalt = new Quotes(line.substring(first + 1, second));
119                 R2<Quotes, Quotes> both = Row.of(qmain, qalt);
120                 String last = line.substring(second);
121                 String[] locales = last.split("\\s*;\\s*");
122                 for (String locale : locales) {
123                     if (!localeString.reset(locale).matches()) {
124                         throw new IllegalArgumentException("<" + locale + "> in " + line);
125                     }
126                     String localeCode = localeString.group(1);
locales2delimiters.put(localeCode, both)127                     locales2delimiters.put(localeCode, both);
128                 }
129             }
130         }
131     }
132 }
133