KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > js > FileSystemBasedFileFinder


1 package net.sf.uitags.js;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.net.URL JavaDoc;
6
7 import javax.servlet.ServletContext JavaDoc;
8
9 final class FileSystemBasedFileFinder extends FileFinder {
10   private ServletContext JavaDoc servletContext;
11   private String JavaDoc containingDirName;
12
13   FileSystemBasedFileFinder(
14       ServletContext JavaDoc servletContext, String JavaDoc containingDirName) {
15     this.servletContext = servletContext;
16     this.containingDirName = correctDirName(containingDirName);
17   }
18
19   private String JavaDoc correctDirName(String JavaDoc name) {
20     name = name.trim();
21
22     // Ensure dir starts with a slash
23
if (!name.startsWith("/")) {
24       name = "/" + name;
25     }
26
27     // Ensure dir ends with a slash
28
if (!name.endsWith("/")) {
29       name = name + "/";
30     }
31
32     return name;
33   }
34
35   protected InputStream JavaDoc openInputStream(String JavaDoc fileName) throws IOException JavaDoc {
36     return getUrl(fileName).openStream();
37   }
38
39   protected boolean supportsFilesNotFromSuites() {
40     return true;
41   }
42
43   protected long getLastModified(String JavaDoc fileName) throws IOException JavaDoc {
44     return getUrl(fileName).openConnection().getLastModified();
45   }
46
47   private URL JavaDoc getUrl(String JavaDoc fileName) throws IOException JavaDoc {
48     return this.servletContext.getResource(this.containingDirName + fileName);
49   }
50
51   /**
52    * This method is provided for the sole purpose of allowing testing.
53    */

54   String JavaDoc getCorrectedDirName() {
55     return this.containingDirName;
56   }
57 }
58
Popular Tags