1 package org.unicode.cldr.tool;
2 
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Collection;
6 import java.util.Map;
7 import java.util.Map.Entry;
8 import java.util.Set;
9 
10 import org.unicode.cldr.draft.FileUtilities;
11 import org.unicode.cldr.util.SupplementalDataInfo;
12 
13 import com.google.common.base.Joiner;
14 import com.ibm.icu.impl.Relation;
15 import com.ibm.icu.impl.Row;
16 import com.ibm.icu.impl.Row.R2;
17 
18 public class GenerateBcp47Text {
19     static final boolean SHOW_ONLY_WITHOUT_DESCRIPTION = false;
20 
21     static SupplementalDataInfo info = SupplementalDataInfo.getInstance();
22     static Map<R2<String, String>, String> deprecatedMap = info.getBcp47Deprecated();
23 
24     Relation<String, String> extension2Keys = info.getBcp47Extension2Keys();
25     Relation<String, String> keys2subtypes = info.getBcp47Keys();
26     Relation<R2<String, String>, String> keySubtype2Aliases = info.getBcp47Aliases();
27     Map<R2<String, String>, String> keySubtype2Description = info.getBcp47Descriptions();
28     Map<R2<String, String>, String> keySubtype2Since = info.getBcp47Since();
29     R2<String, String> probe = Row.of("", "");
30 
main(String[] args)31     public static void main(String[] args) throws IOException {
32         new GenerateBcp47Text().run();
33     }
34 
run()35     private void run() throws IOException {
36         for (Entry<String, Set<String>> extensionAndKeys : extension2Keys.keyValuesSet()) {
37 
38             String extension = extensionAndKeys.getKey();
39             PrintWriter out = FileUtilities.openUTF8Writer(FormattedFileWriter.CHART_TARGET_DIR, "bcp47-" + extension + ".txt");
40             showField(out, "Version", ToolConstants.CHART_DISPLAY_VERSION);
41             showField(out, "Extension", extension);
42 
43             Set<String> keys = extensionAndKeys.getValue();
44             for (String key : keys) {
45                 showRecord(out, extension, key, "");
46             }
47             for (Entry<String, Set<String>> keyAndSubtype : keys2subtypes.keyValuesSet()) {
48                 String key = keyAndSubtype.getKey();
49                 if (!keys.contains(key)) {
50                     continue;
51                 }
52                 Set<String> subtypes = keyAndSubtype.getValue();
53                 for (String subtype : subtypes) {
54                     showRecord(out, extension, key, subtype);
55                 }
56             }
57             out.close();
58         }
59     }
60 
61     /**
62      * %%
63      * Type: language
64      * Subtag: ab
65      * Description: Abkhazian
66      * Added: 2005-10-16
67      * Suppress-Script: Cyrl
68      * %%
69      *
70      * @param key
71      * @param string
72      */
showRecord(PrintWriter out, String extension, String key, String subtype)73     private void showRecord(PrintWriter out, String extension, String key, String subtype) {
74         // TODO Auto-generated method stub
75         probe.set0(key).set1(subtype);
76         final String description = keySubtype2Description.get(probe);
77         final String since = keySubtype2Since.get(probe);
78         if (SHOW_ONLY_WITHOUT_DESCRIPTION && description != null) return;
79         out.println("%%");
80         // showField(out, "Extension", extension);
81         showField(out, "Key", key);
82         showField(out, "Subtype", subtype);
83         showField(out, "Aliases", keySubtype2Aliases.get(probe));
84         showField(out, "Description", description);
85         showField(out, "Since", since);
86         String deprecatedValue = deprecatedMap.get(Row.of(key, subtype));
87         if (!"false".equals(deprecatedValue)) {
88             showField(out, "Deprecated", deprecatedValue);
89         }
90     }
91 
showField(PrintWriter out, String title, Collection<String> set)92     private void showField(PrintWriter out, String title, Collection<String> set) {
93         showField(out, title, set == null || set.isEmpty() ? null : Joiner.on(", ").join(set));
94     }
95 
showField(PrintWriter out, String title, String item)96     private void showField(PrintWriter out, String title, String item) {
97         out.write(item == null || item.isEmpty() ? "" : title + ": " + item + System.lineSeparator());
98     }
99 }
100