• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.tool;
2 
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Locale;
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.CLDRPaths;
13 import org.unicode.cldr.util.Pair;
14 import org.unicode.cldr.util.XMLFileReader;
15 import org.unicode.cldr.util.XPathParts;
16 
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.collect.ImmutableMap.Builder;
19 import com.google.common.collect.ImmutableSortedSet;
20 
21 public class SubdivisionNames {
22 
23     public static final String SUBDIVISION_PATH_PREFIX = "//ldml/localeDisplayNames/subdivisions/subdivision";
24 
25     private final Map<String, String> subdivisionToName;
26 
SubdivisionNames(String locale)27     public SubdivisionNames(String locale) {
28         Builder<String, String> builder = ImmutableMap.builder();
29         List<Pair<String, String>> data = new ArrayList<>();
30         XMLFileReader.loadPathValues(CLDRPaths.COMMON_DIRECTORY + "subdivisions/"
31             + locale
32             + ".xml", data, true);
33         for (Pair<String, String> pair : data) {
34             // <subdivision type="AD-02">Canillo</subdivision>
35             String rawPath = pair.getFirst();
36             XPathParts path = XPathParts.getFrozenInstance(rawPath);
37             if (!"subdivision".equals(path.getElement(-1))) {
38                 continue;
39             }
40             String type = path.getAttributeValue(-1, "type");
41             String name = pair.getSecond();
42             builder.put(type, name);
43         }
44         subdivisionToName = builder.build();
45     }
46 
getAvailableLocales()47     public static Set<String> getAvailableLocales() {
48         TreeSet<String> result = new TreeSet<>();
49         File baseDir = new File(CLDRPaths.COMMON_DIRECTORY + "subdivisions/");
50         for (String file : baseDir.list()) {
51             if (file.endsWith(".xml")) {
52                 result.add(file.substring(0, file.length() - 4));
53             }
54         }
55         return ImmutableSortedSet.copyOf(result);
56     }
57 
entrySet()58     public Set<Entry<String, String>> entrySet() {
59         return subdivisionToName.entrySet();
60     }
61 
get(String subdivision)62     public String get(String subdivision) {
63         return subdivisionToName.get(subdivision);
64     }
65 
keySet()66     public Set<String> keySet() {
67         return subdivisionToName.keySet();
68     }
69 
getPathFromCode(String code)70     public static String getPathFromCode(String code) {
71         // <subdivision type="AD-02">Canillo</subdivision>
72         return SUBDIVISION_PATH_PREFIX
73             + "[@type=\"" + code + "\"]";
74     }
75 
getRegionFromSubdivision(String sdCode)76     public static String getRegionFromSubdivision(String sdCode) {
77         return sdCode.compareTo("A") < 0 ? sdCode.substring(0, 3) : sdCode.substring(0, 2).toUpperCase(Locale.ENGLISH);
78     }
79 
getSubregion(String sdCode)80     public static String getSubregion(String sdCode) {
81         return sdCode.compareTo("A") < 0 ? sdCode.substring(3) : sdCode.substring(2).toUpperCase(Locale.ENGLISH);
82     }
83 
isRegionCode(String regionOrSubdivision)84     public static boolean isRegionCode(String regionOrSubdivision) {
85         return regionOrSubdivision.length() == 2 || (regionOrSubdivision.length() == 3 && regionOrSubdivision.compareTo("A") < 0);
86     }
87 
toIsoFormat(String sdCode)88     public static String toIsoFormat(String sdCode) {
89         sdCode = sdCode.toUpperCase(Locale.ENGLISH);
90         int insertion = sdCode.compareTo("A") < 0 ? 3 : 2;
91         return sdCode.substring(0, insertion) + "-" + sdCode.substring(insertion);
92     }
93 }
94