xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/tool/PrepareRootAnnotations.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.tool;
2 
3 import com.ibm.icu.dev.util.UnicodeMap;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6 import org.unicode.cldr.draft.FileUtilities;
7 import org.unicode.cldr.util.CLDRFile;
8 import org.unicode.cldr.util.CLDRPaths;
9 import org.unicode.cldr.util.Emoji;
10 import org.unicode.cldr.util.Factory;
11 import org.unicode.cldr.util.SimpleFactory;
12 import org.unicode.cldr.util.XPathParts;
13 
14 public class PrepareRootAnnotations {
15 
main(String[] args)16     public static void main(String[] args) throws IOException {
17         // flesh out root with new values
18         Factory factoryAnnotations = SimpleFactory.make(CLDRPaths.ANNOTATIONS_DIRECTORY, ".*");
19         CLDRFile oldAnnotations = factoryAnnotations.make("root", false);
20         UnicodeMap<String> oldValues = new UnicodeMap<>();
21         for (String path : oldAnnotations) {
22             XPathParts parts = XPathParts.getFrozenInstance(path);
23             if (parts.getElement(1).equals("identity")) {
24                 continue;
25             }
26             String cp = parts.getAttributeValue(-1, "cp");
27             String value = oldAnnotations.getStringValue(path);
28             oldValues.put(cp, value);
29         }
30         CLDRFile annotations = oldAnnotations.cloneAsThawed();
31         int counter = oldValues.size();
32         for (String cp : Emoji.getNonConstructed()) {
33             String value = oldValues.get(cp);
34             if (value == null) {
35                 oldValues.put(cp, value = "E" + (counter++));
36             }
37             String base = "//ldml/annotations/annotation[@cp=\"" + cp + "\"]";
38             String namePath = base + Emoji.TYPE_TTS;
39             String keywordPath = base;
40             annotations.add(namePath, value);
41             annotations.add(keywordPath, value);
42         }
43 
44         try (PrintWriter pw =
45                 FileUtilities.openUTF8Writer(
46                         CLDRPaths.GEN_DIRECTORY + "annotations/", "root.xml")) {
47             annotations.write(pw);
48             pw.flush();
49         }
50     }
51 }
52