1
//$Id: DTDEntityResolver.java 10033 2006-06-21 06:23:30Z christian.bauer@jboss.com $
2
//Contributed by Markus Meissner
3
package org.hibernate.util;
5
import java.io.InputStream;
6
import java.io.Serializable;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.xml.sax.EntityResolver;
11
import org.xml.sax.InputSource;
14
* An {@link EntityResolver} implementation which attempts to resolve
15
* various systemId URLs to local classpath lookups<ol>
16
* <li>Any systemId URL beginning with <tt>http://hibernate.sourceforge.net/</tt> is
17
* searched for as a classpath resource in the classloader which loaded the
18
* Hibernate classes.</li>
19
* <li>Any systemId URL using <tt>classpath</tt> as the scheme (i.e. starting
20
* with <tt>classpath://</tt> is searched for as a classpath resource using first
21
* the current thread context classloader and then the classloader which loaded
22
* the Hibernate classes.
25
* Any entity references which cannot be resolved in relation to the above
26
* rules result in returning null, which should force the SAX reader to
27
* handle the entity reference in its default manner.
29
public class DTDEntityResolver implements EntityResolver, Serializable {
31
private static final Log log = LogFactory.getLog( DTDEntityResolver.class );
33
private static final String HIBERNATE_NAMESPACE = "http://hibernate.sourceforge.net/";
34
private static final String USER_NAMESPACE = "classpath://";
36
public InputSource resolveEntity(String publicId, String systemId) {
37
if ( systemId != null ) {
38
log.debug( "trying to resolve system-id [" + systemId + "]" );
39
if ( systemId.startsWith( HIBERNATE_NAMESPACE ) ) {
40
log.debug( "recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/" );
41
String path = "org/hibernate/" + systemId.substring( HIBERNATE_NAMESPACE.length() );
42
InputStream dtdStream = resolveInHibernateNamespace( path );
43
if ( dtdStream == null ) {
44
log.debug( "unable to locate [" + systemId + "] on classpath" );
45
if ( systemId.substring( HIBERNATE_NAMESPACE.length() ).indexOf( "2.0" ) > -1 ) {
46
log.error( "Don't use old DTDs, read the Hibernate 3.x Migration Guide!" );
50
log.debug( "located [" + systemId + "] in classpath" );
51
InputSource source = new InputSource( dtdStream );
52
source.setPublicId( publicId );
53
source.setSystemId( systemId );
57
else if ( systemId.startsWith( USER_NAMESPACE ) ) {
58
log.debug( "recognized local namespace; attempting to resolve on classpath" );
59
String path = systemId.substring( USER_NAMESPACE.length() );
60
InputStream stream = resolveInLocalNamespace( path );
61
if ( stream == null ) {
62
log.debug( "unable to locate [" + systemId + "] on classpath" );
65
log.debug( "located [" + systemId + "] in classpath" );
66
InputSource source = new InputSource( stream );
67
source.setPublicId( publicId );
68
source.setSystemId( systemId );
73
// use default behavior
77
protected InputStream resolveInHibernateNamespace(String path) {
78
return this.getClass().getClassLoader().getResourceAsStream( path );
81
protected InputStream resolveInLocalNamespace(String path) {
83
return ConfigHelper.getUserResourceAsStream( path );
85
catch( Throwable t ) {