~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/util/DTDEntityResolver.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
4
 
 
5
import java.io.InputStream;
 
6
import java.io.Serializable;
 
7
 
 
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;
 
12
 
 
13
/**
 
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.
 
23
 * </ol>
 
24
 * <p/>
 
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.
 
28
 */
 
29
public class DTDEntityResolver implements EntityResolver, Serializable {
 
30
 
 
31
        private static final Log log = LogFactory.getLog( DTDEntityResolver.class );
 
32
 
 
33
        private static final String HIBERNATE_NAMESPACE = "http://hibernate.sourceforge.net/";
 
34
        private static final String USER_NAMESPACE = "classpath://";
 
35
 
 
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!" );
 
47
                                        }
 
48
                                }
 
49
                                else {
 
50
                                        log.debug( "located [" + systemId + "] in classpath" );
 
51
                                        InputSource source = new InputSource( dtdStream );
 
52
                                        source.setPublicId( publicId );
 
53
                                        source.setSystemId( systemId );
 
54
                                        return source;
 
55
                                }
 
56
                        }
 
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" );
 
63
                                }
 
64
                                else {
 
65
                                        log.debug( "located [" + systemId + "] in classpath" );
 
66
                                        InputSource source = new InputSource( stream );
 
67
                                        source.setPublicId( publicId );
 
68
                                        source.setSystemId( systemId );
 
69
                                        return source;
 
70
                                }
 
71
                        }
 
72
                }
 
73
                // use default behavior
 
74
                return null;
 
75
        }
 
76
 
 
77
        protected InputStream resolveInHibernateNamespace(String path) {
 
78
                return this.getClass().getClassLoader().getResourceAsStream( path );
 
79
        }
 
80
 
 
81
        protected InputStream resolveInLocalNamespace(String path) {
 
82
                try {
 
83
                        return ConfigHelper.getUserResourceAsStream( path );
 
84
                }
 
85
                catch( Throwable t ) {
 
86
                        return null;
 
87
                }
 
88
        }
 
89
}