KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.soto.util;
2
3 import java.io.File JavaDoc;
4 import java.io.FileNotFoundException JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7
8
9 /**
10  * Resolves resources from the file system.
11  *
12  * <pre>
13  * FileResourceHandler handler = new FileResourceHandler();
14  *
15  * // resolving a file URL
16  * InputStream is = handler.getResource("file:/opt/some/file.txt");
17  *
18  * // resolving an absolute path
19  * InputStream is = handler.getResource("/opt/some/file.txt");
20  *
21  * // resolving a relative path (assuming current dir is /opt)
22  * InputStream is = handler.getResource("some/file.txt");
23  *
24  * </pre>
25  *
26  * @author Yanick Duchesne
27  * <dl>
28  * <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>
29  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
30  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
31  * </dl>
32  */

33 public class FileResourceHandler implements ResourceHandler, Schemes {
34
35   private boolean _fallBackToClasspath = true;
36   private String JavaDoc _basedir;
37
38   /**
39    * @see org.sapia.soto.util.ResourceHandler#getResourceObject(java.lang.String)
40    */

41   public Resource getResourceObject(String JavaDoc uri) throws IOException JavaDoc {
42     String JavaDoc fileUri = mangleUri(uri);
43     File JavaDoc f = getFile(fileUri);
44
45     if (f.exists()) {
46       return new FileResource(f);
47     } else if (_fallBackToClasspath) {
48       return new ClasspathResourceHandler().getResourceObject(uri);
49     } else {
50       throw new FileNotFoundException JavaDoc(uri);
51     }
52   }
53
54   /**
55    * @param uri a URI.
56    * @return the <code>File</code> corresponding to the given URI.
57    */

58   public File JavaDoc getFile(String JavaDoc uri) {
59     uri = mangleUri(Utils.chopScheme(uri));
60
61     return new File JavaDoc(uri);
62   }
63
64   /**
65    * @see org.sapia.soto.util.ResourceHandler#getResource(String)
66    */

67   public InputStream JavaDoc getResource(String JavaDoc uri) throws IOException JavaDoc {
68     String JavaDoc fileUri = mangleUri(uri);
69     File JavaDoc f = getFile(fileUri);
70
71     if (f.exists()) {
72       return f.toURL().openStream();
73     } else if (_fallBackToClasspath) {
74       return new ClasspathResourceHandler().getResourceObject(uri)
75                                            .getInputStream();
76     } else {
77       throw new FileNotFoundException JavaDoc(uri);
78     }
79   }
80
81   /**
82    * @param fallback if <code>true</code>, this instance will resort to looking up the
83    * classpath if it cannot find a resource on the file system.
84    */

85   public void setFallBackToClasspath(boolean fallback) {
86     _fallBackToClasspath = fallback;
87   }
88
89   /**
90    * Sets the directory from which relative paths should be evaluated.
91    * @param basedir the path to a directory
92    */

93   public void setBasedir(String JavaDoc basedir) {
94     _basedir = basedir;
95   }
96   
97   /**
98    * @see org.sapia.soto.util.ResourceHandler#accepts(java.lang.String)
99    */

100   public boolean accepts(String JavaDoc uri) {
101         String JavaDoc scheme = Utils.getScheme(uri);
102         if(scheme == null || scheme.length() == 0 || scheme.equals(SCHEME_FILE)){
103             return true;
104         }
105         return false;
106   }
107
108
109   private String JavaDoc mangleUri(String JavaDoc uri) {
110     if ((_basedir != null) && Utils.isRelativePath(uri)) {
111       return Utils.getRelativePath(_basedir, uri, false);
112     } else {
113       return uri;
114     }
115   }
116 }
117
Popular Tags