KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.uitags.js;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.InputStreamReader JavaDoc;
7
8 import javax.servlet.ServletContext JavaDoc;
9
10 import net.sf.uitags.util.ResourceCloser;
11
12 /**
13  * A class that knows how to find (and access the contents of) a file.
14  *
15  * @author jonni
16  */

17 abstract class FileFinder {
18   static FileFinder getInstance(
19       ServletContext JavaDoc context, String JavaDoc containingDirName) {
20     if (containingDirName == null) {
21       return new ClasspathBasedFileFinder();
22     }
23
24     return new FileSystemBasedFileFinder(context, containingDirName);
25   }
26
27   final String JavaDoc readContents(String JavaDoc fileName) throws IOException JavaDoc {
28     StringBuffer JavaDoc out = new StringBuffer JavaDoc();
29
30     InputStream JavaDoc in = null;
31     BufferedReader JavaDoc contents = null;
32     try {
33       in = openInputStream(fileName);
34       contents = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
35       while (contents.ready()) {
36         out.append(contents.readLine());
37         out.append("\n");
38       }
39     }
40     finally {
41       ResourceCloser.close(contents);
42       ResourceCloser.close(in);
43     }
44
45     return out.toString();
46   }
47
48   /**
49    * To be implemented by the child class, this method opens an input stream.
50    * As soon as this method returns the child class should give up control of
51    * the input stream. It should not, for example, attempt to close the
52    * input stream.
53    */

54   protected abstract InputStream JavaDoc openInputStream(String JavaDoc fileName)
55       throws IOException JavaDoc;
56
57   /**
58    * Returns a <code>boolean</code> to indicate whether the finder is able
59    * to find a file that is not part of uitags suites.
60    */

61   protected abstract boolean supportsFilesNotFromSuites();
62
63   /**
64    * Returns the specified file's last modified time or 0 if unknown.
65    */

66   protected abstract long getLastModified(String JavaDoc fileName)
67       throws IOException JavaDoc;
68 }
69
Popular Tags