KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > conf > ClassPathEntityResolver


1 // $Id: ClassPathEntityResolver.java,v 1.2 2005/04/23 12:44:05 belaban Exp $
2

3 package org.jgroups.conf;
4
5 /**
6  *
7  * @author Filip Hanik (<a HREF="mailto:filip@filip.net">filip@filip.net)
8  * @author Bela Ban
9  * @version $Id: ClassPathEntityResolver.java,v 1.2 2005/04/23 12:44:05 belaban Exp $
10  */

11
12 import org.xml.sax.EntityResolver JavaDoc;
13 import org.xml.sax.InputSource JavaDoc;
14 import org.xml.sax.SAXException JavaDoc;
15
16 import java.io.InputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.net.URL JavaDoc;
19
20 public class ClassPathEntityResolver implements EntityResolver JavaDoc {
21     public String JavaDoc mDefaultJGroupsDTD="jgroups-protocol.dtd";
22
23     public ClassPathEntityResolver() {
24     }
25
26     public ClassPathEntityResolver(String JavaDoc dtdName) {
27         mDefaultJGroupsDTD=dtdName;
28     }
29
30     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
31         InputSource JavaDoc source=new InputSource JavaDoc(getInputStream(systemId));
32         return source;
33     }
34
35     protected InputStream JavaDoc getInputStream(String JavaDoc dtdurl)
36             throws java.io.IOException JavaDoc {
37         String JavaDoc url=dtdurl;
38         if(url == null) url=mDefaultJGroupsDTD;
39         //1. first try to load the DTD from an actual URL
40
try {
41             URL JavaDoc inurl=new URL JavaDoc(url);
42             return inurl.openStream();
43         }
44         catch(Exception JavaDoc ignore) {
45         }
46         //2. then try to load it from the classpath
47

48         InputStream JavaDoc stream=Thread.currentThread().getContextClassLoader().getResourceAsStream(url);
49         if(stream == null) {
50             throw new IOException JavaDoc("Could not locate the DTD with name:[" + url + "] in the classpath.");
51         }
52         else
53             return stream;
54     }
55 }
56
Popular Tags