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