aboutsummaryrefslogtreecommitdiff
path: root/tools/cldr-code/src/main/java/org/unicode/cldr/util/XMLSource.java
blob: 672bf4efac07bd44d259d1b78dca8fa271c291a7 (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
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
/*
 ******************************************************************************
 * Copyright (C) 2005-2011, International Business Machines Corporation and   *
 * others. All Rights Reserved.                                               *
 ******************************************************************************
 */

package org.unicode.cldr.util;

import com.google.common.collect.Iterators;
import com.ibm.icu.impl.Utility;
import com.ibm.icu.util.Freezable;
import com.ibm.icu.util.Output;
import com.ibm.icu.util.VersionInfo;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.WeakHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.unicode.cldr.util.CLDRFile.DraftStatus;
import org.unicode.cldr.util.LocaleInheritanceInfo.Reason;
import org.unicode.cldr.util.XPathParts.Comments;
import org.xml.sax.Locator;

/**
 * Overall process is described in
 * http://cldr.unicode.org/development/development-process/design-proposals/resolution-of-cldr-files
 * Please update that document if major changes are made.
 */
public abstract class XMLSource implements Freezable<XMLSource>, Iterable<String> {
    public static final String CODE_FALLBACK_ID = "code-fallback";
    public static final String ROOT_ID = "root";
    public static final boolean USE_PARTS_IN_ALIAS = false;
    private static final String TRACE_INDENT = " "; // "\t"
    private static Map<String, String> allowDuplicates = new HashMap<>();

    private String localeID;
    private boolean nonInheriting;
    private TreeMap<String, String> aliasCache;
    private LinkedHashMap<String, List<String>> reverseAliasCache;
    protected boolean locked;
    transient String[] fixedPath = new String[1];

    /**
     * This class represents a source location of an XPath.
     *
     * @see com.ibm.icu.dev.test.TestFmwk.SourceLocation
     */
    public static class SourceLocation {
        static final String FILE_PREFIX = "file://";
        private String system;
        private int line;
        private int column;

        /**
         * Initialize from an XML Locator
         *
         * @param locator
         */
        public SourceLocation(Locator locator) {
            this(locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
        }

        public SourceLocation(String system, int line, int column) {
            this.system = system.intern();
            this.line = line;
            this.column = column;
        }

        public String getSystem() {
            // Trim prefix lazily.
            if (system.startsWith(FILE_PREFIX)) {
                return system.substring(FILE_PREFIX.length());
            } else {
                return system;
            }
        }

        public int getLine() {
            return line;
        }

        public int getColumn() {
            return column;
        }

        /**
         * The toString() format is suitable for printing to the command line and has the format
         * 'file:line:column: '
         */
        @Override
        public String toString() {
            return toString(null);
        }

        /**
         * The toString() format is suitable for printing to the command line and has the format
         * 'file:line:column: ' A good leading base path might be CLDRPaths.BASE_DIRECTORY
         *
         * @param basePath path to trim
         */
        public String toString(final String basePath) {
            return getSystem(basePath) + ":" + getLine() + ":" + getColumn() + ": ";
        }

        /**
         * Format location suitable for GitHub annotations, skips leading base bath A good leading
         * base path might be CLDRPaths.BASE_DIRECTORY
         *
         * @param basePath path to trim
         * @return
         */
        public String forGitHub(String basePath) {
            return "file=" + getSystem(basePath) + ",line=" + getLine() + ",col=" + getColumn();
        }

        /** Format location suitable for GitHub annotations */
        public String forGitHub() {
            return forGitHub(null);
        }

        /**
         * as with getSystem(), but skips the leading base path if identical. A good leading path
         * might be CLDRPaths.BASE_DIRECTORY
         *
         * @param basePath path to trim
         */
        public String getSystem(String basePath) {
            String path = getSystem();
            if (basePath != null && !basePath.isEmpty() && path.startsWith(basePath)) {
                path = path.substring(basePath.length());
                // Handle case where the path did NOT start with a slash
                if (path.startsWith("/") && !basePath.endsWith("/")) {
                    path = path.substring(1); // skip leading /
                }
            }
            return path;
        }
    }

    /*
     * For testing, make it possible to disable multiple caches:
     * getFullPathAtDPathCache, getSourceLocaleIDCache, aliasCache, reverseAliasCache
     */
    protected boolean cachingIsEnabled = true;

    public void disableCaching() {
        cachingIsEnabled = false;
    }

    public static class AliasLocation {
        public final String pathWhereFound;
        public final String localeWhereFound;

        public AliasLocation(String pathWhereFound, String localeWhereFound) {
            this.pathWhereFound = pathWhereFound;
            this.localeWhereFound = localeWhereFound;
        }
    }

    // Listeners are stored using weak references so that they can be garbage collected.
    private List<WeakReference<Listener>> listeners = new ArrayList<>();

    public String getLocaleID() {
        return localeID;
    }

    public void setLocaleID(String localeID) {
        if (locked) throw new UnsupportedOperationException("Attempt to modify locked object");
        this.localeID = localeID;
    }

    /**
     * Adds all the path,value pairs in tempMap. The paths must be Full Paths.
     *
     * @param tempMap
     * @param conflict_resolution
     */
    public void putAll(Map<String, String> tempMap, int conflict_resolution) {
        for (Iterator<String> it = tempMap.keySet().iterator(); it.hasNext(); ) {
            String path = it.next();
            if (conflict_resolution == CLDRFile.MERGE_KEEP_MINE && getValueAtPath(path) != null)
                continue;
            putValueAtPath(path, tempMap.get(path));
        }
    }

    /**
     * Adds all the path, value pairs in otherSource.
     *
     * @param otherSource
     * @param conflict_resolution
     */
    public void putAll(XMLSource otherSource, int conflict_resolution) {
        for (Iterator<String> it = otherSource.iterator(); it.hasNext(); ) {
            String path = it.next();
            final String oldValue = getValueAtDPath(path);
            if (conflict_resolution == CLDRFile.MERGE_KEEP_MINE && oldValue != null) {
                continue;
            }
            final String newValue = otherSource.getValueAtDPath(path);
            if (newValue.equals(oldValue)) {
                continue;
            }
            String fullPath = putValueAtPath(otherSource.getFullPathAtDPath(path), newValue);
            addSourceLocation(fullPath, otherSource.getSourceLocation(fullPath));
        }
    }

    /**
     * Removes all the paths in the collection. WARNING: must be distinguishedPaths
     *
     * @param xpaths
     */
    public void removeAll(Collection<String> xpaths) {
        for (Iterator<String> it = xpaths.iterator(); it.hasNext(); ) {
            removeValueAtDPath(it.next());
        }
    }

    /**
     * Tests whether the full path for this dpath is draft or now.
     *
     * @param path
     * @return
     */
    public boolean isDraft(String path) {
        String fullpath = getFullPath(path);
        if (path == null) {
            return false;
        }
        if (fullpath.indexOf("[@draft=") < 0) {
            return false;
        }
        XPathParts parts = XPathParts.getFrozenInstance(fullpath);
        return parts.containsAttribute("draft");
    }

    @Override
    public boolean isFrozen() {
        return locked;
    }

    /**
     * Adds the path,value pair. The path must be full path.
     *
     * @param xpath
     * @param value
     */
    public String putValueAtPath(String xpath, String value) {
        if (locked) {
            throw new UnsupportedOperationException("Attempt to modify locked object");
        }
        String distinguishingXPath = CLDRFile.getDistinguishingXPath(xpath, fixedPath);
        putValueAtDPath(distinguishingXPath, value);
        if (!fixedPath[0].equals(distinguishingXPath)) {
            clearCache();
            putFullPathAtDPath(distinguishingXPath, fixedPath[0]);
        }
        return distinguishingXPath;
    }

    /** Gets those paths that allow duplicates */
    public static Map<String, String> getPathsAllowingDuplicates() {
        return allowDuplicates;
    }

    /** A listener for XML source data. */
    public static interface Listener {
        /**
         * Called whenever the source being listened to has a data change.
         *
         * @param xpath The xpath that had its value changed.
         * @param source back-pointer to the source that changed
         */
        public void valueChanged(String xpath, XMLSource source);
    }

    /** Internal class. Immutable! */
    public static final class Alias {
        private final String newLocaleID;
        private final String oldPath;
        private final String newPath;
        private final boolean pathsEqual;
        static final Pattern aliasPattern =
                Pattern.compile(
                        "(?:\\[@source=\"([^\"]*)\"])?(?:\\[@path=\"([^\"]*)\"])?(?:\\[@draft=\"([^\"]*)\"])?");
        // constant, so no need to sync

        public static Alias make(String aliasPath) {
            int pos = aliasPath.indexOf("/alias");
            if (pos < 0) return null; // quickcheck
            String aliasParts = aliasPath.substring(pos + 6);
            String oldPath = aliasPath.substring(0, pos);
            String newPath = null;

            return new Alias(pos, oldPath, newPath, aliasParts);
        }

        private Alias(int pos, String oldPath, String newPath, String aliasParts) {
            Matcher matcher = aliasPattern.matcher(aliasParts);
            if (!matcher.matches()) {
                throw new IllegalArgumentException("bad alias pattern for " + aliasParts);
            }
            String newLocaleID = matcher.group(1);
            if (newLocaleID != null && newLocaleID.equals("locale")) {
                newLocaleID = null;
            }
            String relativePath2 = matcher.group(2);
            if (newPath == null) {
                newPath = oldPath;
            }
            if (relativePath2 != null) {
                newPath = addRelative(newPath, relativePath2);
            }

            boolean pathsEqual = oldPath.equals(newPath);

            if (pathsEqual && newLocaleID == null) {
                throw new IllegalArgumentException(
                        "Alias must have different path or different source. AliasPath: "
                                + aliasParts
                                + ", Alias: "
                                + newPath
                                + ", "
                                + newLocaleID);
            }

            this.newLocaleID = newLocaleID;
            this.oldPath = oldPath;
            this.newPath = newPath;
            this.pathsEqual = pathsEqual;
        }

        /**
         * Create a new path from an old path + relative portion. Basically, each ../ at the front
         * of the relative portion removes a trailing element+attributes from the old path.
         * WARNINGS: 1. It could fail if an attribute value contains '/'. This should not be the
         * case except in alias elements, but need to verify. 2. Also assumes that there are no
         * extra /'s in the relative or old path. 3. If we verified that the relative paths always
         * used " in place of ', we could also save a step.
         *
         * <p>Maybe we could clean up #2 and #3 when reading in a CLDRFile the first time?
         *
         * @param oldPath
         * @param relativePath
         * @return
         */
        static String addRelative(String oldPath, String relativePath) {
            if (relativePath.startsWith("//")) {
                return relativePath;
            }
            while (relativePath.startsWith("../")) {
                relativePath = relativePath.substring(3);
                // strip extra "/". Shouldn't occur, but just to be safe.
                while (relativePath.startsWith("/")) {
                    relativePath = relativePath.substring(1);
                }
                // strip last element
                oldPath = stripLastElement(oldPath);
            }
            return oldPath + "/" + relativePath.replace('\'', '"');
        }

        static final Pattern MIDDLE_OF_ATTRIBUTE_VALUE = PatternCache.get("[^\"]*\"\\]");

        public static String stripLastElement(String oldPath) {
            int oldPos = oldPath.lastIndexOf('/');
            // verify that we are not in the middle of an attribute value
            Matcher verifyElement = MIDDLE_OF_ATTRIBUTE_VALUE.matcher(oldPath.substring(oldPos));
            while (verifyElement.lookingAt()) {
                oldPos = oldPath.lastIndexOf('/', oldPos - 1);
                // will throw exception if we didn't find anything
                verifyElement.reset(oldPath.substring(oldPos));
            }
            oldPath = oldPath.substring(0, oldPos);
            return oldPath;
        }

        @Override
        public String toString() {
            return "newLocaleID: "
                    + newLocaleID
                    + ",\t"
                    + "oldPath: "
                    + oldPath
                    + ",\n\t"
                    + "newPath: "
                    + newPath;
        }

        /**
         * This function is called on the full path, when we know the distinguishing path matches
         * the oldPath. So we just want to modify the base of the path
         */
        public String changeNewToOld(String fullPath, String newPath, String oldPath) {
            // do common case quickly
            if (fullPath.startsWith(newPath)) {
                return oldPath + fullPath.substring(newPath.length());
            }

            // fullPath will be the same as newPath, except for some attributes at the end.
            // add those attributes to oldPath, starting from the end.
            XPathParts partsOld = XPathParts.getFrozenInstance(oldPath);
            XPathParts partsNew = XPathParts.getFrozenInstance(newPath);
            XPathParts partsFull = XPathParts.getFrozenInstance(fullPath);
            Map<String, String> attributesFull = partsFull.getAttributes(-1);
            Map<String, String> attributesNew = partsNew.getAttributes(-1);
            Map<String, String> attributesOld = partsOld.getAttributes(-1);
            for (Iterator<String> it = attributesFull.keySet().iterator(); it.hasNext(); ) {
                String attribute = it.next();
                if (attributesNew.containsKey(attribute)) continue;
                attributesOld.put(attribute, attributesFull.get(attribute));
            }
            String result = partsOld.toString();
            return result;
        }

        public String getOldPath() {
            return oldPath;
        }

        public String getNewLocaleID() {
            return newLocaleID;
        }

        public String getNewPath() {
            return newPath;
        }

        public String composeNewAndOldPath(String path) {
            return newPath + path.substring(oldPath.length());
        }

        public String composeOldAndNewPath(String path) {
            return oldPath + path.substring(newPath.length());
        }

        public boolean pathsEqual() {
            return pathsEqual;
        }

        public static boolean isAliasPath(String path) {
            return path.contains("/alias");
        }
    }

    /**
     * This method should be overridden.
     *
     * @return a mapping of paths to their aliases. Note that since root is the only locale to have
     *     aliases, all other locales will have no mappings.
     */
    protected synchronized TreeMap<String, String> getAliases() {
        if (!cachingIsEnabled) {
            /*
             * Always create and return a new "aliasMap" instead of this.aliasCache
             * Probably expensive!
             */
            return loadAliases();
        }

        /*
         * The cache assumes that aliases will never change over the lifetime of an XMLSource.
         */
        if (aliasCache == null) {
            aliasCache = loadAliases();
        }
        return aliasCache;
    }

    /**
     * Look for aliases and create mappings for them. Aliases are only ever found in root.
     *
     * <p>return aliasMap the new map
     */
    private TreeMap<String, String> loadAliases() {
        TreeMap<String, String> aliasMap = new TreeMap<>();
        for (String path : this) {
            if (!Alias.isAliasPath(path)) {
                continue;
            }
            String fullPath = getFullPathAtDPath(path);
            Alias temp = Alias.make(fullPath);
            if (temp == null) {
                continue;
            }
            aliasMap.put(temp.getOldPath(), temp.getNewPath());
        }
        return aliasMap;
    }

    /**
     * @return a reverse mapping of aliases
     */
    private LinkedHashMap<String, List<String>> getReverseAliases() {
        if (cachingIsEnabled && reverseAliasCache != null) {
            return reverseAliasCache;
        }
        // Aliases are only ever found in root.
        Map<String, String> aliases = getAliases();
        Map<String, List<String>> reverse = new HashMap<>();
        for (Map.Entry<String, String> entry : aliases.entrySet()) {
            List<String> list = reverse.get(entry.getValue());
            if (list == null) {
                list = new ArrayList<>();
                reverse.put(entry.getValue(), list);
            }
            list.add(entry.getKey());
        }
        // Sort map.
        LinkedHashMap<String, List<String>> reverseAliasMap =
                new LinkedHashMap<>(new TreeMap<>(reverse));
        if (cachingIsEnabled) {
            reverseAliasCache = reverseAliasMap;
        }
        return reverseAliasMap;
    }

    /**
     * Clear "any internal caches" (or only aliasCache?) for this XMLSource.
     *
     * <p>Called only by XMLSource.putValueAtPath and XMLSource.removeValueAtPath
     *
     * <p>Note: this method does not affect other caches: reverseAliasCache,
     * getFullPathAtDPathCache, getSourceLocaleIDCache
     */
    private void clearCache() {
        aliasCache = null;
    }

    /**
     * Return the localeID of the XMLSource where the path was found SUBCLASSING: must be overridden
     * in a resolving locale
     *
     * @param path the given path
     * @param status if not null, to have status.pathWhereFound filled in
     * @return the localeID
     */
    public String getSourceLocaleID(String path, CLDRFile.Status status) {
        if (status != null) {
            status.pathWhereFound = CLDRFile.getDistinguishingXPath(path, null);
        }
        return getLocaleID();
    }

    /**
     * Same as getSourceLocaleID, with unused parameter skipInheritanceMarker. This is defined so
     * that the version for ResolvingSource can be defined and called for a ResolvingSource that is
     * declared as an XMLSource.
     *
     * @param path the given path
     * @param status if not null, to have status.pathWhereFound filled in
     * @param skipInheritanceMarker ignored
     * @return the localeID
     */
    public String getSourceLocaleIdExtended(
            String path,
            CLDRFile.Status status,
            @SuppressWarnings("unused") boolean skipInheritanceMarker,
            List<LocaleInheritanceInfo> list) {
        final String locale = getSourceLocaleID(path, status);
        if (list != null) {
            if (hasValueAtDPath(path)) {
                list.add(
                        new LocaleInheritanceInfo(
                                locale, path, LocaleInheritanceInfo.Reason.value));
                // Since we’re not resolving, there’s no way to look for a Bailey value here.
            } else {
                list.add(
                        new LocaleInheritanceInfo(
                                locale, path, LocaleInheritanceInfo.Reason.none)); // not found
            }
        }
        return locale;
    }

    /**
     * Remove the value. SUBCLASSING: must be overridden in a resolving locale
     *
     * @param xpath
     */
    public void removeValueAtPath(String xpath) {
        if (locked) throw new UnsupportedOperationException("Attempt to modify locked object");
        clearCache();
        removeValueAtDPath(CLDRFile.getDistinguishingXPath(xpath, null));
    }

    /**
     * Get the value. SUBCLASSING: must be overridden in a resolving locale
     *
     * @param xpath
     * @return
     */
    public String getValueAtPath(String xpath) {
        return getValueAtDPath(CLDRFile.getDistinguishingXPath(xpath, null));
    }

    /**
     * Get the full path for a distinguishing path SUBCLASSING: must be overridden in a resolving
     * locale
     *
     * @param xpath
     * @return
     */
    public String getFullPath(String xpath) {
        return getFullPathAtDPath(CLDRFile.getDistinguishingXPath(xpath, null));
    }

    /**
     * Put the full path for this distinguishing path The caller will have processed the path, and
     * only call this with the distinguishing path SUBCLASSING: must be overridden
     */
    public abstract void putFullPathAtDPath(String distinguishingXPath, String fullxpath);

    /**
     * Put the distinguishing path, value. The caller will have processed the path, and only call
     * this with the distinguishing path SUBCLASSING: must be overridden
     */
    public abstract void putValueAtDPath(String distinguishingXPath, String value);

    /**
     * Remove the path, and the full path, and value corresponding to the path. The caller will have
     * processed the path, and only call this with the distinguishing path SUBCLASSING: must be
     * overridden
     */
    public abstract void removeValueAtDPath(String distinguishingXPath);

    /**
     * Get the value at the given distinguishing path The caller will have processed the path, and
     * only call this with the distinguishing path SUBCLASSING: must be overridden
     */
    public abstract String getValueAtDPath(String path);

    public boolean hasValueAtDPath(String path) {
        return (getValueAtDPath(path) != null);
    }

    /**
     * Get the Last-Change Date (if known) when the value was changed. SUBCLASSING: may be
     * overridden. defaults to NULL.
     *
     * @return last change date (if known), else null
     */
    public Date getChangeDateAtDPath(String path) {
        return null;
    }

    /**
     * Get the full path at the given distinguishing path The caller will have processed the path,
     * and only call this with the distinguishing path SUBCLASSING: must be overridden
     */
    public abstract String getFullPathAtDPath(String path);

    /**
     * Get the comments for the source. TODO: integrate the Comments class directly into this class
     * SUBCLASSING: must be overridden
     */
    public abstract Comments getXpathComments();

    /**
     * Set the comments for the source. TODO: integrate the Comments class directly into this class
     * SUBCLASSING: must be overridden
     */
    public abstract void setXpathComments(Comments comments);

    /**
     * @return an iterator over the distinguished paths
     */
    @Override
    public abstract Iterator<String> iterator();

    /**
     * @return an iterator over the distinguished paths that start with the prefix. SUBCLASSING:
     *     Normally overridden for efficiency
     */
    public Iterator<String> iterator(String prefix) {
        if (prefix == null || prefix.length() == 0) return iterator();
        return Iterators.filter(iterator(), s -> s.startsWith(prefix));
    }

    public Iterator<String> iterator(Matcher pathFilter) {
        if (pathFilter == null) return iterator();
        return Iterators.filter(iterator(), s -> pathFilter.reset(s).matches());
    }

    /**
     * @return returns whether resolving or not SUBCLASSING: Only changed for resolving subclasses
     */
    public boolean isResolving() {
        return false;
    }

    /**
     * Returns the unresolved version of this XMLSource. SUBCLASSING: Override in resolving sources.
     */
    public XMLSource getUnresolving() {
        return this;
    }

    /** SUBCLASSING: must be overridden */
    @Override
    public XMLSource cloneAsThawed() {
        try {
            XMLSource result = (XMLSource) super.clone();
            result.locked = false;
            return result;
        } catch (CloneNotSupportedException e) {
            throw new InternalError("should never happen");
        }
    }

    /** for debugging only */
    @Override
    public String toString() {
        StringBuffer result = new StringBuffer();
        for (Iterator<String> it = iterator(); it.hasNext(); ) {
            String path = it.next();
            String value = getValueAtDPath(path);
            String fullpath = getFullPathAtDPath(path);
            result.append(fullpath)
                    .append(" =\t ")
                    .append(value)
                    .append(CldrUtility.LINE_SEPARATOR);
        }
        return result.toString();
    }

    /** for debugging only */
    public String toString(String regex) {
        Matcher matcher = PatternCache.get(regex).matcher("");
        StringBuffer result = new StringBuffer();
        for (Iterator<String> it = iterator(matcher); it.hasNext(); ) {
            String path = it.next();
            String value = getValueAtDPath(path);
            String fullpath = getFullPathAtDPath(path);
            result.append(fullpath)
                    .append(" =\t ")
                    .append(value)
                    .append(CldrUtility.LINE_SEPARATOR);
        }
        return result.toString();
    }

    /**
     * @return returns whether supplemental or not
     */
    public boolean isNonInheriting() {
        return nonInheriting;
    }

    /**
     * @return sets whether supplemental. Normally only called internall.
     */
    public void setNonInheriting(boolean nonInheriting) {
        if (locked) throw new UnsupportedOperationException("Attempt to modify locked object");
        this.nonInheriting = nonInheriting;
    }

    /**
     * Internal class for doing resolution
     *
     * @author davis
     */
    public static class ResolvingSource extends XMLSource implements Listener {
        private XMLSource currentSource;
        private LinkedHashMap<String, XMLSource> sources;

        @Override
        public boolean isResolving() {
            return true;
        }

        @Override
        public XMLSource getUnresolving() {
            return sources.get(getLocaleID());
        }

        /*
         * If there is an alias, then inheritance gets tricky.
         * If there is a path //ldml/xyz/.../uvw/alias[@path=...][@source=...]
         * then the parent for //ldml/xyz/.../uvw/abc/.../def/
         * is source, and the path to search for is really: //ldml/xyz/.../uvw/path/abc/.../def/
         */
        public static final boolean TRACE_VALUE = CldrUtility.getProperty("TRACE_VALUE", false);

        // Map<String,String> getValueAtDPathCache = new HashMap();

        @Override
        public String getValueAtDPath(String xpath) {
            if (DEBUG_PATH != null && DEBUG_PATH.matcher(xpath).find()) {
                System.out.println("Getting value for Path: " + xpath);
            }
            if (TRACE_VALUE)
                System.out.println(
                        "\t*xpath: "
                                + xpath
                                + CldrUtility.LINE_SEPARATOR
                                + "\t*source: "
                                + currentSource.getClass().getName()
                                + CldrUtility.LINE_SEPARATOR
                                + "\t*locale: "
                                + currentSource.getLocaleID());
            String result = null;
            AliasLocation fullStatus =
                    getCachedFullStatus(xpath, true /* skipInheritanceMarker */, null);
            if (fullStatus != null) {
                if (TRACE_VALUE) {
                    System.out.println("\t*pathWhereFound: " + fullStatus.pathWhereFound);
                    System.out.println("\t*localeWhereFound: " + fullStatus.localeWhereFound);
                }
                result = getSource(fullStatus).getValueAtDPath(fullStatus.pathWhereFound);
            }
            if (TRACE_VALUE) System.out.println("\t*value: " + result);
            return result;
        }

        @Override
        public SourceLocation getSourceLocation(String xpath) {
            SourceLocation result = null;
            final String dPath = CLDRFile.getDistinguishingXPath(xpath, null);
            // getCachedFullStatus wants a dPath
            AliasLocation fullStatus =
                    getCachedFullStatus(dPath, true /* skipInheritanceMarker */, null);
            if (fullStatus != null) {
                result =
                        getSource(fullStatus)
                                .getSourceLocation(xpath); // getSourceLocation wants fullpath
            }
            return result;
        }

        public XMLSource getSource(AliasLocation fullStatus) {
            XMLSource source = sources.get(fullStatus.localeWhereFound);
            return source == null ? constructedItems : source;
        }

        Map<String, String> getFullPathAtDPathCache = new HashMap<>();

        @Override
        public String getFullPathAtDPath(String xpath) {
            String result = currentSource.getFullPathAtDPath(xpath);
            if (result != null) {
                return result;
            }
            // This is tricky. We need to find the alias location's path and full path.
            // then we need to the the non-distinguishing elements from them,
            // and add them into the requested path.
            AliasLocation fullStatus =
                    getCachedFullStatus(xpath, true /* skipInheritanceMarker */, null);
            if (fullStatus != null) {
                String fullPathWhereFound =
                        getSource(fullStatus).getFullPathAtDPath(fullStatus.pathWhereFound);
                if (fullPathWhereFound == null) {
                    result = null;
                } else if (fullPathWhereFound.equals(fullStatus.pathWhereFound)) {
                    result = xpath; // no difference
                } else {
                    result = getFullPath(xpath, fullStatus, fullPathWhereFound);
                }
            }
            return result;
        }

        @Override
        public Date getChangeDateAtDPath(String xpath) {
            Date result = currentSource.getChangeDateAtDPath(xpath);
            if (result != null) {
                return result;
            }
            AliasLocation fullStatus =
                    getCachedFullStatus(xpath, true /* skipInheritanceMarker */, null);
            if (fullStatus != null) {
                result = getSource(fullStatus).getChangeDateAtDPath(fullStatus.pathWhereFound);
            }
            return result;
        }

        private String getFullPath(
                String xpath, AliasLocation fullStatus, String fullPathWhereFound) {
            String result = null;
            if (this.cachingIsEnabled) {
                result = getFullPathAtDPathCache.get(xpath);
            }
            if (result == null) {
                // find the differences, and add them into xpath
                // we do this by walking through each element, adding the corresponding attribute
                // values.
                // we add attributes FROM THE END, in case the lengths are different!
                XPathParts xpathParts =
                        XPathParts.getFrozenInstance(xpath)
                                .cloneAsThawed(); // not frozen, for putAttributeValue
                XPathParts fullPathWhereFoundParts =
                        XPathParts.getFrozenInstance(fullPathWhereFound);
                XPathParts pathWhereFoundParts =
                        XPathParts.getFrozenInstance(fullStatus.pathWhereFound);
                int offset = xpathParts.size() - pathWhereFoundParts.size();

                for (int i = 0; i < pathWhereFoundParts.size(); ++i) {
                    Map<String, String> fullAttributes = fullPathWhereFoundParts.getAttributes(i);
                    Map<String, String> attributes = pathWhereFoundParts.getAttributes(i);
                    if (!attributes.equals(fullAttributes)) { // add differences
                        for (String key : fullAttributes.keySet()) {
                            if (!attributes.containsKey(key)) {
                                String value = fullAttributes.get(key);
                                xpathParts.putAttributeValue(i + offset, key, value);
                            }
                        }
                    }
                }
                result = xpathParts.toString();
                if (cachingIsEnabled) {
                    getFullPathAtDPathCache.put(xpath, result);
                }
            }
            return result;
        }

        /**
         * Return the "George Bailey" value, i.e., the value that would obtain if the value didn't
         * exist (in the first source). Often the Bailey value comes from the parent locale (such as
         * "fr") of a sublocale (such as "fr_CA"). Sometimes the Bailey value comes from an alias
         * which may be a different path in the same locale.
         *
         * @param xpath the given path
         * @param pathWhereFound if not null, to be filled in with the path where found
         * @param localeWhereFound if not null, to be filled in with the locale where found
         * @return the Bailey value
         */
        @Override
        public String getBaileyValue(
                String xpath, Output<String> pathWhereFound, Output<String> localeWhereFound) {
            AliasLocation fullStatus =
                    getPathLocation(
                            xpath, true /* skipFirst */, true /* skipInheritanceMarker */, null);
            if (localeWhereFound != null) {
                localeWhereFound.value = fullStatus.localeWhereFound;
            }
            if (pathWhereFound != null) {
                pathWhereFound.value = fullStatus.pathWhereFound;
            }
            return getSource(fullStatus).getValueAtDPath(fullStatus.pathWhereFound);
        }

        /**
         * Get the AliasLocation that would be returned by getPathLocation (with skipFirst false),
         * using a cache for efficiency
         *
         * @param xpath the given path
         * @param skipInheritanceMarker if true, skip sources in which value is INHERITANCE_MARKER
         * @return the AliasLocation
         */
        private AliasLocation getCachedFullStatus(
                String xpath, boolean skipInheritanceMarker, List<LocaleInheritanceInfo> list) {
            /*
             * Skip the cache in the special and relatively rare cases where skipInheritanceMarker is false.
             *
             * Note: we might consider using a cache also when skipInheritanceMarker is false.
             * Can't use the same cache for skipInheritanceMarker true and false.
             * Could use two caches, or add skipInheritanceMarker to the key (append 'T' or 'F' to xpath).
             * The situation is complicated by use of getSourceLocaleIDCache also in valueChanged.
             *
             * There is no caching problem with skipFirst, since that is always false here -- though
             * getBaileyValue could use a cache if there was one for skipFirst true.
             *
             * Also skip caching if the list is non-null, for tracing.
             */
            if (!skipInheritanceMarker || !cachingIsEnabled || (list != null)) {
                return getPathLocation(xpath, false /* skipFirst */, skipInheritanceMarker, list);
            }
            synchronized (getSourceLocaleIDCache) {
                AliasLocation fullStatus = getSourceLocaleIDCache.get(xpath);
                if (fullStatus == null) {
                    fullStatus =
                            getPathLocation(
                                    xpath, false /* skipFirst */, skipInheritanceMarker, null);
                    getSourceLocaleIDCache.put(xpath, fullStatus); // cache copy
                }
                return fullStatus;
            }
        }

        @Override
        public String getWinningPath(String xpath) {
            String result = currentSource.getWinningPath(xpath);
            if (result != null) return result;
            AliasLocation fullStatus =
                    getCachedFullStatus(xpath, true /* skipInheritanceMarker */, null);
            if (fullStatus != null) {
                result = getSource(fullStatus).getWinningPath(fullStatus.pathWhereFound);
            } else {
                result = xpath;
            }
            return result;
        }

        private transient Map<String, AliasLocation> getSourceLocaleIDCache = new WeakHashMap<>();

        /**
         * Get the source locale ID for the given path, for this ResolvingSource.
         *
         * @param distinguishedXPath the given path
         * @param status if not null, to have status.pathWhereFound filled in
         * @return the localeID, as a string
         */
        @Override
        public String getSourceLocaleID(String distinguishedXPath, CLDRFile.Status status) {
            return getSourceLocaleIdExtended(
                    distinguishedXPath, status, true /* skipInheritanceMarker */, null);
        }

        /**
         * Same as ResolvingSource.getSourceLocaleID, with additional parameter
         * skipInheritanceMarker, which is passed on to getCachedFullStatus and getPathLocation.
         *
         * @param distinguishedXPath the given path
         * @param status if not null, to have status.pathWhereFound filled in
         * @param skipInheritanceMarker if true, skip sources in which value is INHERITANCE_MARKER
         * @return the localeID, as a string
         */
        @Override
        public String getSourceLocaleIdExtended(
                String distinguishedXPath,
                CLDRFile.Status status,
                boolean skipInheritanceMarker,
                List<LocaleInheritanceInfo> list) {
            AliasLocation fullStatus =
                    getCachedFullStatus(distinguishedXPath, skipInheritanceMarker, list);
            if (status != null) {
                status.pathWhereFound = fullStatus.pathWhereFound;
            }
            return fullStatus.localeWhereFound;
        }

        static final Pattern COUNT_EQUALS = PatternCache.get("\\[@count=\"[^\"]*\"]");

        /**
         * Get the AliasLocation, containing path and locale where found, for the given path, for
         * this ResolvingSource.
         *
         * @param xpath the given path
         * @param skipFirst true if we're getting the Bailey value (caller is getBaileyValue), else
         *     false (caller is getCachedFullStatus)
         * @param skipInheritanceMarker if true, skip sources in which value is INHERITANCE_MARKER
         * @return the AliasLocation
         *     <p>skipInheritanceMarker must be true when the caller is getBaileyValue, so that the
         *     caller will not return INHERITANCE_MARKER as the George Bailey value. When the caller
         *     is getMissingStatus, we're not getting the Bailey value, and skipping
         *     INHERITANCE_MARKER here could take us up to "root", which getMissingStatus would
         *     misinterpret to mean the item should be listed under Missing in the Dashboard.
         *     Therefore skipInheritanceMarker needs to be false when getMissingStatus is the
         *     caller. Note that we get INHERITANCE_MARKER when there are votes for inheritance, but
         *     when there are no votes getValueAtDPath returns null so we don't get
         *     INHERITANCE_MARKER.
         *     <p>Situation for CheckCoverage.handleCheck may be similar to getMissingStatus, see
         *     ticket 11720.
         *     <p>For other callers, we stick with skipInheritanceMarker true for now, to retain the
         *     behavior before the skipInheritanceMarker parameter was added, but we should be alert
         *     for the possibility that skipInheritanceMarker should be false in some other cases
         *     <p>References: https://unicode.org/cldr/trac/ticket/11765
         *     https://unicode.org/cldr/trac/ticket/11720 https://unicode.org/cldr/trac/ticket/11103
         */
        private AliasLocation getPathLocation(
                String xpath,
                boolean skipFirst,
                boolean skipInheritanceMarker,
                List<LocaleInheritanceInfo> list) {

            //   When calculating the Bailey values, we track the final
            //   return value as firstValue. If non-null, this will become
            //   the function's ultimate return value.
            AliasLocation firstValue = null;
            for (XMLSource source : sources.values()) {
                if (skipFirst) {
                    skipFirst = false;
                    continue;
                }
                String value = source.getValueAtDPath(xpath);
                String localeID = source.getLocaleID();
                if (value != null) {
                    if (skipInheritanceMarker && CldrUtility.INHERITANCE_MARKER.equals(value)) {
                        if (list != null) {
                            list.add(
                                    new LocaleInheritanceInfo(
                                            localeID, xpath, Reason.inheritanceMarker));
                        }
                        // skip the inheritance marker and keep going
                    } else {
                        // We have a “hard” value.
                        if (list == null) {
                            return new AliasLocation(xpath, localeID);
                        }
                        if (CldrUtility.INHERITANCE_MARKER.equals(value)) {
                            list.add(
                                    new LocaleInheritanceInfo(
                                            localeID, xpath, Reason.inheritanceMarker));
                        } else {
                            list.add(new LocaleInheritanceInfo(localeID, xpath, Reason.value));
                        }
                        // Now, keep looping to add additional Bailey values.
                        // Note that we will typically exit the recursion (terminal state)
                        // with Reason.codeFallback or Reason.none
                        if (firstValue == null) {
                            // Save this, this will eventually be the function return.
                            firstValue = new AliasLocation(xpath, localeID);
                            // Everything else is only for Bailey.
                        } // else: we’re already looping.
                    }
                } else if (list != null) {
                    // No value, but we do have a list to update
                    // Note that the path wasn't found in this locale
                    // This also gives a trace of the locale inheritance
                    list.add(new LocaleInheritanceInfo(localeID, xpath, Reason.none));
                }
            }
            // Path not found, check if an alias exists
            final String rootAliasLocale = XMLSource.ROOT_ID; // Locale ID for aliases
            TreeMap<String, String> aliases = sources.get(rootAliasLocale).getAliases();
            String aliasedPath = aliases.get(xpath);

            if (aliasedPath == null) {
                // Check if there is an alias for a subset xpath.
                // If there are one or more matching aliases, lowerKey() will
                // return the alias with the longest matching prefix since the
                // hashmap is sorted according to xpath.

                //                // The following is a work in progress
                //                // We need to recurse, since we might have a chain of aliases
                //                while (true) {
                String possibleSubpath = aliases.lowerKey(xpath);
                if (possibleSubpath != null && xpath.startsWith(possibleSubpath)) {
                    aliasedPath =
                            aliases.get(possibleSubpath)
                                    + xpath.substring(possibleSubpath.length());
                    if (list != null) {
                        // It's an explicit alias, just at a parent element (subset xpath)
                        list.add(
                                new LocaleInheritanceInfo(
                                        rootAliasLocale, aliasedPath, Reason.alias));
                    }
                    //                        xpath = aliasedPath;
                    //                    } else {
                    //                        break;
                    //                    }
                }
            } else {
                if (list != null) {
                    // explicit, exact alias at this location
                    list.add(new LocaleInheritanceInfo(rootAliasLocale, aliasedPath, Reason.alias));
                }
            }

            // alts are special; they act like there is a root alias to the path without the alt.
            if (aliasedPath == null && xpath.contains("[@alt=")) {
                aliasedPath = XPathParts.getPathWithoutAlt(xpath);
                if (list != null) {
                    list.add(
                            new LocaleInheritanceInfo(
                                    null, aliasedPath, Reason.removedAttribute, "alt"));
                }
            }

            // counts are special; they act like there is a root alias to 'other'
            // and in the special case of currencies, other => null
            // //ldml/numbers/currencies/currency[@type="BRZ"]/displayName[@count="other"] =>
            // //ldml/numbers/currencies/currency[@type="BRZ"]/displayName
            if (aliasedPath == null && xpath.contains("[@count=")) {
                aliasedPath = COUNT_EQUALS.matcher(xpath).replaceAll("[@count=\"other\"]");
                if (aliasedPath.equals(xpath)) {
                    if (xpath.contains("/displayName")) {
                        aliasedPath = COUNT_EQUALS.matcher(xpath).replaceAll("");
                        if (aliasedPath.equals(xpath)) {
                            throw new RuntimeException("Internal error");
                        }
                    } else {
                        // the replacement failed, do not alias
                        aliasedPath = null;
                    }
                }
                if (list != null && aliasedPath != null) {
                    // two different paths above reach here
                    list.add(
                            new LocaleInheritanceInfo(
                                    null, aliasedPath, Reason.changedAttribute, "count"));
                }
            }

            if (aliasedPath != null) {
                // Call getCachedFullStatus recursively to avoid recalculating cached aliases.
                AliasLocation cachedFullStatus =
                        getCachedFullStatus(aliasedPath, skipInheritanceMarker, list);
                // We call the above first, to update the list (if needed)
                if (firstValue == null) {
                    // not looping due to Bailey - return the cached status.
                    return cachedFullStatus;
                } else {
                    // Bailey loop. Return the first value.
                    return firstValue;
                }
            }

            // Fallback location.
            if (list != null) {
                // Not using CODE_FALLBACK_ID as it is implicit in the reason
                list.add(new LocaleInheritanceInfo(null, xpath, Reason.codeFallback));
            }
            if (firstValue == null) {
                return new AliasLocation(xpath, CODE_FALLBACK_ID);
            } else {
                return firstValue;
            }
        }

        /**
         * We have to go through the source, add all the paths, then recurse to parents However,
         * aliases are tricky, so watch it.
         */
        static final boolean TRACE_FILL = CldrUtility.getProperty("TRACE_FILL", false);

        static final String DEBUG_PATH_STRING = CldrUtility.getProperty("DEBUG_PATH", null);
        static final Pattern DEBUG_PATH =
                DEBUG_PATH_STRING == null ? null : PatternCache.get(DEBUG_PATH_STRING);
        static final boolean SKIP_FALLBACKID = CldrUtility.getProperty("SKIP_FALLBACKID", false);

        static final int MAX_LEVEL = 40; /* Throw an error if it goes past this. */

        /**
         * Initialises the set of xpaths that a fully resolved XMLSource contains.
         * http://cldr.unicode.org/development/development-process/design-proposals/resolution-of-cldr-files.
         * Information about the aliased path and source locale ID of each xpath is not
         * precalculated here since it doesn't appear to improve overall performance.
         */
        private Set<String> fillKeys() {
            Set<String> paths = findNonAliasedPaths();
            // Find aliased paths and loop until no more aliases can be found.
            Set<String> newPaths = paths;
            int level = 0;
            boolean newPathsFound = false;
            do {
                // Debugging code to protect against an infinite loop.
                if (TRACE_FILL && DEBUG_PATH == null || level > MAX_LEVEL) {
                    System.out.println(
                            Utility.repeat(TRACE_INDENT, level)
                                    + "# paths waiting to be aliased: "
                                    + newPaths.size());
                    System.out.println(
                            Utility.repeat(TRACE_INDENT, level) + "# paths found: " + paths.size());
                }
                if (level > MAX_LEVEL) throw new IllegalArgumentException("Stack overflow");

                String[] sortedPaths = new String[newPaths.size()];
                newPaths.toArray(sortedPaths);
                Arrays.sort(sortedPaths);

                newPaths = getDirectAliases(sortedPaths);
                newPathsFound = paths.addAll(newPaths);
                level++;
            } while (newPathsFound);
            return paths;
        }

        /**
         * Creates the set of resolved paths for this ResolvingSource while ignoring aliasing.
         *
         * @return
         */
        private Set<String> findNonAliasedPaths() {
            HashSet<String> paths = new HashSet<>();

            // Get all XMLSources used during resolution.
            List<XMLSource> sourceList = new ArrayList<>(sources.values());
            if (!SKIP_FALLBACKID) {
                sourceList.add(constructedItems);
            }

            // Make a pass through, filling all the direct paths, excluding aliases, and collecting
            // others
            for (XMLSource curSource : sourceList) {
                for (String xpath : curSource) {
                    paths.add(xpath);
                }
            }
            return paths;
        }

        /**
         * Takes in a list of xpaths and returns a new set of paths that alias directly to those
         * existing xpaths.
         *
         * @param paths a sorted list of xpaths
         * @return the new set of paths
         */
        private Set<String> getDirectAliases(String[] paths) {
            HashSet<String> newPaths = new HashSet<>();
            // Keep track of the current path index: since it's sorted, we
            // never have to backtrack.
            int pathIndex = 0;
            LinkedHashMap<String, List<String>> reverseAliases = getReverseAliases();
            for (String subpath : reverseAliases.keySet()) {
                // Find the first path that matches the current alias.
                while (pathIndex < paths.length && paths[pathIndex].compareTo(subpath) < 0) {
                    pathIndex++;
                }

                // Alias all paths that match the current alias.
                String xpath;
                List<String> list = reverseAliases.get(subpath);
                int endIndex = pathIndex;
                int suffixStart = subpath.length();
                // Suffixes should always start with an element and not an
                // attribute to prevent invalid aliasing.
                while (endIndex < paths.length
                        && (xpath = paths[endIndex]).startsWith(subpath)
                        && xpath.charAt(suffixStart) == '/') {
                    String suffix = xpath.substring(suffixStart);
                    for (String reverseAlias : list) {
                        String reversePath = reverseAlias + suffix;
                        newPaths.add(reversePath);
                    }
                    endIndex++;
                }
                if (endIndex == paths.length) break;
            }
            return newPaths;
        }

        private LinkedHashMap<String, List<String>> getReverseAliases() {
            return sources.get("root").getReverseAliases();
        }

        private transient Set<String> cachedKeySet = null;

        /**
         * @return an iterator over all the xpaths in this XMLSource.
         */
        @Override
        public Iterator<String> iterator() {
            return getCachedKeySet().iterator();
        }

        private Set<String> getCachedKeySet() {
            if (cachedKeySet == null) {
                cachedKeySet = fillKeys();
                cachedKeySet = Collections.unmodifiableSet(cachedKeySet);
            }
            return cachedKeySet;
        }

        @Override
        public void putFullPathAtDPath(String distinguishingXPath, String fullxpath) {
            throw new UnsupportedOperationException("Resolved CLDRFiles are read-only");
        }

        @Override
        public void putValueAtDPath(String distinguishingXPath, String value) {
            throw new UnsupportedOperationException("Resolved CLDRFiles are read-only");
        }

        @Override
        public Comments getXpathComments() {
            return currentSource.getXpathComments();
        }

        @Override
        public void setXpathComments(Comments path) {
            throw new UnsupportedOperationException("Resolved CLDRFiles are read-only");
        }

        @Override
        public void removeValueAtDPath(String xpath) {
            throw new UnsupportedOperationException("Resolved CLDRFiles are  read-only");
        }

        @Override
        public XMLSource freeze() {
            return this; // No-op. ResolvingSource is already read-only.
        }

        @Override
        public void valueChanged(String xpath, XMLSource nonResolvingSource) {
            if (!cachingIsEnabled) {
                return;
            }
            synchronized (getSourceLocaleIDCache) {
                AliasLocation location = getSourceLocaleIDCache.remove(xpath);
                if (location == null) {
                    return;
                }
                // Paths aliasing to this path (directly or indirectly) may be affected,
                // so clear them as well.
                // There's probably a more elegant way to fix the paths than simply
                // throwing everything out.
                Set<String> dependentPaths = getDirectAliases(new String[] {xpath});
                if (dependentPaths.size() > 0) {
                    for (String path : dependentPaths) {
                        getSourceLocaleIDCache.remove(path);
                    }
                }
            }
        }

        /**
         * Creates a new ResolvingSource with the given locale resolution chain.
         *
         * @param sourceList the list of XMLSources to look in during resolution, ordered from the
         *     current locale up to root.
         */
        public ResolvingSource(List<XMLSource> sourceList) {
            // Sanity check for root.
            if (sourceList == null
                    || !sourceList.get(sourceList.size() - 1).getLocaleID().equals("root")) {
                throw new IllegalArgumentException("Last element should be root");
            }
            currentSource = sourceList.get(0); // Convenience variable
            sources = new LinkedHashMap<>();
            for (XMLSource source : sourceList) {
                sources.put(source.getLocaleID(), source);
            }

            // Add listeners to all locales except root, since we don't expect
            // root to change programatically.
            for (int i = 0, limit = sourceList.size() - 1; i < limit; i++) {
                sourceList.get(i).addListener(this);
            }
        }

        @Override
        public String getLocaleID() {
            return currentSource.getLocaleID();
        }

        private static final String[] keyDisplayNames = {
            "calendar", "cf", "collation", "currency", "hc", "lb", "ms", "numbers"
        };
        private static final String[][] typeDisplayNames = {
            {"account", "cf"},
            {"ahom", "numbers"},
            {"arab", "numbers"},
            {"arabext", "numbers"},
            {"armn", "numbers"},
            {"armnlow", "numbers"},
            {"bali", "numbers"},
            {"beng", "numbers"},
            {"big5han", "collation"},
            {"brah", "numbers"},
            {"buddhist", "calendar"},
            {"cakm", "numbers"},
            {"cham", "numbers"},
            {"chinese", "calendar"},
            {"compat", "collation"},
            {"coptic", "calendar"},
            {"cyrl", "numbers"},
            {"dangi", "calendar"},
            {"deva", "numbers"},
            {"diak", "numbers"},
            {"dictionary", "collation"},
            {"ducet", "collation"},
            {"emoji", "collation"},
            {"eor", "collation"},
            {"ethi", "numbers"},
            {"ethiopic", "calendar"},
            {"ethiopic-amete-alem", "calendar"},
            {"fullwide", "numbers"},
            {"gb2312han", "collation"},
            {"geor", "numbers"},
            {"gong", "numbers"},
            {"gonm", "numbers"},
            {"gregorian", "calendar"},
            {"grek", "numbers"},
            {"greklow", "numbers"},
            {"gujr", "numbers"},
            {"guru", "numbers"},
            {"h11", "hc"},
            {"h12", "hc"},
            {"h23", "hc"},
            {"h24", "hc"},
            {"hanidec", "numbers"},
            {"hans", "numbers"},
            {"hansfin", "numbers"},
            {"hant", "numbers"},
            {"hantfin", "numbers"},
            {"hebr", "numbers"},
            {"hebrew", "calendar"},
            {"hmng", "numbers"},
            {"hmnp", "numbers"},
            {"indian", "calendar"},
            {"islamic", "calendar"},
            {"islamic-civil", "calendar"},
            {"islamic-rgsa", "calendar"},
            {"islamic-tbla", "calendar"},
            {"islamic-umalqura", "calendar"},
            {"iso8601", "calendar"},
            {"japanese", "calendar"},
            {"java", "numbers"},
            {"jpan", "numbers"},
            {"jpanfin", "numbers"},
            {"kali", "numbers"},
            {"kawi", "numbers"},
            {"khmr", "numbers"},
            {"knda", "numbers"},
            {"lana", "numbers"},
            {"lanatham", "numbers"},
            {"laoo", "numbers"},
            {"latn", "numbers"},
            {"lepc", "numbers"},
            {"limb", "numbers"},
            {"loose", "lb"},
            {"mathbold", "numbers"},
            {"mathdbl", "numbers"},
            {"mathmono", "numbers"},
            {"mathsanb", "numbers"},
            {"mathsans", "numbers"},
            {"metric", "ms"},
            {"mlym", "numbers"},
            {"modi", "numbers"},
            {"mong", "numbers"},
            {"mroo", "numbers"},
            {"mtei", "numbers"},
            {"mymr", "numbers"},
            {"mymrshan", "numbers"},
            {"mymrtlng", "numbers"},
            {"nagm", "numbers"},
            {"nkoo", "numbers"},
            {"normal", "lb"},
            {"olck", "numbers"},
            {"orya", "numbers"},
            {"osma", "numbers"},
            {"persian", "calendar"},
            {"phonebook", "collation"},
            {"pinyin", "collation"},
            {"reformed", "collation"},
            {"roc", "calendar"},
            {"rohg", "numbers"},
            {"roman", "numbers"},
            {"romanlow", "numbers"},
            {"saur", "numbers"},
            {"search", "collation"},
            {"searchjl", "collation"},
            {"shrd", "numbers"},
            {"sind", "numbers"},
            {"sinh", "numbers"},
            {"sora", "numbers"},
            {"standard", "cf"},
            {"standard", "collation"},
            {"strict", "lb"},
            {"stroke", "collation"},
            {"sund", "numbers"},
            {"takr", "numbers"},
            {"talu", "numbers"},
            {"taml", "numbers"},
            {"tamldec", "numbers"},
            {"tnsa", "numbers"},
            {"telu", "numbers"},
            {"thai", "numbers"},
            {"tibt", "numbers"},
            {"tirh", "numbers"},
            {"traditional", "collation"},
            {"unihan", "collation"},
            {"uksystem", "ms"},
            {"ussystem", "ms"},
            {"vaii", "numbers"},
            {"wara", "numbers"},
            {"wcho", "numbers"},
            {"zhuyin", "collation"}
        };

        private static final boolean SKIP_SINGLEZONES = false;
        private static XMLSource constructedItems = new SimpleXMLSource(CODE_FALLBACK_ID);

        static {
            StandardCodes sc = StandardCodes.make();
            Map<String, Set<String>> countries_zoneSet = sc.getCountryToZoneSet();
            Map<String, String> zone_countries = sc.getZoneToCounty();

            for (int typeNo = 0; typeNo <= CLDRFile.TZ_START; ++typeNo) {
                String type = CLDRFile.getNameName(typeNo);
                String type2 =
                        (typeNo == CLDRFile.CURRENCY_SYMBOL)
                                ? CLDRFile.getNameName(CLDRFile.CURRENCY_NAME)
                                : (typeNo >= CLDRFile.TZ_START) ? "tzid" : type;
                Set<String> codes = sc.getSurveyToolDisplayCodes(type2);
                for (Iterator<String> codeIt = codes.iterator(); codeIt.hasNext(); ) {
                    String code = codeIt.next();
                    String value = code;
                    if (typeNo == CLDRFile.TZ_EXEMPLAR) { // skip single-zone countries
                        if (SKIP_SINGLEZONES) {
                            String country = zone_countries.get(code);
                            Set<String> s = countries_zoneSet.get(country);
                            if (s != null && s.size() == 1) continue;
                        }
                        value = TimezoneFormatter.getFallbackName(value);
                    } else if (typeNo == CLDRFile.LANGUAGE_NAME) {
                        if (ROOT_ID.equals(value)) {
                            continue;
                        }
                    }
                    addFallbackCode(typeNo, code, value);
                }
            }

            String[] extraCodes = {
                "ar_001", "de_AT", "de_CH", "en_AU", "en_CA", "en_GB", "en_US", "es_419", "es_ES",
                "es_MX", "fa_AF", "fr_CA", "fr_CH", "frc", "hi_Latn", "lou", "nds_NL", "nl_BE",
                "pt_BR", "pt_PT", "ro_MD", "sw_CD", "zh_Hans", "zh_Hant"
            };
            for (String extraCode : extraCodes) {
                addFallbackCode(CLDRFile.LANGUAGE_NAME, extraCode, extraCode);
            }

            addFallbackCode(CLDRFile.LANGUAGE_NAME, "en_GB", "en_GB", "short");
            addFallbackCode(CLDRFile.LANGUAGE_NAME, "en_US", "en_US", "short");
            addFallbackCode(CLDRFile.LANGUAGE_NAME, "az", "az", "short");

            addFallbackCode(CLDRFile.LANGUAGE_NAME, "ckb", "ckb", "menu");
            addFallbackCode(CLDRFile.LANGUAGE_NAME, "ckb", "ckb", "variant");
            addFallbackCode(CLDRFile.LANGUAGE_NAME, "hi_Latn", "hi_Latn", "variant");
            addFallbackCode(CLDRFile.LANGUAGE_NAME, "yue", "yue", "menu");
            addFallbackCode(CLDRFile.LANGUAGE_NAME, "zh", "zh", "menu");
            addFallbackCode(CLDRFile.LANGUAGE_NAME, "zh_Hans", "zh", "long");
            addFallbackCode(CLDRFile.LANGUAGE_NAME, "zh_Hant", "zh", "long");

            addFallbackCode(CLDRFile.SCRIPT_NAME, "Hans", "Hans", "stand-alone");
            addFallbackCode(CLDRFile.SCRIPT_NAME, "Hant", "Hant", "stand-alone");

            addFallbackCode(CLDRFile.TERRITORY_NAME, "GB", "GB", "short");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "HK", "HK", "short");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "MO", "MO", "short");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "PS", "PS", "short");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "US", "US", "short");

            addFallbackCode(
                    CLDRFile.TERRITORY_NAME, "CD", "CD", "variant"); // add other geopolitical items
            addFallbackCode(CLDRFile.TERRITORY_NAME, "CG", "CG", "variant");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "CI", "CI", "variant");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "CZ", "CZ", "variant");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "FK", "FK", "variant");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "TL", "TL", "variant");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "SZ", "SZ", "variant");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "IO", "IO", "biot");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "IO", "IO", "chagos");

            // new alternate name

            addFallbackCode(CLDRFile.TERRITORY_NAME, "NZ", "NZ", "variant");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "TR", "TR", "variant");

            addFallbackCode(CLDRFile.TERRITORY_NAME, "XA", "XA");
            addFallbackCode(CLDRFile.TERRITORY_NAME, "XB", "XB");

            addFallbackCode(
                    "//ldml/dates/calendars/calendar[@type=\"gregorian\"]/eras/eraAbbr/era[@type=\"0\"]",
                    "BCE",
                    "variant");
            addFallbackCode(
                    "//ldml/dates/calendars/calendar[@type=\"gregorian\"]/eras/eraAbbr/era[@type=\"1\"]",
                    "CE",
                    "variant");
            addFallbackCode(
                    "//ldml/dates/calendars/calendar[@type=\"gregorian\"]/eras/eraNames/era[@type=\"0\"]",
                    "BCE",
                    "variant");
            addFallbackCode(
                    "//ldml/dates/calendars/calendar[@type=\"gregorian\"]/eras/eraNames/era[@type=\"1\"]",
                    "CE",
                    "variant");
            addFallbackCode(
                    "//ldml/dates/calendars/calendar[@type=\"gregorian\"]/eras/eraNarrow/era[@type=\"0\"]",
                    "BCE",
                    "variant");
            addFallbackCode(
                    "//ldml/dates/calendars/calendar[@type=\"gregorian\"]/eras/eraNarrow/era[@type=\"1\"]",
                    "CE",
                    "variant");

            for (int i = 0; i < keyDisplayNames.length; ++i) {
                constructedItems.putValueAtPath(
                        "//ldml/localeDisplayNames/keys/key"
                                + "[@type=\""
                                + keyDisplayNames[i]
                                + "\"]",
                        keyDisplayNames[i]);
            }
            for (int i = 0; i < typeDisplayNames.length; ++i) {
                constructedItems.putValueAtPath(
                        "//ldml/localeDisplayNames/types/type"
                                + "[@key=\""
                                + typeDisplayNames[i][1]
                                + "\"]"
                                + "[@type=\""
                                + typeDisplayNames[i][0]
                                + "\"]",
                        typeDisplayNames[i][0]);
            }
            constructedItems.freeze();
            allowDuplicates = Collections.unmodifiableMap(allowDuplicates);
        }

        private static void addFallbackCode(int typeNo, String code, String value) {
            addFallbackCode(typeNo, code, value, null);
        }

        private static void addFallbackCode(int typeNo, String code, String value, String alt) {
            String fullpath = CLDRFile.getKey(typeNo, code);
            String distinguishingPath = addFallbackCodeToConstructedItems(fullpath, value, alt);
            if (typeNo == CLDRFile.LANGUAGE_NAME
                    || typeNo == CLDRFile.SCRIPT_NAME
                    || typeNo == CLDRFile.TERRITORY_NAME) {
                allowDuplicates.put(distinguishingPath, code);
            }
        }

        private static void addFallbackCode(
                String fullpath, String value, String alt) { // assumes no allowDuplicates for this
            addFallbackCodeToConstructedItems(fullpath, value, alt); // ignore unneeded return value
        }

        private static String addFallbackCodeToConstructedItems(
                String fullpath, String value, String alt) {
            if (alt != null) {
                // Insert the @alt= string after the last occurrence of "]"
                StringBuffer fullpathBuf = new StringBuffer(fullpath);
                fullpath =
                        fullpathBuf
                                .insert(fullpathBuf.lastIndexOf("]") + 1, "[@alt=\"" + alt + "\"]")
                                .toString();
            }
            return constructedItems.putValueAtPath(fullpath, value);
        }

        @Override
        public boolean isHere(String path) {
            return currentSource.isHere(path); // only test one level
        }

        @Override
        public void getPathsWithValue(String valueToMatch, String pathPrefix, Set<String> result) {
            // NOTE: No caching is currently performed here because the unresolved
            // locales already cache their value-path mappings, and it's not
            // clear yet how much further caching would speed this up.

            // Add all non-aliased paths with the specified value.
            List<XMLSource> children = new ArrayList<>();
            Set<String> filteredPaths = new HashSet<>();
            for (XMLSource source : sources.values()) {
                Set<String> pathsWithValue = new HashSet<>();
                source.getPathsWithValue(valueToMatch, pathPrefix, pathsWithValue);
                // Don't add a path with the value if it is overridden by a child locale.
                for (String pathWithValue : pathsWithValue) {
                    if (!sourcesHavePath(pathWithValue, children)) {
                        filteredPaths.add(pathWithValue);
                    }
                }
                children.add(source);
            }

            // Find all paths that alias to the specified value, then filter by
            // path prefix.
            Set<String> aliases = new HashSet<>();
            Set<String> oldAliases = new HashSet<>(filteredPaths);
            Set<String> newAliases;
            do {
                String[] sortedPaths = new String[oldAliases.size()];
                oldAliases.toArray(sortedPaths);
                Arrays.sort(sortedPaths);
                newAliases = getDirectAliases(sortedPaths);
                oldAliases = newAliases;
                aliases.addAll(newAliases);
            } while (newAliases.size() > 0);

            // get the aliases, but only the ones that have values that match
            String norm = null;
            for (String alias : aliases) {
                if (alias.startsWith(pathPrefix)) {
                    if (norm == null && valueToMatch != null) {
                        norm = SimpleXMLSource.normalize(valueToMatch);
                    }
                    String value = getValueAtDPath(alias);
                    if (value != null && SimpleXMLSource.normalize(value).equals(norm)) {
                        filteredPaths.add(alias);
                    }
                }
            }

            result.addAll(filteredPaths);
        }

        private boolean sourcesHavePath(String xpath, List<XMLSource> sources) {
            for (XMLSource source : sources) {
                if (source.hasValueAtDPath(xpath)) return true;
            }
            return false;
        }

        @Override
        public VersionInfo getDtdVersionInfo() {
            return currentSource.getDtdVersionInfo();
        }
    }

    /**
     * See CLDRFile isWinningPath for documentation
     *
     * @param path
     * @return
     */
    public boolean isWinningPath(String path) {
        return getWinningPath(path).equals(path);
    }

    /**
     * See CLDRFile getWinningPath for documentation. Default implementation is that it removes
     * draft and [@alt="...proposed..." if possible
     *
     * @param path
     * @return
     */
    public String getWinningPath(String path) {
        String newPath = CLDRFile.getNondraftNonaltXPath(path);
        if (!newPath.equals(path)) {
            String value = getValueAtPath(newPath); // ensure that it still works
            if (value != null) {
                return newPath;
            }
        }
        return path;
    }

    /** Adds a listener to this XML source. */
    public void addListener(Listener listener) {
        listeners.add(new WeakReference<>(listener));
    }

    /**
     * Notifies all listeners that the winning value for the given path has changed.
     *
     * @param xpath the xpath where the change occurred.
     */
    public void notifyListeners(String xpath) {
        int i = 0;
        while (i < listeners.size()) {
            Listener listener = listeners.get(i).get();
            if (listener == null) { // listener has been garbage-collected.
                listeners.remove(i);
            } else {
                listener.valueChanged(xpath, this);
                i++;
            }
        }
    }

    /**
     * return true if the path in this file (without resolution). Default implementation is to just
     * see if the path has a value. The resolved source must just test the top level.
     *
     * @param path
     * @return
     */
    public boolean isHere(String path) {
        return getValueAtPath(path) != null;
    }

    /**
     * Find all the distinguished paths having values matching valueToMatch, and add them to result.
     *
     * @param valueToMatch
     * @param pathPrefix
     * @param result
     */
    public abstract void getPathsWithValue(
            String valueToMatch, String pathPrefix, Set<String> result);

    public VersionInfo getDtdVersionInfo() {
        return null;
    }

    @SuppressWarnings("unused")
    public String getBaileyValue(
            String xpath, Output<String> pathWhereFound, Output<String> localeWhereFound) {
        return null; // only a resolving xmlsource will return a value
    }

    // HACK, should be field on XMLSource
    public DtdType getDtdType() {
        final Iterator<String> it = iterator();
        if (it.hasNext()) {
            String path = it.next();
            return DtdType.fromPath(path);
        }
        return null;
    }

    /** XMLNormalizingDtdType is set in XMLNormalizingHandler loading XML process */
    private DtdType XMLNormalizingDtdType;

    private static final boolean LOG_PROGRESS = false;

    public DtdType getXMLNormalizingDtdType() {
        return this.XMLNormalizingDtdType;
    }

    public void setXMLNormalizingDtdType(DtdType dtdType) {
        this.XMLNormalizingDtdType = dtdType;
    }

    /**
     * Sets the initial comment, replacing everything that was there Use in XMLNormalizingHandler
     * only
     */
    public XMLSource setInitialComment(String comment) {
        if (locked) throw new UnsupportedOperationException("Attempt to modify locked object");
        Log.logln(LOG_PROGRESS, "SET initial Comment: \t" + comment);
        this.getXpathComments().setInitialComment(comment);
        return this;
    }

    /** Use in XMLNormalizingHandler only */
    public XMLSource addComment(String xpath, String comment, Comments.CommentType type) {
        if (locked) throw new UnsupportedOperationException("Attempt to modify locked object");
        Log.logln(LOG_PROGRESS, "ADDING Comment: \t" + type + "\t" + xpath + " \t" + comment);
        if (xpath == null || xpath.length() == 0) {
            this.getXpathComments()
                    .setFinalComment(
                            CldrUtility.joinWithSeparation(
                                    this.getXpathComments().getFinalComment(),
                                    XPathParts.NEWLINE,
                                    comment));
        } else {
            xpath = CLDRFile.getDistinguishingXPath(xpath, null);
            this.getXpathComments().addComment(type, xpath, comment);
        }
        return this;
    }

    /** Use in XMLNormalizingHandler only */
    public String getFullXPath(String xpath) {
        if (xpath == null) {
            throw new NullPointerException("Null distinguishing xpath");
        }
        String result = this.getFullPath(xpath);
        return result != null
                ? result
                : xpath; // we can't add any non-distinguishing values if there is nothing there.
    }

    /** Add a new element to a XMLSource Use in XMLNormalizingHandler only */
    public XMLSource add(String currentFullXPath, String value) {
        if (locked) throw new UnsupportedOperationException("Attempt to modify locked object");
        Log.logln(
                LOG_PROGRESS,
                "ADDING: \t" + currentFullXPath + " \t" + value + "\t" + currentFullXPath);
        try {
            this.putValueAtPath(currentFullXPath, value);
        } catch (RuntimeException e) {
            throw new IllegalArgumentException(
                    "failed adding " + currentFullXPath + ",\t" + value, e);
        }
        return this;
    }

    /**
     * Get frozen normalized XMLSource
     *
     * @param localeId
     * @param dirs
     * @param minimalDraftStatus
     * @return XMLSource
     */
    public static XMLSource getFrozenInstance(
            String localeId, List<File> dirs, DraftStatus minimalDraftStatus) {
        return XMLNormalizingLoader.getFrozenInstance(localeId, dirs, minimalDraftStatus);
    }

    /**
     * Add a SourceLocation to this full XPath. Base implementation does nothing.
     *
     * @param currentFullXPath
     * @param location
     * @return
     */
    public XMLSource addSourceLocation(String currentFullXPath, SourceLocation location) {
        return this;
    }

    /**
     * Get the SourceLocation for a specific XPath. Base implementation always returns null.
     *
     * @param fullXPath
     * @return
     */
    public SourceLocation getSourceLocation(String fullXPath) {
        return null;
    }
}