KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > resource > FileResourceHandler


1 package org.sapia.resource;
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 import java.net.URI JavaDoc;
8 import java.net.URISyntaxException JavaDoc;
9
10 /**
11  * Resolves resources from the file system.
12  *
13  * <pre>
14  * FileResourceHandler handler = new FileResourceHandler();
15  *
16  * // resolving a file URL
17  * InputStream is = handler.getResource(&quot;file:/opt/some/file.txt&quot;);
18  *
19  * // resolving an absolute path
20  * InputStream is = handler.getResource(&quot;/opt/some/file.txt&quot;);
21  *
22  * // resolving a relative path (assuming current dir is /opt)
23  * InputStream is = handler.getResource(&quot;some/file.txt&quot;);
24  *
25  *
26  * </pre>
27  *
28  * @see org.sapia.resource.FileResource
29  *
30  * @author Yanick Duchesne
31  */

32 public class FileResourceHandler implements ResourceHandler, Schemes {
33
34   private boolean _fallBackToClasspath = true;
35   private String JavaDoc _basedir = System.getProperty("user.dir");
36
37   public Resource getResourceObject(String JavaDoc uri) throws IOException JavaDoc {
38     String JavaDoc fileUri = mangleUri(uri);
39     File JavaDoc f = getFile(fileUri);
40     
41     if(f.exists()){
42       return new FileResource(f);
43     } else if (f.getAbsoluteFile().exists()) {
44       return new FileResource(f.getAbsoluteFile());
45     } else if(_fallBackToClasspath) {
46       return new ClasspathResourceHandler().getResourceObject(uri);
47     } else {
48       throw new ResourceNotFoundException(uri);
49     }
50   }
51
52   /**
53    * @param uri
54    * a URI.
55    * @return the <code>File</code> corresponding to the given URI.
56    */

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

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

92   public void setBasedir(String JavaDoc basedir) {
93     _basedir = basedir;
94   }
95
96   public boolean accepts(String JavaDoc uri) {
97     return doAccepts(Utils.getScheme(uri));
98   }
99   
100   public boolean accepts(URI JavaDoc uri) {
101     return doAccepts(uri.getScheme());
102   }
103   
104   private boolean doAccepts(String JavaDoc scheme){
105     if(scheme == null || scheme.length() == 0 || scheme.equals(SCHEME_FILE)) {
106       return true;
107     }
108     return false;
109   }
110
111   private String JavaDoc mangleUri(String JavaDoc uri) throws IOException JavaDoc{
112     URI JavaDoc u;
113     if(Utils.hasScheme(uri)){
114       u = Utils.toURIObject(uri);
115     }
116     else{
117       try{
118         u = new URI JavaDoc(SCHEME_FILE+":"+uri);
119       }catch(URISyntaxException JavaDoc e){
120         throw new IOException JavaDoc(e.getMessage());
121       }
122     }
123
124     if((_basedir != null) && !Utils.isAbsolute(u)) {
125       return Utils.getRelativePath(_basedir, uri, false);
126     } else {
127       return Utils.chopScheme(uri);
128     }
129   }
130 }
131
Popular Tags