From: <ste...@us...> - 2006-02-24 16:57:36
|
Update of /cvsroot/hibernate/Hibernate3/src/org/hibernate/util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6704/src/org/hibernate/util Modified Files: Tag: Branch_3_1 DTDEntityResolver.java Log Message: HHH-1236 & HHH-1526 Index: DTDEntityResolver.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate3/src/org/hibernate/util/DTDEntityResolver.java,v retrieving revision 1.7 retrieving revision 1.7.2.1 diff -u -d -r1.7 -r1.7.2.1 --- DTDEntityResolver.java 14 Aug 2005 06:57:29 -0000 1.7 +++ DTDEntityResolver.java 24 Feb 2006 16:57:32 -0000 1.7.2.1 @@ -2,9 +2,7 @@ //Contributed by Markus Meissner package org.hibernate.util; -import java.io.IOException; import java.io.InputStream; -import java.io.ObjectInputStream; import java.io.Serializable; import org.apache.commons.logging.Log; @@ -14,68 +12,63 @@ public class DTDEntityResolver implements EntityResolver, Serializable { - private static final Log log = LogFactory.getLog(DTDEntityResolver.class); - - private static final String URL = "http://hibernate.sourceforge.net/"; - private transient ClassLoader resourceLoader; - - /** - * Default constructor using DTDEntityResolver classloader for - * resource loading. - */ - public DTDEntityResolver() { - //backward compatibility - resourceLoader = this.getClass().getClassLoader(); - } + private static final Log log = LogFactory.getLog( DTDEntityResolver.class ); - /** - * Set the class loader used to load resouces - * - * @param resourceLoader class loader to use - */ - public DTDEntityResolver(ClassLoader resourceLoader) { - this.resourceLoader = resourceLoader; - } + private static final String HIBERNATE_NAMESPACE = "http://hibernate.sourceforge.net/"; + private static final String LOCAL_NAMESPACE = "file://"; - public InputSource resolveEntity (String publicId, String systemId) { - if ( systemId!=null && systemId.startsWith(URL) ) { - log.debug("trying to locate " + systemId + " in classpath under org/hibernate/"); - // Search for DTD - String path = "org/hibernate/" + systemId.substring( URL.length() ); - InputStream dtdStream = resourceLoader==null ? - getClass().getResourceAsStream(path) : - resourceLoader.getResourceAsStream(path); - if (dtdStream==null) { - log.debug(systemId + " not found in classpath"); - if ( systemId.substring( URL.length()).indexOf("2.0")>-1 ) { - log.error("Don't use old DTDs, read the Hibernate 3.x Migration Guide!"); + public InputSource resolveEntity(String publicId, String systemId) { + if ( systemId != null ) { + log.debug( "trying to resolve system-id [" + systemId + "]" ); + if ( systemId.startsWith( HIBERNATE_NAMESPACE ) ) { + log.debug( "recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/" ); + String path = "org/hibernate/" + systemId.substring( HIBERNATE_NAMESPACE.length() ); + InputStream dtdStream = resolveInHibernateNamespace( path ); + if ( dtdStream == null ) { + log.debug( "unable to locate [" + systemId + "] on classpath" ); + if ( systemId.substring( HIBERNATE_NAMESPACE.length() ).indexOf( "2.0" ) > -1 ) { + log.error( "Don't use old DTDs, read the Hibernate 3.x Migration Guide!" ); + } + } + else { + log.debug( "located [" + systemId + "] in classpath" ); + InputSource source = new InputSource( dtdStream ); + source.setPublicId( publicId ); + source.setSystemId( systemId ); + return source; } - return null; } - else { - log.debug("found " + systemId + " in classpath"); - InputSource source = new InputSource(dtdStream); - source.setPublicId(publicId); - source.setSystemId(systemId); - return source; + else if ( systemId.startsWith( LOCAL_NAMESPACE ) ) { + log.debug( "recognized local namespace; attempting to resolve on classpath" ); + String path = systemId.substring( LOCAL_NAMESPACE.length() ); + InputStream stream = resolveInLocalNamespace( path ); + if ( stream == null ) { + log.debug( "unable to locate [" + systemId + "] on classpath" ); + } + else { + log.debug( "unable to locate [" + systemId + "] on classpath" ); + InputSource source = new InputSource( stream ); + source.setPublicId( publicId ); + source.setSystemId( systemId ); + return source; + } } } - else { - // use the default behaviour - return null; - } + // use default behavior + return null; } - private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { - /** to allow serialization of configuration */ - ois.defaultReadObject(); - this.resourceLoader = this.getClass().getClassLoader(); + protected InputStream resolveInHibernateNamespace(String path) { + return this.getClass().getClassLoader().getResourceAsStream( path ); } -} - - - - - - + protected InputStream resolveInLocalNamespace(String path) { + // attempt to find local stuff on the context classloader first... + InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream( path ); + if ( stream == null ) { + // next try our classloader + stream = getClass().getClassLoader().getResourceAsStream( path ); + } + return stream; + } +} |