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 
63     @Override
writeContents(FormattedFileWriter pw)64     public void writeContents(FormattedFileWriter pw) throws IOException {
65         FileCopier.ensureDirectoryExists(DIR);
66         FileCopier.copy(Chart.class, "index.css", DIR);
67         FormattedFileWriter.copyIncludeHtmls(DIR);
68 
69         FormattedFileWriter.Anchors anchors = new FormattedFileWriter.Anchors();
70         writeSubcharts(anchors);
71         pw.setIndex("Main Chart Index", "../index.html");
72         pw.write(anchors.toString());
73     }
74 
writeSubcharts(Anchors anchors)75     public void writeSubcharts(Anchors anchors) throws IOException {
76         CLDRConfig cldrConfig = CLDRConfig.getInstance();
77         File[] paths = {
78             new File(CLDRPaths.SUBDIVISIONS_DIRECTORY) };
79 
80         Factory factory = SimpleFactory.make(paths, ".*");
81         CLDRFile english = factory.make("en", true);
82 
83         Set<String> subdivisions = Validity.getInstance().getStatusToCodes(LstrType.subdivision).get(Status.regular);
84         // set up right order for columns
85 
86         Map<String, String> nameToCode = new LinkedHashMap<>();
87         Relation<LanguageGroup, R3<Integer, String, String>> groupToNameAndCodeSorted = Relation.of(
88             new EnumMap<LanguageGroup, Set<R3<Integer, String, String>>>(LanguageGroup.class),
89             TreeSet.class);
90 
91         Set<String> locales = factory.getAvailable();
92         for (String locale : locales) {
93             if (locale.equals("root")) {
94                 continue;
95             }
96             if (locale.equals("en")) { // make first
97                 continue;
98             }
99             if (locale.startsWith("en")) {
100                 int debug = 0;
101             }
102             String name = ENGLISH.getName(locale, true);
103             int baseEnd = locale.indexOf('_');
104             ULocale loc = new ULocale(baseEnd < 0 ? locale : locale.substring(0, baseEnd));
105             LanguageGroup group = LanguageGroup.get(loc);
106             int rank = LanguageGroup.rankInGroup(loc);
107             groupToNameAndCodeSorted.put(group, Row.of(rank, name, locale));
108         }
109 
110         for (Entry<LanguageGroup, Set<R3<Integer, String, String>>> groupPairs : groupToNameAndCodeSorted.keyValuesSet()) {
111             LanguageGroup group = groupPairs.getKey();
112             String ename = ENGLISH.getName("en", true);
113             nameToCode.clear();
114             nameToCode.put(ename, "en"); // always have english first
115 
116             // add English variants if they exist
117 
118             for (R3<Integer, String, String> pair : groupPairs.getValue()) {
119                 String name = pair.get1();
120                 String locale = pair.get2();
121                 if (locale.startsWith("en_")) {
122                     nameToCode.put(name, locale);
123                 }
124             }
125 
126             for (R3<Integer, String, String> pair : groupPairs.getValue()) {
127                 String name = pair.get1();
128                 String locale = pair.get2();
129 
130                 nameToCode.put(name, locale);
131             }
132 
133             // now build table with right order for columns
134             double width = ((int) ((99.0 / (locales.size() + 1)) * 1000)) / 1000.0;
135             //String widthString = "class='source' width='"+ width + "%'";
136             String widthStringTarget = "class='target' width='" + width + "%'";
137 
138             TablePrinter tablePrinter = new TablePrinter()
139                 .addColumn("Name", "class='source' width='1%'", null, "class='source'", true)
140                 .addColumn("Code", "class='source' width='1%'", CldrUtility.getDoubleLinkMsg(), "class='source'", true)
141             //.addColumn("Formal Name", "class='source' width='" + width + "%'", null, "class='source'", true)
142             ;
143 
144             for (Entry<String, String> entry : nameToCode.entrySet()) {
145                 String name = entry.getKey();
146                 tablePrinter.addColumn(name, widthStringTarget, null, "class='target'", true);
147             }
148             // sort the characters
149             for (String code : subdivisions) {
150                 tablePrinter
151                     .addRow()
152                     .addCell(english.getName(CLDRFile.TERRITORY_NAME, code.substring(0, 2).toUpperCase(Locale.ENGLISH)))
153                     .addCell(code);
154                 for (Entry<String, String> nameAndLocale : nameToCode.entrySet()) {
155                     String name = nameAndLocale.getKey();
156                     String locale = nameAndLocale.getValue();
157                     CLDRFile cldrFile = factory.make(locale, false);
158                     String name2 = cldrFile.getStringValue("//ldml/localeDisplayNames/subdivisions/subdivision[@type=\"" + code
159                         + "\"]");
160                     tablePrinter.addCell(name2 == null ? "" : name2);
161                 }
162                 tablePrinter.finishRow();
163             }
164             final String name = group.toString();
165             new Subchart(name + " Subdivision Names", FileUtilities.anchorize(name), tablePrinter).writeChart(anchors);
166         }
167     }
168 
169     private class Subchart extends Chart {
170         String title;
171         String file;
172         private TablePrinter tablePrinter;
173 
174         @Override
getShowDate()175         public boolean getShowDate() {
176             return false;
177         }
178 
Subchart(String title, String file, TablePrinter tablePrinter)179         public Subchart(String title, String file, TablePrinter tablePrinter) {
180             super();
181             this.title = title;
182             this.file = file;
183             this.tablePrinter = tablePrinter;
184         }
185 
186         @Override
getDirectory()187         public String getDirectory() {
188             return DIR;
189         }
190 
191         @Override
getTitle()192         public String getTitle() {
193             return title;
194         }
195 
196         @Override
getFileName()197         public String getFileName() {
198             return file;
199         }
200 
201         @Override
getExplanation()202         public String getExplanation() {
203             return "<p>This table shows the subdivision names for a group of related languages (plus English) for easier comparison. "
204                 + "The coverage depends on the availability of data in wikidata for these names.</p>\n";
205         }
206 
207         @Override
writeContents(FormattedFileWriter pw)208         public void writeContents(FormattedFileWriter pw) throws IOException {
209             pw.write(tablePrinter.toTable());
210         }
211     }
212 }
213