01: /*
02: * @(#)FileLoader.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.lib;
10:
11: import java.io.*;
12: import java.net.*;
13:
14: class FileLoader implements Loader {
15: private File dir;
16: private URL base;
17:
18: FileLoader(URL url) throws IOException {
19: base = url;
20: if (!"file".equals(url.getProtocol())) {
21: throw new IllegalArgumentException("url");
22: }
23: dir = new File(url.getFile().replace('/', File.separatorChar));
24: }
25:
26: public Resource getResource(final String name) {
27: final URL url;
28: try {
29: url = new URL(base, name);
30: if (url.getFile().startsWith(base.getFile()) == false) {
31: return null;
32: }
33: final File file = new File(dir, name.replace('/',
34: File.separatorChar));
35: if (file.exists()) {
36: return new Resource() {
37: public URL getURL() {
38: return url;
39: }
40:
41: public InputStream getInputStream()
42: throws IOException {
43: return new FileInputStream(file);
44: }
45:
46: public int getContentLength() throws IOException {
47: return (int) file.length();
48: }
49: };
50: }
51: } catch (Exception e) {
52: return null;
53: }
54: return null;
55: }
56: }
|