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<String, String>(new UTF16.StringComparator(true, true, 0)); 24 private static Set<String> parseErrors = new LinkedHashSet<String>(); 25 getCodeFromName(String display)26 public static String getCodeFromName(String display) { 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 (SHOW_SKIP && result == null) { 48 System.out.println("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 = (String) 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 handle(String line)100 public boolean handle(String line) { 101 if (line.trim().length() == 0) { 102 return true; // don't show skips 103 } 104 String[] pieces = line.split(";"); 105 String country = pieces[0].trim(); 106 if (!goodAvailableCodes.contains(country)) { 107 108 } 109 addName(pieces[2].trim(), country); 110 return true; 111 } 112 } 113 addName(String key, String code)114 static void addName(String key, String code) { 115 addName2(key, code); 116 String trial = reverseComma(key); 117 if (trial != null) { 118 addName2(trial, code); 119 } 120 } 121 addName2(String key, String code)122 private static void addName2(String key, String code) { 123 key = key.toLowerCase(Locale.ENGLISH); 124 if (key.startsWith("the ")) { 125 key = key.substring(4); 126 } 127 String old = nameToCountryCode.get(key); 128 if (old != null && !code.equals(old)) { 129 System.err.println("Conflict!!" + key + "\t" + old + "\t" + code); 130 return; 131 } 132 nameToCountryCode.put(key, code); 133 } 134 getParseErrors()135 public static Set<String> getParseErrors() { 136 return parseErrors; 137 } 138 139 } 140