1 22 package org.jboss.util.xml; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.MalformedURLException ; 29 import java.net.URI ; 30 import java.net.URISyntaxException ; 31 import java.net.URL ; 32 33 import org.jboss.logging.Logger; 34 import org.jboss.util.xml.catalog.CatalogManager; 35 import org.jboss.util.xml.catalog.Resolver; 36 import org.xml.sax.InputSource ; 37 38 46 public class CatalogLocation 47 { 48 49 private static Logger log = Logger.getLogger(CatalogLocation.class); 50 51 55 private static final String [] catalogFilesNames = 56 {"META-INF/jax-ws-catalog.xml", "WEB-INF/jax-ws-catalog.xml", "jax-ws-catalog.xml"}; 57 58 private final Resolver catologResolver; 59 60 private final URL location; 61 62 private boolean isLastEntityResolved = false; 63 64 static 65 { 66 System.setProperty("xml.catalog.allowPI", "true"); 69 System.setProperty("xml.catalog.prefer", "public"); 71 System.setProperty("xml.catalog.verbosity", "0"); 74 } 75 76 81 public CatalogLocation(URL url) throws IOException 82 { 83 catologResolver = new Resolver(); 84 catologResolver.setCatalogManager(CatalogManager.getStaticManager()); 85 catologResolver.setupReaders(); 86 catologResolver.parseCatalog(url); 87 this.location=url; 88 } 89 90 99 public InputSource resolveEntity(String publicId, String systemId) throws MalformedURLException , IOException 100 { 101 String resolvedURI = catologResolver.resolveSystem(systemId); 102 103 if (resolvedURI == null) 104 { 105 resolvedURI = catologResolver.resolvePublic(publicId, systemId); 106 } 107 108 if (resolvedURI != null) 109 { 110 final InputSource is = new InputSource (); 111 is.setPublicId(publicId); 112 is.setSystemId(systemId); 113 is.setByteStream(this.loadResource(resolvedURI)); 114 this.isLastEntityResolved = true; 115 return is; 116 } 117 else 118 { 119 this.isLastEntityResolved = false; 121 return null; 122 } 123 } 124 125 133 public static URL lookupCatalogFiles() throws IOException 134 { 135 URL url = null; 136 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 139 140 for (int i = 0; i < catalogFilesNames.length; i++) 141 { 142 url = loader.getResource(catalogFilesNames[i]); 143 if (url != null) 145 { 146 break; 147 } 148 } 149 150 return url; 151 } 152 153 159 public boolean isEntityResolved() 160 { 161 return this.isLastEntityResolved; 162 } 163 164 172 private InputStream loadResource(String resolvedURI) throws IOException 173 { 174 try 175 { 176 final URI toLoad = new URI (resolvedURI); 177 InputStream inputStream = null; 178 if (toLoad != null) 179 { 180 try 181 { 182 inputStream = new FileInputStream (new File (toLoad)); 183 } 184 catch (IOException e) 185 { 186 log.error("Failed to open url stream", e); 187 throw e; 188 } 189 } 190 return inputStream; 191 } 192 catch (URISyntaxException e) 193 { 194 log.error("The URI (" + resolvedURI + ") is malfomed"); 195 throw new IOException ("The URI (" + resolvedURI + ") is malfomed"); 196 } 197 } 198 199 204 public boolean equals(Object other) 205 { 206 boolean back=false; 207 if (other!=null && other instanceof CatalogLocation){ 208 final CatalogLocation otherC=(CatalogLocation)other; 209 back=this.location.equals(otherC.location); 210 } 211 212 return back; 213 } 214 215 219 public int hashCode() 220 { 221 return this.location.hashCode(); 222 } 223 224 225 } 226 | Popular Tags |