1 3 56 57 package org.jboss.util.xml.catalog.helpers; 58 59 import java.util.Hashtable ; 60 import java.net.URL ; 61 import java.net.MalformedURLException ; 62 import java.io.InputStream ; 63 64 import javax.xml.transform.URIResolver ; 65 import javax.xml.transform.Source ; 66 import javax.xml.transform.sax.SAXSource ; 67 import javax.xml.transform.TransformerException ; 68 69 import org.xml.sax.EntityResolver ; 70 import org.xml.sax.InputSource ; 71 72 89 public class BootstrapResolver implements EntityResolver , URIResolver { 90 91 public static final String xmlCatalogXSD = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.xsd"; 92 93 94 public static final String xmlCatalogRNG = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.rng"; 95 96 97 public static final String xmlCatalogPubId = "-//OASIS//DTD XML Catalogs V1.0//EN"; 98 99 100 public static final String xmlCatalogSysId = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"; 101 102 103 private Hashtable publicMap = new Hashtable (); 104 105 106 private Hashtable systemMap = new Hashtable (); 107 108 109 private Hashtable uriMap = new Hashtable (); 110 111 112 public BootstrapResolver() { 113 URL url = this.getClass().getResource("/org/apache/xml/resolver/etc/catalog.dtd"); 114 if (url != null) { 115 publicMap.put(xmlCatalogPubId, url.toString()); 116 systemMap.put(xmlCatalogSysId, url.toString()); 117 } 118 119 url = this.getClass().getResource("/org/apache/xml/resolver/etc/catalog.rng"); 120 if (url != null) { 121 uriMap.put(xmlCatalogRNG, url.toString()); 122 } 123 124 url = this.getClass().getResource("/org/apache/xml/resolver/etc/catalog.xsd"); 125 if (url != null) { 126 uriMap.put(xmlCatalogXSD, url.toString()); 127 } 128 } 129 130 131 public InputSource resolveEntity (String publicId, String systemId) { 132 String resolved = null; 133 134 if (systemId != null && systemMap.containsKey(systemId)) { 135 resolved = (String ) systemMap.get(systemId); 136 } else if (publicId != null && publicMap.containsKey(publicId)) { 137 resolved = (String ) publicMap.get(publicId); 138 } 139 140 if (resolved != null) { 141 try { 142 InputSource iSource = new InputSource (resolved); 143 iSource.setPublicId(publicId); 144 145 URL url = new URL (resolved); 157 InputStream iStream = url.openStream(); 158 iSource.setByteStream(iStream); 159 160 return iSource; 161 } catch (Exception e) { 162 return null; 164 } 165 } 166 167 return null; 168 } 169 170 171 public Source resolve(String href, String base) 172 throws TransformerException { 173 174 String uri = href; 175 String fragment = null; 176 int hashPos = href.indexOf("#"); 177 if (hashPos >= 0) { 178 uri = href.substring(0, hashPos); 179 fragment = href.substring(hashPos+1); 180 } 181 182 String result = null; 183 if (href != null && uriMap.containsKey(href)) { 184 result = (String ) uriMap.get(href); 185 } 186 187 if (result == null) { 188 try { 189 URL url = null; 190 191 if (base==null) { 192 url = new URL (uri); 193 result = url.toString(); 194 } else { 195 URL baseURL = new URL (base); 196 url = (href.length()==0 ? baseURL : new URL (baseURL, uri)); 197 result = url.toString(); 198 } 199 } catch (java.net.MalformedURLException mue) { 200 String absBase = makeAbsolute(base); 202 if (!absBase.equals(base)) { 203 return resolve(href, absBase); 205 } else { 206 throw new TransformerException ("Malformed URL " 207 + href + "(base " + base + ")", 208 mue); 209 } 210 } 211 } 212 213 SAXSource source = new SAXSource (); 214 source.setInputSource(new InputSource (result)); 215 return source; 216 } 217 218 219 private String makeAbsolute(String uri) { 220 if (uri == null) { 221 uri = ""; 222 } 223 224 try { 225 URL url = new URL (uri); 226 return url.toString(); 227 } catch (MalformedURLException mue) { 228 String dir = System.getProperty("user.dir"); 229 String file = ""; 230 231 if (dir.endsWith("/")) { 232 file = "file://" + dir + uri; 233 } else { 234 file = "file://" + dir + "/" + uri; 235 } 236 237 try { 238 URL fileURL = new URL (file); 239 return fileURL.toString(); 240 } catch (MalformedURLException mue2) { 241 return uri; 243 } 244 } 245 } 246 } 247 | Popular Tags |