1
2
3
4
5
6
7 package org.astrogrid.store.delegate.local;
8 import org.astrogrid.store.delegate.*;
9
10 import java.io.File;
11 import java.net.MalformedURLException;
12 import org.astrogrid.store.Agsl;
13 import java.util.Date;
14
15 /***
16 * Represents a local file (ie a File class) as a StoreFile
17 * <p>
18 * Note that because this uses the File class to navigate around,
19 * it is not necessarily true that child[0].getParent() == child[1].getParent()
20 *
21 * @author M Hill
22 */
23
24
25 public class LocalFile implements StoreFile {
26
27
28 File file = null;
29 LocalFileStore store = null;
30
31 public LocalFile(LocalFileStore theStore, File aFile) {
32 assert aFile != null : "Must set file";
33 assert theStore != null : "Must set store";
34
35 this.file = aFile;
36 this.store = theStore;
37 }
38
39 /*** Lists children files if this is a container - returns null otherwise */
40 public StoreFile[] listFiles() {
41 if (!isFolder()) {
42 return null;
43 }
44
45 File[] localFiles = file.listFiles();
46 StoreFile[] storeFiles = new StoreFile[localFiles.length];
47 for (int i=0;i<localFiles.length;i++) {
48 storeFiles[i] = new LocalFile(store, localFiles[i]);
49 }
50 return storeFiles;
51 }
52
53 /*** Returns parent folder of this file/folder */
54 public StoreFile getParent() {
55
56 String serverPath = getPath();
57 if (serverPath == null) return null;
58
59 return new LocalFile(store, file.getParentFile());
60 }
61
62 /*** Returns true if this is a self-contained file. For example, a database
63 * table might be represented as a StoreFile but it is not a file */
64 public boolean isFile() {
65 return file.isFile();
66 }
67
68 /*** Returns the filename/foldername/tablename/etc */
69 public String getName() {
70 return file.getName();
71 }
72
73 /*** Returns the path to this file on the server */
74 public String getPath() {
75 try {
76 return store.getServerPath(file);
77 }
78 catch (StoreException se) {
79
80 throw new RuntimeException("Program Error: ", se);
81 }
82 }
83
84 /*** Returns the owner of the file */
85 public String getOwner() {
86 return null;
87 }
88
89 /*** Returns the mime type (null if unknown) */
90 public String getMimeType() {
91 if (file.getName().endsWith(".txt")) return "text/plain";
92 if (file.getName().endsWith(".xml")) return "text/xml";
93 if (file.getName().endsWith(".vot")) return "text/xml+votable";
94 if (file.getName().endsWith(".adql")) return "text/xml+adql";
95 if (file.getName().endsWith(".job")) return "text/xml+job";
96 return null;
97 }
98
99 /*** Returns the date the file was last modified (null if unknown) */
100 public Date getModified() {
101 return new Date(file.lastModified());
102 }
103
104 /*** Returns the creation date (null if unknown) */
105 public Date getCreated() {
106 return null;
107 }
108
109 /*** Returns the size of the file in bytes (-1 if unknown) */
110 public long getSize() {
111 return file.length();
112 }
113
114
115 /*** Returns true if this is a container that can hold other files/folders */
116 public boolean isFolder() {
117 return file.isDirectory();
118 }
119
120 /*** Returns true if this represents the same file as the given one */
121 public boolean equals(StoreFile anotherFile) {
122 if (anotherFile instanceof LocalFile) {
123 return file.equals( ((LocalFile) anotherFile).file);
124 }
125 return false;
126 }
127
128 /*** Returns user representation - server path */
129 public String toString() {
130 return getPath();
131 }
132
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190