1 3 19 20 package com.sun.org.apache.xml.internal.resolver.tools; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.URL ; 25 import java.net.MalformedURLException ; 26 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.XMLReader ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.EntityResolver ; 31 32 import javax.xml.transform.sax.SAXSource ; 33 import javax.xml.transform.Source ; 34 import javax.xml.transform.URIResolver ; 35 import javax.xml.transform.TransformerException ; 36 import javax.xml.parsers.ParserConfigurationException ; 37 import javax.xml.parsers.SAXParserFactory ; 38 39 import com.sun.org.apache.xml.internal.resolver.Catalog; 40 import com.sun.org.apache.xml.internal.resolver.CatalogManager; 41 import com.sun.org.apache.xml.internal.resolver.helpers.FileURL; 42 43 62 public class CatalogResolver implements EntityResolver , URIResolver { 63 64 public boolean namespaceAware = true; 65 66 67 public boolean validating = false; 68 69 70 private Catalog catalog = null; 71 72 73 private CatalogManager catalogManager = CatalogManager.getStaticManager(); 74 75 76 public CatalogResolver() { 77 initializeCatalogs(false); 78 } 79 80 81 public CatalogResolver(boolean privateCatalog) { 82 initializeCatalogs(privateCatalog); 83 } 84 85 86 public CatalogResolver(CatalogManager manager) { 87 catalogManager = manager; 88 initializeCatalogs(!catalogManager.getUseStaticCatalog()); 89 } 90 91 92 private void initializeCatalogs(boolean privateCatalog) { 93 catalog = catalogManager.getCatalog(); 94 } 95 96 97 public Catalog getCatalog() { 98 return catalog; 99 } 100 101 121 public String getResolvedEntity (String publicId, String systemId) { 122 String resolved = null; 123 124 if (catalog == null) { 125 catalogManager.debug.message(1, "Catalog resolution attempted with null catalog; ignored"); 126 return null; 127 } 128 129 if (systemId != null) { 130 try { 131 resolved = catalog.resolveSystem(systemId); 132 } catch (MalformedURLException me) { 133 catalogManager.debug.message(1, "Malformed URL exception trying to resolve", 134 publicId); 135 resolved = null; 136 } catch (IOException ie) { 137 catalogManager.debug.message(1, "I/O exception trying to resolve", publicId); 138 resolved = null; 139 } 140 } 141 142 if (resolved == null) { 143 if (publicId != null) { 144 try { 145 resolved = catalog.resolvePublic(publicId, systemId); 146 } catch (MalformedURLException me) { 147 catalogManager.debug.message(1, "Malformed URL exception trying to resolve", 148 publicId); 149 } catch (IOException ie) { 150 catalogManager.debug.message(1, "I/O exception trying to resolve", publicId); 151 } 152 } 153 154 if (resolved != null) { 155 catalogManager.debug.message(2, "Resolved public", publicId, resolved); 156 } 157 } else { 158 catalogManager.debug.message(2, "Resolved system", systemId, resolved); 159 } 160 161 return resolved; 162 } 163 164 191 public InputSource resolveEntity (String publicId, String systemId) { 192 String resolved = getResolvedEntity(publicId, systemId); 193 194 if (resolved != null) { 195 try { 196 InputSource iSource = new InputSource (resolved); 197 iSource.setPublicId(publicId); 198 199 URL url = new URL (resolved); 211 InputStream iStream = url.openStream(); 212 iSource.setByteStream(iStream); 213 214 return iSource; 215 } catch (Exception e) { 216 catalogManager.debug.message(1, "Failed to create InputSource", resolved); 217 return null; 218 } 219 } 220 221 return null; 222 } 223 224 225 public Source resolve(String href, String base) 226 throws TransformerException { 227 228 String uri = href; 229 String fragment = null; 230 int hashPos = href.indexOf("#"); 231 if (hashPos >= 0) { 232 uri = href.substring(0, hashPos); 233 fragment = href.substring(hashPos+1); 234 } 235 236 String result = null; 237 238 try { 239 result = catalog.resolveURI(href); 240 } catch (Exception e) { 241 } 243 244 if (result == null) { 245 try { 246 URL url = null; 247 248 if (base==null) { 249 url = new URL (uri); 250 result = url.toString(); 251 } else { 252 URL baseURL = new URL (base); 253 url = (href.length()==0 ? baseURL : new URL (baseURL, uri)); 254 result = url.toString(); 255 } 256 } catch (java.net.MalformedURLException mue) { 257 String absBase = makeAbsolute(base); 259 if (!absBase.equals(base)) { 260 return resolve(href, absBase); 262 } else { 263 throw new TransformerException ("Malformed URL " 264 + href + "(base " + base + ")", 265 mue); 266 } 267 } 268 } 269 270 catalogManager.debug.message(2, "Resolved URI", href, result); 271 272 SAXSource source = new SAXSource (); 273 source.setInputSource(new InputSource (result)); 274 setEntityResolver(source); 275 return source; 276 } 277 278 299 private void setEntityResolver(SAXSource source) throws TransformerException { 300 XMLReader reader = source.getXMLReader(); 301 if (reader == null) { 302 SAXParserFactory spFactory = SAXParserFactory.newInstance(); 303 spFactory.setNamespaceAware(true); 304 try { 305 reader = spFactory.newSAXParser().getXMLReader(); 306 } 307 catch (ParserConfigurationException ex) { 308 throw new TransformerException (ex); 309 } 310 catch (SAXException ex) { 311 throw new TransformerException (ex); 312 } 313 } 314 reader.setEntityResolver(this); 315 source.setXMLReader(reader); 316 } 317 318 319 private String makeAbsolute(String uri) { 320 if (uri == null) { 321 uri = ""; 322 } 323 324 try { 325 URL url = new URL (uri); 326 return url.toString(); 327 } catch (MalformedURLException mue) { 328 try { 329 URL fileURL = FileURL.makeURL(uri); 330 return fileURL.toString(); 331 } catch (MalformedURLException mue2) { 332 return uri; 334 } 335 } 336 } 337 } 338 | Popular Tags |