1 3 19 20 package com.sun.org.apache.xml.internal.resolver.helpers; 21 22 import java.util.Hashtable ; 23 import java.net.URL ; 24 import java.net.MalformedURLException ; 25 import java.io.InputStream ; 26 27 import javax.xml.transform.URIResolver ; 28 import javax.xml.transform.Source ; 29 import javax.xml.transform.sax.SAXSource ; 30 import javax.xml.transform.TransformerException ; 31 32 import org.xml.sax.EntityResolver ; 33 import org.xml.sax.InputSource ; 34 35 52 public class BootstrapResolver implements EntityResolver , URIResolver { 53 54 public static final String xmlCatalogXSD = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.xsd"; 55 56 57 public static final String xmlCatalogRNG = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.rng"; 58 59 60 public static final String xmlCatalogPubId = "-//OASIS//DTD XML Catalogs V1.0//EN"; 61 62 63 public static final String xmlCatalogSysId = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"; 64 65 66 private Hashtable publicMap = new Hashtable (); 67 68 69 private Hashtable systemMap = new Hashtable (); 70 71 72 private Hashtable uriMap = new Hashtable (); 73 74 75 public BootstrapResolver() { 76 URL url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.dtd"); 77 if (url != null) { 78 publicMap.put(xmlCatalogPubId, url.toString()); 79 systemMap.put(xmlCatalogSysId, url.toString()); 80 } 81 82 url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.rng"); 83 if (url != null) { 84 uriMap.put(xmlCatalogRNG, url.toString()); 85 } 86 87 url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.xsd"); 88 if (url != null) { 89 uriMap.put(xmlCatalogXSD, url.toString()); 90 } 91 } 92 93 94 public InputSource resolveEntity (String publicId, String systemId) { 95 String resolved = null; 96 97 if (systemId != null && systemMap.containsKey(systemId)) { 98 resolved = (String ) systemMap.get(systemId); 99 } else if (publicId != null && publicMap.containsKey(publicId)) { 100 resolved = (String ) publicMap.get(publicId); 101 } 102 103 if (resolved != null) { 104 try { 105 InputSource iSource = new InputSource (resolved); 106 iSource.setPublicId(publicId); 107 108 URL url = new URL (resolved); 120 InputStream iStream = url.openStream(); 121 iSource.setByteStream(iStream); 122 123 return iSource; 124 } catch (Exception e) { 125 return null; 127 } 128 } 129 130 return null; 131 } 132 133 134 public Source resolve(String href, String base) 135 throws TransformerException { 136 137 String uri = href; 138 String fragment = null; 139 int hashPos = href.indexOf("#"); 140 if (hashPos >= 0) { 141 uri = href.substring(0, hashPos); 142 fragment = href.substring(hashPos+1); 143 } 144 145 String result = null; 146 if (href != null && uriMap.containsKey(href)) { 147 result = (String ) uriMap.get(href); 148 } 149 150 if (result == null) { 151 try { 152 URL url = null; 153 154 if (base==null) { 155 url = new URL (uri); 156 result = url.toString(); 157 } else { 158 URL baseURL = new URL (base); 159 url = (href.length()==0 ? baseURL : new URL (baseURL, uri)); 160 result = url.toString(); 161 } 162 } catch (java.net.MalformedURLException mue) { 163 String absBase = makeAbsolute(base); 165 if (!absBase.equals(base)) { 166 return resolve(href, absBase); 168 } else { 169 throw new TransformerException ("Malformed URL " 170 + href + "(base " + base + ")", 171 mue); 172 } 173 } 174 } 175 176 SAXSource source = new SAXSource (); 177 source.setInputSource(new InputSource (result)); 178 return source; 179 } 180 181 182 private String makeAbsolute(String uri) { 183 if (uri == null) { 184 uri = ""; 185 } 186 187 try { 188 URL url = new URL (uri); 189 return url.toString(); 190 } catch (MalformedURLException mue) { 191 try { 192 URL fileURL = FileURL.makeURL(uri); 193 return fileURL.toString(); 194 } catch (MalformedURLException mue2) { 195 return uri; 197 } 198 } 199 } 200 } 201 | Popular Tags |