KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > clazzy > loader > FileSystemLoader


1 package org.sapia.clazzy.loader;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.net.MalformedURLException JavaDoc;
8 import java.net.URL JavaDoc;
9
10 import org.sapia.clazzy.URLEnabled;
11 import org.sapia.clazzy.Utils;
12
13 /**
14  * Implements the <code>Loader</code> interface over a file directory.
15  *
16  * @author Yanick Duchesne
17  *
18  * <dl>
19  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
20  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
21  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
22  * </dl>
23  */

24 public class FileSystemLoader implements Loader, URLEnabled{
25   
26   private File JavaDoc _baseDir;
27   
28   public FileSystemLoader(File JavaDoc baseDir){
29     _baseDir = baseDir;
30   }
31   
32   public File JavaDoc getBaseDir(){
33     return _baseDir;
34   }
35   
36   /**
37    * @return the <code>URL</code> of the <code>File</code> representing the
38    * base directory to which this instance corresponds.
39    *
40    * @throws MalformedURLException
41    */

42   public URL JavaDoc getURL() throws MalformedURLException JavaDoc {
43     return _baseDir.toURL();
44   }
45   
46   /**
47    * @see org.sapia.clazzy.loader.Loader#getURL(java.lang.String)
48    */

49   public URL JavaDoc getURL(String JavaDoc resourceName) {
50     try{
51       File JavaDoc toReturn = new File JavaDoc(_baseDir, resourceName);
52       if(toReturn.exists() && toReturn.isFile()){
53         return toReturn.toURL();
54       }
55       return null;
56     }catch(MalformedURLException JavaDoc e){
57       return null;
58     }
59   }
60   
61   /**
62    * @see org.sapia.clazzy.loader.Loader#loadBytes(java.lang.String)
63    */

64   public byte[] loadBytes(String JavaDoc resourceName){
65     File JavaDoc toLoad = new File JavaDoc(_baseDir, resourceName);
66     if(!toLoad.exists()){
67       return null;
68     }
69     InputStream JavaDoc byteStream = null;
70     try {
71       byteStream = new FileInputStream JavaDoc(toLoad);
72       return Utils.streamToBytes(byteStream);
73     } catch(IOException JavaDoc e) {
74       return null;
75     } finally {
76       if(byteStream != null) {
77         try {
78           byteStream.close();
79         } catch(IOException JavaDoc e) {
80         }
81       }
82     }
83   }
84   
85   /**
86    * @see org.sapia.clazzy.loader.Loader#close()
87    */

88   public void close() {
89   }
90 }
91
Popular Tags