1 package org.unicode.cldr.tool;
2 
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.EnumMap;
6 import java.util.LinkedHashMap;
7 import java.util.Locale;
8 import java.util.Map;
9 import java.util.Map.Entry;
10 import java.util.Set;
11 import java.util.TreeSet;
12 
13 import org.unicode.cldr.draft.FileUtilities;
14 import org.unicode.cldr.tool.FormattedFileWriter.Anchors;
15 import org.unicode.cldr.util.CLDRConfig;
16 import org.unicode.cldr.util.CLDRFile;
17 import org.unicode.cldr.util.CLDRPaths;
18 import org.unicode.cldr.util.CldrUtility;
19 import org.unicode.cldr.util.Factory;
20 import org.unicode.cldr.util.FileCopier;
21 import org.unicode.cldr.util.LanguageGroup;
22 import org.unicode.cldr.util.SimpleFactory;
23 import org.unicode.cldr.util.StandardCodes.LstrType;
24 import org.unicode.cldr.util.Validity;
25 import org.unicode.cldr.util.Validity.Status;
26 
27 import com.ibm.icu.impl.Relation;
28 import com.ibm.icu.impl.Row;
29 import com.ibm.icu.impl.Row.R3;
30 import com.ibm.icu.util.ULocale;
31 
32 public class ChartSubdivisionNames extends Chart {
33 
34     private static final String MAIN_HEADER = "<p>This chart shows the subdivision names. "
35         + "Most of them (except in English and for a few others) are derived from wikidata, with some filtering.</p>";
36     private static final boolean DEBUG = false;
37     private static final String DIR = CLDRPaths.CHART_DIRECTORY + "subdivisionNames/";
38 
main(String[] args)39     public static void main(String[] args) {
40         new ChartSubdivisionNames().writeChart(null);
41     }
42 
43     @Override
getDirectory()44     public String getDirectory() {
45         return DIR;
46     }
47 
48     @Override
getTitle()49     public String getTitle() {
50         return "Subdivision Name Charts";
51     }
52 
53     @Override
getFileName()54     public String getFileName() {
55         return "index";
56     }
57 
58     @Override
getExplanation()59     public String getExplanation() {
60         return MAIN_HEADER + "<p>The charts are presented in groups of related languages, for easier comparison.<p>";
61     }
62 
writeContents(FormattedFileWriter pw)63     public void writeContents(FormattedFileWriter pw) throws IOException {
64         FileCopier.ensureDirectoryExists(DIR);
65         FileCopier.copy(Chart.class, "index.css", DIR);
66 
67         FormattedFileWriter.Anchors anchors = new FormattedFileWriter.Anchors();
68         writeSubcharts(anchors);
69         pw.setIndex("Main Chart Index", "../index.html");
70         pw.write(anchors.toString());
71     }
72 
writeSubcharts(Anchors anchors)73     public void writeSubcharts(Anchors anchors) throws IOException {
74         CLDRConfig cldrConfig = CLDRConfig.getInstance();
75         File[] paths = {
76             new File(CLDRPaths.SUBDIVISIONS_DIRECTORY) };
77 
78         Factory factory = SimpleFactory.make(paths, ".*");
79         CLDRFile english = factory.make("en", true);
80 
81         Set<String> subdivisions = Validity.getInstance().getStatusToCodes(LstrType.subdivision).get(Status.regular);
82         // set up right order for columns
83 
84         Map<String, String> nameToCode = new LinkedHashMap<String, String>();
85         Relation<LanguageGroup, R3<Integer, String, String>> groupToNameAndCodeSorted = Relation.of(
86             new EnumMap<LanguageGroup, Set<R3<Integer, String, String>>>(LanguageGroup.class),
87             TreeSet.class);
88 
89         Set<String> locales = factory.getAvailable();
90         for (String locale : locales) {
91             if (locale.equals("root")) {
92                 continue;
93             }
94             if (locale.equals("en")) { // make first
95                 continue;
96             }
97             if (locale.startsWith("en")) {
98                 int debug = 0;
99             }
100             String name = ENGLISH.getName(locale, true);
101             int baseEnd = locale.indexOf('_');
102             ULocale loc = new ULocale(baseEnd < 0 ? locale : locale.substring(0, baseEnd));
103             LanguageGroup group = LanguageGroup.get(loc);
104             int rank = LanguageGroup.rankInGroup(loc);
105             groupToNameAndCodeSorted.put(group, Row.of(rank, name, locale));
106         }
107 
108         for (Entry<LanguageGroup, Set<R3<Integer, String, String>>> groupPairs : groupToNameAndCodeSorted.keyValuesSet()) {
109             LanguageGroup group = groupPairs.getKey();
110             String ename = ENGLISH.getName("en", true);
111             nameToCode.clear();
112             nameToCode.put(ename, "en"); // always have english first
113 
114             // add English variants if they exist
115 
116             for (R3<Integer, String, String> pair : groupPairs.getValue()) {
117                 String name = pair.get1();
118                 String locale = pair.get2();
119                 if (locale.startsWith("en_")) {
120                     nameToCode.put(name, locale);
121                 }
122             }
123 
124             for (R3<Integer, String, String> pair : groupPairs.getValue()) {
125                 String name = pair.get1();
126                 String locale = pair.get2();
127 
128                 nameToCode.put(name, locale);
129             }
130 
131             // now build table with right order for columns
132             double width = ((int) ((99.0 / (locales.size() + 1)) * 1000)) / 1000.0;
133             //String widthString = "class='source' width='"+ width + "%'";
134             String widthStringTarget = "class='target' width='" + width + "%'";
135 
136             TablePrinter tablePrinter = new TablePrinter()
137                 .addColumn("Name", "class='source' width='1%'", null, "class='source'", true)
138                 .addColumn("Code", "class='source' width='1%'", CldrUtility.getDoubleLinkMsg(), "class='source'", true)
139             //.addColumn("Formal Name", "class='source' width='" + width + "%'", null, "class='source'", true)
140             ;
141 
142             for (Entry<String, String> entry : nameToCode.entrySet()) {
143                 String name = entry.getKey();
144                 tablePrinter.addColumn(name, widthStringTarget, null, "class='target'", true);
145             }
146             // sort the characters
147             for (String code : subdivisions) {
148                 tablePrinter
149                     .addRow()
150                     .addCell(english.getName(CLDRFile.TERRITORY_NAME, code.substring(0, 2).toUpperCase(Locale.ENGLISH)))
151                     .addCell(code);
152                 for (Entry<String, String> nameAndLocale : nameToCode.entrySet()) {
153                     String name = nameAndLocale.getKey();
154                     String locale = nameAndLocale.getValue();
155                     CLDRFile cldrFile = factory.make(locale, false);
156                     String name2 = cldrFile.getStringValue("//ldml/localeDisplayNames/subdivisions/subdivision[@type=\"" + code
157                         + "\"]");
158                     tablePrinter.addCell(name2 == null ? "" : name2);
159                 }
160                 tablePrinter.finishRow();
161             }
162             final String name = group.toString();
163             new Subchart(name + " Subdivision Names", FileUtilities.anchorize(name), tablePrinter).writeChart(anchors);
164         }
165     }
166 
167     private class Subchart extends Chart {
168         String title;
169         String file;
170         private TablePrinter tablePrinter;
171 
172         @Override
getShowDate()173         public boolean getShowDate() {
174             return false;
175         }
176 
Subchart(String title, String file, TablePrinter tablePrinter)177         public Subchart(String title, String file, TablePrinter tablePrinter) {
178             super();
179             this.title = title;
180             this.file = file;
181             this.tablePrinter = tablePrinter;
182         }
183 
184         @Override
getDirectory()185         public String getDirectory() {
186             return DIR;
187         }
188 
189         @Override
getTitle()190         public String getTitle() {
191             return title;
192         }
193 
194         @Override
getFileName()195         public String getFileName() {
196             return file;
197         }
198 
199         @Override
getExplanation()200         public String getExplanation() {
201             return "<p>This table shows the subdivision names for a group of related languages (plus English) for easier comparison. "
202                 + "The coverage depends on the availability of data in wikidata for these names.</p>\n";
203         }
204 
205         @Override
writeContents(FormattedFileWriter pw)206         public void writeContents(FormattedFileWriter pw) throws IOException {
207             pw.write(tablePrinter.toTable());
208         }
209     }
210 }
211