KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > util > ClasspathResourceHandler


1 package org.sapia.soto.util;
2
3 import java.io.FileNotFoundException JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6
7 import java.net.URL JavaDoc;
8
9 /**
10  * Handles resources on the classpath.
11  *
12  * <pre>
13  * ClassPathResourceHandler handler = new ClassPathResourceHandler();
14  * URL url = new URL(handler.getResourceObject("some/resource/path").getURI());
15  * ...
16  * </pre>
17  *
18  * @author Yanick Duchesne
19  * <dl>
20  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
21  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
22  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
23  * </dl>
24  */

25 public class ClasspathResourceHandler implements ResourceHandler, Schemes {
26     
27   /**
28    * Constructor for FileResourceHandler.
29    */

30   public ClasspathResourceHandler() {
31     super();
32   }
33
34   /**
35    * @see org.sapia.soto.util.ResourceHandler#getResourceObject(java.lang.String)
36    */

37   public Resource getResourceObject(String JavaDoc uri) throws IOException JavaDoc {
38     String JavaDoc path = Utils.chopScheme(uri.replace('\\', '/'));
39
40     if (path.charAt(0) == '/') {
41       path = path.substring(1);
42     }
43
44     URL JavaDoc url = null;
45
46     url = getClass().getClassLoader().getResource(path);
47
48     if (url == null) {
49       url = Thread.currentThread().getContextClassLoader().getResource(path);
50     }
51
52     if (url == null) {
53       url = ClassLoader.getSystemResource(path);
54     }
55
56     if (url == null) {
57       throw new FileNotFoundException JavaDoc(path);
58     }
59
60     return new UrlResource(url);
61   }
62
63   /**
64    * @see org.sapia.soto.util.ResourceHandler#getResource(String)
65    */

66   public InputStream JavaDoc getResource(String JavaDoc uri) throws IOException JavaDoc {
67     return getResourceObject(uri).getInputStream();
68   }
69   
70   /**
71    * @see org.sapia.soto.util.ResourceHandler#accepts(java.lang.String)
72    */

73   public boolean accepts(String JavaDoc uri) {
74         String JavaDoc scheme = Utils.getScheme(uri);
75         if(scheme == null || scheme.length() == 0 || scheme.equals(SCHEME_RESOURCE)){
76             return true;
77         }
78     return false;
79   }
80
81 }
82
Popular Tags