aboutsummaryrefslogtreecommitdiff
path: root/src/com/sun/org/apache/xml/internal/resolver/Catalog.java
blob: 9aff9f093459b96ecc01122dd256c7700f82ac82 (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
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
/*
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
 */
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// Catalog.java - Represents OASIS Open Catalog files.
package com.sun.org.apache.xml.internal.resolver;

import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
import com.sun.org.apache.xml.internal.resolver.helpers.FileURL;
import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
import com.sun.org.apache.xml.internal.resolver.readers.CatalogReader;
import com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader;
import com.sun.org.apache.xml.internal.resolver.readers.SAXCatalogReader;
import com.sun.org.apache.xml.internal.resolver.readers.TR9401CatalogReader;

import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.xml.parsers.SAXParserFactory;

/**
 * Represents OASIS Open Catalog files.
 *
 * <p>This class implements the semantics of OASIS Open Catalog files
 * (defined by
 * <a href="http://www.oasis-open.org/html/a401.htm">OASIS Technical
 * Resolution 9401:1997 (Amendment 2 to TR 9401)</a>).</p>
 *
 * <p>The primary purpose of the Catalog is to associate resources in the
 * document with local system identifiers. Some entities
 * (document types, XML entities, and notations) have names and all of them
 * can have either public or system identifiers or both. (In XML, only a
 * notation can have a public identifier without a system identifier, but
 * the methods implemented in this class obey the Catalog semantics
 * from the SGML
 * days when system identifiers were optional.)</p>
 *
 * <p>The system identifiers returned by the resolution methods in this
 * class are valid, i.e. usable by, and in fact constructed by, the
 * <tt>java.net.URL</tt> class. Unfortunately, this class seems to behave in
 * somewhat non-standard ways and the system identifiers returned may
 * not be directly usable in a browser or filesystem context.
 *
 * <p>This class recognizes all of the Catalog entries defined in
 * TR9401:1997:</p>
 *
 * <ul>
 * <li><b>BASE</b>
 * changes the base URI for resolving relative system identifiers. The
 * initial base URI is the URI of the location of the catalog (which is,
 * in turn, relative to the location of the current working directory
 * at startup, as returned by the <tt>user.dir</tt> system property).</li>
 * <li><b>CATALOG</b>
 * processes other catalog files. An included catalog occurs logically
 * at the end of the including catalog.</li>
 * <li><b>DELEGATE_PUBLIC</b>
 * specifies alternate catalogs for some public identifiers. The delegated
 * catalogs are not loaded until they are needed, but they are cached
 * once loaded.</li>
 * <li><b>DELEGATE_SYSTEM</b>
 * specifies alternate catalogs for some system identifiers. The delegated
 * catalogs are not loaded until they are needed, but they are cached
 * once loaded.</li>
 * <li><b>DELEGATE_URI</b>
 * specifies alternate catalogs for some URIs. The delegated
 * catalogs are not loaded until they are needed, but they are cached
 * once loaded.</li>
 * <li><b>REWRITE_SYSTEM</b>
 * specifies alternate prefix for a system identifier.</li>
 * <li><b>REWRITE_URI</b>
 * specifies alternate prefix for a URI.</li>
 * <li><b>SYSTEM_SUFFIX</b>
 * maps any system identifier that ends with a particular suffix to another
 * system identifier.</li>
 * <li><b>URI_SUFFIX</b>
 * maps any URI that ends with a particular suffix to another URI.</li>
 * <li><b>DOCTYPE</b>
 * associates the names of root elements with URIs. (In other words, an XML
 * processor might infer the doctype of an XML document that does not include
 * a doctype declaration by looking for the DOCTYPE entry in the
 * catalog which matches the name of the root element of the document.)</li>
 * <li><b>DOCUMENT</b>
 * provides a default document.</li>
 * <li><b>DTDDECL</b>
 * recognized and silently ignored. Not relevant for XML.</li>
 * <li><b>ENTITY</b>
 * associates entity names with URIs.</li>
 * <li><b>LINKTYPE</b>
 * recognized and silently ignored. Not relevant for XML.</li>
 * <li><b>NOTATION</b>
 * associates notation names with URIs.</li>
 * <li><b>OVERRIDE</b>
 * changes the override behavior. Initial behavior is set by the
 * system property <tt>xml.catalog.override</tt>. The default initial
 * behavior is 'YES', that is, entries in the catalog override
 * system identifiers specified in the document.</li>
 * <li><b>PUBLIC</b>
 * maps a public identifier to a system identifier.</li>
 * <li><b>SGMLDECL</b>
 * recognized and silently ignored. Not relevant for XML.</li>
 * <li><b>SYSTEM</b>
 * maps a system identifier to another system identifier.</li>
 * <li><b>URI</b>
 * maps a URI to another URI.</li>
 * </ul>
 *
 * <p>Note that BASE entries are treated as described by RFC2396. In
 * particular, this has the counter-intuitive property that after a BASE
 * entry identifing "http://example.com/a/b/c" as the base URI,
 * the relative URI "foo" is resolved to the absolute URI
 * "http://example.com/a/b/foo". You must provide the trailing slash if
 * you do not want the final component of the path to be discarded as a
 * filename would in a URI for a resource: "http://example.com/a/b/c/".
 * </p>
 *
 * <p>Note that subordinate catalogs (all catalogs except the first,
 * including CATALOG and DELEGATE* catalogs) are only loaded if and when
 * they are required.</p>
 *
 * <p>This class relies on classes which implement the CatalogReader
 * interface to actually load catalog files. This allows the catalog
 * semantics to be implemented for TR9401 text-based catalogs, XML
 * catalogs, or any number of other storage formats.</p>
 *
 * <p>Additional catalogs may also be loaded with the
 * {@link #parseCatalog} method.</p>
 * </dd>
 * </dl>
 *
 * <p><b>Change Log:</b></p>
 * <dl>
 * <dt>2.0</dt>
 * <dd><p>Rewrite to use CatalogReaders.</p></dd>
 * <dt>1.1</dt>
 * <dd><p>Allow quoted components in <tt>xml.catalog.files</tt>
 * so that URLs containing colons can be used on Unix.
 * The string passed to <tt>xml.catalog.files</tt> can now have the form:</p>
 * <pre>
 * unquoted-path-with-no-sep-chars:"double-quoted path with or without sep chars":'single-quoted path with or without sep chars'
 * </pre>
 * <p>(Where ":" is the separater character in this example.)</p>
 * <p>If an unquoted path contains an embedded double or single quote
 * character, no special processig is performed on that character. No
 * path can contain separater characters, double, and single quotes
 * simultaneously.</p>
 * <p>Fix bug in calculation of BASE entries: if
 * a catalog contains multiple BASE entries, each is relative to the preceding
 * base, not the default base URI of the catalog.</p>
 * </dd>
 * <dt>1.0.1</dt>
 * <dd><p>Fixed a bug in the calculation of the list of subordinate catalogs.
 * This bug caused an infinite loop where parsing would alternately process
 * two catalogs indefinitely.</p>
 * </dd>
 * </dl>
 *
 * @see CatalogReader
 * @see CatalogEntry
 *
 * @author Norman Walsh
 * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
 *
 * @version 1.0
 *
 * <p>Derived from public domain code originally published by Arbortext,
 * Inc.</p>
 */
public class Catalog {
  /** The BASE Catalog Entry type. */
  public static final int BASE     = CatalogEntry.addEntryType("BASE", 1);

  /** The CATALOG Catalog Entry type. */
  public static final int CATALOG  = CatalogEntry.addEntryType("CATALOG", 1);

  /** The DOCUMENT Catalog Entry type. */
  public static final int DOCUMENT = CatalogEntry.addEntryType("DOCUMENT", 1);

  /** The OVERRIDE Catalog Entry type. */
  public static final int OVERRIDE = CatalogEntry.addEntryType("OVERRIDE", 1);

  /** The SGMLDECL Catalog Entry type. */
  public static final int SGMLDECL = CatalogEntry.addEntryType("SGMLDECL", 1);

  /** The DELEGATE_PUBLIC Catalog Entry type. */
  public static final int DELEGATE_PUBLIC = CatalogEntry.addEntryType("DELEGATE_PUBLIC", 2);

  /** The DELEGATE_SYSTEM Catalog Entry type. */
  public static final int DELEGATE_SYSTEM = CatalogEntry.addEntryType("DELEGATE_SYSTEM", 2);

  /** The DELEGATE_URI Catalog Entry type. */
  public static final int DELEGATE_URI = CatalogEntry.addEntryType("DELEGATE_URI", 2);

  /** The DOCTYPE Catalog Entry type. */
  public static final int DOCTYPE  = CatalogEntry.addEntryType("DOCTYPE", 2);

  /** The DTDDECL Catalog Entry type. */
  public static final int DTDDECL  = CatalogEntry.addEntryType("DTDDECL", 2);

  /** The ENTITY Catalog Entry type. */
  public static final int ENTITY   = CatalogEntry.addEntryType("ENTITY", 2);

  /** The LINKTYPE Catalog Entry type. */
  public static final int LINKTYPE = CatalogEntry.addEntryType("LINKTYPE", 2);

  /** The NOTATION Catalog Entry type. */
  public static final int NOTATION = CatalogEntry.addEntryType("NOTATION", 2);

  /** The PUBLIC Catalog Entry type. */
  public static final int PUBLIC   = CatalogEntry.addEntryType("PUBLIC", 2);

  /** The SYSTEM Catalog Entry type. */
  public static final int SYSTEM   = CatalogEntry.addEntryType("SYSTEM", 2);

  /** The URI Catalog Entry type. */
  public static final int URI      = CatalogEntry.addEntryType("URI", 2);

  /** The REWRITE_SYSTEM Catalog Entry type. */
  public static final int REWRITE_SYSTEM = CatalogEntry.addEntryType("REWRITE_SYSTEM", 2);

  /** The REWRITE_URI Catalog Entry type. */
  public static final int REWRITE_URI = CatalogEntry.addEntryType("REWRITE_URI", 2);
  /** The SYSTEM_SUFFIX Catalog Entry type. */
  public static final int SYSTEM_SUFFIX = CatalogEntry.addEntryType("SYSTEM_SUFFIX", 2);
  /** The URI_SUFFIX Catalog Entry type. */
  public static final int URI_SUFFIX = CatalogEntry.addEntryType("URI_SUFFIX", 2);

  /**
   * The base URI for relative system identifiers in the catalog.
   * This may be changed by BASE entries in the catalog.
   */
  protected URL base;

  /** The base URI of the Catalog file currently being parsed. */
  protected URL catalogCwd;

  /** The catalog entries currently known to the system. */
  protected Vector catalogEntries = new Vector();

  /** The default initial override setting. */
  protected boolean default_override = true;

  /** The catalog manager in use for this instance. */
  protected CatalogManager catalogManager = CatalogManager.getStaticManager();

  /**
   * A vector of catalog files to be loaded.
   *
   * <p>This list is initially established by
   * <code>loadSystemCatalogs</code> when
   * it parses the system catalog list, but CATALOG entries may
   * contribute to it during the course of parsing.</p>
   *
   * @see #loadSystemCatalogs
   * @see #localCatalogFiles
   */
  protected Vector catalogFiles = new Vector();

  /**
   * A vector of catalog files constructed during processing of
   * CATALOG entries in the current catalog.
   *
   * <p>This two-level system is actually necessary to correctly implement
   * the semantics of the CATALOG entry. If one catalog file includes
   * another with a CATALOG entry, the included catalog logically
   * occurs <i>at the end</i> of the including catalog, and after any
   * preceding CATALOG entries. In other words, the CATALOG entry
   * cannot insert anything into the middle of a catalog file.</p>
   *
   * <p>When processing reaches the end of each catalog files, any
   * elements on this vector are added to the front of the
   * <code>catalogFiles</code> vector.</p>
   *
   * @see #catalogFiles
   */
  protected Vector localCatalogFiles = new Vector();

  /**
   * A vector of Catalogs.
   *
   * <p>The semantics of Catalog resolution are such that each
   * catalog is effectively a list of Catalogs (in other words,
   * a recursive list of Catalog instances).</p>
   *
   * <p>Catalogs that are processed as the result of CATALOG or
   * DELEGATE* entries are subordinate to the catalog that contained
   * them, but they may in turn have subordinate catalogs.</p>
   *
   * <p>Catalogs are only loaded when they are needed, so this vector
   * initially contains a list of Catalog filenames (URLs). If, during
   * processing, one of these catalogs has to be loaded, the resulting
   * Catalog object is placed in the vector, effectively caching it
   * for the next query.</p>
   */
  protected Vector catalogs = new Vector();

  /**
   * A vector of DELEGATE* Catalog entries constructed during
   * processing of the Catalog.
   *
   * <p>This two-level system has two purposes; first, it allows
   * us to sort the DELEGATE* entries by the length of the partial
   * public identifier so that a linear search encounters them in
   * the correct order and second, it puts them all at the end of
   * the Catalog.</p>
   *
   * <p>When processing reaches the end of each catalog file, any
   * elements on this vector are added to the end of the
   * <code>catalogEntries</code> vector. This assures that matching
   * PUBLIC keywords are encountered before DELEGATE* entries.</p>
   */
  protected Vector localDelegate = new Vector();

  /**
   * A hash of CatalogReaders.
   *
   * <p>This hash maps MIME types to elements in the readerArr
   * vector. This allows the Catalog to quickly locate the reader
   * for a particular MIME type.</p>
   */
  protected Map<String, Integer> readerMap = new HashMap<>();

  /**
   * A vector of CatalogReaders.
   *
   * <p>This vector contains all of the readers in the order that they
   * were added. In the event that a catalog is read from a file, where
   * the MIME type is unknown, each reader is attempted in turn until
   * one succeeds.</p>
   */
  protected Vector readerArr = new Vector();

  /**
   * Constructs an empty Catalog.
   *
   * <p>The constructor interrogates the relevant system properties
   * using the default (static) CatalogManager
   * and initializes the catalog data structures.</p>
   */
  public Catalog() {
    // nop;
  }

  /**
   * Constructs an empty Catalog with a specific CatalogManager.
   *
   * <p>The constructor interrogates the relevant system properties
   * using the specified Catalog Manager
   * and initializes the catalog data structures.</p>
   */
  public Catalog(CatalogManager manager) {
    catalogManager = manager;
  }

  /**
   * Return the CatalogManager used by this catalog.
   *
   */
  public CatalogManager getCatalogManager() {
    return catalogManager;
  }

  /**
   * Establish the CatalogManager used by this catalog.
   *
   */
  public void setCatalogManager(CatalogManager manager) {
    catalogManager = manager;
  }

  /**
   * Setup readers.
   */
  public void setupReaders() {
    SAXParserFactory spf = catalogManager.useServicesMechanism() ?
                    SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
    spf.setNamespaceAware(true);
    spf.setValidating(false);

    SAXCatalogReader saxReader = new SAXCatalogReader(spf);

    saxReader.setCatalogParser(null, "XMLCatalog",
                               "com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader");

    saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName,
                               "catalog",
                               "com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader");

    addReader("application/xml", saxReader);

    TR9401CatalogReader textReader = new TR9401CatalogReader();
    addReader("text/plain", textReader);
  }

  /**
   * Add a new CatalogReader to the Catalog.
   *
   * <p>This method allows you to add a new CatalogReader to the
   * catalog. The reader will be associated with the specified mimeType.
   * You can only have one reader per mimeType.</p>
   *
   * <p>In the absence of a mimeType (e.g., when reading a catalog
   * directly from a file on the local system), the readers are attempted
   * in the order that you add them to the Catalog.</p>
   *
   * <p>Note that subordinate catalogs (created by CATALOG or
   * DELEGATE* entries) get a copy of the set of readers present in
   * the primary catalog when they are created. Readers added subsequently
   * will not be available. For this reason, it is best to add all
   * of the readers before the first call to parse a catalog.</p>
   *
   * @param mimeType The MIME type associated with this reader.
   * @param reader The CatalogReader to use.
   */
  public void addReader(String mimeType, CatalogReader reader) {
    if (readerMap.containsKey(mimeType)) {
      Integer pos = readerMap.get(mimeType);
      readerArr.set(pos, reader);
    } else {
      readerArr.add(reader);
      Integer pos = readerArr.size()-1;
      readerMap.put(mimeType, pos);
    }
  }

  /**
   * Copies the reader list from the current Catalog to a new Catalog.
   *
   * <p>This method is used internally when constructing a new catalog.
   * It copies the current reader associations over to the new catalog.
   * </p>
   *
   * @param newCatalog The new Catalog.
   */
  protected void copyReaders(Catalog newCatalog) {
    // Have to copy the readers in the right order...convert hash to arr
    Vector mapArr = new Vector(readerMap.size());

    // Pad the mapArr out to the right length
    for (int count = 0; count < readerMap.size(); count++) {
      mapArr.add(null);
    }

    for (Map.Entry<String, Integer> entry : readerMap.entrySet()) {
        mapArr.set(entry.getValue(), entry.getKey());
    }

    for (int count = 0; count < mapArr.size(); count++) {
      String mimeType = (String) mapArr.get(count);
      Integer pos = readerMap.get(mimeType);
      newCatalog.addReader(mimeType,
                           (CatalogReader)
                           readerArr.get(pos));
    }
  }

  /**
   * Create a new Catalog object.
   *
   * <p>This method constructs a new instance of the running Catalog
   * class (which might be a subtype of com.sun.org.apache.xml.internal.resolver.Catalog).
   * All new catalogs are managed by the same CatalogManager.
   * </p>
   *
   * <p>N.B. All Catalog subtypes should call newCatalog() to construct
   * a new Catalog. Do not simply use "new Subclass()" since that will
   * confuse future subclasses.</p>
   */
  protected Catalog newCatalog() {
    String catalogClass = this.getClass().getName();

    try {
      Catalog c = (Catalog) (Class.forName(catalogClass).newInstance());
      c.setCatalogManager(catalogManager);
      copyReaders(c);
      return c;
    } catch (ClassNotFoundException cnfe) {
      catalogManager.debug.message(1, "Class Not Found Exception: " + catalogClass);
    } catch (IllegalAccessException iae) {
      catalogManager.debug.message(1, "Illegal Access Exception: " + catalogClass);
    } catch (InstantiationException ie) {
      catalogManager.debug.message(1, "Instantiation Exception: " + catalogClass);
    } catch (ClassCastException cce) {
      catalogManager.debug.message(1, "Class Cast Exception: " + catalogClass);
    } catch (Exception e) {
      catalogManager.debug.message(1, "Other Exception: " + catalogClass);
    }

    Catalog c = new Catalog();
    c.setCatalogManager(catalogManager);
    copyReaders(c);
    return c;
  }

  /**
   * Returns the current base URI.
   */
  public String getCurrentBase() {
    return base.toString();
  }

  /**
   * Returns the default override setting associated with this
   * catalog.
   *
   * <p>All catalog files loaded by this catalog will have the
   * initial override setting specified by this default.</p>
   */
  public String getDefaultOverride() {
    if (default_override) {
      return "yes";
    } else {
      return "no";
    }
  }

  /**
   * Load the system catalog files.
   *
   * <p>The method adds all of the
   * catalogs specified in the <tt>xml.catalog.files</tt> property
   * to the Catalog list.</p>
   *
   * @throws MalformedURLException  One of the system catalogs is
   * identified with a filename that is not a valid URL.
   * @throws IOException One of the system catalogs cannot be read.
   */
  public void loadSystemCatalogs()
    throws MalformedURLException, IOException {

    Vector catalogs = catalogManager.getCatalogFiles();
    if (catalogs != null) {
      for (int count = 0; count < catalogs.size(); count++) {
        catalogFiles.addElement(catalogs.elementAt(count));
      }
    }

    if (catalogFiles.size() > 0) {
      // This is a little odd. The parseCatalog() method expects
      // a filename, but it adds that name to the end of the
      // catalogFiles vector, and then processes that vector.
      // This allows the system to handle CATALOG entries
      // correctly.
      //
      // In this init case, we take the last element off the
      // catalogFiles vector and pass it to parseCatalog. This
      // will "do the right thing" in the init case, and allow
      // parseCatalog() to do the right thing in the non-init
      // case. Honest.
      //
      String catfile = (String) catalogFiles.lastElement();
      catalogFiles.removeElement(catfile);
      parseCatalog(catfile);
    }
  }

  /**
   * Parse a catalog file, augmenting internal data structures.
   *
   * @param fileName The filename of the catalog file to process
   *
   * @throws MalformedURLException The fileName cannot be turned into
   * a valid URL.
   * @throws IOException Error reading catalog file.
   */
  public synchronized void parseCatalog(String fileName)
    throws MalformedURLException, IOException {

    default_override = catalogManager.getPreferPublic();
    catalogManager.debug.message(4, "Parse catalog: " + fileName);

    // Put the file into the list of catalogs to process...
    // In all cases except the case when initCatalog() is the
    // caller, this will be the only catalog initially in the list...
    catalogFiles.addElement(fileName);

    // Now process all the pending catalogs...
    parsePendingCatalogs();
  }

  /**
   * Parse a catalog file, augmenting internal data structures.
   *
   * <p>Catalogs retrieved over the net may have an associated MIME type.
   * The MIME type can be used to select an appropriate reader.</p>
   *
   * @param mimeType The MIME type of the catalog file.
   * @param is The InputStream from which the catalog should be read
   *
   * @throws CatalogException Failed to load catalog
   * mimeType.
   * @throws IOException Error reading catalog file.
   */
  public synchronized void parseCatalog(String mimeType, InputStream is)
    throws IOException, CatalogException {

    default_override = catalogManager.getPreferPublic();
    catalogManager.debug.message(4, "Parse " + mimeType + " catalog on input stream");

    CatalogReader reader = null;

    if (readerMap.containsKey(mimeType)) {
      int arrayPos = ((Integer) readerMap.get(mimeType)).intValue();
      reader = (CatalogReader) readerArr.get(arrayPos);
    }

    if (reader == null) {
      String msg = "No CatalogReader for MIME type: " + mimeType;
      catalogManager.debug.message(2, msg);
      throw new CatalogException(CatalogException.UNPARSEABLE, msg);
    }

    reader.readCatalog(this, is);

    // Now process all the pending catalogs...
    parsePendingCatalogs();
  }

  /**
   * Parse a catalog document, augmenting internal data structures.
   *
   * <p>This method supports catalog files stored in jar files: e.g.,
   * jar:file:///path/to/filename.jar!/path/to/catalog.xml". That URI
   * doesn't survive transmogrification through the URI processing that
   * the parseCatalog(String) performs and passing it as an input stream
   * doesn't set the base URI appropriately.</p>
   *
   * <p>Written by Stefan Wachter (2002-09-26)</p>
   *
   * @param aUrl The URL of the catalog document to process
   *
   * @throws IOException Error reading catalog file.
   */
  public synchronized void parseCatalog(URL aUrl) throws IOException {
    catalogCwd = aUrl;
    base = aUrl;

    default_override = catalogManager.getPreferPublic();
    catalogManager.debug.message(4, "Parse catalog: " + aUrl.toString());

    DataInputStream inStream = null;
    boolean parsed = false;

    for (int count = 0; !parsed && count < readerArr.size(); count++) {
      CatalogReader reader = (CatalogReader) readerArr.get(count);

      try {
        inStream = new DataInputStream(aUrl.openStream());
      } catch (FileNotFoundException fnfe) {
        // No catalog; give up!
        break;
      }

      try {
        reader.readCatalog(this, inStream);
        parsed=true;
      } catch (CatalogException ce) {
        if (ce.getExceptionType() == CatalogException.PARSE_FAILED) {
          // give up!
          break;
        } else {
          // try again!
        }
      }

      try {
        inStream.close();
      } catch (IOException e) {
        //nop
      }
    }

    if (parsed) parsePendingCatalogs();
  }

  /**
   * Parse all of the pending catalogs.
   *
   * <p>Catalogs may refer to other catalogs, this method parses
   * all of the currently pending catalog files.</p>
   */
  protected synchronized void parsePendingCatalogs()
    throws MalformedURLException, IOException {

    if (!localCatalogFiles.isEmpty()) {
      // Move all the localCatalogFiles into the front of
      // the catalogFiles queue
      Vector newQueue = new Vector();
      Enumeration q = localCatalogFiles.elements();
      while (q.hasMoreElements()) {
        newQueue.addElement(q.nextElement());
      }

      // Put the rest of the catalogs on the end of the new list
      for (int curCat = 0; curCat < catalogFiles.size(); curCat++) {
        String catfile = (String) catalogFiles.elementAt(curCat);
        newQueue.addElement(catfile);
      }

      catalogFiles = newQueue;
      localCatalogFiles.clear();
    }

    // Suppose there are no catalog files to process, but the
    // single catalog already parsed included some delegate
    // entries? Make sure they don't get lost.
    if (catalogFiles.isEmpty() && !localDelegate.isEmpty()) {
      Enumeration e = localDelegate.elements();
      while (e.hasMoreElements()) {
        catalogEntries.addElement(e.nextElement());
      }
      localDelegate.clear();
    }

    // Now process all the files on the catalogFiles vector. This
    // vector can grow during processing if CATALOG entries are
    // encountered in the catalog
    while (!catalogFiles.isEmpty()) {
      String catfile = (String) catalogFiles.elementAt(0);
      try {
        catalogFiles.remove(0);
      } catch (ArrayIndexOutOfBoundsException e) {
        // can't happen
      }

      if (catalogEntries.size() == 0 && catalogs.size() == 0) {
        // We haven't parsed any catalogs yet, let this
        // catalog be the first...
        try {
          parseCatalogFile(catfile);
        } catch (CatalogException ce) {
          System.out.println("FIXME: " + ce.toString());
        }
      } else {
        // This is a subordinate catalog. We save its name,
        // but don't bother to load it unless it's necessary.
        catalogs.addElement(catfile);
      }

      if (!localCatalogFiles.isEmpty()) {
        // Move all the localCatalogFiles into the front of
        // the catalogFiles queue
        Vector newQueue = new Vector();
        Enumeration q = localCatalogFiles.elements();
        while (q.hasMoreElements()) {
          newQueue.addElement(q.nextElement());
        }

        // Put the rest of the catalogs on the end of the new list
        for (int curCat = 0; curCat < catalogFiles.size(); curCat++) {
          catfile = (String) catalogFiles.elementAt(curCat);
          newQueue.addElement(catfile);
        }

        catalogFiles = newQueue;
        localCatalogFiles.clear();
      }

      if (!localDelegate.isEmpty()) {
        Enumeration e = localDelegate.elements();
        while (e.hasMoreElements()) {
          catalogEntries.addElement(e.nextElement());
        }
        localDelegate.clear();
      }
    }

    // We've parsed them all, reinit the vector...
    catalogFiles.clear();
  }

  /**
   * Parse a single catalog file, augmenting internal data structures.
   *
   * @param fileName The filename of the catalog file to process
   *
   * @throws MalformedURLException The fileName cannot be turned into
   * a valid URL.
   * @throws IOException Error reading catalog file.
   */
  protected synchronized void parseCatalogFile(String fileName)
    throws MalformedURLException, IOException, CatalogException {

    CatalogEntry entry;

    // The base-base is the cwd. If the catalog file is specified
    // with a relative path, this assures that it gets resolved
    // properly...
    try {
      // tack on a basename because URLs point to files not dirs
      catalogCwd = FileURL.makeURL("basename");
    } catch (MalformedURLException e) {
      catalogManager.debug.message(1, "Malformed URL on cwd", "user.dir");
      catalogCwd = null;
    }

    // The initial base URI is the location of the catalog file
    try {
      base = new URL(catalogCwd, fixSlashes(fileName));
    } catch (MalformedURLException e) {
      try {
        base = new URL("file:" + fixSlashes(fileName));
      } catch (MalformedURLException e2) {
        catalogManager.debug.message(1, "Malformed URL on catalog filename",
                      fixSlashes(fileName));
        base = null;
      }
    }

    catalogManager.debug.message(2, "Loading catalog", fileName);
    catalogManager.debug.message(4, "Default BASE", base.toString());

    fileName = base.toString();

    DataInputStream inStream = null;
    boolean parsed = false;
    boolean notFound = false;

    for (int count = 0; !parsed && count < readerArr.size(); count++) {
      CatalogReader reader = (CatalogReader) readerArr.get(count);

      try {
        notFound = false;
        inStream = new DataInputStream(base.openStream());
      } catch (FileNotFoundException fnfe) {
        // No catalog; give up!
        notFound = true;
        break;
      }

      try {
        reader.readCatalog(this, inStream);
        parsed = true;
      } catch (CatalogException ce) {
        if (ce.getExceptionType() == CatalogException.PARSE_FAILED) {
          // give up!
          break;
        } else {
          // try again!
        }
      }

      try {
        inStream.close();
      } catch (IOException e) {
        //nop
      }
    }

    if (!parsed) {
      if (notFound) {
        catalogManager.debug.message(3, "Catalog does not exist", fileName);
      } else {
        catalogManager.debug.message(1, "Failed to parse catalog", fileName);
      }
    }
  }

  /**
   * Cleanup and process a Catalog entry.
   *
   * <p>This method processes each Catalog entry, changing mapped
   * relative system identifiers into absolute ones (based on the current
   * base URI), and maintaining other information about the current
   * catalog.</p>
   *
   * @param entry The CatalogEntry to process.
   */
  public void addEntry(CatalogEntry entry) {
    int type = entry.getEntryType();

    if (type == BASE) {
      String value = entry.getEntryArg(0);
      URL newbase = null;

      if (base == null) {
        catalogManager.debug.message(5, "BASE CUR", "null");
      } else {
        catalogManager.debug.message(5, "BASE CUR", base.toString());
      }
      catalogManager.debug.message(4, "BASE STR", value);

      try {
        value = fixSlashes(value);
        newbase = new URL(base, value);
      } catch (MalformedURLException e) {
        try {
          newbase = new URL("file:" + value);
        } catch (MalformedURLException e2) {
          catalogManager.debug.message(1, "Malformed URL on base", value);
          newbase = null;
        }
      }

      if (newbase != null) {
        base = newbase;
      }

      catalogManager.debug.message(5, "BASE NEW", base.toString());
    } else if (type == CATALOG) {
      String fsi = makeAbsolute(entry.getEntryArg(0));

      catalogManager.debug.message(4, "CATALOG", fsi);

      localCatalogFiles.addElement(fsi);
    } else if (type == PUBLIC) {
      String publicid = PublicId.normalize(entry.getEntryArg(0));
      String systemid = makeAbsolute(normalizeURI(entry.getEntryArg(1)));

      entry.setEntryArg(0, publicid);
      entry.setEntryArg(1, systemid);

      catalogManager.debug.message(4, "PUBLIC", publicid, systemid);

      catalogEntries.addElement(entry);
    } else if (type == SYSTEM) {
      String systemid = normalizeURI(entry.getEntryArg(0));
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));

      entry.setEntryArg(1, fsi);

      catalogManager.debug.message(4, "SYSTEM", systemid, fsi);

      catalogEntries.addElement(entry);
    } else if (type == URI) {
      String uri = normalizeURI(entry.getEntryArg(0));
      String altURI = makeAbsolute(normalizeURI(entry.getEntryArg(1)));

      entry.setEntryArg(1, altURI);

      catalogManager.debug.message(4, "URI", uri, altURI);

      catalogEntries.addElement(entry);
    } else if (type == DOCUMENT) {
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0)));
      entry.setEntryArg(0, fsi);

      catalogManager.debug.message(4, "DOCUMENT", fsi);

      catalogEntries.addElement(entry);
    } else if (type == OVERRIDE) {
      catalogManager.debug.message(4, "OVERRIDE", entry.getEntryArg(0));

      catalogEntries.addElement(entry);
    } else if (type == SGMLDECL) {
      // meaningless in XML
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0)));
      entry.setEntryArg(0, fsi);

      catalogManager.debug.message(4, "SGMLDECL", fsi);

      catalogEntries.addElement(entry);
    } else if (type == DELEGATE_PUBLIC) {
      String ppi = PublicId.normalize(entry.getEntryArg(0));
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));

      entry.setEntryArg(0, ppi);
      entry.setEntryArg(1, fsi);

      catalogManager.debug.message(4, "DELEGATE_PUBLIC", ppi, fsi);

      addDelegate(entry);
    } else if (type == DELEGATE_SYSTEM) {
      String psi = normalizeURI(entry.getEntryArg(0));
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));

      entry.setEntryArg(0, psi);
      entry.setEntryArg(1, fsi);

      catalogManager.debug.message(4, "DELEGATE_SYSTEM", psi, fsi);

      addDelegate(entry);
    } else if (type == DELEGATE_URI) {
      String pui = normalizeURI(entry.getEntryArg(0));
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));

      entry.setEntryArg(0, pui);
      entry.setEntryArg(1, fsi);

      catalogManager.debug.message(4, "DELEGATE_URI", pui, fsi);

      addDelegate(entry);
    } else if (type == REWRITE_SYSTEM) {
      String psi = normalizeURI(entry.getEntryArg(0));
      String rpx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));

      entry.setEntryArg(0, psi);
      entry.setEntryArg(1, rpx);

      catalogManager.debug.message(4, "REWRITE_SYSTEM", psi, rpx);

      catalogEntries.addElement(entry);
    } else if (type == REWRITE_URI) {
      String pui = normalizeURI(entry.getEntryArg(0));
      String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));

      entry.setEntryArg(0, pui);
      entry.setEntryArg(1, upx);

      catalogManager.debug.message(4, "REWRITE_URI", pui, upx);

      catalogEntries.addElement(entry);
    } else if (type == SYSTEM_SUFFIX) {
      String pui = normalizeURI(entry.getEntryArg(0));
      String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));

      entry.setEntryArg(0, pui);
      entry.setEntryArg(1, upx);

      catalogManager.debug.message(4, "SYSTEM_SUFFIX", pui, upx);

      catalogEntries.addElement(entry);
    } else if (type == URI_SUFFIX) {
      String pui = normalizeURI(entry.getEntryArg(0));
      String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));

      entry.setEntryArg(0, pui);
      entry.setEntryArg(1, upx);

      catalogManager.debug.message(4, "URI_SUFFIX", pui, upx);

      catalogEntries.addElement(entry);
    } else if (type == DOCTYPE) {
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
      entry.setEntryArg(1, fsi);

      catalogManager.debug.message(4, "DOCTYPE", entry.getEntryArg(0), fsi);

      catalogEntries.addElement(entry);
    } else if (type == DTDDECL) {
      // meaningless in XML
      String fpi = PublicId.normalize(entry.getEntryArg(0));
      entry.setEntryArg(0, fpi);
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
      entry.setEntryArg(1, fsi);

      catalogManager.debug.message(4, "DTDDECL", fpi, fsi);

      catalogEntries.addElement(entry);
    } else if (type == ENTITY) {
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
      entry.setEntryArg(1, fsi);

      catalogManager.debug.message(4, "ENTITY", entry.getEntryArg(0), fsi);

      catalogEntries.addElement(entry);
    } else if (type == LINKTYPE) {
      // meaningless in XML
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
      entry.setEntryArg(1, fsi);

      catalogManager.debug.message(4, "LINKTYPE", entry.getEntryArg(0), fsi);

      catalogEntries.addElement(entry);
    } else if (type == NOTATION) {
      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
      entry.setEntryArg(1, fsi);

      catalogManager.debug.message(4, "NOTATION", entry.getEntryArg(0), fsi);

      catalogEntries.addElement(entry);
    } else {
      catalogEntries.addElement(entry);
    }
  }

  /**
   * Handle unknown CatalogEntry types.
   *
   * <p>This method exists to allow subclasses to deal with unknown
   * entry types.</p>
   */
  public void unknownEntry(Vector strings) {
    if (strings != null && strings.size() > 0) {
      String keyword = (String) strings.elementAt(0);
      catalogManager.debug.message(2, "Unrecognized token parsing catalog", keyword);
    }
  }

  /**
   * Parse all subordinate catalogs.
   *
   * <p>This method recursively parses all of the subordinate catalogs.
   * If this method does not throw an exception, you can be confident that
   * no subsequent call to any resolve*() method will either, with two
   * possible exceptions:</p>
   *
   * <ol>
   * <li><p>Delegated catalogs are re-parsed each time they are needed
   * (because a variable list of them may be needed in each case,
   * depending on the length of the matching partial public identifier).</p>
   * <p>But they are parsed by this method, so as long as they don't
   * change or disappear while the program is running, they shouldn't
   * generate errors later if they don't generate errors now.</p>
   * <li><p>If you add new catalogs with <code>parseCatalog</code>, they
   * won't be loaded until they are needed or until you call
   * <code>parseAllCatalogs</code> again.</p>
   * </ol>
   *
   * <p>On the other hand, if you don't call this method, you may
   * successfully parse documents without having to load all possible
   * catalogs.</p>
   *
   * @throws MalformedURLException The filename (URL) for a
   * subordinate or delegated catalog is not a valid URL.
   * @throws IOException Error reading some subordinate or delegated
   * catalog file.
   */
  public void parseAllCatalogs()
    throws MalformedURLException, IOException {

    // Parse all the subordinate catalogs
    for (int catPos = 0; catPos < catalogs.size(); catPos++) {
      Catalog c = null;

      try {
        c = (Catalog) catalogs.elementAt(catPos);
      } catch (ClassCastException e) {
        String catfile = (String) catalogs.elementAt(catPos);
        c = newCatalog();

        c.parseCatalog(catfile);
        catalogs.setElementAt(c, catPos);
        c.parseAllCatalogs();
      }
    }

    // Parse all the DELEGATE catalogs
    Enumeration en = catalogEntries.elements();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();
      if (e.getEntryType() == DELEGATE_PUBLIC
          || e.getEntryType() == DELEGATE_SYSTEM
          || e.getEntryType() == DELEGATE_URI) {
        Catalog dcat = newCatalog();
        dcat.parseCatalog(e.getEntryArg(1));
      }
    }
  }


  /**
   * Return the applicable DOCTYPE system identifier.
   *
   * @param entityName The name of the entity (element) for which
   * a doctype is required.
   * @param publicId The nominal public identifier for the doctype
   * (as provided in the source document).
   * @param systemId The nominal system identifier for the doctype
   * (as provided in the source document).
   *
   * @return The system identifier to use for the doctype.
   *
   * @throws MalformedURLException The formal system identifier of a
   * subordinate catalog cannot be turned into a valid URL.
   * @throws IOException Error reading subordinate catalog file.
   */
  public String resolveDoctype(String entityName,
                               String publicId,
                               String systemId)
    throws MalformedURLException, IOException {
    String resolved = null;

    catalogManager.debug.message(3, "resolveDoctype("
                  +entityName+","+publicId+","+systemId+")");

    systemId = normalizeURI(systemId);

    if (publicId != null && publicId.startsWith("urn:publicid:")) {
      publicId = PublicId.decodeURN(publicId);
    }

    if (systemId != null && systemId.startsWith("urn:publicid:")) {
      systemId = PublicId.decodeURN(systemId);
      if (publicId != null && !publicId.equals(systemId)) {
        catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
        systemId = null;
      } else {
        publicId = systemId;
        systemId = null;
      }
    }

    if (systemId != null) {
      // If there's a SYSTEM entry in this catalog, use it
      resolved = resolveLocalSystem(systemId);
      if (resolved != null) {
        return resolved;
      }
    }

    if (publicId != null) {
      // If there's a PUBLIC entry in this catalog, use it
      resolved = resolveLocalPublic(DOCTYPE,
                                    entityName,
                                    publicId,
                                    systemId);
      if (resolved != null) {
        return resolved;
      }
    }

    // If there's a DOCTYPE entry in this catalog, use it
    boolean over = default_override;
    Enumeration en = catalogEntries.elements();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();
      if (e.getEntryType() == OVERRIDE) {
        over = e.getEntryArg(0).equalsIgnoreCase("YES");
        continue;
      }

      if (e.getEntryType() == DOCTYPE
          && e.getEntryArg(0).equals(entityName)) {
        if (over || systemId == null) {
          return e.getEntryArg(1);
        }
      }
    }

    // Otherwise, look in the subordinate catalogs
    return resolveSubordinateCatalogs(DOCTYPE,
                                      entityName,
                                      publicId,
                                      systemId);
  }

  /**
   * Return the applicable DOCUMENT entry.
   *
   * @return The system identifier to use for the doctype.
   *
   * @throws MalformedURLException The formal system identifier of a
   * subordinate catalog cannot be turned into a valid URL.
   * @throws IOException Error reading subordinate catalog file.
   */
  public String resolveDocument()
    throws MalformedURLException, IOException {
    // If there's a DOCUMENT entry, return it

    catalogManager.debug.message(3, "resolveDocument");

    Enumeration en = catalogEntries.elements();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();
      if (e.getEntryType() == DOCUMENT) {
        return e.getEntryArg(0);
      }
    }

    return resolveSubordinateCatalogs(DOCUMENT,
                                      null, null, null);
  }

  /**
   * Return the applicable ENTITY system identifier.
   *
   * @param entityName The name of the entity for which
   * a system identifier is required.
   * @param publicId The nominal public identifier for the entity
   * (as provided in the source document).
   * @param systemId The nominal system identifier for the entity
   * (as provided in the source document).
   *
   * @return The system identifier to use for the entity.
   *
   * @throws MalformedURLException The formal system identifier of a
   * subordinate catalog cannot be turned into a valid URL.
   * @throws IOException Error reading subordinate catalog file.
   */
  public String resolveEntity(String entityName,
                              String publicId,
                              String systemId)
    throws MalformedURLException, IOException {
    String resolved = null;

    catalogManager.debug.message(3, "resolveEntity("
                  +entityName+","+publicId+","+systemId+")");

    systemId = normalizeURI(systemId);

    if (publicId != null && publicId.startsWith("urn:publicid:")) {
      publicId = PublicId.decodeURN(publicId);
    }

    if (systemId != null && systemId.startsWith("urn:publicid:")) {
      systemId = PublicId.decodeURN(systemId);
      if (publicId != null && !publicId.equals(systemId)) {
        catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
        systemId = null;
      } else {
        publicId = systemId;
        systemId = null;
      }
    }

    if (systemId != null) {
      // If there's a SYSTEM entry in this catalog, use it
      resolved = resolveLocalSystem(systemId);
      if (resolved != null) {
        return resolved;
      }
    }

    if (publicId != null) {
      // If there's a PUBLIC entry in this catalog, use it
      resolved = resolveLocalPublic(ENTITY,
                                    entityName,
                                    publicId,
                                    systemId);
      if (resolved != null) {
        return resolved;
      }
    }

    // If there's a ENTITY entry in this catalog, use it
    boolean over = default_override;
    Enumeration en = catalogEntries.elements();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();
      if (e.getEntryType() == OVERRIDE) {
        over = e.getEntryArg(0).equalsIgnoreCase("YES");
        continue;
      }

      if (e.getEntryType() == ENTITY
          && e.getEntryArg(0).equals(entityName)) {
        if (over || systemId == null) {
          return e.getEntryArg(1);
        }
      }
    }

    // Otherwise, look in the subordinate catalogs
    return resolveSubordinateCatalogs(ENTITY,
                                      entityName,
                                      publicId,
                                      systemId);
  }

  /**
   * Return the applicable NOTATION system identifier.
   *
   * @param notationName The name of the notation for which
   * a doctype is required.
   * @param publicId The nominal public identifier for the notation
   * (as provided in the source document).
   * @param systemId The nominal system identifier for the notation
   * (as provided in the source document).
   *
   * @return The system identifier to use for the notation.
   *
   * @throws MalformedURLException The formal system identifier of a
   * subordinate catalog cannot be turned into a valid URL.
   * @throws IOException Error reading subordinate catalog file.
   */
  public String resolveNotation(String notationName,
                                String publicId,
                                String systemId)
    throws MalformedURLException, IOException {
    String resolved = null;

    catalogManager.debug.message(3, "resolveNotation("
                  +notationName+","+publicId+","+systemId+")");

    systemId = normalizeURI(systemId);

    if (publicId != null && publicId.startsWith("urn:publicid:")) {
      publicId = PublicId.decodeURN(publicId);
    }

    if (systemId != null && systemId.startsWith("urn:publicid:")) {
      systemId = PublicId.decodeURN(systemId);
      if (publicId != null && !publicId.equals(systemId)) {
        catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
        systemId = null;
      } else {
        publicId = systemId;
        systemId = null;
      }
    }

    if (systemId != null) {
      // If there's a SYSTEM entry in this catalog, use it
      resolved = resolveLocalSystem(systemId);
      if (resolved != null) {
        return resolved;
      }
    }

    if (publicId != null) {
      // If there's a PUBLIC entry in this catalog, use it
      resolved = resolveLocalPublic(NOTATION,
                                    notationName,
                                    publicId,
                                    systemId);
      if (resolved != null) {
        return resolved;
      }
    }

    // If there's a NOTATION entry in this catalog, use it
    boolean over = default_override;
    Enumeration en = catalogEntries.elements();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();
      if (e.getEntryType() == OVERRIDE) {
        over = e.getEntryArg(0).equalsIgnoreCase("YES");
        continue;
      }

      if (e.getEntryType() == NOTATION
          && e.getEntryArg(0).equals(notationName)) {
        if (over || systemId == null) {
          return e.getEntryArg(1);
        }
      }
    }

    // Otherwise, look in the subordinate catalogs
    return resolveSubordinateCatalogs(NOTATION,
                                      notationName,
                                      publicId,
                                      systemId);
  }

  /**
   * Return the applicable PUBLIC or SYSTEM identifier.
   *
   * <p>This method searches the Catalog and returns the system
   * identifier specified for the given system or
   * public identifiers. If
   * no appropriate PUBLIC or SYSTEM entry is found in the Catalog,
   * null is returned.</p>
   *
   * @param publicId The public identifier to locate in the catalog.
   * Public identifiers are normalized before comparison.
   * @param systemId The nominal system identifier for the entity
   * in question (as provided in the source document).
   *
   * @throws MalformedURLException The formal system identifier of a
   * subordinate catalog cannot be turned into a valid URL.
   * @throws IOException Error reading subordinate catalog file.
   *
   * @return The system identifier to use.
   * Note that the nominal system identifier is not returned if a
   * match is not found in the catalog, instead null is returned
   * to indicate that no match was found.
   */
  public String resolvePublic(String publicId, String systemId)
    throws MalformedURLException, IOException {

    catalogManager.debug.message(3, "resolvePublic("+publicId+","+systemId+")");

    systemId = normalizeURI(systemId);

    if (publicId != null && publicId.startsWith("urn:publicid:")) {
      publicId = PublicId.decodeURN(publicId);
    }

    if (systemId != null && systemId.startsWith("urn:publicid:")) {
      systemId = PublicId.decodeURN(systemId);
      if (publicId != null && !publicId.equals(systemId)) {
        catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
        systemId = null;
      } else {
        publicId = systemId;
        systemId = null;
      }
    }

    // If there's a SYSTEM entry in this catalog, use it
    if (systemId != null) {
      String resolved = resolveLocalSystem(systemId);
      if (resolved != null) {
        return resolved;
      }
    }

    // If there's a PUBLIC entry in this catalog, use it
    String resolved = resolveLocalPublic(PUBLIC,
                                         null,
                                         publicId,
                                         systemId);
    if (resolved != null) {
      return resolved;
    }

    // Otherwise, look in the subordinate catalogs
    return resolveSubordinateCatalogs(PUBLIC,
                                      null,
                                      publicId,
                                      systemId);
  }

  /**
   * Return the applicable PUBLIC or SYSTEM identifier.
   *
   * <p>This method searches the Catalog and returns the system
   * identifier specified for the given system or public identifiers.
   * If no appropriate PUBLIC or SYSTEM entry is found in the Catalog,
   * delegated Catalogs are interrogated.</p>
   *
   * <p>There are four possible cases:</p>
   *
   * <ul>
   * <li>If the system identifier provided matches a SYSTEM entry
   * in the current catalog, the SYSTEM entry is returned.
   * <li>If the system identifier is not null, the PUBLIC entries
   * that were encountered when OVERRIDE YES was in effect are
   * interrogated and the first matching entry is returned.</li>
   * <li>If the system identifier is null, then all of the PUBLIC
   * entries are interrogated and the first matching entry
   * is returned. This may not be the same as the preceding case, if
   * some PUBLIC entries are encountered when OVERRIDE NO is in effect. In
   * XML, the only place where a public identifier may occur without
   * a system identifier is in a notation declaration.</li>
   * <li>Finally, if the public identifier matches one of the partial
   * public identifiers specified in a DELEGATE* entry in
   * the Catalog, the delegated catalog is interrogated. The first
   * time that the delegated catalog is required, it will be
   * retrieved and parsed. It is subsequently cached.
   * </li>
   * </ul>
   *
   * @param entityType The CatalogEntry type for which this query is
   * being conducted. This is necessary in order to do the approprate
   * query on a delegated catalog.
   * @param entityName The name of the entity being searched for, if
   * appropriate.
   * @param publicId The public identifier of the entity in question.
   * @param systemId The nominal system identifier for the entity
   * in question (as provided in the source document).
   *
   * @throws MalformedURLException The formal system identifier of a
   * delegated catalog cannot be turned into a valid URL.
   * @throws IOException Error reading delegated catalog file.
   *
   * @return The system identifier to use.
   * Note that the nominal system identifier is not returned if a
   * match is not found in the catalog, instead null is returned
   * to indicate that no match was found.
   */
  protected synchronized String resolveLocalPublic(int entityType,
                                                   String entityName,
                                                   String publicId,
                                                   String systemId)
    throws MalformedURLException, IOException {

    // Always normalize the public identifier before attempting a match
    publicId = PublicId.normalize(publicId);

    // If there's a SYSTEM entry in this catalog, use it
    if (systemId != null) {
      String resolved = resolveLocalSystem(systemId);
      if (resolved != null) {
        return resolved;
      }
    }

    // If there's a PUBLIC entry in this catalog, use it
    boolean over = default_override;
    Enumeration en = catalogEntries.elements();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();
      if (e.getEntryType() == OVERRIDE) {
        over = e.getEntryArg(0).equalsIgnoreCase("YES");
        continue;
      }

      if (e.getEntryType() == PUBLIC
          && e.getEntryArg(0).equals(publicId)) {
        if (over || systemId == null) {
          return e.getEntryArg(1);
        }
      }
    }

    // If there's a DELEGATE_PUBLIC entry in this catalog, use it
    over = default_override;
    en = catalogEntries.elements();
    Vector delCats = new Vector();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();
      if (e.getEntryType() == OVERRIDE) {
        over = e.getEntryArg(0).equalsIgnoreCase("YES");
        continue;
      }

      if (e.getEntryType() == DELEGATE_PUBLIC
          && (over || systemId == null)) {
        String p = (String) e.getEntryArg(0);
        if (p.length() <= publicId.length()
            && p.equals(publicId.substring(0, p.length()))) {
          // delegate this match to the other catalog

          delCats.addElement(e.getEntryArg(1));
        }
      }
    }

    if (delCats.size() > 0) {
      Enumeration enCats = delCats.elements();

      if (catalogManager.debug.getDebug() > 1) {
        catalogManager.debug.message(2, "Switching to delegated catalog(s):");
        while (enCats.hasMoreElements()) {
          String delegatedCatalog = (String) enCats.nextElement();
          catalogManager.debug.message(2, "\t" + delegatedCatalog);
        }
      }

      Catalog dcat = newCatalog();

      enCats = delCats.elements();
      while (enCats.hasMoreElements()) {
        String delegatedCatalog = (String) enCats.nextElement();
        dcat.parseCatalog(delegatedCatalog);
      }

      return dcat.resolvePublic(publicId, null);
    }

    // Nada!
    return null;
  }

  /**
   * Return the applicable SYSTEM system identifier.
   *
   * <p>If a SYSTEM entry exists in the Catalog
   * for the system ID specified, return the mapped value.</p>
   *
   * <p>On Windows-based operating systems, the comparison between
   * the system identifier provided and the SYSTEM entries in the
   * Catalog is case-insensitive.</p>
   *
   * @param systemId The system ID to locate in the catalog.
   *
   * @return The resolved system identifier.
   *
   * @throws MalformedURLException The formal system identifier of a
   * subordinate catalog cannot be turned into a valid URL.
   * @throws IOException Error reading subordinate catalog file.
   */
  public String resolveSystem(String systemId)
    throws MalformedURLException, IOException {

    catalogManager.debug.message(3, "resolveSystem("+systemId+")");

    systemId = normalizeURI(systemId);

    if (systemId != null && systemId.startsWith("urn:publicid:")) {
      systemId = PublicId.decodeURN(systemId);
      return resolvePublic(systemId, null);
    }

    // If there's a SYSTEM entry in this catalog, use it
    if (systemId != null) {
      String resolved = resolveLocalSystem(systemId);
      if (resolved != null) {
        return resolved;
      }
    }

    // Otherwise, look in the subordinate catalogs
    return resolveSubordinateCatalogs(SYSTEM,
                                      null,
                                      null,
                                      systemId);
  }

  /**
   * Return the applicable SYSTEM system identifier in this
   * catalog.
   *
   * <p>If a SYSTEM entry exists in the catalog file
   * for the system ID specified, return the mapped value.</p>
   *
   * @param systemId The system ID to locate in the catalog
   *
   * @return The mapped system identifier or null
   */
  protected String resolveLocalSystem(String systemId)
    throws MalformedURLException, IOException {

    String osname = SecuritySupport.getSystemProperty("os.name");
    boolean windows = (osname.indexOf("Windows") >= 0);
    Enumeration en = catalogEntries.elements();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();
      if (e.getEntryType() == SYSTEM
          && (e.getEntryArg(0).equals(systemId)
              || (windows
                  && e.getEntryArg(0).equalsIgnoreCase(systemId)))) {
        return e.getEntryArg(1);
      }
    }

    // If there's a REWRITE_SYSTEM entry in this catalog, use it
    en = catalogEntries.elements();
    String startString = null;
    String prefix = null;
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();

      if (e.getEntryType() == REWRITE_SYSTEM) {
        String p = (String) e.getEntryArg(0);
        if (p.length() <= systemId.length()
            && p.equals(systemId.substring(0, p.length()))) {
          // Is this the longest prefix?
          if (startString == null
              || p.length() > startString.length()) {
            startString = p;
            prefix = e.getEntryArg(1);
          }
        }
      }
    }

    if (prefix != null) {
      // return the systemId with the new prefix
      return prefix + systemId.substring(startString.length());
    }

    // If there's a SYSTEM_SUFFIX entry in this catalog, use it
    en = catalogEntries.elements();
    String suffixString = null;
    String suffixURI = null;
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();

      if (e.getEntryType() == SYSTEM_SUFFIX) {
        String p = (String) e.getEntryArg(0);
        if (p.length() <= systemId.length()
            && systemId.endsWith(p)) {
          // Is this the longest prefix?
          if (suffixString == null
              || p.length() > suffixString.length()) {
            suffixString = p;
            suffixURI = e.getEntryArg(1);
          }
        }
      }
    }

    if (suffixURI != null) {
      // return the systemId for the suffix
      return suffixURI;
    }

    // If there's a DELEGATE_SYSTEM entry in this catalog, use it
    en = catalogEntries.elements();
    Vector delCats = new Vector();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();

      if (e.getEntryType() == DELEGATE_SYSTEM) {
        String p = (String) e.getEntryArg(0);
        if (p.length() <= systemId.length()
            && p.equals(systemId.substring(0, p.length()))) {
          // delegate this match to the other catalog

          delCats.addElement(e.getEntryArg(1));
        }
      }
    }

    if (delCats.size() > 0) {
      Enumeration enCats = delCats.elements();

      if (catalogManager.debug.getDebug() > 1) {
        catalogManager.debug.message(2, "Switching to delegated catalog(s):");
        while (enCats.hasMoreElements()) {
          String delegatedCatalog = (String) enCats.nextElement();
          catalogManager.debug.message(2, "\t" + delegatedCatalog);
        }
      }

      Catalog dcat = newCatalog();

      enCats = delCats.elements();
      while (enCats.hasMoreElements()) {
        String delegatedCatalog = (String) enCats.nextElement();
        dcat.parseCatalog(delegatedCatalog);
      }

      return dcat.resolveSystem(systemId);
    }

    return null;
  }

  /**
   * Return the applicable URI.
   *
   * <p>If a URI entry exists in the Catalog
   * for the URI specified, return the mapped value.</p>
   *
   * <p>URI comparison is case sensitive.</p>
   *
   * @param uri The URI to locate in the catalog.
   *
   * @return The resolved URI.
   *
   * @throws MalformedURLException The system identifier of a
   * subordinate catalog cannot be turned into a valid URL.
   * @throws IOException Error reading subordinate catalog file.
   */
  public String resolveURI(String uri)
    throws MalformedURLException, IOException {

    catalogManager.debug.message(3, "resolveURI("+uri+")");

    uri = normalizeURI(uri);

    if (uri != null && uri.startsWith("urn:publicid:")) {
      uri = PublicId.decodeURN(uri);
      return resolvePublic(uri, null);
    }

    // If there's a URI entry in this catalog, use it
    if (uri != null) {
      String resolved = resolveLocalURI(uri);
      if (resolved != null) {
        return resolved;
      }
    }

    // Otherwise, look in the subordinate catalogs
    return resolveSubordinateCatalogs(URI,
                                      null,
                                      null,
                                      uri);
  }

  /**
   * Return the applicable URI in this catalog.
   *
   * <p>If a URI entry exists in the catalog file
   * for the URI specified, return the mapped value.</p>
   *
   * @param uri The URI to locate in the catalog
   *
   * @return The mapped URI or null
   */
  protected String resolveLocalURI(String uri)
    throws MalformedURLException, IOException {
    Enumeration en = catalogEntries.elements();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();
      if (e.getEntryType() == URI
          && (e.getEntryArg(0).equals(uri))) {
        return e.getEntryArg(1);
      }
    }

    // If there's a REWRITE_URI entry in this catalog, use it
    en = catalogEntries.elements();
    String startString = null;
    String prefix = null;
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();

      if (e.getEntryType() == REWRITE_URI) {
        String p = (String) e.getEntryArg(0);
        if (p.length() <= uri.length()
            && p.equals(uri.substring(0, p.length()))) {
          // Is this the longest prefix?
          if (startString == null
              || p.length() > startString.length()) {
            startString = p;
            prefix = e.getEntryArg(1);
          }
        }
      }
    }

    if (prefix != null) {
      // return the uri with the new prefix
      return prefix + uri.substring(startString.length());
    }

    // If there's a URI_SUFFIX entry in this catalog, use it
    en = catalogEntries.elements();
    String suffixString = null;
    String suffixURI = null;
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();

      if (e.getEntryType() == URI_SUFFIX) {
        String p = (String) e.getEntryArg(0);
        if (p.length() <= uri.length()
            && uri.endsWith(p)) {
          // Is this the longest prefix?
          if (suffixString == null
              || p.length() > suffixString.length()) {
            suffixString = p;
            suffixURI = e.getEntryArg(1);
          }
        }
      }
    }

    if (suffixURI != null) {
      // return the uri for the suffix
      return suffixURI;
    }

    // If there's a DELEGATE_URI entry in this catalog, use it
    en = catalogEntries.elements();
    Vector delCats = new Vector();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();

      if (e.getEntryType() == DELEGATE_URI) {
        String p = (String) e.getEntryArg(0);
        if (p.length() <= uri.length()
            && p.equals(uri.substring(0, p.length()))) {
          // delegate this match to the other catalog

          delCats.addElement(e.getEntryArg(1));
        }
      }
    }

    if (delCats.size() > 0) {
      Enumeration enCats = delCats.elements();

      if (catalogManager.debug.getDebug() > 1) {
        catalogManager.debug.message(2, "Switching to delegated catalog(s):");
        while (enCats.hasMoreElements()) {
          String delegatedCatalog = (String) enCats.nextElement();
          catalogManager.debug.message(2, "\t" + delegatedCatalog);
        }
      }

      Catalog dcat = newCatalog();

      enCats = delCats.elements();
      while (enCats.hasMoreElements()) {
        String delegatedCatalog = (String) enCats.nextElement();
        dcat.parseCatalog(delegatedCatalog);
      }

      return dcat.resolveURI(uri);
    }

    return null;
  }

  /**
   * Search the subordinate catalogs, in order, looking for a match.
   *
   * <p>This method searches the Catalog and returns the system
   * identifier specified for the given entity type with the given
   * name, public, and system identifiers. In some contexts, these
   * may be null.</p>
   *
   * @param entityType The CatalogEntry type for which this query is
   * being conducted. This is necessary in order to do the approprate
   * query on a subordinate catalog.
   * @param entityName The name of the entity being searched for, if
   * appropriate.
   * @param publicId The public identifier of the entity in question
   * (as provided in the source document).
   * @param systemId The nominal system identifier for the entity
   * in question (as provided in the source document). This parameter is
   * overloaded for the URI entry type.
   *
   * @throws MalformedURLException The formal system identifier of a
   * delegated catalog cannot be turned into a valid URL.
   * @throws IOException Error reading delegated catalog file.
   *
   * @return The system identifier to use.
   * Note that the nominal system identifier is not returned if a
   * match is not found in the catalog, instead null is returned
   * to indicate that no match was found.
   */
  protected synchronized String resolveSubordinateCatalogs(int entityType,
                                                           String entityName,
                                                           String publicId,
                                                           String systemId)
    throws MalformedURLException, IOException {

    for (int catPos = 0; catPos < catalogs.size(); catPos++) {
      Catalog c = null;

      try {
        c = (Catalog) catalogs.elementAt(catPos);
      } catch (ClassCastException e) {
        String catfile = (String) catalogs.elementAt(catPos);
        c = newCatalog();

        try {
          c.parseCatalog(catfile);
        } catch (MalformedURLException mue) {
          catalogManager.debug.message(1, "Malformed Catalog URL", catfile);
        } catch (FileNotFoundException fnfe) {
          catalogManager.debug.message(1, "Failed to load catalog, file not found",
                        catfile);
        } catch (IOException ioe) {
          catalogManager.debug.message(1, "Failed to load catalog, I/O error", catfile);
        }

        catalogs.setElementAt(c, catPos);
      }

      String resolved = null;

      // Ok, now what are we supposed to call here?
      if (entityType == DOCTYPE) {
        resolved = c.resolveDoctype(entityName,
                                    publicId,
                                    systemId);
      } else if (entityType == DOCUMENT) {
        resolved = c.resolveDocument();
      } else if (entityType == ENTITY) {
        resolved = c.resolveEntity(entityName,
                                   publicId,
                                   systemId);
      } else if (entityType == NOTATION) {
        resolved = c.resolveNotation(entityName,
                                     publicId,
                                     systemId);
      } else if (entityType == PUBLIC) {
        resolved = c.resolvePublic(publicId, systemId);
      } else if (entityType == SYSTEM) {
        resolved = c.resolveSystem(systemId);
      } else if (entityType == URI) {
        resolved = c.resolveURI(systemId);
      }

      if (resolved != null) {
        return resolved;
      }
    }

    return null;
  }

  // -----------------------------------------------------------------

  /**
   * Replace backslashes with forward slashes. (URLs always use
   * forward slashes.)
   *
   * @param sysid The input system identifier.
   * @return The same system identifier with backslashes turned into
   * forward slashes.
   */
  protected String fixSlashes (String sysid) {
    return sysid.replace('\\', '/');
  }

  /**
   * Construct an absolute URI from a relative one, using the current
   * base URI.
   *
   * @param sysid The (possibly relative) system identifier
   * @return The system identifier made absolute with respect to the
   * current {@link #base}.
   */
  protected String makeAbsolute(String sysid) {
    URL local = null;

    sysid = fixSlashes(sysid);

    try {
      local = new URL(base, sysid);
    } catch (MalformedURLException e) {
      catalogManager.debug.message(1, "Malformed URL on system identifier", sysid);
    }

    if (local != null) {
      return local.toString();
    } else {
      return sysid;
    }
  }

  /**
   * Perform character normalization on a URI reference.
   *
   * @param uriref The URI reference
   * @return The normalized URI reference.
   */
  protected String normalizeURI(String uriref) {
    if (uriref == null) {
      return null;
    }

    byte[] bytes;
    try {
      bytes = uriref.getBytes("UTF-8");
    } catch (UnsupportedEncodingException uee) {
      // this can't happen
      catalogManager.debug.message(1, "UTF-8 is an unsupported encoding!?");
      return uriref;
    }

    StringBuilder newRef = new StringBuilder(bytes.length);
    for (int count = 0; count < bytes.length; count++) {
      int ch = bytes[count] & 0xFF;

      if ((ch <= 0x20)    // ctrl
          || (ch > 0x7F)  // high ascii
          || (ch == 0x22) // "
          || (ch == 0x3C) // <
          || (ch == 0x3E) // >
          || (ch == 0x5C) // \
          || (ch == 0x5E) // ^
          || (ch == 0x60) // `
          || (ch == 0x7B) // {
          || (ch == 0x7C) // |
          || (ch == 0x7D) // }
          || (ch == 0x7F)) {
        newRef.append(encodedByte(ch));
      } else {
        newRef.append((char) bytes[count]);
      }
    }

    return newRef.toString();
  }

  /**
   * Perform %-encoding on a single byte.
   *
   * @param b The 8-bit integer that represents th byte. (Bytes are signed
              but encoding needs to look at the bytes unsigned.)
   * @return The %-encoded string for the byte in question.
   */
  protected String encodedByte (int b) {
    String hex = Integer.toHexString(b).toUpperCase();
    if (hex.length() < 2) {
      return "%0" + hex;
    } else {
      return "%" + hex;
    }
  }

  // -----------------------------------------------------------------

  /**
   * Add to the current list of delegated catalogs.
   *
   * <p>This method always constructs the {@link #localDelegate}
   * vector so that it is ordered by length of partial
   * public identifier.</p>
   *
   * @param entry The DELEGATE catalog entry
   */
  protected void addDelegate(CatalogEntry entry) {
    int pos = 0;
    String partial = entry.getEntryArg(0);

    Enumeration local = localDelegate.elements();
    while (local.hasMoreElements()) {
      CatalogEntry dpe = (CatalogEntry) local.nextElement();
      String dp = dpe.getEntryArg(0);
      if (dp.equals(partial)) {
        // we already have this prefix
        return;
      }
      if (dp.length() > partial.length()) {
        pos++;
      }
      if (dp.length() < partial.length()) {
        break;
      }
    }

    // now insert partial into the vector at [pos]
    if (localDelegate.size() == 0) {
      localDelegate.addElement(entry);
    } else {
      localDelegate.insertElementAt(entry, pos);
    }
  }
}