1 3 19 20 package com.sun.org.apache.xml.internal.resolver; 21 22 import java.io.IOException ; 23 import java.io.FileNotFoundException ; 24 import java.io.InputStream ; 25 import java.io.UnsupportedEncodingException ; 26 import java.io.DataInputStream ; 27 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.Vector ; 31 32 import java.net.URL ; 33 import java.net.MalformedURLException ; 34 35 import javax.xml.parsers.SAXParserFactory ; 36 37 import com.sun.org.apache.xml.internal.resolver.CatalogManager; 38 import com.sun.org.apache.xml.internal.resolver.helpers.PublicId; 39 import com.sun.org.apache.xml.internal.resolver.readers.CatalogReader; 40 import com.sun.org.apache.xml.internal.resolver.readers.SAXCatalogReader; 41 import com.sun.org.apache.xml.internal.resolver.readers.TR9401CatalogReader; 42 import com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader; 43 import com.sun.org.apache.xml.internal.resolver.helpers.FileURL; 44 45 192 public class Catalog { 193 194 public static final int BASE = CatalogEntry.addEntryType("BASE", 1); 195 196 197 public static final int CATALOG = CatalogEntry.addEntryType("CATALOG", 1); 198 199 200 public static final int DOCUMENT = CatalogEntry.addEntryType("DOCUMENT", 1); 201 202 203 public static final int OVERRIDE = CatalogEntry.addEntryType("OVERRIDE", 1); 204 205 206 public static final int SGMLDECL = CatalogEntry.addEntryType("SGMLDECL", 1); 207 208 209 public static final int DELEGATE_PUBLIC = CatalogEntry.addEntryType("DELEGATE_PUBLIC", 2); 210 211 212 public static final int DELEGATE_SYSTEM = CatalogEntry.addEntryType("DELEGATE_SYSTEM", 2); 213 214 215 public static final int DELEGATE_URI = CatalogEntry.addEntryType("DELEGATE_URI", 2); 216 217 218 public static final int DOCTYPE = CatalogEntry.addEntryType("DOCTYPE", 2); 219 220 221 public static final int DTDDECL = CatalogEntry.addEntryType("DTDDECL", 2); 222 223 224 public static final int ENTITY = CatalogEntry.addEntryType("ENTITY", 2); 225 226 227 public static final int LINKTYPE = CatalogEntry.addEntryType("LINKTYPE", 2); 228 229 230 public static final int NOTATION = CatalogEntry.addEntryType("NOTATION", 2); 231 232 233 public static final int PUBLIC = CatalogEntry.addEntryType("PUBLIC", 2); 234 235 236 public static final int SYSTEM = CatalogEntry.addEntryType("SYSTEM", 2); 237 238 239 public static final int URI = CatalogEntry.addEntryType("URI", 2); 240 241 242 public static final int REWRITE_SYSTEM = CatalogEntry.addEntryType("REWRITE_SYSTEM", 2); 243 244 245 public static final int REWRITE_URI = CatalogEntry.addEntryType("REWRITE_URI", 2); 246 247 public static final int SYSTEM_SUFFIX = CatalogEntry.addEntryType("SYSTEM_SUFFIX", 2); 248 249 public static final int URI_SUFFIX = CatalogEntry.addEntryType("URI_SUFFIX", 2); 250 251 255 protected URL base; 256 257 258 protected URL catalogCwd; 259 260 261 protected Vector catalogEntries = new Vector (); 262 263 264 protected boolean default_override = true; 265 266 267 protected CatalogManager catalogManager = CatalogManager.getStaticManager(); 268 269 280 protected Vector catalogFiles = new Vector (); 281 282 299 protected Vector localCatalogFiles = new Vector (); 300 301 318 protected Vector catalogs = new Vector (); 319 320 335 protected Vector localDelegate = new Vector (); 336 337 344 protected Hashtable readerMap = new Hashtable (); 345 346 354 protected Vector readerArr = new Vector (); 355 356 363 public Catalog() { 364 } 366 367 374 public Catalog(CatalogManager manager) { 375 catalogManager = manager; 376 } 377 378 382 public CatalogManager getCatalogManager() { 383 return catalogManager; 384 } 385 386 390 public void setCatalogManager(CatalogManager manager) { 391 catalogManager = manager; 392 } 393 394 397 public void setupReaders() { 398 SAXParserFactory spf = SAXParserFactory.newInstance(); 399 spf.setNamespaceAware(true); 400 spf.setValidating(false); 401 402 SAXCatalogReader saxReader = new SAXCatalogReader(spf); 403 404 saxReader.setCatalogParser(null, "XMLCatalog", 405 "com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader"); 406 407 saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, 408 "catalog", 409 "com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader"); 410 411 addReader("application/xml", saxReader); 412 413 TR9401CatalogReader textReader = new TR9401CatalogReader(); 414 addReader("text/plain", textReader); 415 } 416 417 437 public void addReader(String mimeType, CatalogReader reader) { 438 if (readerMap.containsKey(mimeType)) { 439 Integer pos = (Integer ) readerMap.get(mimeType); 440 readerArr.set(pos.intValue(), reader); 441 } else { 442 readerArr.add(reader); 443 Integer pos = new Integer (readerArr.size()-1); 444 readerMap.put(mimeType, pos); 445 } 446 } 447 448 457 protected void copyReaders(Catalog newCatalog) { 458 Vector mapArr = new Vector (readerMap.size()); 460 461 for (int count = 0; count < readerMap.size(); count++) { 463 mapArr.add(null); 464 } 465 466 Enumeration en = readerMap.keys(); 467 while (en.hasMoreElements()) { 468 String mimeType = (String ) en.nextElement(); 469 Integer pos = (Integer ) readerMap.get(mimeType); 470 mapArr.set(pos.intValue(), mimeType); 471 } 472 473 for (int count = 0; count < mapArr.size(); count++) { 474 String mimeType = (String ) mapArr.get(count); 475 Integer pos = (Integer ) readerMap.get(mimeType); 476 newCatalog.addReader(mimeType, 477 (CatalogReader) 478 readerArr.get(pos.intValue())); 479 } 480 } 481 482 494 protected Catalog newCatalog() { 495 String catalogClass = this.getClass().getName(); 496 497 try { 498 Catalog c = (Catalog) (Class.forName(catalogClass).newInstance()); 499 c.setCatalogManager(catalogManager); 500 copyReaders(c); 501 return c; 502 } catch (ClassNotFoundException cnfe) { 503 catalogManager.debug.message(1, "Class Not Found Exception: " + catalogClass); 504 } catch (IllegalAccessException iae) { 505 catalogManager.debug.message(1, "Illegal Access Exception: " + catalogClass); 506 } catch (InstantiationException ie) { 507 catalogManager.debug.message(1, "Instantiation Exception: " + catalogClass); 508 } catch (ClassCastException cce) { 509 catalogManager.debug.message(1, "Class Cast Exception: " + catalogClass); 510 } catch (Exception e) { 511 catalogManager.debug.message(1, "Other Exception: " + catalogClass); 512 } 513 514 Catalog c = new Catalog(); 515 c.setCatalogManager(catalogManager); 516 copyReaders(c); 517 return c; 518 } 519 520 523 public String getCurrentBase() { 524 return base.toString(); 525 } 526 527 534 public String getDefaultOverride() { 535 if (default_override) { 536 return "yes"; 537 } else { 538 return "no"; 539 } 540 } 541 542 553 public void loadSystemCatalogs() 554 throws MalformedURLException , IOException { 555 556 Vector catalogs = catalogManager.getCatalogFiles(); 557 if (catalogs != null) { 558 for (int count = 0; count < catalogs.size(); count++) { 559 catalogFiles.addElement(catalogs.elementAt(count)); 560 } 561 } 562 563 if (catalogFiles.size() > 0) { 564 String catfile = (String ) catalogFiles.lastElement(); 577 catalogFiles.removeElement(catfile); 578 parseCatalog(catfile); 579 } 580 } 581 582 591 public synchronized void parseCatalog(String fileName) 592 throws MalformedURLException , IOException { 593 594 default_override = catalogManager.getPreferPublic(); 595 catalogManager.debug.message(4, "Parse catalog: " + fileName); 596 597 catalogFiles.addElement(fileName); 601 602 parsePendingCatalogs(); 604 } 605 606 619 public synchronized void parseCatalog(String mimeType, InputStream is) 620 throws IOException , CatalogException { 621 622 default_override = catalogManager.getPreferPublic(); 623 catalogManager.debug.message(4, "Parse " + mimeType + " catalog on input stream"); 624 625 CatalogReader reader = null; 626 627 if (readerMap.containsKey(mimeType)) { 628 int arrayPos = ((Integer ) readerMap.get(mimeType)).intValue(); 629 reader = (CatalogReader) readerArr.get(arrayPos); 630 } 631 632 if (reader == null) { 633 String msg = "No CatalogReader for MIME type: " + mimeType; 634 catalogManager.debug.message(2, msg); 635 throw new CatalogException(CatalogException.UNPARSEABLE, msg); 636 } 637 638 reader.readCatalog(this, is); 639 640 parsePendingCatalogs(); 642 } 643 644 659 public synchronized void parseCatalog(URL aUrl) throws IOException { 660 catalogCwd = aUrl; 661 base = aUrl; 662 663 default_override = catalogManager.getPreferPublic(); 664 catalogManager.debug.message(4, "Parse catalog: " + aUrl.toString()); 665 666 DataInputStream inStream = null; 667 boolean parsed = false; 668 669 for (int count = 0; !parsed && count < readerArr.size(); count++) { 670 CatalogReader reader = (CatalogReader) readerArr.get(count); 671 672 try { 673 inStream = new DataInputStream (aUrl.openStream()); 674 } catch (FileNotFoundException fnfe) { 675 break; 677 } 678 679 try { 680 reader.readCatalog(this, inStream); 681 parsed=true; 682 } catch (CatalogException ce) { 683 if (ce.getExceptionType() == CatalogException.PARSE_FAILED) { 684 break; 686 } else { 687 } 689 } 690 691 try { 692 inStream.close(); 693 } catch (IOException e) { 694 } 696 } 697 698 if (parsed) parsePendingCatalogs(); 699 } 700 701 707 protected synchronized void parsePendingCatalogs() 708 throws MalformedURLException , IOException { 709 710 if (!localCatalogFiles.isEmpty()) { 711 Vector newQueue = new Vector (); 714 Enumeration q = localCatalogFiles.elements(); 715 while (q.hasMoreElements()) { 716 newQueue.addElement(q.nextElement()); 717 } 718 719 for (int curCat = 0; curCat < catalogFiles.size(); curCat++) { 721 String catfile = (String ) catalogFiles.elementAt(curCat); 722 newQueue.addElement(catfile); 723 } 724 725 catalogFiles = newQueue; 726 localCatalogFiles.clear(); 727 } 728 729 if (catalogFiles.isEmpty() && !localDelegate.isEmpty()) { 733 Enumeration e = localDelegate.elements(); 734 while (e.hasMoreElements()) { 735 catalogEntries.addElement(e.nextElement()); 736 } 737 localDelegate.clear(); 738 } 739 740 while (!catalogFiles.isEmpty()) { 744 String catfile = (String ) catalogFiles.elementAt(0); 745 try { 746 catalogFiles.remove(0); 747 } catch (ArrayIndexOutOfBoundsException e) { 748 } 750 751 if (catalogEntries.size() == 0 && catalogs.size() == 0) { 752 try { 755 parseCatalogFile(catfile); 756 } catch (CatalogException ce) { 757 System.out.println("FIXME: " + ce.toString()); 758 } 759 } else { 760 catalogs.addElement(catfile); 763 } 764 765 if (!localCatalogFiles.isEmpty()) { 766 Vector newQueue = new Vector (); 769 Enumeration q = localCatalogFiles.elements(); 770 while (q.hasMoreElements()) { 771 newQueue.addElement(q.nextElement()); 772 } 773 774 for (int curCat = 0; curCat < catalogFiles.size(); curCat++) { 776 catfile = (String ) catalogFiles.elementAt(curCat); 777 newQueue.addElement(catfile); 778 } 779 780 catalogFiles = newQueue; 781 localCatalogFiles.clear(); 782 } 783 784 if (!localDelegate.isEmpty()) { 785 Enumeration e = localDelegate.elements(); 786 while (e.hasMoreElements()) { 787 catalogEntries.addElement(e.nextElement()); 788 } 789 localDelegate.clear(); 790 } 791 } 792 793 catalogFiles.clear(); 795 } 796 797 806 protected synchronized void parseCatalogFile(String fileName) 807 throws MalformedURLException , IOException , CatalogException { 808 809 CatalogEntry entry; 810 811 try { 815 catalogCwd = FileURL.makeURL("basename"); 817 } catch (MalformedURLException e) { 818 String userdir = System.getProperty("user.dir"); 819 userdir.replace('\\', '/'); 820 catalogManager.debug.message(1, "Malformed URL on cwd", userdir); 821 catalogCwd = null; 822 } 823 824 try { 826 base = new URL (catalogCwd, fixSlashes(fileName)); 827 } catch (MalformedURLException e) { 828 try { 829 base = new URL ("file:" + fixSlashes(fileName)); 830 } catch (MalformedURLException e2) { 831 catalogManager.debug.message(1, "Malformed URL on catalog filename", 832 fixSlashes(fileName)); 833 base = null; 834 } 835 } 836 837 catalogManager.debug.message(2, "Loading catalog", fileName); 838 catalogManager.debug.message(4, "Default BASE", base.toString()); 839 840 fileName = base.toString(); 841 842 DataInputStream inStream = null; 843 boolean parsed = false; 844 boolean notFound = false; 845 846 for (int count = 0; !parsed && count < readerArr.size(); count++) { 847 CatalogReader reader = (CatalogReader) readerArr.get(count); 848 849 try { 850 notFound = false; 851 inStream = new DataInputStream (base.openStream()); 852 } catch (FileNotFoundException fnfe) { 853 notFound = true; 855 break; 856 } 857 858 try { 859 reader.readCatalog(this, inStream); 860 parsed = true; 861 } catch (CatalogException ce) { 862 if (ce.getExceptionType() == CatalogException.PARSE_FAILED) { 863 break; 865 } else { 866 } 868 } 869 870 try { 871 inStream.close(); 872 } catch (IOException e) { 873 } 875 } 876 877 if (!parsed) { 878 if (notFound) { 879 catalogManager.debug.message(3, "Catalog does not exist", fileName); 880 } else { 881 catalogManager.debug.message(1, "Failed to parse catalog", fileName); 882 } 883 } 884 } 885 886 896 public void addEntry(CatalogEntry entry) { 897 int type = entry.getEntryType(); 898 899 if (type == BASE) { 900 String value = entry.getEntryArg(0); 901 URL newbase = null; 902 903 if (base == null) { 904 catalogManager.debug.message(5, "BASE CUR", "null"); 905 } else { 906 catalogManager.debug.message(5, "BASE CUR", base.toString()); 907 } 908 catalogManager.debug.message(4, "BASE STR", value); 909 910 try { 911 value = fixSlashes(value); 912 newbase = new URL (base, value); 913 } catch (MalformedURLException e) { 914 try { 915 newbase = new URL ("file:" + value); 916 } catch (MalformedURLException e2) { 917 catalogManager.debug.message(1, "Malformed URL on base", value); 918 newbase = null; 919 } 920 } 921 922 if (newbase != null) { 923 base = newbase; 924 } 925 926 catalogManager.debug.message(5, "BASE NEW", base.toString()); 927 } else if (type == CATALOG) { 928 String fsi = makeAbsolute(entry.getEntryArg(0)); 929 930 catalogManager.debug.message(4, "CATALOG", fsi); 931 932 localCatalogFiles.addElement(fsi); 933 } else if (type == PUBLIC) { 934 String publicid = PublicId.normalize(entry.getEntryArg(0)); 935 String systemid = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 936 937 entry.setEntryArg(0, publicid); 938 entry.setEntryArg(1, systemid); 939 940 catalogManager.debug.message(4, "PUBLIC", publicid, systemid); 941 942 catalogEntries.addElement(entry); 943 } else if (type == SYSTEM) { 944 String systemid = normalizeURI(entry.getEntryArg(0)); 945 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 946 947 entry.setEntryArg(1, fsi); 948 949 catalogManager.debug.message(4, "SYSTEM", systemid, fsi); 950 951 catalogEntries.addElement(entry); 952 } else if (type == URI) { 953 String uri = normalizeURI(entry.getEntryArg(0)); 954 String altURI = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 955 956 entry.setEntryArg(1, altURI); 957 958 catalogManager.debug.message(4, "URI", uri, altURI); 959 960 catalogEntries.addElement(entry); 961 } else if (type == DOCUMENT) { 962 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0))); 963 entry.setEntryArg(0, fsi); 964 965 catalogManager.debug.message(4, "DOCUMENT", fsi); 966 967 catalogEntries.addElement(entry); 968 } else if (type == OVERRIDE) { 969 catalogManager.debug.message(4, "OVERRIDE", entry.getEntryArg(0)); 970 971 catalogEntries.addElement(entry); 972 } else if (type == SGMLDECL) { 973 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0))); 975 entry.setEntryArg(0, fsi); 976 977 catalogManager.debug.message(4, "SGMLDECL", fsi); 978 979 catalogEntries.addElement(entry); 980 } else if (type == DELEGATE_PUBLIC) { 981 String ppi = PublicId.normalize(entry.getEntryArg(0)); 982 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 983 984 entry.setEntryArg(0, ppi); 985 entry.setEntryArg(1, fsi); 986 987 catalogManager.debug.message(4, "DELEGATE_PUBLIC", ppi, fsi); 988 989 addDelegate(entry); 990 } else if (type == DELEGATE_SYSTEM) { 991 String psi = normalizeURI(entry.getEntryArg(0)); 992 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 993 994 entry.setEntryArg(0, psi); 995 entry.setEntryArg(1, fsi); 996 997 catalogManager.debug.message(4, "DELEGATE_SYSTEM", psi, fsi); 998 999 addDelegate(entry); 1000 } else if (type == DELEGATE_URI) { 1001 String pui = normalizeURI(entry.getEntryArg(0)); 1002 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1003 1004 entry.setEntryArg(0, pui); 1005 entry.setEntryArg(1, fsi); 1006 1007 catalogManager.debug.message(4, "DELEGATE_URI", pui, fsi); 1008 1009 addDelegate(entry); 1010 } else if (type == REWRITE_SYSTEM) { 1011 String psi = normalizeURI(entry.getEntryArg(0)); 1012 String rpx = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1013 1014 entry.setEntryArg(0, psi); 1015 entry.setEntryArg(1, rpx); 1016 1017 catalogManager.debug.message(4, "REWRITE_SYSTEM", psi, rpx); 1018 1019 catalogEntries.addElement(entry); 1020 } else if (type == REWRITE_URI) { 1021 String pui = normalizeURI(entry.getEntryArg(0)); 1022 String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1023 1024 entry.setEntryArg(0, pui); 1025 entry.setEntryArg(1, upx); 1026 1027 catalogManager.debug.message(4, "REWRITE_URI", pui, upx); 1028 1029 catalogEntries.addElement(entry); 1030 } else if (type == SYSTEM_SUFFIX) { 1031 String pui = normalizeURI(entry.getEntryArg(0)); 1032 String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 1033 1034 entry.setEntryArg(0, pui); 1035 entry.setEntryArg(1, upx); 1036 1037 catalogManager.debug.message(4, "SYSTEM_SUFFIX", pui, upx); 1038 1039 catalogEntries.addElement(entry); 1040 } else if (type == URI_SUFFIX) { 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, "URI_SUFFIX", 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 en = catalogEntries.elements(); 1157 while (en.hasMoreElements()) { 1158 CatalogEntry e = (CatalogEntry) en.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 en = catalogEntries.elements(); 1233 while (en.hasMoreElements()) { 1234 CatalogEntry e = (CatalogEntry) en.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 en = catalogEntries.elements(); 1271 while (en.hasMoreElements()) { 1272 CatalogEntry e = (CatalogEntry) en.nextElement(); 1273 if (e.getEntryType() == DOCUMENT) { 1274 return e.getEntryArg(0); 1275 } 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 en = catalogEntries.elements(); 1346 while (en.hasMoreElements()) { 1347 CatalogEntry e = (CatalogEntry) en.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 en = catalogEntries.elements(); 1432 while (en.hasMoreElements()) { 1433 CatalogEntry e = (CatalogEntry) en.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 en = catalogEntries.elements(); 1591 while (en.hasMoreElements()) { 1592 CatalogEntry e = (CatalogEntry) en.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 en = catalogEntries.elements(); 1609 Vector delCats = new Vector (); 1610 while (en.hasMoreElements()) { 1611 CatalogEntry e = (CatalogEntry) en.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 enCats = delCats.elements(); 1631 1632 if (catalogManager.debug.getDebug() > 1) { 1633 catalogManager.debug.message(2, "Switching to delegated catalog(s):"); 1634 while (enCats.hasMoreElements()) { 1635 String delegatedCatalog = (String ) enCats.nextElement(); 1636 catalogManager.debug.message(2, "\t" + delegatedCatalog); 1637 } 1638 } 1639 1640 Catalog dcat = newCatalog(); 1641 1642 enCats = delCats.elements(); 1643 while (enCats.hasMoreElements()) { 1644 String delegatedCatalog = (String ) enCats.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 en = catalogEntries.elements(); 1717 while (en.hasMoreElements()) { 1718 CatalogEntry e = (CatalogEntry) en.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 en = catalogEntries.elements(); 1729 String startString = null; 1730 String prefix = null; 1731 while (en.hasMoreElements()) { 1732 CatalogEntry e = (CatalogEntry) en.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 1748 if (prefix != null) { 1749 return prefix + systemId.substring(startString.length()); 1751 } 1752 1753 en = catalogEntries.elements(); 1755 String suffixString = null; 1756 String suffixURI = null; 1757 while (en.hasMoreElements()) { 1758 CatalogEntry e = (CatalogEntry) en.nextElement(); 1759 1760 if (e.getEntryType() == SYSTEM_SUFFIX) { 1761 String p = (String ) e.getEntryArg(0); 1762 if (p.length() <= systemId.length() 1763 && systemId.endsWith(p)) { 1764 if (suffixString == null 1766 || p.length() > suffixString.length()) { 1767 suffixString = p; 1768 suffixURI = e.getEntryArg(1); 1769 } 1770 } 1771 } 1772 } 1773 1774 if (suffixURI != null) { 1775 return suffixURI; 1777 } 1778 1779 en = catalogEntries.elements(); 1781 Vector delCats = new Vector (); 1782 while (en.hasMoreElements()) { 1783 CatalogEntry e = (CatalogEntry) en.nextElement(); 1784 1785 if (e.getEntryType() == DELEGATE_SYSTEM) { 1786 String p = (String ) e.getEntryArg(0); 1787 if (p.length() <= systemId.length() 1788 && p.equals(systemId.substring(0, p.length()))) { 1789 1791 delCats.addElement(e.getEntryArg(1)); 1792 } 1793 } 1794 } 1795 1796 if (delCats.size() > 0) { 1797 Enumeration enCats = delCats.elements(); 1798 1799 if (catalogManager.debug.getDebug() > 1) { 1800 catalogManager.debug.message(2, "Switching to delegated catalog(s):"); 1801 while (enCats.hasMoreElements()) { 1802 String delegatedCatalog = (String ) enCats.nextElement(); 1803 catalogManager.debug.message(2, "\t" + delegatedCatalog); 1804 } 1805 } 1806 1807 Catalog dcat = newCatalog(); 1808 1809 enCats = delCats.elements(); 1810 while (enCats.hasMoreElements()) { 1811 String delegatedCatalog = (String ) enCats.nextElement(); 1812 dcat.parseCatalog(delegatedCatalog); 1813 } 1814 1815 return dcat.resolveSystem(systemId); 1816 } 1817 1818 return null; 1819 } 1820 1821 1837 public String resolveURI(String uri) 1838 throws MalformedURLException , IOException { 1839 1840 catalogManager.debug.message(3, "resolveURI("+uri+")"); 1841 1842 uri = normalizeURI(uri); 1843 1844 if (uri != null && uri.startsWith("urn:publicid:")) { 1845 uri = PublicId.decodeURN(uri); 1846 return resolvePublic(uri, null); 1847 } 1848 1849 if (uri != null) { 1851 String resolved = resolveLocalURI(uri); 1852 if (resolved != null) { 1853 return resolved; 1854 } 1855 } 1856 1857 return resolveSubordinateCatalogs(URI, 1859 null, 1860 null, 1861 uri); 1862 } 1863 1864 1874 protected String resolveLocalURI(String uri) 1875 throws MalformedURLException , IOException { 1876 Enumeration en = catalogEntries.elements(); 1877 while (en.hasMoreElements()) { 1878 CatalogEntry e = (CatalogEntry) en.nextElement(); 1879 if (e.getEntryType() == URI 1880 && (e.getEntryArg(0).equals(uri))) { 1881 return e.getEntryArg(1); 1882 } 1883 } 1884 1885 en = catalogEntries.elements(); 1887 String startString = null; 1888 String prefix = null; 1889 while (en.hasMoreElements()) { 1890 CatalogEntry e = (CatalogEntry) en.nextElement(); 1891 1892 if (e.getEntryType() == REWRITE_URI) { 1893 String p = (String ) e.getEntryArg(0); 1894 if (p.length() <= uri.length() 1895 && p.equals(uri.substring(0, p.length()))) { 1896 if (startString == null 1898 || p.length() > startString.length()) { 1899 startString = p; 1900 prefix = e.getEntryArg(1); 1901 } 1902 } 1903 } 1904 } 1905 1906 if (prefix != null) { 1907 return prefix + uri.substring(startString.length()); 1909 } 1910 1911 en = catalogEntries.elements(); 1913 String suffixString = null; 1914 String suffixURI = null; 1915 while (en.hasMoreElements()) { 1916 CatalogEntry e = (CatalogEntry) en.nextElement(); 1917 1918 if (e.getEntryType() == URI_SUFFIX) { 1919 String p = (String ) e.getEntryArg(0); 1920 if (p.length() <= uri.length() 1921 && uri.endsWith(p)) { 1922 if (suffixString == null 1924 || p.length() > suffixString.length()) { 1925 suffixString = p; 1926 suffixURI = e.getEntryArg(1); 1927 } 1928 } 1929 } 1930 } 1931 1932 if (suffixURI != null) { 1933 return suffixURI; 1935 } 1936 1937 en = catalogEntries.elements(); 1939 Vector delCats = new Vector (); 1940 while (en.hasMoreElements()) { 1941 CatalogEntry e = (CatalogEntry) en.nextElement(); 1942 1943 if (e.getEntryType() == DELEGATE_URI) { 1944 String p = (String ) e.getEntryArg(0); 1945 if (p.length() <= uri.length() 1946 && p.equals(uri.substring(0, p.length()))) { 1947 1949 delCats.addElement(e.getEntryArg(1)); 1950 } 1951 } 1952 } 1953 1954 if (delCats.size() > 0) { 1955 Enumeration enCats = delCats.elements(); 1956 1957 if (catalogManager.debug.getDebug() > 1) { 1958 catalogManager.debug.message(2, "Switching to delegated catalog(s):"); 1959 while (enCats.hasMoreElements()) { 1960 String delegatedCatalog = (String ) enCats.nextElement(); 1961 catalogManager.debug.message(2, "\t" + delegatedCatalog); 1962 } 1963 } 1964 1965 Catalog dcat = newCatalog(); 1966 1967 enCats = delCats.elements(); 1968 while (enCats.hasMoreElements()) { 1969 String delegatedCatalog = (String ) enCats.nextElement(); 1970 dcat.parseCatalog(delegatedCatalog); 1971 } 1972 1973 return dcat.resolveURI(uri); 1974 } 1975 1976 return null; 1977 } 1978 1979 2007 protected synchronized String resolveSubordinateCatalogs(int entityType, 2008 String entityName, 2009 String publicId, 2010 String systemId) 2011 throws MalformedURLException , IOException { 2012 2013 for (int catPos = 0; catPos < catalogs.size(); catPos++) { 2014 Catalog c = null; 2015 2016 try { 2017 c = (Catalog) catalogs.elementAt(catPos); 2018 } catch (ClassCastException e) { 2019 String catfile = (String ) catalogs.elementAt(catPos); 2020 c = newCatalog(); 2021 2022 try { 2023 c.parseCatalog(catfile); 2024 } catch (MalformedURLException mue) { 2025 catalogManager.debug.message(1, "Malformed Catalog URL", catfile); 2026 } catch (FileNotFoundException fnfe) { 2027 catalogManager.debug.message(1, "Failed to load catalog, file not found", 2028 catfile); 2029 } catch (IOException ioe) { 2030 catalogManager.debug.message(1, "Failed to load catalog, I/O error", catfile); 2031 } 2032 2033 catalogs.setElementAt(c, catPos); 2034 } 2035 2036 String resolved = null; 2037 2038 if (entityType == DOCTYPE) { 2040 resolved = c.resolveDoctype(entityName, 2041 publicId, 2042 systemId); 2043 } else if (entityType == DOCUMENT) { 2044 resolved = c.resolveDocument(); 2045 } else if (entityType == ENTITY) { 2046 resolved = c.resolveEntity(entityName, 2047 publicId, 2048 systemId); 2049 } else if (entityType == NOTATION) { 2050 resolved = c.resolveNotation(entityName, 2051 publicId, 2052 systemId); 2053 } else if (entityType == PUBLIC) { 2054 resolved = c.resolvePublic(publicId, systemId); 2055 } else if (entityType == SYSTEM) { 2056 resolved = c.resolveSystem(systemId); 2057 } else if (entityType == URI) { 2058 resolved = c.resolveURI(systemId); 2059 } 2060 2061 if (resolved != null) { 2062 return resolved; 2063 } 2064 } 2065 2066 return null; 2067 } 2068 2069 2071 2079 protected String fixSlashes (String sysid) { 2080 return sysid.replace('\\', '/'); 2081 } 2082 2083 2091 protected String makeAbsolute(String sysid) { 2092 URL local = null; 2093 2094 sysid = fixSlashes(sysid); 2095 2096 try { 2097 local = new URL (base, sysid); 2098 } catch (MalformedURLException e) { 2099 catalogManager.debug.message(1, "Malformed URL on system identifier", sysid); 2100 } 2101 2102 if (local != null) { 2103 return local.toString(); 2104 } else { 2105 return sysid; 2106 } 2107 } 2108 2109 2115 protected String normalizeURI(String uriref) { 2116 String newRef = ""; 2117 byte[] bytes; 2118 2119 if (uriref == null) { 2120 return null; 2121 } 2122 2123 try { 2124 bytes = uriref.getBytes("UTF-8"); 2125 } catch (UnsupportedEncodingException uee) { 2126 catalogManager.debug.message(1, "UTF-8 is an unsupported encoding!?"); 2128 return uriref; 2129 } 2130 2131 for (int count = 0; count < bytes.length; count++) { 2132 int ch = bytes[count] & 0xFF; 2133 2134 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)) { 2146 newRef += encodedByte(ch); 2147 } else { 2148 newRef += (char) bytes[count]; 2149 } 2150 } 2151 2152 return newRef; 2153 } 2154 2155 2162 protected String encodedByte (int b) { 2163 String hex = Integer.toHexString(b).toUpperCase(); 2164 if (hex.length() < 2) { 2165 return "%0" + hex; 2166 } else { 2167 return "%" + hex; 2168 } 2169 } 2170 2171 2173 2182 protected void addDelegate(CatalogEntry entry) { 2183 int pos = 0; 2184 String partial = entry.getEntryArg(0); 2185 2186 Enumeration local = localDelegate.elements(); 2187 while (local.hasMoreElements()) { 2188 CatalogEntry dpe = (CatalogEntry) local.nextElement(); 2189 String dp = dpe.getEntryArg(0); 2190 if (dp.equals(partial)) { 2191 return; 2193 } 2194 if (dp.length() > partial.length()) { 2195 pos++; 2196 } 2197 if (dp.length() < partial.length()) { 2198 break; 2199 } 2200 } 2201 2202 if (localDelegate.size() == 0) { 2204 localDelegate.addElement(entry); 2205 } else { 2206 localDelegate.insertElementAt(entry, pos); 2207 } 2208 } 2209} 2210 2211 | Popular Tags |