KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > DTDEntityResolver


1 //$Id: DTDEntityResolver.java,v 1.4 2005/02/12 07:19:50 steveebersole Exp $
2
//Contributed by Markus Meissner
3
package org.hibernate.util;
4
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.ObjectInputStream JavaDoc;
8 import java.io.Serializable JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.xml.sax.EntityResolver JavaDoc;
13 import org.xml.sax.InputSource JavaDoc;
14
15 public class DTDEntityResolver implements EntityResolver JavaDoc, Serializable JavaDoc {
16
17     private static final Log log = LogFactory.getLog(DTDEntityResolver.class);
18
19     private static final String JavaDoc URL = "http://hibernate.sourceforge.net/";
20     private transient ClassLoader JavaDoc resourceLoader;
21
22     /**
23      * Default constructor using DTDEntityResolver classloader for
24      * resource loading.
25      */

26     public DTDEntityResolver() {
27         //backward compatibility
28
resourceLoader = this.getClass().getClassLoader();
29     }
30
31     /**
32      * Set the class loader used to load resouces
33      *
34      * @param resourceLoader class loader to use
35      */

36     public DTDEntityResolver(ClassLoader JavaDoc resourceLoader) {
37         this.resourceLoader = resourceLoader;
38     }
39
40     public InputSource JavaDoc resolveEntity (String JavaDoc publicId, String JavaDoc systemId) {
41         if ( systemId!=null && systemId.startsWith(URL) ) {
42             log.debug("trying to locate " + systemId + " in classpath under org/hibernate/");
43             // Search for DTD
44
String JavaDoc path = "org/hibernate/" + systemId.substring( URL.length() );
45             InputStream JavaDoc 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 JavaDoc source = new InputSource JavaDoc(dtdStream);
55                 source.setPublicId(publicId);
56                 source.setSystemId(systemId);
57                 return source;
58             }
59         }
60         else {
61             // use the default behaviour
62
return null;
63         }
64     }
65
66     private void readObject(ObjectInputStream JavaDoc ois) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
67         /** to allow serialization of configuration */
68         ois.defaultReadObject();
69         this.resourceLoader = this.getClass().getClassLoader();
70     }
71 }
72
73
74
75
76
77
78
79
Popular Tags