1 |
| package photospace.vfs; |
2 |
| |
3 |
| import java.io.*; |
4 |
| |
5 |
| public class FileSystemImpl |
6 |
| implements java.io.Serializable, FileSystem |
7 |
| { |
8 |
| private File root; |
9 |
| |
10 |
41
| public File getRoot()
|
11 |
| { |
12 |
41
| return root;
|
13 |
| } |
14 |
| |
15 |
8
| public void setRoot(File root)
|
16 |
| throws IOException |
17 |
| { |
18 |
8
| this.root = root.getCanonicalFile();
|
19 |
| } |
20 |
| |
21 |
16
| public File getFile(String path)
|
22 |
| throws IOException |
23 |
| { |
24 |
0
| if (path == null || "".equals(path.trim())) throw new FileNotFoundException("Empty path is not a file.");
|
25 |
16
| File file = new File(getRoot(), path).getCanonicalFile();
|
26 |
0
| if (!file.exists()) throw new FileNotFoundException(path + " not found.");
|
27 |
16
| return file;
|
28 |
| } |
29 |
| |
30 |
2
| public File getAsset(String path)
|
31 |
| throws FileNotFoundException, IOException |
32 |
| { |
33 |
2
| File asset = getFile(path);
|
34 |
0
| if (!asset.isFile()) throw new FileNotFoundException(path + " is not a file.");
|
35 |
2
| return asset;
|
36 |
| } |
37 |
| |
38 |
0
| public File getDirectory(String path)
|
39 |
| throws FileNotFoundException, IOException |
40 |
| { |
41 |
0
| File dir = getFile(path);
|
42 |
0
| if (!dir.isDirectory()) throw new FileNotFoundException(path + " is not a directory.");
|
43 |
0
| return dir;
|
44 |
| } |
45 |
| |
46 |
19
| public String getPath(File file)
|
47 |
| { |
48 |
19
| String path = file.getPath().substring(getRoot().getPath().length());
|
49 |
19
| path = path.replaceAll("\\\\", "/"); //todo: make cross-platform
|
50 |
6
| if (file.isDirectory() && ! path.endsWith("/")) path += "/";
|
51 |
19
| return path;
|
52 |
| } |
53 |
| |
54 |
| } |