• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.test;
2 
3 import java.io.File;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.SortedSet;
9 import java.util.TreeMap;
10 import java.util.concurrent.ConcurrentHashMap;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13 
14 import org.unicode.cldr.draft.FileUtilities;
15 import org.unicode.cldr.util.CLDRConfig;
16 import org.unicode.cldr.util.CLDRFile;
17 import org.unicode.cldr.util.CLDRPaths;
18 import org.unicode.cldr.util.EmojiConstants;
19 import org.unicode.cldr.util.LanguageGroup;
20 import org.unicode.cldr.util.LocaleIDParser;
21 import org.unicode.cldr.util.Organization;
22 import org.unicode.cldr.util.StandardCodes;
23 
24 import com.google.common.collect.ImmutableMap;
25 import com.google.common.collect.ImmutableSet;
26 import com.google.common.collect.ImmutableSortedSet;
27 import com.ibm.icu.util.ULocale;
28 
29 public class EmojiSubdivisionNames {
30     private static final String subdivisionPathPrefix = "//ldml/localeDisplayNames/subdivisions/subdivision[@type=\"";
31     private static final String subdivisionPathSuffix = "\"]";
32     private static final Map<String,Map<String,String>> localeToNameToSubdivisionId = new ConcurrentHashMap<>();
33     private static final Map<String,Map<String,String>> localeToSubdivisionIdToName = new ConcurrentHashMap<>();
34 
35     private static final Pattern SUBDIVISION_PATTERN = Pattern.compile("\\s*<subdivision\\s+type=\"(gb(?:eng|sct|wls))\">([^<]+)</subdivision>");
36     private static final SortedSet<String> SUBDIVISION_FILE_NAMES = ImmutableSortedSet.copyOf(
37         new File(CLDRPaths.SUBDIVISIONS_DIRECTORY).list()
38         );
39 
40     static {
41         localeToNameToSubdivisionId.put("root", Collections.emptyMap());
42     }
43 
getSubdivisionIdToName(String localeID)44     public static Map<String, String> getSubdivisionIdToName(String localeID) {
45         Map<String,String> result = localeToSubdivisionIdToName.get(localeID);
46         if (result == null) {
47             load(localeID);
48             result = localeToSubdivisionIdToName.get(localeID);
49             if (result == null) {
50                 result = Collections.emptyMap();
51             }
52         }
53         return result;
54     }
55 
getNameToSubdivisionPath(String localeID)56     public static Map<String, String> getNameToSubdivisionPath(String localeID) {
57         Map<String,String> result = localeToNameToSubdivisionId.get(localeID);
58         if (result == null) {
59             load(localeID);
60             result = localeToNameToSubdivisionId.get(localeID);
61             if (result == null) {
62                 result = Collections.emptyMap();
63             }
64         }
65         return result;
66     }
67 
load(String localeID)68     private static void load(String localeID) {
69         try {
70             Map<String,String> _subdivisionIdToName;
71             Map<String, String> _nameToSubdivisionId;
72 
73             String fileName = localeID + ".xml";
74             if (SUBDIVISION_FILE_NAMES.contains(fileName)) {
75                 _nameToSubdivisionId = new TreeMap<>();
76                 _subdivisionIdToName = new TreeMap<>();
77                 Matcher m = SUBDIVISION_PATTERN.matcher("");
78                 for (String line : FileUtilities.in(new File(CLDRPaths.SUBDIVISIONS_DIRECTORY, fileName))) {
79                     if (m.reset(line).matches()) {
80                         String xpath = subdivisionPathPrefix + EmojiConstants.toTagSeq(m.group(1)) + subdivisionPathSuffix;
81                         _nameToSubdivisionId.put(m.group(2), xpath);
82                         _subdivisionIdToName.put(m.group(1), m.group(2));
83                     }
84                 }
85                 _nameToSubdivisionId = _nameToSubdivisionId.isEmpty() ? Collections.emptyMap()
86                     : ImmutableMap.copyOf(_nameToSubdivisionId);
87                 _subdivisionIdToName = _subdivisionIdToName.isEmpty() ? Collections.emptyMap()
88                     : ImmutableMap.copyOf(_subdivisionIdToName);
89             } else {
90                 String parentLocaleId = LocaleIDParser.getParent(localeID);
91                 _nameToSubdivisionId = getNameToSubdivisionPath(parentLocaleId);
92                 _subdivisionIdToName = localeToSubdivisionIdToName.get(parentLocaleId);
93             }
94             localeToNameToSubdivisionId.put(localeID, _nameToSubdivisionId);
95             localeToSubdivisionIdToName.put(localeID, _subdivisionIdToName);
96         } catch (Exception e) {}
97     }
98 
99     static Set<String> SUBDIVISIONS = ImmutableSet.of("gbeng", "gbsct", "gbwls");
100 
main(String[] args)101     public static void main(String[] args) {
102         System.out.print("Group\tOrd.\tLocale\tCode");
103         for (String sd : SUBDIVISIONS) {
104             System.out.print('\t');
105             System.out.print(sd);
106 
107         }
108         System.out.println();
109         CLDRFile english = CLDRConfig.getInstance().getEnglish();
110         Set<String> locales = new HashSet<>();
111         for (String filename : SUBDIVISION_FILE_NAMES) {
112             if (filename.endsWith(".xml")) {
113                 String locale = filename.substring(0, filename.length()-4);
114                 Map<String, String> map = getSubdivisionIdToName(locale);
115                 if (!map.isEmpty()) {
116                     locales.add(locale);
117                     ULocale ulocale = new ULocale(locale);
118                     LanguageGroup group = LanguageGroup.get(ulocale);
119                     int rank = LanguageGroup.rankInGroup(ulocale);
120                     System.out.print(group + "\t" + rank + "\t" + english.getName(locale) + "\t" + locale);
121                     for (String sd : SUBDIVISIONS) {
122                         System.out.print('\t');
123                         System.out.print(map.get(sd));
124                     }
125                     System.out.println();
126                 }
127             }
128         }
129         for (String locale : StandardCodes.make().getLocaleCoverageLocales(Organization.cldr)) {
130             if (locales.contains(locale)) {
131                 continue;
132             }
133             ULocale ulocale = new ULocale(locale);
134             String region = ulocale.getCountry();
135             if (!region.isEmpty() && !region.equals("PT") && !region.equals("GB")) {
136                 continue;
137             }
138             LanguageGroup group = LanguageGroup.get(ulocale);
139             int rank = LanguageGroup.rankInGroup(ulocale);
140             System.out.println(group + "\t" + rank + "\t" + english.getName(locale) + "\t" + locale);
141         }
142 
143 //        System.out.println(getSubdivisionIdToName("fr"));
144 //        System.out.println(getNameToSubdivisionPath("fr"));
145     }
146 }