1 package org.unicode.cldr.tool; 2 3 import java.io.IOException; 4 import java.util.Collections; 5 import java.util.LinkedHashSet; 6 import java.util.Locale; 7 import java.util.Map; 8 import java.util.Set; 9 import java.util.TreeMap; 10 11 import org.unicode.cldr.util.CldrUtility; 12 import org.unicode.cldr.util.CldrUtility.LineHandler; 13 import org.unicode.cldr.util.StandardCodes; 14 15 import com.ibm.icu.text.UTF16; 16 import com.ibm.icu.util.ICUUncheckedIOException; 17 import com.ibm.icu.util.ULocale; 18 19 public class CountryCodeConverter { 20 21 private static final boolean SHOW_SKIP = CldrUtility.getProperty("SHOW_SKIP", false); 22 23 private static Map<String, String> nameToCountryCode = new TreeMap<>(new UTF16.StringComparator(true, true, 0)); 24 private static Set<String> parseErrors = new LinkedHashSet<>(); 25 getCodeFromName(String display, boolean showMissing)26 public static String getCodeFromName(String display, boolean showMissing) { 27 String trial = display.trim().toLowerCase(Locale.ENGLISH); 28 if (trial.startsWith("\"") && trial.endsWith("\"")) { 29 trial = trial.substring(1, trial.length() - 2); 30 } 31 if (trial.startsWith("the ")) { 32 trial = trial.substring(4); 33 } 34 String result = nameToCountryCode.get(trial); 35 if ("skip".equals(result)) { 36 return null; 37 } 38 if (result == null) { 39 trial = reverseComma(display); 40 if (trial != null) { 41 result = nameToCountryCode.get(trial); 42 // if (result != null) { 43 // addName(trial, result); 44 // } 45 } 46 } 47 if (result == null && (showMissing || SHOW_SKIP)) { 48 System.err.println("CountryCodeConverter missing code; add to external/alternate_country_names.txt a line like:" + 49 "\t<code>;\t<name>;\t" + display); 50 } 51 return result; 52 } 53 names()54 public static Set<String> names() { 55 return nameToCountryCode.keySet(); 56 } 57 reverseComma(String display)58 private static String reverseComma(String display) { 59 String trial; 60 trial = null; 61 int comma = display.indexOf(','); 62 if (comma >= 0) { 63 trial = display.substring(comma + 1).trim() + " " + display.substring(0, comma).trim(); 64 } 65 return trial; 66 } 67 68 static { 69 try { loadNames()70 loadNames(); 71 } catch (IOException e) { 72 throw new ICUUncheckedIOException(e); 73 } 74 } 75 loadNames()76 static void loadNames() throws IOException { 77 for (String country : ULocale.getISOCountries()) { 78 addName(ULocale.getDisplayCountry("und-" + country, "en"), country); 79 } 80 StandardCodes sc = StandardCodes.make(); 81 Set<String> goodAvailableCodes = sc.getGoodAvailableCodes("territory"); 82 83 for (String country : goodAvailableCodes) { 84 String description = sc.getFullData("territory", country).get(0); 85 if (country.equals("057")) continue; 86 addName(description, country); 87 } 88 CldrUtility.handleFile("external/alternate_country_names.txt", new MyHandler(goodAvailableCodes)); 89 nameToCountryCode = CldrUtility.protectCollection(nameToCountryCode); 90 parseErrors = Collections.unmodifiableSet(parseErrors); 91 } 92 93 static class MyHandler implements LineHandler { 94 private Set<String> goodAvailableCodes; 95 MyHandler(Set<String> goodAvailableCodes)96 public MyHandler(Set<String> goodAvailableCodes) { 97 this.goodAvailableCodes = goodAvailableCodes; 98 } 99 100 @Override handle(String line)101 public boolean handle(String line) { 102 if (line.trim().length() == 0) { 103 return true; // don't show skips 104 } 105 String[] pieces = line.split(";"); 106 String country = pieces[0].trim(); 107 if (!goodAvailableCodes.contains(country)) { 108 109 } 110 addName(pieces[2].trim(), country); 111 return true; 112 } 113 } 114 addName(String key, String code)115 static void addName(String key, String code) { 116 addName2(key, code); 117 String trial = reverseComma(key); 118 if (trial != null) { 119 addName2(trial, code); 120 } 121 } 122 addName2(String key, String code)123 private static void addName2(String key, String code) { 124 key = key.toLowerCase(Locale.ENGLISH); 125 if (key.startsWith("the ")) { 126 key = key.substring(4); 127 } 128 String old = nameToCountryCode.get(key); 129 if (old != null && !code.equals(old)) { 130 System.err.println("Conflict!!" + key + "\t" + old + "\t" + code); 131 return; 132 } 133 nameToCountryCode.put(key, code); 134 } 135 getParseErrors()136 public static Set<String> getParseErrors() { 137 return parseErrors; 138 } 139 140 } 141