aboutsummaryrefslogtreecommitdiffstats
path: root/tools/java/org/unicode/cldr/util/StandardCodes.java
blob: 158440263c1357fc2dbac3a1b71c3dab62d5a5a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
/*
 **********************************************************************
 * Copyright (c) 2002-2011, International Business Machines
 * Corporation and others.  All Rights Reserved.
 **********************************************************************
 * Author: Mark Davis
 **********************************************************************
 */
package org.unicode.cldr.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Pattern;

import org.unicode.cldr.draft.ScriptMetadata;
import org.unicode.cldr.draft.ScriptMetadata.IdUsage;
import org.unicode.cldr.util.Iso639Data.Type;
import org.unicode.cldr.util.ZoneParser.ZoneLine;

import com.ibm.icu.impl.Relation;
import com.ibm.icu.lang.UCharacter;
import com.ibm.icu.text.UnicodeSet;
import com.ibm.icu.util.ICUUncheckedIOException;
import com.ibm.icu.util.Output;

/**
 * Provides access to various codes used by CLDR: RFC 3066, ISO 4217, Olson
 * tzids
 */
public class StandardCodes {

    public enum CodeType {
        language, script, territory, extlang, grandfathered, redundant, variant, currency, tzid;
        public static CodeType from(String name) {
            if ("region".equals(name)) {
                return territory;
            }
            return CodeType.valueOf(name);
        }
    }

    private static final Set<CodeType> TypeSet = Collections.unmodifiableSet(EnumSet.allOf(CodeType.class));

    private static final Set<String> TypeStringSet;
    static {
        LinkedHashSet<String> foo = new LinkedHashSet<String>();
        for (CodeType x : CodeType.values()) {
            foo.add(x.toString());
        }
        TypeStringSet = Collections.unmodifiableSet(foo);
    }

    public static final String DESCRIPTION_SEPARATOR = "\u25AA";

    public static final String NO_COUNTRY = "001";

    private static StandardCodes singleton;

    private EnumMap<CodeType, Map<String, List<String>>> type_code_data = new EnumMap<CodeType, Map<String, List<String>>>(
        CodeType.class);

    private EnumMap<CodeType, Map<String, List<String>>> type_name_codes = new EnumMap<CodeType, Map<String, List<String>>>(
        CodeType.class);

    private EnumMap<CodeType, Map<String, String>> type_code_preferred = new EnumMap<CodeType, Map<String, String>>(
        CodeType.class);

    private Map<String, Set<String>> country_modernCurrency = new TreeMap<String, Set<String>>();

    private Map<CodeType, Set<String>> goodCodes = new TreeMap<CodeType, Set<String>>();

    private static final boolean DEBUG = false;

    /**
     * Get the singleton copy of the standard codes.
     */
    static public synchronized StandardCodes make() {
        if (singleton == null)
            singleton = new StandardCodes();
        return singleton;
    }

    /**
     * The data is the name in the case of RFC3066 codes, and the country code in
     * the case of TZIDs and ISO currency codes. If the country code is missing,
     * uses ZZ.
     */
    public String getData(String type, String code) {
        Map<String, List<String>> code_data = getCodeData(type);
        if (code_data == null)
            return null;
        List<String> list = code_data.get(code);
        if (list == null)
            return null;
        return list.get(0);
    }

    /**
     * @return the full data for the type and code For the data in lstreg, it is
     *         description | date | canonical_value | recommended_prefix #
     *         comments
     */
    public List<String> getFullData(String type, String code) {
        Map<String, List<String>> code_data = getCodeData(type);
        if (code_data == null)
            return null;
        return code_data.get(code);
    }

    /**
     * @return the full data for the type and code For the data in lstreg, it is
     *         description | date | canonical_value | recommended_prefix #
     *         comments
     */
    public List<String> getFullData(CodeType type, String code) {
        Map<String, List<String>> code_data = type_code_data.get(type);
        if (code_data == null)
            return null;
        return code_data.get(code);
    }

    private Map<String, List<String>> getCodeData(String type) {
        return getCodeData(CodeType.from(type));
    }

    private Map<String, List<String>> getCodeData(CodeType type) {
        return type_code_data.get(type);
    }

    /**
     * Get at the language registry values, as a Map from label to value.
     *
     * @param type
     * @param code
     * @return
     */
    public Map<String, String> getLangData(String type, String code) {
        try {
            if (type.equals("territory"))
                type = "region";
            else if (type.equals("variant")) code = code.toLowerCase(Locale.ENGLISH);
            return (Map) ((Map) getLStreg().get(type)).get(code);
        } catch (RuntimeException e) {
            return null;
        }
    }

    /**
     * Return a replacement code, if available. If not, return null.
     *
     */
    public String getReplacement(String type, String code) {
        if (type.equals("currency"))
            return null; // no replacement codes for currencies
        List<String> data = getFullData(type, code);
        if (data == null)
            return null;
        // if available, the replacement is a non-empty value other than --, in
        // position 2.
        if (data.size() < 3)
            return null;
        String replacement = (String) data.get(2);
        if (!replacement.equals("") && !replacement.equals("--"))
            return replacement;
        return null;
    }

    /**
     * Return the list of codes that have the same data. For example, returns all
     * currency codes for a country. If there is a preferred one, it is first.
     *
     * @param type
     * @param data
     * @return
     */
    @Deprecated
    public List<String> getCodes(String type, String data) {
        return getCodes(CodeType.valueOf(type), data);
    }

    /**
     * Return the list of codes that have the same data. For example, returns all
     * currency codes for a country. If there is a preferred one, it is first.
     */
    public List<String> getCodes(CodeType type, String data) {
        Map<String, List<String>> data_codes = type_name_codes.get(type);
        if (data_codes == null)
            return null;
        return Collections.unmodifiableList(data_codes.get(data));
    }

    /**
     * Where there is a preferred code, return it.
     */
    @Deprecated
    public String getPreferred(String type, String code) {
        return getPreferred(CodeType.valueOf(type), code);
    }

    /**
     * Where there is a preferred code, return it.
     */

    public String getPreferred(CodeType type, String code) {
        Map<String, String> code_preferred = type_code_preferred.get(type);
        if (code_preferred == null)
            return code;
        String newCode = code_preferred.get(code);
        if (newCode == null)
            return code;
        return newCode;
    }

    /**
     * Get all the available types
     */
    public Set<String> getAvailableTypes() {
        return TypeStringSet;
    }

    /**
     * Get all the available types
     */
    public Set<CodeType> getAvailableTypesEnum() {
        return TypeSet;
    }

    /**
     * Get all the available codes for a given type
     *
     * @param type
     * @return
     */
    public Set<String> getAvailableCodes(String type) {
        return getAvailableCodes(CodeType.from(type));
    }

    /**
     * Get all the available codes for a given type
     *
     * @param type
     * @return
     */
    public Set<String> getAvailableCodes(CodeType type) {
        Map<String, List<String>> code_name = type_code_data.get(type);
        return Collections.unmodifiableSet(code_name.keySet());
    }

    public Set<String> getGoodAvailableCodes(String stringType) {
        return getGoodAvailableCodes(CodeType.from(stringType));
    }

    /**
     * Get all the available "real" codes for a given type, excluding private use,
     * but including some deprecated codes. Use SupplementalDataInfo getLocaleAliases to
     * exclude others.
     *
     * @param type
     * @return
     */
    public Set<String> getGoodAvailableCodes(CodeType type) {
        Set<String> result = goodCodes.get(type);
        if (result == null) {
            synchronized (goodCodes) {
                Map<String, List<String>> code_name = getCodeData(type);
                SupplementalDataInfo sd = SupplementalDataInfo.getInstance();
                if (code_name == null)
                    return null;
                result = new TreeSet<String>(code_name.keySet());
                switch (type) {
                case currency:
                    break; // nothing special                    
                case language:
                    return sd.getCLDRLanguageCodes();
                case script:
                    return sd.getCLDRScriptCodes();
                case tzid:
                    break; // nothing special
                default:
                    for (Iterator<String> it = result.iterator(); it.hasNext();) {
                        String code = (String) it.next();
                        if (code.equals("root") || code.equals("QO"))
                            continue;
                        List<String> data = getFullData(type, code);
                        if (data.size() < 3) {
                            if (DEBUG)
                                System.out.println(code + "\t" + data);
                        }
                        if ("PRIVATE USE".equalsIgnoreCase(data.get(0))
                            || (!data.get(2).equals("") && !data.get(2).equals("--"))) {
                            // System.out.println("Removing: " + code);
                            it.remove();
                        }
                    }
                }
                result = Collections.unmodifiableSet(result);
                goodCodes.put(type, result);
            }
        }
        return result;
    }

    private static Set<String> GOOD_COUNTRIES;

    public Set<String> getGoodCountries() {
        synchronized (goodCodes) {
            if (GOOD_COUNTRIES == null) {
                Set<String> temp = new LinkedHashSet<String>();
                for (String s : getGoodAvailableCodes(CodeType.territory)) {
                    if (isCountry(s)) {
                        temp.add(s);
                    }
                }
                GOOD_COUNTRIES = Collections.unmodifiableSet(temp);
            }
        }
        return GOOD_COUNTRIES;
    }

    /**
     * Gets the modern currency.
     */
    public Set<String> getMainCurrencies(String countryCode) {
        return country_modernCurrency.get(countryCode);
    }

    private EnumMap<Organization, Map<String, Level>> platform_locale_level = null;
    private EnumMap<Organization, Relation<Level, String>> platform_level_locale = null;
    private Map<String, Map<String, String>> platform_locale_levelString = null;

//    /**
//     * Get rid of this
//     *
//     * @param type
//     * @return
//     * @throws IOException
//     * @deprecated
//     */
//    public String getEffectiveLocaleType(String type) throws IOException {
//        if ((type != null) && (getLocaleCoverageOrganizations().contains(Organization.valueOf(type)))) {
//            return type;
//        } else {
//            return null; // the default.. for now..
//        }
//    }

    static Comparator caseless = new Comparator() {

        public int compare(Object arg0, Object arg1) {
            String s1 = (String) arg0;
            String s2 = (String) arg1;
            return s1.compareToIgnoreCase(s2);
        }

    };

    /**
     * Returns locales according to status. It returns a Map of Maps, key 1 is
     * either IBM or Java (perhaps more later), key 2 is the Level.
     *
     * @deprecated
     */
    public Map<Organization, Map<String, Level>> getLocaleTypes() {
        synchronized (StandardCodes.class) {
            if (platform_locale_level == null) {
                loadPlatformLocaleStatus();
            }
        }
        return platform_locale_level;
    }

    /**
     * Return map of locales to levels
     * @param org
     * @return
     */
    public Map<String, Level> getLocaleToLevel(Organization org) {
        return getLocaleTypes().get(org);
    }

    public Level getLocaleCoverageLevel(String organization, String desiredLocale) {
        return getLocaleCoverageLevel(Organization.fromString(organization), desiredLocale);
    }

    public Level getLocaleCoverageLevel(Organization organization, String desiredLocale) {
        return getLocaleCoverageLevel(organization, desiredLocale, new Output<LocaleCoverageType>());
    }

    public enum LocaleCoverageType {
        explicit, parent, star, undetermined
    }

    /**
     * Returns coverage level of locale according to organization. Returns Level.UNDETERMINED if information is missing.
     * A locale of "*" in the data means "everything else".
     */
    public Level getLocaleCoverageLevel(Organization organization, String desiredLocale, Output<LocaleCoverageType> coverageType) {
        synchronized (StandardCodes.class) {
            if (platform_locale_level == null) {
                loadPlatformLocaleStatus();
            }
        }
        coverageType.value = LocaleCoverageType.undetermined;
        if (organization == null) {
            return Level.UNDETERMINED;
        }
        Map<String, Level> locale_status = platform_locale_level.get(organization);
        if (locale_status == null) {
            return Level.UNDETERMINED;
        }
        // see if there is a parent
        String originalLocale = desiredLocale;
        while (desiredLocale != null) {
            Level status = locale_status.get(desiredLocale);
            if (status != null && status != Level.UNDETERMINED) {
                coverageType.value = originalLocale == desiredLocale ? LocaleCoverageType.explicit : LocaleCoverageType.parent;
                return status;
            }
            desiredLocale = LocaleIDParser.getParent(desiredLocale);
        }
        Level status = locale_status.get("*");
        if (status != null && status != Level.UNDETERMINED) {
            coverageType.value = LocaleCoverageType.star;
            return status;
        }
        return Level.UNDETERMINED;
    }

    /**
     * Returns coverage level of locale according to organization. Returns Level.UNDETERMINED if information is missing.
     */
    public Level getDefaultLocaleCoverageLevel(Organization organization) {
        return getLocaleCoverageLevel(organization, "*");
    }

    public Set<Organization> getLocaleCoverageOrganizations() {
        synchronized (StandardCodes.class) {
            if (platform_locale_level == null) {
                loadPlatformLocaleStatus();
            }
        }
        return platform_locale_level.keySet();
    }

    public Set<String> getLocaleCoverageOrganizationStrings() {
        synchronized (StandardCodes.class) {
            if (platform_locale_level == null) {
                loadPlatformLocaleStatus();
            }
        }
        return platform_locale_levelString.keySet();
    }

    public Set<String> getLocaleCoverageLocales(String organization) {
        return getLocaleCoverageLocales(Organization.fromString(organization));
    }

    public Set<String> getLocaleCoverageLocales(Organization organization) {
        synchronized (StandardCodes.class) {
            if (platform_locale_level == null) {
                loadPlatformLocaleStatus();
            }
        }
        return platform_locale_level.get(organization).keySet();
    }

    public Relation<Level, String> getLevelsToLocalesFor(Organization organization) {
        synchronized (StandardCodes.class) {
            if (platform_level_locale == null) {
                loadPlatformLocaleStatus();
            }
        }
        return platform_level_locale.get(organization);
    }

    public Set<String> getLocaleCoverageLocales(Organization organization, Set<Level> choice) {
        Set<String> result = new LinkedHashSet<String>();
        for (String locale : getLocaleCoverageLocales(organization)) {
            if (choice.contains(getLocaleCoverageLevel(organization, locale))) {
                result.add(locale);
            }
        }
        return result;
    }

    private void loadPlatformLocaleStatus() {
        LocaleIDParser parser = new LocaleIDParser();
        platform_locale_level = new EnumMap<Organization, Map<String, Level>>(Organization.class);
        SupplementalDataInfo sd = SupplementalDataInfo.getInstance();
        Set<String> defaultContentLocales = sd.getDefaultContentLocales();
        String line;
        try {
            BufferedReader lstreg = CldrUtility.getUTF8Data("Locales.txt");
            while (true) {
                line = lstreg.readLine();
                if (line == null)
                    break;
                int commentPos = line.indexOf('#');
                if (commentPos >= 0) {
                    line = line.substring(0, commentPos);
                }
                line = line.trim();
                if (line.length() == 0)
                    continue;
                List<String> stuff = CldrUtility.splitList(line, ';', true);
                Organization organization;

                // verify that the organization is valid
                try {
                    organization = Organization.fromString(stuff.get(0));
                } catch (Exception e) {
                    throw new IllegalArgumentException("Invalid organization in Locales.txt: " + line);
                }

                // verify that the locale is valid BCP47
                String locale = (String) stuff.get(1);
                if (!locale.equals("*")) {
                    parser.set(locale);
                    String valid = validate(parser);
                    if (valid.length() != 0) {
                        throw new IllegalArgumentException("Invalid locale in Locales.txt: " + line);
                    }
                    locale = parser.toString(); // normalize

                    // verify that the locale is not a default content locale
                    if (defaultContentLocales.contains(locale)) {
                        throw new IllegalArgumentException("Cannot have default content locale in Locales.txt: " + line);
                    }
                }

                Level status = Level.get((String) stuff.get(2));
                if (status == Level.UNDETERMINED) {
                    System.out.println("Warning: Level unknown on: " + line);
                }
                Map<String, Level> locale_status = platform_locale_level.get(organization);
                if (locale_status == null) {
                    platform_locale_level.put(organization, locale_status = new TreeMap<String, Level>());
                }
                locale_status.put(locale, status);
                if (!locale.equals("*")) {
                    String scriptLoc = parser.getLanguageScript();
                    if (locale_status.get(scriptLoc) == null)
                        locale_status.put(scriptLoc, status);
                    String lang = parser.getLanguage();
                    if (locale_status.get(lang) == null)
                        locale_status.put(lang, status);
                }
            }
        } catch (IOException e) {
            throw new ICUUncheckedIOException("Internal Error", e);
        }

        // now reset the parent to be the max of the children
        for (Organization platform : platform_locale_level.keySet()) {
            Map<String, Level> locale_level = platform_locale_level.get(platform);
            for (String locale : locale_level.keySet()) {
                parser.set(locale);
                Level childLevel = locale_level.get(locale);

                String language = parser.getLanguage();
                if (!language.equals(locale)) {
                    Level languageLevel = (Level) locale_level.get(language);
                    if (languageLevel == null || languageLevel.compareTo(childLevel) < 0) {
                        locale_level.put(language, childLevel);
                    }
                }
                String oldLanguage = language;
                language = parser.getLanguageScript();
                if (!language.equals(oldLanguage)) {
                    Level languageLevel = (Level) locale_level.get(language);
                    if (languageLevel == null || languageLevel.compareTo(childLevel) < 0) {
                        locale_level.put(language, childLevel);
                    }
                }
            }
        }
        // backwards compat hack
        platform_locale_levelString = new TreeMap<String, Map<String, String>>();
        platform_level_locale = new EnumMap<>(Organization.class);
        for (Organization platform : platform_locale_level.keySet()) {
            Map<String, String> locale_levelString = new TreeMap<String, String>();
            platform_locale_levelString.put(platform.toString(), locale_levelString);
            Map<String, Level> locale_level = platform_locale_level.get(platform);
            for (String locale : locale_level.keySet()) {
                locale_levelString.put(locale, locale_level.get(locale).toString());
            }
            Relation level_locale = Relation.of(new EnumMap(Level.class), HashSet.class);
            level_locale.addAllInverted(locale_level).freeze();
            platform_level_locale.put(platform, level_locale);
        }
        CldrUtility.protectCollection(platform_level_locale);
        platform_locale_level = CldrUtility.protectCollection(platform_locale_level);
        platform_locale_levelString = CldrUtility.protectCollection(platform_locale_levelString);
    }

    private String validate(LocaleIDParser parser) {
        String message = "";
        String lang = parser.getLanguage();
        if (lang.length() == 0) {
            message += ", Missing language";
        } else if (!getAvailableCodes("language").contains(lang)) {
            message += ", Invalid language code: " + lang;
        }
        String script = parser.getScript();
        if (script.length() != 0 && !getAvailableCodes("script").contains(script)) {
            message += ", Invalid script code: " + script;
        }
        String territory = parser.getRegion();
        if (territory.length() != 0 && !getAvailableCodes("territory").contains(territory)) {
            message += ", Invalid territory code: " + lang;
        }
        return message.length() == 0 ? message : message.substring(2);
    }

    /**
     * Ascertain that the given locale in in the given group specified by the
     * organization
     *
     * @param locale
     * @param group
     * @param org
     * @return boolean
     */
    public boolean isLocaleInGroup(String locale, String group, Organization org) {
        return group.equals(getGroup(locale, org));
    }

    public boolean isLocaleInGroup(String locale, String group, String org) {
        return isLocaleInGroup(locale, group, Organization.fromString(org));
    }

    public String getGroup(String locale, String org) {
        return getGroup(locale, Organization.fromString(org));
    }

    /**
     * Gets the coverage group given a locale and org
     *
     * @param locale
     * @param org
     * @return group if availble, null if not
     */
    public String getGroup(String locale, Organization org) {
        Level l = getLocaleCoverageLevel(org, locale);
        if (l.equals(Level.UNDETERMINED)) {
            return null;
        } else {
            return l.toString();
        }
    }

    // ========== PRIVATES ==========

    private StandardCodes() {
        String[] files = { /* "lstreg.txt", */"ISO4217.txt" }; // , "TZID.txt"
        type_code_preferred.put(CodeType.tzid, new TreeMap<String, String>());
        add(CodeType.language, "root", "Root");
        String originalLine = null;
        for (int fileIndex = 0; fileIndex < files.length; ++fileIndex) {
            try {
                BufferedReader lstreg = CldrUtility.getUTF8Data(files[fileIndex]);
                while (true) {
                    String line = originalLine = lstreg.readLine();
                    if (line == null)
                        break;
                    if (line.startsWith("\uFEFF")) {
                        line = line.substring(1);
                    }
                    line = line.trim();
                    int commentPos = line.indexOf('#');
                    String comment = "";
                    if (commentPos >= 0) {
                        comment = line.substring(commentPos + 1).trim();
                        line = line.substring(0, commentPos);
                    }
                    if (line.length() == 0)
                        continue;
                    List<String> pieces = CldrUtility.splitList(line, '|', true,
                        new ArrayList<String>());
                    CodeType type = CodeType.from(pieces.get(0));
                    pieces.remove(0);

                    String code = pieces.get(0);
                    pieces.remove(0);
                    if (type.equals("date")) {
                        continue;
                    }

                    String oldName = pieces.get(0);
                    int pos = oldName.indexOf(';');
                    if (pos >= 0) {
                        oldName = oldName.substring(0, pos).trim();
                        pieces.set(0, oldName);
                    }

                    List<String> data = pieces;
                    if (comment.indexOf("deprecated") >= 0) {
                        // System.out.println(originalLine);
                        if (data.get(2).toString().length() == 0) {
                            data.set(2, "--");
                        }
                    }
                    if (oldName.equalsIgnoreCase("PRIVATE USE")) {
                        int separatorPos = code.indexOf("..");
                        if (separatorPos < 0) {
                            add(type, code, data);
                        } else {
                            String current = code.substring(0, separatorPos);
                            String end = code.substring(separatorPos + 2);
                            // System.out.println(">>" + code + "\t" + current + "\t" + end);
                            for (; current.compareTo(end) <= 0; current = nextAlpha(current)) {
                                // System.out.println(">" + current);
                                add(type, current, data);
                            }
                        }
                        continue;
                    }
                    if (!type.equals("tzid")) {
                        add(type, code, data);
                        if (type.equals("currency")) {
                            // currency | TPE | Timor Escudo | TP | EAST TIMOR | O
                            if (data.get(3).equals("C")) {
                                String country = (String) data.get(1);
                                Set<String> codes = country_modernCurrency.get(country);
                                if (codes == null) {
                                    country_modernCurrency.put(country, codes = new TreeSet<String>());
                                }
                                codes.add(code);
                            }
                        }
                        continue;
                    }
                    // type = tzid
                    // List codes = (List) Utility.splitList(code, ',', true, new
                    // ArrayList());
                    String preferred = null;
                    for (int i = 0; i < pieces.size(); ++i) {
                        code = (String) pieces.get(i);
                        add(type, code, data);
                        if (preferred == null)
                            preferred = code;
                        else {
                            Map<String, String> code_preferred = type_code_preferred.get(type);
                            code_preferred.put(code, preferred);
                        }
                    }
                }
                lstreg.close();
            } catch (Exception e) {
                System.err.println("WARNING: " + files[fileIndex]
                    + " may be a corrupted UTF-8 file. Please check.");
                throw (IllegalArgumentException) new IllegalArgumentException(
                    "Can't read " + files[fileIndex] + "\t" + originalLine)
                        .initCause(e);
            }
            country_modernCurrency = CldrUtility.protectCollection(country_modernCurrency);
        }

        // data is: description | date | canonical_value | recommended_prefix #
        // comments
        // HACK, just rework

        Map<String, Map<String, Map<String, String>>> languageRegistry = getLStreg();
        // languageRegistry = CldrUtility.protectCollection(languageRegistry);

        for (String type : languageRegistry.keySet()) {
            CodeType type2 = CodeType.from(type);
            Map<String, Map<String, String>> m = languageRegistry.get(type);
            for (String code : m.keySet()) {
                Map<String, String> mm = m.get(code);
                List<String> data = new ArrayList<String>(0);
                data.add(mm.get("Description"));
                data.add(mm.get("Added"));
                String pref = mm.get("Preferred-Value");
                if (pref == null) {
                    pref = mm.get("Deprecated");
                    if (pref == null)
                        pref = "";
                    else
                        pref = "deprecated";
                }
                data.add(pref);
                if (type.equals("variant")) {
                    code = code.toUpperCase();
                }
                // data.add(mm.get("Recommended_Prefix"));
                // {"region", "BQ", "Description", "British Antarctic Territory",
                // "Preferred-Value", "AQ", "CLDR", "True", "Deprecated", "True"},
                add(type2, code, data);
            }
        }

        Map<String, List<String>> m = getZoneData();
        for (Iterator<String> it = m.keySet().iterator(); it.hasNext();) {
            String code = it.next();
            add(CodeType.tzid, code, m.get(code).toString());
        }
    }

    /**
     * @param current
     * @return
     */
    private static String nextAlpha(String current) {
        // Don't care that this is inefficient
        int value = 0;
        for (int i = 0; i < current.length(); ++i) {
            char c = current.charAt(i);
            c -= c < 'a' ? 'A' : 'a';
            value = value * 26 + c;
        }
        value += 1;
        String result = "";
        for (int i = 0; i < current.length(); ++i) {
            result = (char) ((value % 26) + 'A') + result;
            value = value / 26;
        }
        if (UCharacter.toLowerCase(current).equals(current)) {
            result = UCharacter.toLowerCase(result);
        } else if (UCharacter.toUpperCase(current).equals(current)) {
            // do nothing
        } else {
            result = UCharacter.toTitleCase(result, null);
        }
        return result;
    }

    /**
     * @param string
     * @param string2
     * @param string3
     */
    private void add(CodeType type, String string2, String string3) {
        List<String> l = new ArrayList<String>();
        l.add(string3);
        add(type, string2, l);
    }

    private void add(CodeType type, String code, List<String> otherData) {
        // hack
        if (type == CodeType.script) {
            if (code.equals("Qaai")) {
                otherData = new ArrayList<String>(otherData);
                otherData.set(0, "Inherited");
            } else if (code.equals("Zyyy")) {
                otherData = new ArrayList<String>(otherData);
                otherData.set(0, "Common");
            }
        }

        // assume name is the first item

        String name = otherData.get(0);

        // add to main list
        Map<String, List<String>> code_data = getCodeData(type);
        if (code_data == null) {
            code_data = new TreeMap<String, List<String>>();
            type_code_data.put(type, code_data);
        }
        List<String> lastData = code_data.get(code);
        if (lastData != null) {
            lastData.addAll(otherData);
        } else {
            code_data.put(code, otherData);
        }

        // now add mapping from name to codes
        Map<String, List<String>> name_codes = type_name_codes.get(type);
        if (name_codes == null) {
            name_codes = new TreeMap<String, List<String>>();
            type_name_codes.put(type, name_codes);
        }
        List<String> codes = name_codes.get(name);
        if (codes == null) {
            codes = new ArrayList<String>();
            name_codes.put(name, codes);
        }
        codes.add(code);
    }

    private List<String> DELETED3166 = Collections.unmodifiableList(Arrays
        .asList(new String[] { "BQ", "BU", "CT", "DD", "DY", "FQ", "FX", "HV",
            "JT", "MI", "NH", "NQ", "NT", "PC", "PU", "PZ", "RH", "SU", "TP",
            "VD", "WK", "YD", "YU", "ZR" }));

    public List<String> getOld3166() {
        return DELETED3166;
    }

    private Map<String, List<String>> WorldBankInfo;

    public Map<String, List<String>> getWorldBankInfo() {
        if (WorldBankInfo == null) {
            List<String> temp = fillFromCommaFile("WorldBankInfo.txt", false);
            WorldBankInfo = new HashMap<String, List<String>>();
            for (String line : temp) {
                List<String> row = CldrUtility.splitList(line, ';', true);
                String key = row.get(0);
                row.remove(0);
                WorldBankInfo.put(key, row);
            }
            WorldBankInfo = CldrUtility.protectCollection(WorldBankInfo);
        }
        return WorldBankInfo;
    }

    Set<String> moribundLanguages;

    public Set<String> getMoribundLanguages() {
        if (moribundLanguages == null) {
            List<String> temp = fillFromCommaFile("moribund_languages.txt", true);
            moribundLanguages = new TreeSet<String>();
            moribundLanguages.addAll(temp);
            moribundLanguages = CldrUtility.protectCollection(moribundLanguages);
        }
        return moribundLanguages;
    }

    // produces a list of the 'clean' lines
    private List<String> fillFromCommaFile(String filename, boolean trim) {
        try {
            List<String> result = new ArrayList<String>();
            String line;
            BufferedReader lstreg = CldrUtility.getUTF8Data(filename);
            while (true) {
                line = lstreg.readLine();
                if (line == null)
                    break;
                int commentPos = line.indexOf('#');
                if (commentPos >= 0) {
                    line = line.substring(0, commentPos);
                }
                if (trim) {
                    line = line.trim();
                }
                if (line.length() == 0)
                    continue;
                result.add(line);
            }
            return result;
        } catch (Exception e) {
            throw (RuntimeException) new IllegalArgumentException(
                "Can't process file: data/" + filename).initCause(e);
        }
    }

    // return a complex map. language -> arn -> {"Comments" -> "x",
    // "Description->y,...}
    static String[][] extras = {
        { "language", "root", "Description", "Root", "CLDR", "True" },
        // { "language", "cch", "Description", "Atsam", "CLDR", "True" },
        // { "language", "kaj", "Description", "Jju", "CLDR", "True" },
        // { "language", "kcg", "Description", "Tyap", "CLDR", "True" },
        // { "language", "kfo", "Description", "Koro", "CLDR", "True" },
        // { "language", "mfe", "Description", "Morisyen", "CLDR", "True" },
        // { "region", "172", "Description", "Commonwealth of Independent States", "CLDR", "True" },
        // { "region", "062", "Description", "South-Central Asia", "CLDR", "True" },
        // { "region", "003", "Description", "North America", "CLDR", "True" },
        //        { "variant", "POLYTONI", "Description", "Polytonic Greek", "CLDR", "True", "Preferred-Value", "POLYTON" },
        { "variant", "REVISED", "Description", "Revised Orthography", "CLDR", "True" },
        { "variant", "SAAHO", "Description", "Dialect", "CLDR", "True" },
        { "variant", "POSIX", "Description", "Computer-Style", "CLDR", "True" },
        // {"region", "172", "Description", "Commonwealth of Independent States",
        // "CLDR", "True"},
        // { "region", "", "Description", "European Union", "CLDR", "True" },
        { "region", "ZZ", "Description", "Unknown or Invalid Region", "CLDR", "True" },
        { "region", "QO", "Description", "Outlying Oceania", "CLDR", "True" },
        { "region", "XK", "Description", "Kosovo", "CLDR", "True" },
        { "script", "Qaai", "Description", "Inherited", "CLDR", "True" },
        // {"region", "003", "Description", "North America", "CLDR", "True"},
        // {"region", "062", "Description", "South-central Asia", "CLDR", "True"},
        // {"region", "200", "Description", "Czechoslovakia", "CLDR", "True"},
        // {"region", "830", "Description", "Channel Islands", "CLDR", "True"},
        // {"region", "833", "Description", "Isle of Man", "CLDR", "True"},

        // {"region", "NT", "Description", "Neutral Zone (formerly between Saudi
        // Arabia & Iraq)", "CLDR", "True", "Deprecated", "True"},
        // {"region", "SU", "Description", "Union of Soviet Socialist Republics",
        // "CLDR", "True", "Deprecated", "True"},
        // {"region", "BQ", "Description", "British Antarctic Territory",
        // "Preferred-Value", "AQ", "CLDR", "True", "Deprecated", "True"},
        // {"region", "CT", "Description", "Canton and Enderbury Islands",
        // "Preferred-Value", "KI", "CLDR", "True", "Deprecated", "True"},
        // {"region", "FQ", "Description", "French Southern and Antarctic Territories
        // (now split between AQ and TF)", "CLDR", "True", "Deprecated", "True"},
        // {"region", "JT", "Description", "Johnston Island", "Preferred-Value", "UM",
        // "CLDR", "True", "Deprecated", "True"},
        // {"region", "MI", "Description", "Midway Islands", "Preferred-Value", "UM",
        // "CLDR", "True", "Deprecated", "True"},
        // {"region", "NQ", "Description", "Dronning Maud Land", "Preferred-Value",
        // "AQ", "CLDR", "True", "Deprecated", "True"},
        // {"region", "PC", "Description", "Pacific Islands Trust Territory (divided
        // into FM, MH, MP, and PW)", "Preferred-Value", "AQ", "CLDR", "True",
        // "Deprecated", "True"},
        // {"region", "PU", "Description", "U.S. Miscellaneous Pacific Islands",
        // "Preferred-Value", "UM", "CLDR", "True", "Deprecated", "True"},
        // {"region", "PZ", "Description", "Panama Canal Zone", "Preferred-Value",
        // "PA", "CLDR", "True", "Deprecated", "True"},
        // {"region", "VD", "Description", "North Vietnam", "Preferred-Value", "VN",
        // "CLDR", "True", "Deprecated", "True"},
        // {"region", "WK", "Description", "Wake Island", "Preferred-Value", "UM",
        // "CLDR", "True", "Deprecated", "True"},
    };

    static final String registryName = CldrUtility.getProperty("registry", "language-subtag-registry");

    public enum LstrType {
        language("und", "zxx", "mul", "mis", "root"), script("Zzzz", "Zsym", "Zxxx", "Zmth"), region("ZZ"), variant(), extlang(true, false), grandfathered(true,
            false), redundant(true, false),
        /** specialized codes for validity; TODO: rename LstrType **/
        currency(false, true, "XXX"), subdivision(false, true), unit(false, true);

        public final Set<String> specials;
        public final String unknown;
        public final boolean isLstr;
        public final boolean isUnicode;

        private LstrType(String... unknownValue) {
            this(true, true, unknownValue);
        }

        private LstrType(boolean lstr, boolean unicode, String... unknownValue) {
            unknown = unknownValue.length == 0 ? null : unknownValue[0];
            LinkedHashSet<String> set = new LinkedHashSet<>(Arrays.asList(unknownValue));
            if (unknown != null) {
                set.remove(unknown);
            }
            specials = Collections.unmodifiableSet(set);
            isLstr = lstr;
            isUnicode = unicode;
        }

        // 
        static final Pattern WELLFORMED = Pattern.compile("([0-9]{3}|[a-zA-Z]{2})[a-zA-Z0-9]{1,4}");

        boolean isWellFormed(String candidate) {
            switch (this) {
            case subdivision:
                return WELLFORMED.matcher(candidate).matches();
            default:
                throw new UnsupportedOperationException();
            }
        }
    }

    public enum LstrField {
        Type, Subtag, Description, Added, Scope, Tag, Suppress_Script, Macrolanguage, Deprecated, Preferred_Value, Comments, Prefix, CLDR;
        public static LstrField from(String s) {
            return LstrField.valueOf(s.trim().replace("-", "_"));
        }
    }

    static Map<String, Map<String, Map<String, String>>> LSTREG;
    static Map<LstrType, Map<String, Map<LstrField, String>>> LSTREG_ENUM;
    static Map<LstrType, Map<String, Map<LstrField, String>>> LSTREG_RAW;

    /**
     * Returns a map like {extlang={aao={Added=2009-07-29, Description=Algerian Saharan Arabic, ...<br>
     * That is, type => subtype => map<tag,value>. Descriptions are concatenated together, separated by
     * DESCRIPTION_SEPARATOR.
     *
     * @return
     */
    public static Map<String, Map<String, Map<String, String>>> getLStreg() {
        if (LSTREG == null) {
            initLstr();
        }
        return LSTREG;
    }

    /**
     * Returns a map like {extlang={aao={Added=2009-07-29, Description=Algerian Saharan Arabic, ...<br>
     * That is, type => subtype => map<tag,value>. Descriptions are concatenated together, separated by
     * DESCRIPTION_SEPARATOR.
     *
     * @return
     */
    public static Map<LstrType, Map<String, Map<LstrField, String>>> getEnumLstreg() {
        if (LSTREG_ENUM == null) {
            initLstr();
        }
        return LSTREG_ENUM;
    }

    public static Map<LstrType, Map<String, Map<LstrField, String>>> getLstregEnumRaw() {
        if (LSTREG_ENUM == null) {
            initLstr();
        }
        return LSTREG_RAW;
    }

    private static void initLstr() {
        Map<LstrType, Map<String, Map<LstrField, String>>> result2 = new TreeMap<LstrType, Map<String, Map<LstrField, String>>>();

        int lineNumber = 1;

        Set<String> funnyTags = new TreeSet<String>();
        String line;
        try {
            BufferedReader lstreg = CldrUtility.getUTF8Data(registryName);
            LstrType lastType = null;
            String lastTag = null;
            Map<String, Map<LstrField, String>> subtagData = null;
            Map<LstrField, String> currentData = null;
            LstrField lastLabel = null;
            String lastRest = null;
            boolean inRealContent = false;
//            Map<String, String> translitCache = new HashMap<String, String>();
            for (;; ++lineNumber) {
                line = lstreg.readLine();
                if (line == null)
                    break;
                if (line.length() == 0)
                    continue; // skip blanks
                if (line.startsWith("File-Date: ")) {
                    if (DEBUG) System.out.println("Language Subtag Registry: " + line);
                    inRealContent = true;
                    continue;
                }
                if (!inRealContent) {
                    // skip until we get to real content
                    continue;
                }
                // skip cruft
                if (line.startsWith("Internet-Draft")) {
                    continue;
                }
                if (line.startsWith("Ewell")) {
                    continue;
                }
                if (line.startsWith("\f")) {
                    continue;
                }
                if (line.startsWith("4.  Security Considerations")) {
                    break;
                }

                if (line.startsWith("%%"))
                    continue; // skip separators (ok, since data starts with Type:
                if (line.startsWith(" ")) {
                    currentData.put(lastLabel, lastRest + " " + line.trim());
                    continue;
                }

                /*
                 * Type: language Subtag: aa Description: Afar Added: 2005-10-16
                 * Suppress-Script: Latn
                 */
                int pos2 = line.indexOf(':');
                LstrField label = LstrField.from(line.substring(0, pos2));
                String rest = line.substring(pos2 + 1).trim();
                if (label == LstrField.Type) {
                    subtagData = CldrUtility.get(result2, lastType = LstrType.valueOf(rest));
                    if (subtagData == null) {
                        result2.put(LstrType.valueOf(rest), subtagData = new TreeMap<String, Map<LstrField, String>>());
                    }
                } else if (label == LstrField.Subtag
                    || label == LstrField.Tag) {
                    lastTag = rest;
                    String endTag = null;
                    // Subtag: qaa..qtz
                    int pos = lastTag.indexOf("..");
                    if (pos >= 0) {
                        endTag = lastTag.substring(pos + 2);
                        lastTag = lastTag.substring(0, pos);
                    }
                    currentData = new TreeMap<LstrField, String>();
                    if (endTag == null) {
                        putSubtagData(lastTag, subtagData, currentData);
                        languageCount.add(lastType, 1);
                        // System.out.println(languageCount.getCount(lastType) + "\t" + lastType + "\t" + lastTag);
                    } else {
                        for (; lastTag.compareTo(endTag) <= 0; lastTag = nextAlpha(lastTag)) {
                            // System.out.println(">" + current);
                            putSubtagData(lastTag, subtagData, currentData);
                            languageCount.add(lastType, 1);
                            // System.out.println(languageCount.getCount(lastType) + "\t" + lastType + "\t" + lastTag);
                        }

                    }
                    // label.equalsIgnoreCase("Added") || label.equalsIgnoreCase("Suppress-Script")) {
                    // skip
                    // } else if (pieces.length < 2) {
                    // System.out.println("Odd Line: " + lastType + "\t" + lastTag + "\t" + line);
                } else {
                    lastLabel = label;
                    // The following code was removed because in the standard tests (TestAll) both lastRest and rest were always equal.
                    //                    if(!translitCache.containsKey(rest)) {
                    //                        lastRest = TransliteratorUtilities.fromXML.transliterate(rest);
                    //                        translitCache.put(rest, lastRest);
                    //                        if (!lastRest.equals(rest)) {
                    //                            System.out.println(System.currentTimeMillis()+" initLStr: LastRest: '"+lastRest+"' Rest: '"+rest+"'");
                    //                        }
                    //                    } else {
                    //                        lastRest = translitCache.get(rest);
                    //                    }
                    lastRest = rest;
                    String oldValue = (String) CldrUtility.get(currentData, lastLabel);
                    if (oldValue != null) {
                        lastRest = oldValue + DESCRIPTION_SEPARATOR + lastRest;
                    }
                    currentData.put(lastLabel, lastRest);
                }
            }
        } catch (Exception e) {
            throw (RuntimeException) new IllegalArgumentException(
                "Can't process file: data/"
                    + registryName + ";\t at line " + lineNumber).initCause(e);
        } finally {
            if (!funnyTags.isEmpty()) {
                if (DEBUG)
                    System.out.println("Funny tags: " + funnyTags);
            }
        }
        // copy raw
        Map<LstrType, Map<String, Map<LstrField, String>>> rawLstreg = new TreeMap<LstrType, Map<String, Map<LstrField, String>>>();
        for (Entry<LstrType, Map<String, Map<LstrField, String>>> entry1 : result2.entrySet()) {
            LstrType key1 = entry1.getKey();
            TreeMap<String, Map<LstrField, String>> raw1 = new TreeMap<String, Map<LstrField, String>>();
            rawLstreg.put(key1, raw1);
            for (Entry<String, Map<LstrField, String>> entry2 : entry1.getValue().entrySet()) {
                String key2 = entry2.getKey();
                final Map<LstrField, String> value2 = entry2.getValue();
                TreeMap<LstrField, String> raw2 = new TreeMap<LstrField, String>();
                raw2.putAll(value2);
                raw1.put(key2, raw2);
            }
        }
        LSTREG_RAW = CldrUtility.protectCollection(rawLstreg);

        // add extras
        for (int i = 0; i < extras.length; ++i) {
            Map<String, Map<LstrField, String>> subtagData = CldrUtility.get(result2, LstrType.valueOf(extras[i][0]));
            if (subtagData == null) {
                result2.put(LstrType.valueOf(extras[i][0]), subtagData = new TreeMap<String, Map<LstrField, String>>());
            }
            Map<LstrField, String> labelData = new TreeMap<LstrField, String>();
            for (int j = 2; j < extras[i].length; j += 2) {
                labelData.put(LstrField.from(extras[i][j]), extras[i][j + 1]);
            }
            Map<LstrField, String> old = CldrUtility.get(subtagData, extras[i][1]);
            if (old != null) {
                if (!"Private use".equals(CldrUtility.get(old, LstrField.Description))) {
                    throw new IllegalArgumentException("REPLACING data for " + extras[i][1] + "\t" + old + "\twith"
                        + labelData);
                }
            }
            if (false) {
                System.out.println((old != null ? "REPLACING" + "\t" + old : "ADDING") +
                    " data for " + extras[i][1] + "\twith" + labelData);
            }
            subtagData.put(extras[i][1], labelData);
        }
        // build compatibility map
        Map<String, Map<String, Map<String, String>>> result = new LinkedHashMap<String, Map<String, Map<String, String>>>();
        for (Entry<LstrType, Map<String, Map<LstrField, String>>> entry : result2.entrySet()) {
            Map<String, Map<String, String>> copy2 = new LinkedHashMap<String, Map<String, String>>();
            result.put(entry.getKey().toString(), copy2);
            for (Entry<String, Map<LstrField, String>> entry2 : entry.getValue().entrySet()) {
                Map<String, String> copy3 = new LinkedHashMap<String, String>();
                copy2.put(entry2.getKey(), copy3);
                for (Entry<LstrField, String> entry3 : entry2.getValue().entrySet()) {
                    copy3.put(entry3.getKey().toString(), entry3.getValue());
                }
            }
        }
        LSTREG = CldrUtility.protectCollection(result);
        LSTREG_ENUM = CldrUtility.protectCollection(result2);
    }

    private static <K, K2, V> Map<K2, V> putSubtagData(K lastTag, Map<K, Map<K2, V>> subtagData, Map<K2, V> currentData) {
        Map<K2, V> oldData = subtagData.get(lastTag);
        if (oldData != null) {
            if (oldData.get("CLDR") != null) {
                System.out.println("overriding: " + lastTag + ", " + oldData);
            } else {
                throw new IllegalArgumentException("Duplicate tag: " + lastTag);
            }
        }
        return subtagData.put(lastTag, currentData);
    }

    static Counter<LstrType> languageCount = new Counter<LstrType>();

    public static Counter<LstrType> getLanguageCount() {
        return languageCount;
    }

    ZoneParser zoneParser = new ZoneParser();

    // static public final Set<String> MODERN_SCRIPTS = Collections
    // .unmodifiableSet(new TreeSet(
    // // "Bali " +
    // // "Bugi " +
    // // "Copt " +
    // // "Hano " +
    // // "Osma " +
    // // "Qaai " +
    // // "Sylo " +
    // // "Syrc " +
    // // "Tagb " +
    // // "Tglg " +
    // Arrays
    // .asList("Hans Hant Jpan Hrkt Kore Arab Armn Bali Beng Bopo Cans Cham Cher Cyrl Deva Ethi Geor Grek Gujr Guru Hani Hang Hebr Hira Knda Kana Kali Khmr Laoo Latn Lepc Limb Mlym Mong Mymr Talu Nkoo Olck Orya Saur Sinh Tale Taml Telu Thaa Thai Tibt Tfng Vaii Yiii"
    // .split("\\s+"))));

    // updated to http://www.unicode.org/reports/tr31/tr31-9.html#Specific_Character_Adjustments

    /**
     * @deprecated
     */
    public Map<String, List<ZoneLine>> getZone_rules() {
        return zoneParser.getZone_rules();
    }

    /**
     * @deprecated
     */
    public Map<String, List<String>> getZoneData() {
        return zoneParser.getZoneData();
    }

    /**
     * @deprecated
     */
    public Set<String> getCanonicalTimeZones() {
        return zoneParser.getZoneData().keySet();
    }

    /**
     * @deprecated
     */
    public Map<String, Set<String>> getCountryToZoneSet() {
        return zoneParser.getCountryToZoneSet();
    }

    /**
     * @deprecated
     */
    public List<String> getDeprecatedZoneIDs() {
        return zoneParser.getDeprecatedZoneIDs();
    }

    /**
     * @deprecated
     */
    public Comparator<String> getTZIDComparator() {
        return zoneParser.getTZIDComparator();
    }

    /**
     * @deprecated
     */
    public Map<String, Set<String>> getZoneLinkNew_OldSet() {
        return zoneParser.getZoneLinkNew_OldSet();
    }

    /**
     * @deprecated
     */
    public Map<String, String> getZoneLinkold_new() {
        return zoneParser.getZoneLinkold_new();
    }

    /**
     * @deprecated
     */
    public Map getZoneRuleID_rules() {
        return zoneParser.getZoneRuleID_rules();
    }

    /**
     * @deprecated
     */
    public Map<String, String> getZoneToCounty() {
        return zoneParser.getZoneToCounty();
    }

    /**
     * @deprecated
     */
    public String getZoneVersion() {
        return zoneParser.getVersion();
    }

    public static String fixLanguageTag(String languageSubtag) {
        if (languageSubtag.equals("mo")) { // fix special cases
            return "ro";
        } else if (languageSubtag.equals("no")) {
            return "nb";
        }
        return languageSubtag;
    }

    public boolean isModernLanguage(String languageCode) {
        if (getMoribundLanguages().contains(languageCode)) return false;
        Type type = Iso639Data.getType(languageCode);
        if (type == Type.Living) return true;
        if (languageCode.equals("eo")) return true; // exception for Esperanto
        // Scope scope = Iso639Data.getScope(languageCode);
        // if (scope == Scope.Collection) return false;
        return false;
    }

    public static boolean isScriptModern(String script) {
        ScriptMetadata.Info info = ScriptMetadata.getInfo(script);
        if (info == null) {
            if (false) throw new IllegalArgumentException("No script metadata for: " + script);
            return false;
        }
        IdUsage idUsage = info.idUsage;
        return idUsage != IdUsage.EXCLUSION && idUsage != IdUsage.UNKNOWN;
    }

    static final Pattern whitespace = PatternCache.get("\\s+");
    static Set<String> filteredCurrencies = null;

    public Set<String> getSurveyToolDisplayCodes(String type) {
        return getGoodAvailableCodes(type);
    }

    static UnicodeSet COUNTRY = new UnicodeSet("[a-zA-Z]").freeze();

    /**
     * Quick check for whether valid country. Not complete: should use Validity
     * @param territory
     * @return
     */
    public static boolean isCountry(String territory) {
        switch (territory) {
        case "ZZ":
        case "QO":
        case "EU":
        case "UN":
        case "EZ":
            return false;
        default:
            return territory.length() == 2 && COUNTRY.containsAll(territory);
        }
    }

    public boolean isLstregPrivateUse(String type, String code) {
        Map<String, String> lStregData = getLStreg().get(type).get(code);
        return lStregData.get("Description").equalsIgnoreCase("private use");
    }

    public boolean isLstregDeprecated(String type, String code) {
        Map<String, String> lStregData = getLStreg().get(type).get(code);
        return lStregData.get("Deprecated") != null;
    }
}