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.google.common.base.Joiner; 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", "main", "subdivisions"); 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 + Chart.dataScrapeMessage(null, null, "common/supplemental/subdivisions.xml", "common/validity/subdivision.xml"); 46 } 47 48 @Override writeContents(FormattedFileWriter pw)49 public void writeContents(FormattedFileWriter pw) throws IOException { 50 51 TablePrinter tablePrinter = new TablePrinter() 52 .addColumn("Region", "class='source'", null, "class='source'", true) 53 .setSortPriority(1) 54 .addColumn("Code", "class='source'", CldrUtility.getDoubleLinkMsg(), "class='source'", true) 55 .setBreakSpans(true) 56 57 .addColumn("Subdivision1", "class='target'", null, "class='target'", true) 58 .setSortPriority(2) 59 .addColumn("Code", "class='target'", CldrUtility.getDoubleLinkMsg(), "class='target'", true) 60 .setBreakSpans(true) 61 62 .addColumn("Subdivision2", "class='target'", null, "class='target'", true) 63 .setSortPriority(3) 64 .addColumn("Code", "class='target'", CldrUtility.getDoubleLinkMsg(), "class='target'", true); 65 66 Map<String, R2<List<String>, String>> aliases = SDI.getLocaleAliasInfo().get("subdivision"); 67 68 Set<String> remainder = new HashSet<>(Validity.getInstance().getStatusToCodes(LstrType.region).get(Status.regular)); 69 Relation<String, String> inverseAliases = Relation.of(new HashMap(), TreeSet.class); 70 for (Entry<String, R2<List<String>, String>> entry : aliases.entrySet()) { 71 List<String> value = entry.getValue().get0(); 72 inverseAliases.putAll(value, entry.getKey()); 73 } 74 75 for (String container : SDI.getContainersForSubdivisions()) { 76 boolean containerIsRegion = SubdivisionNames.isRegionCode(container); 77 String region = containerIsRegion ? container : SubdivisionNames.getRegionFromSubdivision(container); 78 // int pos = container.indexOf('-'); 79 // String region = pos < 0 ? container : container.substring(0, pos); 80 for (String contained : SDI.getContainedSubdivisions(container)) { 81 if (contained.equals("usas")) { 82 int debug = 0; 83 } 84 if (SDI.getContainedSubdivisions(contained) != null) { 85 continue; 86 } 87 String s1 = containerIsRegion ? contained : container; 88 String s2 = containerIsRegion ? "" : contained; 89 90 String name1 = getName(s1); 91 String name2 = getName(s2); 92 93 // mark aliases 94 R2<List<String>, String> a1 = aliases.get(s1); 95 if (a1 != null) { 96 name1 = "= " + a1.get0().get(0) + " (" + name1 + ")"; 97 } 98 R2<List<String>, String> a2 = aliases.get(s2); 99 if (a2 != null) { 100 name2 = "= " + a2.get0().get(0) + " (" + name2 + ")"; 101 } 102 tablePrinter.addRow() 103 .addCell(ENGLISH.getName(CLDRFile.TERRITORY_NAME, region)) 104 .addCell(region) 105 .addCell(name1) 106 //.addCell(type) 107 .addCell(s1) 108 .addCell(name2) 109 .addCell(s2) 110 .finishRow(); 111 remainder.remove(region); 112 } 113 } 114 for (String region : remainder) { 115 Set<String> regionAliases = inverseAliases.get(region); 116 tablePrinter.addRow() 117 .addCell(ENGLISH.getName(CLDRFile.TERRITORY_NAME, region)) 118 .addCell(region) 119 .addCell(regionAliases == null ? "«none»" : "=" + Joiner.on(", ") 120 .join(regionAliases)) 121 //.addCell(type) 122 .addCell("") 123 .addCell("") 124 .addCell("") 125 .finishRow(); 126 } 127 pw.write(tablePrinter.toTable()); 128 } 129 getName(String s1)130 private static String getName(String s1) { 131 return s1.isEmpty() ? "" : TransliteratorUtilities.toHTML.transform(CldrUtility.ifNull(EN.get(s1), "")); 132 } 133 134 } 135