1 package org.hibernate.util; 4 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.io.ObjectInputStream ; 8 import java.io.Serializable ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.xml.sax.EntityResolver ; 13 import org.xml.sax.InputSource ; 14 15 public class DTDEntityResolver implements EntityResolver , Serializable { 16 17 private static final Log log = LogFactory.getLog(DTDEntityResolver.class); 18 19 private static final String URL = "http://hibernate.sourceforge.net/"; 20 private transient ClassLoader resourceLoader; 21 22 26 public DTDEntityResolver() { 27 resourceLoader = this.getClass().getClassLoader(); 29 } 30 31 36 public DTDEntityResolver(ClassLoader resourceLoader) { 37 this.resourceLoader = resourceLoader; 38 } 39 40 public InputSource resolveEntity (String publicId, String systemId) { 41 if ( systemId!=null && systemId.startsWith(URL) ) { 42 log.debug("trying to locate " + systemId + " in classpath under org/hibernate/"); 43 String path = "org/hibernate/" + systemId.substring( URL.length() ); 45 InputStream dtdStream = resourceLoader==null ? 46 getClass().getResourceAsStream(path) : 47 resourceLoader.getResourceAsStream(path); 48 if (dtdStream==null) { 49 log.debug(systemId + " not found in classpath"); 50 return null; 51 } 52 else { 53 log.debug("found " + systemId + " in classpath"); 54 InputSource source = new InputSource (dtdStream); 55 source.setPublicId(publicId); 56 source.setSystemId(systemId); 57 return source; 58 } 59 } 60 else { 61 return null; 63 } 64 } 65 66 private void readObject(ObjectInputStream ois) throws IOException , ClassNotFoundException { 67 68 ois.defaultReadObject(); 69 this.resourceLoader = this.getClass().getClassLoader(); 70 } 71 } 72 73 74 75 76 77 78 79 | Popular Tags |