1 3 56 57 package org.jboss.util.xml.catalog; 58 59 import java.io.IOException ; 60 import java.io.FileNotFoundException ; 61 import java.io.InputStream ; 62 import java.io.UnsupportedEncodingException ; 63 import java.io.DataInputStream ; 64 import java.util.Enumeration ; 65 import java.util.Hashtable ; 66 import java.util.Vector ; 67 import java.net.URL ; 68 import java.net.MalformedURLException ; 69 import org.jboss.util.xml.catalog.CatalogManager; 70 import org.jboss.util.xml.catalog.helpers.PublicId; 71 import org.jboss.util.xml.catalog.readers.CatalogReader; 72 import org.jboss.util.xml.catalog.readers.OASISXMLCatalogReader; 73 import org.jboss.util.xml.catalog.readers.SAXCatalogReader; 74 import org.jboss.util.xml.catalog.readers.TR9401CatalogReader; 75 76 import javax.xml.parsers.SAXParserFactory ; 77 78 220 public class Catalog { 221 222 public static final int BASE = CatalogEntry.addEntryType("BASE", 1); 223 224 225 public static final int CATALOG = CatalogEntry.addEntryType("CATALOG", 1); 226 227 228 public static final int DOCUMENT = CatalogEntry.addEntryType("DOCUMENT", 1); 229 230 231 public static final int OVERRIDE = CatalogEntry.addEntryType("OVERRIDE", 1); 232 233 234 public static final int SGMLDECL = CatalogEntry.addEntryType("SGMLDECL", 1); 235 236 237 public static final int DELEGATE_PUBLIC = CatalogEntry.addEntryType("DELEGATE_PUBLIC", 2); 238 239 240 public static final int DELEGATE_SYSTEM = CatalogEntry.addEntryType("DELEGATE_SYSTEM", 2); 241 242 243 public static final int DELEGATE_URI = CatalogEntry.addEntryType("DELEGATE_URI", 2); 244 245 246 public static final int DOCTYPE = CatalogEntry.addEntryType("DOCTYPE", 2); 247 248 249 public static final int DTDDECL = CatalogEntry.addEntryType("DTDDECL", 2); 250 251 252 public static final int ENTITY = CatalogEntry.addEntryType("ENTITY", 2); 253 254 255 public static final int LINKTYPE = CatalogEntry.addEntryType("LINKTYPE", 2); 256 257 258 public static final int NOTATION = CatalogEntry.addEntryType("NOTATION", 2); 259 260 261 public static final int PUBLIC = CatalogEntry.addEntryType("PUBLIC", 2); 262 263 264 public static final int SYSTEM = CatalogEntry.addEntryType("SYSTEM", 2); 265 266 267 public static final int URI = CatalogEntry.addEntryType("URI", 2); 268 269 270 public static final int REWRITE_SYSTEM = CatalogEntry.addEntryType("REWRITE_SYSTEM", 2); 271 272 273 public static final int REWRITE_URI = CatalogEntry.addEntryType("REWRITE_URI", 2); 274 275 279 protected URL base; 280 281 282 protected URL catalogCwd; 283 284 285 protected Vector catalogEntries = new Vector (); 286 287 288 protected boolean default_override = true; 289 290 291 protected CatalogManager catalogManager = CatalogManager.getStaticManager(); 292 293 304 protected Vector catalogFiles = new Vector (); 305 306 323 protected Vector localCatalogFiles = new Vector (); 324 325 342 protected Vector catalogs = new Vector (); 343 344 359 protected Vector localDelegate = new Vector (); 360 361 368 protected Hashtable readerMap = new Hashtable (); 369 370 378 protected Vector readerArr = new Vector (); 379 380 387 public Catalog() { 388 } 390 391 398 public Catalog(CatalogManager manager) { 399 catalogManager = manager; 400 } 401 402 406 public CatalogManager getCatalogManager() { 407 return catalogManager; 408 } 409 410 414 public void setCatalogManager(CatalogManager manager) { 415 catalogManager = manager; 416 } 417 418 421 public void setupReaders() { 422 SAXParserFactory spf = SAXParserFactory.newInstance(); 423 spf.setNamespaceAware(true); 424 spf.setValidating(false); 425 426 SAXCatalogReader saxReader = new SAXCatalogReader(spf); 427 428 saxReader.setCatalogParser(null, "XMLCatalog", 429 "org.apache.xml.resolver.readers.XCatalogReader"); 430 431 saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, 432 "catalog", 433 "org.apache.xml.resolver.readers.OASISXMLCatalogReader"); 434 435 addReader("application/xml", saxReader); 436 437 TR9401CatalogReader textReader = new TR9401CatalogReader(); 438 addReader("text/plain", textReader); 439 } 440 441 461 public void addReader(String mimeType, CatalogReader reader) { 462 if (readerMap.containsKey(mimeType)) { 463 Integer pos = (Integer ) readerMap.get(mimeType); 464 readerArr.set(pos.intValue(), reader); 465 } else { 466 readerArr.add(reader); 467 Integer pos = new Integer (readerArr.size()-1); 468 readerMap.put(mimeType, pos); 469 } 470 } 471 472 481 protected void copyReaders(Catalog newCatalog) { 482 Vector mapArr = new Vector (readerMap.size()); 484 485 for (int count = 0; count < readerMap.size(); count++) { 487 mapArr.add(null); 488 } 489 490 Enumeration enumt = readerMap.keys(); 491 while (enumt.hasMoreElements()) { 492 String mimeType = (String ) enumt.nextElement(); 493 Integer pos = (Integer ) readerMap.get(mimeType); 494 mapArr.set(pos.intValue(), mimeType); 495 } 496 497 for (int count = 0; count < mapArr.size(); count++) { 498 String mimeType = (String ) mapArr.get(count); 499 Integer pos = (Integer ) readerMap.get(mimeType); 500 newCatalog.addReader(mimeType, 501 (CatalogReader) 502 readerArr.get(pos.intValue())); 503 } 504 } 505 506 518 protected Catalog newCatalog() { 519 String catalogClass = this.getClass().getName(); 520 521 try { 522 Catalog c = (Catalog) (Class.forName(catalogClass).newInstance()); 523 c.setCatalogManager(catalogManager); 524 copyReaders(c); 525 return c; 526 } catch (ClassNotFoundException cnfe) { 527 catalogManager.debug.message(1, "Class Not Found Exception: " + catalogClass); 528 } catch (IllegalAccessException iae) { 529 catalogManager.debug.message(1, "Illegal Access Exception: " + catalogClass); 530 } catch (InstantiationException ie) { 531 catalogManager.debug.message(1, "Instantiation Exception: " + catalogClass); 532 } catch (ClassCastException cce) { 533 catalogManager.debug.message(1, "Class Cast Exception: " + catalogClass); 534 } catch (Exception e) { 535 catalogManager.debug.message(1, "Other Exception: " + catalogClass); 536 } 537 538 Catalog c = new Catalog(); 539 c.setCatalogManager(catalogManager); 540 copyReaders(c); 541 return c; 542 } 543 544 547 public String getCurrentBase() { 548 return base.toString(); 549 } 550 551 558 public String getDefaultOverride() { 559 if (default_override) { 560 return "yes"; 561 } else { 562 return "no"; 563 } 564 } 565 566 577 public void loadSystemCatalogs() 578 throws MalformedURLException , IOException { 579 580 Vector catalogs = catalogManager.getCatalogFiles(); 581 if (catalogs != null) { 582 for (int count = 0; count < catalogs.size(); count++) { 583 catalogFiles.addElement(catalogs.elementAt(count)); 584 } 585 } 586 587 if (catalogFiles.size() > 0) { 588 String catfile = (String ) catalogFiles.lastElement(); 601 catalogFiles.removeElement(catfile); 602 parseCatalog(catfile); 603 } 604 } 605 606 615 public synchronized void parseCatalog(String fileName) 616 throws MalformedURLException , IOException { 617 618 default_override = catalogManager.getPreferPublic(); 619 catalogManager.debug.message(4, "Parse catalog: " + fileName); 620 621 catalogFiles.addElement(fileName); 625 626 parsePendingCatalogs(); 628 } 629 630 643 public synchronized void parseCatalog(String mimeType, InputStream is) 644 throws IOException , CatalogException { 645 646 default_override = catalogManager.getPreferPublic(); 647 catalogManager.debug.message(4, "Parse " + mimeType + " catalog on input stream"); 648 649 CatalogReader reader = null; 650 651 if (readerMap.containsKey(mimeType)) { 652 int arrayPos = ((Integer ) readerMap.get(mimeType)).intValue(); 653 reader = (CatalogReader) readerArr.get(arrayPos); 654 } 655 656 if (reader == null) { 657 String msg = "No CatalogReader for MIME type: " + mimeType; 658 catalogManager.debug.message(2, msg); 659 throw new CatalogException(CatalogException.UNPARSEABLE, msg); 660 } 661 662 reader.readCatalog(this, is); 663 664 parsePendingCatalogs(); 666 } 667 668 683 public synchronized void parseCatalog(URL aUrl) throws IOException { 684 catalogCwd = aUrl; 685 base = aUrl; 686 687 default_override = catalogManager.getPreferPublic(); 688 catalogManager.debug.message(4, "Parse catalog: " + aUrl.toString()); 689 690 DataInputStream inStream = null; 691 boolean parsed = false; 692 693 for (int count = 0; !parsed && count < readerArr.size(); count++) { 694 CatalogReader reader = (CatalogReader) readerArr.get(count); 695 696 try { 697 inStream = new DataInputStream (aUrl.openStream()); 698 } catch (FileNotFoundException fnfe) { 699 break; 701 } 702 703 try { 704 reader.readCatalog(this, inStream); 705 parsed=true; 706 } catch (CatalogException ce) { 707 if (ce.getExceptionType() == CatalogException.PARSE_FAILED) { 708 break; 710 } else { 711 } 713 } 714 715 try { 716 inStream.close(); 717 } catch (IOException e) { 718 } 720 } 721 722 if (parsed) parsePendingCatalogs(); 723 } 724 725 731 protected synchronized void parsePendingCatalogs() 732 throws MalformedURLException , IOException { 733 734 if (!localCatalogFiles.isEmpty()) { 735 Vector newQueue = new Vector (); 738 Enumeration q = localCatalogFiles.elements(); 739 while (q.hasMoreElements()) { 740 newQueue.addElement(q.nextElement()); 741 } 742 743 for (int curCat = 0; curCat < catalogFiles.size(); curCat++) { 745 String catfile = (String ) catalogFiles.elementAt(curCat); 746 newQueue.addElement(catfile); 747 } 748 749 catalogFiles = newQueue; 750 localCatalogFiles.clear(); 751 } 752 753 if (catalogFiles.isEmpty() && !localDelegate.isEmpty()) { 757 Enumeration e = localDelegate.elements(); 758 while (e.hasMoreElements()) { 759 catalogEntries.addElement(e.nextElement()); 760 } 761 localDelegate.clear(); 762 } 763 764 while (!catalogFiles.isEmpty()) { 768 String catfile = (String ) catalogFiles.elementAt(0); 769 try { 770 catalogFiles.remove(0); 771 } catch (ArrayIndexOutOfBoundsException e) { 772 } 774 775 if (catalogEntries.size() == 0 && catalogs.size() == 0) { 776 try { 779 parseCatalogFile(catfile); 780 } catch (CatalogException ce) { 781 System.out.println("FIXME: " + ce.toString()); 782 } 783 } else { 784 catalogs.addElement(catfile); 787 } 788 789 if (!localCatalogFiles.isEmpty()) { 790 Vector newQueue = new Vector (); 793 Enumeration q = localCatalogFiles.elements(); 794 while (q.hasMoreElements()) { 795 newQueue.addElement(q.nextElement()); 796 } 797 798 for (int curCat = 0; curCat < catalogFiles.size(); curCat++) { 800 catfile = (String ) catalogFiles.elementAt(curCat); 801 newQueue.addElement(catfile); 802 } 803 804 catalogFiles = newQueue; 805 localCatalogFiles.clear(); 806 } 807 808 if (!localDelegate.isEmpty()) { 809 Enumeration e = localDelegate.elements(); 810 while (e.hasMoreElements()) { 811 catalogEntries.addElement(e.nextElement()); 812 } 813 localDelegate.clear(); 814 } 815 } 816 817 catalogFiles.clear(); 819 } 820 821 830 protected synchronized void parseCatalogFile(String fileName) 831 throws MalformedURLException , IOException , CatalogException { 832 833 CatalogEntry entry; 834 835 try { 839 String userdir = fixSlashes(System.getProperty("user.dir")); 841 catalogCwd = new URL ("file:" + userdir + "/basename"); 842 } catch (MalformedURLException e) { 843 String userdir = fixSlashes(System.getProperty("user.dir")); 844 catalogManager.debug.message(1, "Malformed URL on cwd", userdir); 845 catalogCwd = null; 846 } 847 848 try { 850 base = new URL (catalogCwd, fixSlashes(fileName)); 851 } catch (MalformedURLException e) { 852 try { 853 base = new URL ("file:" + fixSlashes(fileName)); 854 } catch (MalformedURLException e2) { 855 catalogManager.debug.message(1, "Malformed URL on catalog filename", 856 fixSlashes(fileName)); 857 base = null; 858 } 859 } 860 861 catalogManager.debug.message(2, "Loading catalog", fileName); 862 catalogManager.debug.message(4, "Default BASE", base.toString()); 863 864 fileName = base.toString(); 865 866 DataInputStream inStream = null; 867 boolean parsed = false; 868 boolean notFound = false; 869 870 for (int count = 0; !parsed && count < readerArr.size(); count++) { 871 CatalogReader reader = (CatalogReader) readerArr.get(count); 872 873 try { 874 notFound = false; 875 inStream = new DataInputStream (base.openStream()); 876 } catch (FileNotFoundException fnfe) { 877 notFound = true; 879 break; 880 } 881 882 try { 883 reader.readCatalog(this, inStream); 884 parsed = true; 885 } catch (CatalogException ce) { 886 if (ce.getExceptionType() == CatalogException.PARSE_FAILED) { 887 break; 889 } else { 890 } 892 } 893 894 try { 895 inStream.close(); 896 } catch (IOException e) { 897 } 899 } 900 901 if (!parsed) { 902 if (notFound) { 903 catalogManager.debug.message(3, "Catalog does not exist", fileName); 904 } else { 905 catalogManager.debug.message(1, "Failed to parse catalog", fileName); 906 } 907 } 908 } 909 910 920 public void addEntry(CatalogEntry entry) { 921 int type = entry.getEntryType(); 922 923 if (type == BASE) { 924 String value = entry.getEntryArg(0); 925 URL newbase = null; 926 927 catalogManager.debug.message(5, "BASE CUR", base.toString()); 928 catalogManager.debug.message(4, "BASE STR", value); 929 930 try { 931 value = fixSlashes(value); 932 newbase = new URL (base, value); 933 } catch (MalformedURLException e) { 934 try { 935 newbase = new URL ("file:" + value); 936 } catch (MalformedURLException e2) { 937 catalogManager.debug.message(1, "Malformed URL on base", value); 938 newbase = null; 939 } 940 } 941 942 if (newbase != null) { 943 base = newbase; 944 } 945 946 catalogManager.debug.message(5, "BASE NEW", base.toString()); 947 } else if (type == CATALOG) { 948 String fsi = makeAbsolute(entry.getEntryArg(0)); 949 950 catalogManager.debug.message(4, "CATALOG", fsi); 951 952 localCatalogFiles.addElement(fsi); 953 } else if (type == PUBLIC) { 954 String publicid = PublicId.normalize(entry.getEntryArg(0)); 955 String systemid = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 956 957 entry.setEntryArg(0, publicid); 958 entry.setEntryArg(1, systemid); 959 960 catalogManager.debug.message(4, "PUBLIC", publicid, systemid); 961 962 catalogEntries.addElement(entry); 963 } else if (type == SYSTEM) { 964 String systemid = normalizeURI(entry.getEntryArg(0)); 965 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 966 967 entry.setEntryArg(1, fsi); 968 969 catalogManager.debug.message(4, "SYSTEM", systemid, fsi); 970 971 catalogEntries.addElement(entry); 972 } else if (type == URI) { 973 String uri = normalizeURI(entry.getEntryArg(0)); 974 String altURI = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 975 976 entry.setEntryArg(1, altURI); 977 978 catalogManager.debug.message(4, "URI", uri, altURI); 979 980 catalogEntries.addElement(entry); 981 } else if (type == DOCUMENT) { 982 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0))); 983 entry.setEntryArg(0, fsi); 984 985 catalogManager.debug.message(4, "DOCUMENT", fsi); 986 987 catalogEntries.addElement(entry); 988 } else if (type == OVERRIDE) { 989 catalogManager.debug.message(4, "OVERRIDE", entry.getEntryArg(0)); 990 991 catalogEntries.addElement(entry); 992 } else if (type == SGMLDECL) { 993 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0))); 995 entry.setEntryArg(0, fsi); 996 997 catalogManager.debug.message(4, "SGMLDECL", fsi); 998 999 catalogEntries.addElement(entry); 1000 } else if (type == DELEGATE_PUBLIC) { 1001 String ppi = PublicId.normalize(entry.getEntryArg(0)); 1002 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1003 1004 entry.setEntryArg(0, ppi); 1005 entry.setEntryArg(1, fsi); 1006 1007 catalogManager.debug.message(4, "DELEGATE_PUBLIC", ppi, fsi); 1008 1009 addDelegate(entry); 1010 } else if (type == DELEGATE_SYSTEM) { 1011 String psi = normalizeURI(entry.getEntryArg(0)); 1012 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1013 1014 entry.setEntryArg(0, psi); 1015 entry.setEntryArg(1, fsi); 1016 1017 catalogManager.debug.message(4, "DELEGATE_SYSTEM", psi, fsi); 1018 1019 addDelegate(entry); 1020 } else if (type == DELEGATE_URI) { 1021 String pui = normalizeURI(entry.getEntryArg(0)); 1022 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1023 1024 entry.setEntryArg(0, pui); 1025 entry.setEntryArg(1, fsi); 1026 1027 catalogManager.debug.message(4, "DELEGATE_URI", pui, fsi); 1028 1029 addDelegate(entry); 1030 } else if (type == REWRITE_SYSTEM) { 1031 String psi = normalizeURI(entry.getEntryArg(0)); 1032 String rpx = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1033 1034 entry.setEntryArg(0, psi); 1035 entry.setEntryArg(1, rpx); 1036 1037 catalogManager.debug.message(4, "REWRITE_SYSTEM", psi, rpx); 1038 1039 catalogEntries.addElement(entry); 1040 } else if (type == REWRITE_URI) { 1041 String pui = normalizeURI(entry.getEntryArg(0)); 1042 String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1043 1044 entry.setEntryArg(0, pui); 1045 entry.setEntryArg(1, upx); 1046 1047 catalogManager.debug.message(4, "REWRITE_URI", pui, upx); 1048 1049 catalogEntries.addElement(entry); 1050 } else if (type == DOCTYPE) { 1051 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1052 entry.setEntryArg(1, fsi); 1053 1054 catalogManager.debug.message(4, "DOCTYPE", entry.getEntryArg(0), fsi); 1055 1056 catalogEntries.addElement(entry); 1057 } else if (type == DTDDECL) { 1058 String fpi = PublicId.normalize(entry.getEntryArg(0)); 1060 entry.setEntryArg(0, fpi); 1061 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1062 entry.setEntryArg(1, fsi); 1063 1064 catalogManager.debug.message(4, "DTDDECL", fpi, fsi); 1065 1066 catalogEntries.addElement(entry); 1067 } else if (type == ENTITY) { 1068 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1069 entry.setEntryArg(1, fsi); 1070 1071 catalogManager.debug.message(4, "ENTITY", entry.getEntryArg(0), fsi); 1072 1073 catalogEntries.addElement(entry); 1074 } else if (type == LINKTYPE) { 1075 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1077 entry.setEntryArg(1, fsi); 1078 1079 catalogManager.debug.message(4, "LINKTYPE", entry.getEntryArg(0), fsi); 1080 1081 catalogEntries.addElement(entry); 1082 } else if (type == NOTATION) { 1083 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1084 entry.setEntryArg(1, fsi); 1085 1086 catalogManager.debug.message(4, "NOTATION", entry.getEntryArg(0), fsi); 1087 1088 catalogEntries.addElement(entry); 1089 } else { 1090 catalogEntries.addElement(entry); 1091 } 1092 } 1093 1094 1100 public void unknownEntry(Vector strings) { 1101 if (strings != null && strings.size() > 0) { 1102 String keyword = (String ) strings.elementAt(0); 1103 catalogManager.debug.message(2, "Unrecognized token parsing catalog", keyword); 1104 } 1105 } 1106 1107 1136 public void parseAllCatalogs() 1137 throws MalformedURLException , IOException { 1138 1139 for (int catPos = 0; catPos < catalogs.size(); catPos++) { 1141 Catalog c = null; 1142 1143 try { 1144 c = (Catalog) catalogs.elementAt(catPos); 1145 } catch (ClassCastException e) { 1146 String catfile = (String ) catalogs.elementAt(catPos); 1147 c = newCatalog(); 1148 1149 c.parseCatalog(catfile); 1150 catalogs.setElementAt(c, catPos); 1151 c.parseAllCatalogs(); 1152 } 1153 } 1154 1155 Enumeration enumt = catalogEntries.elements(); 1157 while (enumt.hasMoreElements()) { 1158 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1159 if (e.getEntryType() == DELEGATE_PUBLIC 1160 || e.getEntryType() == DELEGATE_SYSTEM 1161 || e.getEntryType() == DELEGATE_URI) { 1162 Catalog dcat = newCatalog(); 1163 dcat.parseCatalog(e.getEntryArg(1)); 1164 } 1165 } 1166 } 1167 1168 1169 1185 public String resolveDoctype(String entityName, 1186 String publicId, 1187 String systemId) 1188 throws MalformedURLException , IOException { 1189 String resolved = null; 1190 1191 catalogManager.debug.message(3, "resolveDoctype(" 1192 +entityName+","+publicId+","+systemId+")"); 1193 1194 systemId = normalizeURI(systemId); 1195 1196 if (publicId != null && publicId.startsWith("urn:publicid:")) { 1197 publicId = PublicId.decodeURN(publicId); 1198 } 1199 1200 if (systemId != null && systemId.startsWith("urn:publicid:")) { 1201 systemId = PublicId.decodeURN(systemId); 1202 if (publicId != null && !publicId.equals(systemId)) { 1203 catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier"); 1204 systemId = null; 1205 } else { 1206 publicId = systemId; 1207 systemId = null; 1208 } 1209 } 1210 1211 if (systemId != null) { 1212 resolved = resolveLocalSystem(systemId); 1214 if (resolved != null) { 1215 return resolved; 1216 } 1217 } 1218 1219 if (publicId != null) { 1220 resolved = resolveLocalPublic(DOCTYPE, 1222 entityName, 1223 publicId, 1224 systemId); 1225 if (resolved != null) { 1226 return resolved; 1227 } 1228 } 1229 1230 boolean over = default_override; 1232 Enumeration enumt = catalogEntries.elements(); 1233 while (enumt.hasMoreElements()) { 1234 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1235 if (e.getEntryType() == OVERRIDE) { 1236 over = e.getEntryArg(0).equalsIgnoreCase("YES"); 1237 continue; 1238 } 1239 1240 if (e.getEntryType() == DOCTYPE 1241 && e.getEntryArg(0).equals(entityName)) { 1242 if (over || systemId == null) { 1243 return e.getEntryArg(1); 1244 } 1245 } 1246 } 1247 1248 return resolveSubordinateCatalogs(DOCTYPE, 1250 entityName, 1251 publicId, 1252 systemId); 1253 } 1254 1255 1264 public String resolveDocument() 1265 throws MalformedURLException , IOException { 1266 1268 catalogManager.debug.message(3, "resolveDocument"); 1269 1270 Enumeration enumt = catalogEntries.elements(); 1271 while (enumt.hasMoreElements()) { 1272 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1273 if (e.getEntryType() == DOCUMENT) { 1274 return e.getEntryArg(1); } 1276 } 1277 1278 return resolveSubordinateCatalogs(DOCUMENT, 1279 null, null, null); 1280 } 1281 1282 1298 public String resolveEntity(String entityName, 1299 String publicId, 1300 String systemId) 1301 throws MalformedURLException , IOException { 1302 String resolved = null; 1303 1304 catalogManager.debug.message(3, "resolveEntity(" 1305 +entityName+","+publicId+","+systemId+")"); 1306 1307 systemId = normalizeURI(systemId); 1308 1309 if (publicId != null && publicId.startsWith("urn:publicid:")) { 1310 publicId = PublicId.decodeURN(publicId); 1311 } 1312 1313 if (systemId != null && systemId.startsWith("urn:publicid:")) { 1314 systemId = PublicId.decodeURN(systemId); 1315 if (publicId != null && !publicId.equals(systemId)) { 1316 catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier"); 1317 systemId = null; 1318 } else { 1319 publicId = systemId; 1320 systemId = null; 1321 } 1322 } 1323 1324 if (systemId != null) { 1325 resolved = resolveLocalSystem(systemId); 1327 if (resolved != null) { 1328 return resolved; 1329 } 1330 } 1331 1332 if (publicId != null) { 1333 resolved = resolveLocalPublic(ENTITY, 1335 entityName, 1336 publicId, 1337 systemId); 1338 if (resolved != null) { 1339 return resolved; 1340 } 1341 } 1342 1343 boolean over = default_override; 1345 Enumeration enumt = catalogEntries.elements(); 1346 while (enumt.hasMoreElements()) { 1347 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1348 if (e.getEntryType() == OVERRIDE) { 1349 over = e.getEntryArg(0).equalsIgnoreCase("YES"); 1350 continue; 1351 } 1352 1353 if (e.getEntryType() == ENTITY 1354 && e.getEntryArg(0).equals(entityName)) { 1355 if (over || systemId == null) { 1356 return e.getEntryArg(1); 1357 } 1358 } 1359 } 1360 1361 return resolveSubordinateCatalogs(ENTITY, 1363 entityName, 1364 publicId, 1365 systemId); 1366 } 1367 1368 1384 public String resolveNotation(String notationName, 1385 String publicId, 1386 String systemId) 1387 throws MalformedURLException , IOException { 1388 String resolved = null; 1389 1390 catalogManager.debug.message(3, "resolveNotation(" 1391 +notationName+","+publicId+","+systemId+")"); 1392 1393 systemId = normalizeURI(systemId); 1394 1395 if (publicId != null && publicId.startsWith("urn:publicid:")) { 1396 publicId = PublicId.decodeURN(publicId); 1397 } 1398 1399 if (systemId != null && systemId.startsWith("urn:publicid:")) { 1400 systemId = PublicId.decodeURN(systemId); 1401 if (publicId != null && !publicId.equals(systemId)) { 1402 catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier"); 1403 systemId = null; 1404 } else { 1405 publicId = systemId; 1406 systemId = null; 1407 } 1408 } 1409 1410 if (systemId != null) { 1411 resolved = resolveLocalSystem(systemId); 1413 if (resolved != null) { 1414 return resolved; 1415 } 1416 } 1417 1418 if (publicId != null) { 1419 resolved = resolveLocalPublic(NOTATION, 1421 notationName, 1422 publicId, 1423 systemId); 1424 if (resolved != null) { 1425 return resolved; 1426 } 1427 } 1428 1429 boolean over = default_override; 1431 Enumeration enumt = catalogEntries.elements(); 1432 while (enumt.hasMoreElements()) { 1433 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1434 if (e.getEntryType() == OVERRIDE) { 1435 over = e.getEntryArg(0).equalsIgnoreCase("YES"); 1436 continue; 1437 } 1438 1439 if (e.getEntryType() == NOTATION 1440 && e.getEntryArg(0).equals(notationName)) { 1441 if (over || systemId == null) { 1442 return e.getEntryArg(1); 1443 } 1444 } 1445 } 1446 1447 return resolveSubordinateCatalogs(NOTATION, 1449 notationName, 1450 publicId, 1451 systemId); 1452 } 1453 1454 1477 public String resolvePublic(String publicId, String systemId) 1478 throws MalformedURLException , IOException { 1479 1480 catalogManager.debug.message(3, "resolvePublic("+publicId+","+systemId+")"); 1481 1482 systemId = normalizeURI(systemId); 1483 1484 if (publicId != null && publicId.startsWith("urn:publicid:")) { 1485 publicId = PublicId.decodeURN(publicId); 1486 } 1487 1488 if (systemId != null && systemId.startsWith("urn:publicid:")) { 1489 systemId = PublicId.decodeURN(systemId); 1490 if (publicId != null && !publicId.equals(systemId)) { 1491 catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier"); 1492 systemId = null; 1493 } else { 1494 publicId = systemId; 1495 systemId = null; 1496 } 1497 } 1498 1499 if (systemId != null) { 1501 String resolved = resolveLocalSystem(systemId); 1502 if (resolved != null) { 1503 return resolved; 1504 } 1505 } 1506 1507 String resolved = resolveLocalPublic(PUBLIC, 1509 null, 1510 publicId, 1511 systemId); 1512 if (resolved != null) { 1513 return resolved; 1514 } 1515 1516 return resolveSubordinateCatalogs(PUBLIC, 1518 null, 1519 publicId, 1520 systemId); 1521 } 1522 1523 1571 protected synchronized String resolveLocalPublic(int entityType, 1572 String entityName, 1573 String publicId, 1574 String systemId) 1575 throws MalformedURLException , IOException { 1576 1577 publicId = PublicId.normalize(publicId); 1579 1580 if (systemId != null) { 1582 String resolved = resolveLocalSystem(systemId); 1583 if (resolved != null) { 1584 return resolved; 1585 } 1586 } 1587 1588 boolean over = default_override; 1590 Enumeration enumt = catalogEntries.elements(); 1591 while (enumt.hasMoreElements()) { 1592 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1593 if (e.getEntryType() == OVERRIDE) { 1594 over = e.getEntryArg(0).equalsIgnoreCase("YES"); 1595 continue; 1596 } 1597 1598 if (e.getEntryType() == PUBLIC 1599 && e.getEntryArg(0).equals(publicId)) { 1600 if (over || systemId == null) { 1601 return e.getEntryArg(1); 1602 } 1603 } 1604 } 1605 1606 over = default_override; 1608 enumt = catalogEntries.elements(); 1609 Vector delCats = new Vector (); 1610 while (enumt.hasMoreElements()) { 1611 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1612 if (e.getEntryType() == OVERRIDE) { 1613 over = e.getEntryArg(0).equalsIgnoreCase("YES"); 1614 continue; 1615 } 1616 1617 if (e.getEntryType() == DELEGATE_PUBLIC 1618 && (over || systemId == null)) { 1619 String p = (String ) e.getEntryArg(0); 1620 if (p.length() <= publicId.length() 1621 && p.equals(publicId.substring(0, p.length()))) { 1622 1624 delCats.addElement(e.getEntryArg(1)); 1625 } 1626 } 1627 } 1628 1629 if (delCats.size() > 0) { 1630 Enumeration enumCats = delCats.elements(); 1631 1632 if (catalogManager.debug.getDebug() > 1) { 1633 catalogManager.debug.message(2, "Switching to delegated catalog(s):"); 1634 while (enumCats.hasMoreElements()) { 1635 String delegatedCatalog = (String ) enumCats.nextElement(); 1636 catalogManager.debug.message(2, "\t" + delegatedCatalog); 1637 } 1638 } 1639 1640 Catalog dcat = newCatalog(); 1641 1642 enumCats = delCats.elements(); 1643 while (enumCats.hasMoreElements()) { 1644 String delegatedCatalog = (String ) enumCats.nextElement(); 1645 dcat.parseCatalog(delegatedCatalog); 1646 } 1647 1648 return dcat.resolvePublic(publicId, null); 1649 } 1650 1651 return null; 1653 } 1654 1655 1673 public String resolveSystem(String systemId) 1674 throws MalformedURLException , IOException { 1675 1676 catalogManager.debug.message(3, "resolveSystem("+systemId+")"); 1677 1678 systemId = normalizeURI(systemId); 1679 1680 if (systemId != null && systemId.startsWith("urn:publicid:")) { 1681 systemId = PublicId.decodeURN(systemId); 1682 return resolvePublic(systemId, null); 1683 } 1684 1685 if (systemId != null) { 1687 String resolved = resolveLocalSystem(systemId); 1688 if (resolved != null) { 1689 return resolved; 1690 } 1691 } 1692 1693 return resolveSubordinateCatalogs(SYSTEM, 1695 null, 1696 null, 1697 systemId); 1698 } 1699 1700 1711 protected String resolveLocalSystem(String systemId) 1712 throws MalformedURLException , IOException { 1713 1714 String osname = System.getProperty("os.name"); 1715 boolean windows = (osname.indexOf("Windows") >= 0); 1716 Enumeration enumt = catalogEntries.elements(); 1717 while (enumt.hasMoreElements()) { 1718 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1719 if (e.getEntryType() == SYSTEM 1720 && (e.getEntryArg(0).equals(systemId) 1721 || (windows 1722 && e.getEntryArg(0).equalsIgnoreCase(systemId)))) { 1723 return e.getEntryArg(1); 1724 } 1725 } 1726 1727 enumt = catalogEntries.elements(); 1729 String startString = null; 1730 String prefix = null; 1731 while (enumt.hasMoreElements()) { 1732 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1733 1734 if (e.getEntryType() == REWRITE_SYSTEM) { 1735 String p = (String ) e.getEntryArg(0); 1736 if (p.length() <= systemId.length() 1737 && p.equals(systemId.substring(0, p.length()))) { 1738 if (startString == null 1740 || p.length() > startString.length()) { 1741 startString = p; 1742 prefix = e.getEntryArg(1); 1743 } 1744 } 1745 } 1746 1747 if (prefix != null) { 1748 return prefix + systemId.substring(startString.length()); 1750 } 1751 } 1752 1753 enumt = catalogEntries.elements(); 1755 Vector delCats = new Vector (); 1756 while (enumt.hasMoreElements()) { 1757 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1758 1759 if (e.getEntryType() == DELEGATE_SYSTEM) { 1760 String p = (String ) e.getEntryArg(0); 1761 if (p.length() <= systemId.length() 1762 && p.equals(systemId.substring(0, p.length()))) { 1763 1765 delCats.addElement(e.getEntryArg(1)); 1766 } 1767 } 1768 } 1769 1770 if (delCats.size() > 0) { 1771 Enumeration enumCats = delCats.elements(); 1772 1773 if (catalogManager.debug.getDebug() > 1) { 1774 catalogManager.debug.message(2, "Switching to delegated catalog(s):"); 1775 while (enumCats.hasMoreElements()) { 1776 String delegatedCatalog = (String ) enumCats.nextElement(); 1777 catalogManager.debug.message(2, "\t" + delegatedCatalog); 1778 } 1779 } 1780 1781 Catalog dcat = newCatalog(); 1782 1783 enumCats = delCats.elements(); 1784 while (enumCats.hasMoreElements()) { 1785 String delegatedCatalog = (String ) enumCats.nextElement(); 1786 dcat.parseCatalog(delegatedCatalog); 1787 } 1788 1789 return dcat.resolveSystem(systemId); 1790 } 1791 1792 return null; 1793 } 1794 1795 1811 public String resolveURI(String uri) 1812 throws MalformedURLException , IOException { 1813 1814 catalogManager.debug.message(3, "resolveURI("+uri+")"); 1815 1816 uri = normalizeURI(uri); 1817 1818 if (uri != null && uri.startsWith("urn:publicid:")) { 1819 uri = PublicId.decodeURN(uri); 1820 return resolvePublic(uri, null); 1821 } 1822 1823 if (uri != null) { 1825 String resolved = resolveLocalURI(uri); 1826 if (resolved != null) { 1827 return resolved; 1828 } 1829 } 1830 1831 return resolveSubordinateCatalogs(URI, 1833 null, 1834 null, 1835 uri); 1836 } 1837 1838 1848 protected String resolveLocalURI(String uri) 1849 throws MalformedURLException , IOException { 1850 Enumeration enumt = catalogEntries.elements(); 1851 while (enumt.hasMoreElements()) { 1852 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1853 if (e.getEntryType() == URI 1854 && (e.getEntryArg(0).equals(uri))) { 1855 return e.getEntryArg(1); 1856 } 1857 } 1858 1859 enumt = catalogEntries.elements(); 1861 String startString = null; 1862 String prefix = null; 1863 while (enumt.hasMoreElements()) { 1864 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1865 1866 if (e.getEntryType() == REWRITE_URI) { 1867 String p = (String ) e.getEntryArg(0); 1868 if (p.length() <= uri.length() 1869 && p.equals(uri.substring(0, p.length()))) { 1870 if (startString == null 1872 || p.length() > startString.length()) { 1873 startString = p; 1874 prefix = e.getEntryArg(1); 1875 } 1876 } 1877 } 1878 1879 if (prefix != null) { 1880 return prefix + uri.substring(startString.length()); 1882 } 1883 } 1884 1885 enumt = catalogEntries.elements(); 1887 Vector delCats = new Vector (); 1888 while (enumt.hasMoreElements()) { 1889 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 1890 1891 if (e.getEntryType() == DELEGATE_URI) { 1892 String p = (String ) e.getEntryArg(0); 1893 if (p.length() <= uri.length() 1894 && p.equals(uri.substring(0, p.length()))) { 1895 1897 delCats.addElement(e.getEntryArg(1)); 1898 } 1899 } 1900 } 1901 1902 if (delCats.size() > 0) { 1903 Enumeration enumCats = delCats.elements(); 1904 1905 if (catalogManager.debug.getDebug() > 1) { 1906 catalogManager.debug.message(2, "Switching to delegated catalog(s):"); 1907 while (enumCats.hasMoreElements()) { 1908 String delegatedCatalog = (String ) enumCats.nextElement(); 1909 catalogManager.debug.message(2, "\t" + delegatedCatalog); 1910 } 1911 } 1912 1913 Catalog dcat = newCatalog(); 1914 1915 enumCats = delCats.elements(); 1916 while (enumCats.hasMoreElements()) { 1917 String delegatedCatalog = (String ) enumCats.nextElement(); 1918 dcat.parseCatalog(delegatedCatalog); 1919 } 1920 1921 return dcat.resolveURI(uri); 1922 } 1923 1924 return null; 1925 } 1926 1927 1955 protected synchronized String resolveSubordinateCatalogs(int entityType, 1956 String entityName, 1957 String publicId, 1958 String systemId) 1959 throws MalformedURLException , IOException { 1960 1961 for (int catPos = 0; catPos < catalogs.size(); catPos++) { 1962 Catalog c = null; 1963 1964 try { 1965 c = (Catalog) catalogs.elementAt(catPos); 1966 } catch (ClassCastException e) { 1967 String catfile = (String ) catalogs.elementAt(catPos); 1968 c = newCatalog(); 1969 1970 try { 1971 c.parseCatalog(catfile); 1972 } catch (MalformedURLException mue) { 1973 catalogManager.debug.message(1, "Malformed Catalog URL", catfile); 1974 } catch (FileNotFoundException fnfe) { 1975 catalogManager.debug.message(1, "Failed to load catalog, file not found", 1976 catfile); 1977 } catch (IOException ioe) { 1978 catalogManager.debug.message(1, "Failed to load catalog, I/O error", catfile); 1979 } 1980 1981 catalogs.setElementAt(c, catPos); 1982 } 1983 1984 String resolved = null; 1985 1986 if (entityType == DOCTYPE) { 1988 resolved = c.resolveDoctype(entityName, 1989 publicId, 1990 systemId); 1991 } else if (entityType == DOCUMENT) { 1992 resolved = c.resolveDocument(); 1993 } else if (entityType == ENTITY) { 1994 resolved = c.resolveEntity(entityName, 1995 publicId, 1996 systemId); 1997 } else if (entityType == NOTATION) { 1998 resolved = c.resolveNotation(entityName, 1999 publicId, 2000 systemId); 2001 } else if (entityType == PUBLIC) { 2002 resolved = c.resolvePublic(publicId, systemId); 2003 } else if (entityType == SYSTEM) { 2004 resolved = c.resolveSystem(systemId); 2005 } else if (entityType == URI) { 2006 resolved = c.resolveURI(systemId); 2007 } 2008 2009 if (resolved != null) { 2010 return resolved; 2011 } 2012 } 2013 2014 return null; 2015 } 2016 2017 2019 2027 protected String fixSlashes (String sysid) { 2028 return sysid.replace('\\', '/'); 2029 } 2030 2031 2039 protected String makeAbsolute(String sysid) { 2040 URL local = null; 2041 2042 sysid = fixSlashes(sysid); 2043 2044 try { 2045 local = new URL (base, sysid); 2046 } catch (MalformedURLException e) { 2047 catalogManager.debug.message(1, "Malformed URL on system identifier", sysid); 2048 } 2049 2050 if (local != null) { 2051 return local.toString(); 2052 } else { 2053 return sysid; 2054 } 2055 } 2056 2057 2063 protected String normalizeURI(String uriref) { 2064 String newRef = ""; 2065 byte[] bytes; 2066 2067 if (uriref == null) { 2068 return null; 2069 } 2070 2071 try { 2072 bytes = uriref.getBytes("UTF-8"); 2073 } catch (UnsupportedEncodingException uee) { 2074 catalogManager.debug.message(1, "UTF-8 is an unsupported encoding!?"); 2076 return uriref; 2077 } 2078 2079 for (int count = 0; count < bytes.length; count++) { 2080 int ch = bytes[count] & 0xFF; 2081 2082 if ((ch <= 0x20) || (ch > 0x7F) || (ch == 0x22) || (ch == 0x3C) || (ch == 0x3E) || (ch == 0x5C) || (ch == 0x5E) || (ch == 0x60) || (ch == 0x7B) || (ch == 0x7C) || (ch == 0x7D) || (ch == 0x7F)) { 2094 newRef += encodedByte(ch); 2095 } else { 2096 newRef += (char) bytes[count]; 2097 } 2098 } 2099 2100 return newRef; 2101 } 2102 2103 2110 protected String encodedByte (int b) { 2111 String hex = Integer.toHexString(b).toUpperCase(); 2112 if (hex.length() < 2) { 2113 return "%0" + hex; 2114 } else { 2115 return "%" + hex; 2116 } 2117 } 2118 2119 2121 2130 protected void addDelegate(CatalogEntry entry) { 2131 int pos = 0; 2132 String partial = entry.getEntryArg(0); 2133 2134 Enumeration local = localDelegate.elements(); 2135 while (local.hasMoreElements()) { 2136 CatalogEntry dpe = (CatalogEntry) local.nextElement(); 2137 String dp = dpe.getEntryArg(0); 2138 if (dp.equals(partial)) { 2139 return; 2141 } 2142 if (dp.length() > partial.length()) { 2143 pos++; 2144 } 2145 if (dp.length() < partial.length()) { 2146 break; 2147 } 2148 } 2149 2150 if (localDelegate.size() == 0) { 2152 localDelegate.addElement(entry); 2153 } else { 2154 localDelegate.insertElementAt(entry, pos); 2155 } 2156 } 2157} 2158 2159 | Popular Tags |