1 package org.unicode.cldr.tool;
2 
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Map.Entry;
9 import java.util.Set;
10 import java.util.TreeSet;
11 
12 import org.unicode.cldr.util.CLDRFile;
13 import org.unicode.cldr.util.CldrUtility;
14 import org.unicode.cldr.util.StandardCodes.LstrType;
15 import org.unicode.cldr.util.TransliteratorUtilities;
16 import org.unicode.cldr.util.Validity;
17 import org.unicode.cldr.util.Validity.Status;
18 
19 import com.ibm.icu.dev.util.CollectionUtilities;
20 import com.ibm.icu.impl.Relation;
21 import com.ibm.icu.impl.Row.R2;
22 
23 public class ChartSubdivisions extends Chart {
24 
25     static SubdivisionNames EN = new SubdivisionNames("en");
26 
main(String[] args)27     public static void main(String[] args) {
28         new ChartSubdivisions().writeChart(null);
29     }
30 
31     @Override
getDirectory()32     public String getDirectory() {
33         return FormattedFileWriter.CHART_TARGET_DIR;
34     }
35 
36     @Override
getTitle()37     public String getTitle() {
38         return "Territory Subdivisions";
39     }
40 
41     @Override
getExplanation()42     public String getExplanation() {
43         return "<p>Shows the subdivisions of territories, using the Unicode Subdivision Codes with the English names (and sort order). "
44             + "For more information see the LDML spec.<p>";
45     }
46 
47     @Override
writeContents(FormattedFileWriter pw)48     public void writeContents(FormattedFileWriter pw) throws IOException {
49 
50         TablePrinter tablePrinter = new TablePrinter()
51             .addColumn("Region", "class='source'", null, "class='source'", true)
52             .setSortPriority(1)
53             .addColumn("Code", "class='source'", CldrUtility.getDoubleLinkMsg(), "class='source'", true)
54             .setBreakSpans(true)
55 
56             .addColumn("Subdivision1", "class='target'", null, "class='target'", true)
57             .setSortPriority(2)
58             .addColumn("Code", "class='target'", CldrUtility.getDoubleLinkMsg(), "class='target'", true)
59             .setBreakSpans(true)
60 
61             .addColumn("Subdivision2", "class='target'", null, "class='target'", true)
62             .setSortPriority(3)
63             .addColumn("Code", "class='target'", CldrUtility.getDoubleLinkMsg(), "class='target'", true);
64 
65         Map<String, R2<List<String>, String>> aliases = SDI.getLocaleAliasInfo().get("subdivision");
66 
67         Set<String> remainder = new HashSet<>(Validity.getInstance().getStatusToCodes(LstrType.region).get(Status.regular));
68         Relation<String, String> inverseAliases = Relation.of(new HashMap(), TreeSet.class);
69         for (Entry<String, R2<List<String>, String>> entry : aliases.entrySet()) {
70             List<String> value = entry.getValue().get0();
71             inverseAliases.putAll(value, entry.getKey());
72         }
73 
74         for (String container : SDI.getContainersForSubdivisions()) {
75             boolean containerIsRegion = SubdivisionNames.isRegionCode(container);
76             String region = containerIsRegion ? container : SubdivisionNames.getRegionFromSubdivision(container);
77 //            int pos = container.indexOf('-');
78 //            String region = pos < 0 ? container : container.substring(0, pos);
79             for (String contained : SDI.getContainedSubdivisions(container)) {
80                 if (contained.equals("usas")) {
81                     int debug = 0;
82                 }
83                 if (SDI.getContainedSubdivisions(contained) != null) {
84                     continue;
85                 }
86                 String s1 = containerIsRegion ? contained : container;
87                 String s2 = containerIsRegion ? "" : contained;
88 
89                 String name1 = getName(s1);
90                 String name2 = getName(s2);
91 
92                 // mark aliases
93                 R2<List<String>, String> a1 = aliases.get(s1);
94                 if (a1 != null) {
95                     name1 = "= " + a1.get0().get(0) + " (" + name1 + ")";
96                 }
97                 R2<List<String>, String> a2 = aliases.get(s2);
98                 if (a2 != null) {
99                     name2 = "= " + a2.get0().get(0) + " (" + name2 + ")";
100                 }
101                 tablePrinter.addRow()
102                     .addCell(ENGLISH.getName(CLDRFile.TERRITORY_NAME, region))
103                     .addCell(region)
104                     .addCell(name1)
105                     //.addCell(type)
106                     .addCell(s1)
107                     .addCell(name2)
108                     .addCell(s2)
109                     .finishRow();
110                 remainder.remove(region);
111             }
112         }
113         for (String region : remainder) {
114             Set<String> regionAliases = inverseAliases.get(region);
115             tablePrinter.addRow()
116                 .addCell(ENGLISH.getName(CLDRFile.TERRITORY_NAME, region))
117                 .addCell(region)
118                 .addCell(regionAliases == null ? "«none»" : "=" + CollectionUtilities.join(regionAliases, ", "))
119                 //.addCell(type)
120                 .addCell("")
121                 .addCell("")
122                 .addCell("")
123                 .finishRow();
124         }
125         pw.write(tablePrinter.toTable());
126     }
127 
getName(String s1)128     private static String getName(String s1) {
129         return s1.isEmpty() ? "" : TransliteratorUtilities.toHTML.transform(CldrUtility.ifNull(EN.get(s1), ""));
130     }
131 
132 }
133