1
2
3
4
5
6
7
8
9
10 package org.astrogrid.store.delegate.myspace;
11 import java.io.FileNotFoundException;
12 import java.net.MalformedURLException;
13 import java.util.Hashtable;
14 import java.util.StringTokenizer;
15 import org.astrogrid.store.Agsl;
16 import org.astrogrid.store.Msrl;
17 import org.astrogrid.store.delegate.StoreFile;
18
19 /***
20 * Represents a folder in myspace. Not threadsafe.
21 * See also comments on @link MySpaceFile
22 *
23 * @author mch
24 */
25
26
27 public class MySpaceFolder extends MySpaceFile {
28
29 Hashtable children = new Hashtable();
30
31 boolean isRoot = false;
32
33 /*** Create folder from a myspace reference. Use to create root folders - to
34 * create entries that are children, use the constructor that tkaes a parent
35 public MySpaceFolder(Msrl givenMsrl, String givenName) {
36 this.agsl = givenMsrl.toAgsl();
37 name = givenName;
38 }
39
40 /** Creates a folder that is a child of another one. */
41 public MySpaceFolder(MySpaceFolder parent, String childName) {
42 super(parent, childName);
43 }
44
45 /*** Creates the root folder */
46 public MySpaceFolder(String childName) {
47 super(null, childName);
48 isRoot = true;
49 }
50
51 /*** Adds the given StoreFile as a child that exists in this folder */
52 public void add(StoreFile child) {
53 children.put(child.getName(), child);
54 }
55
56 /*** Returns the StoreFile representation of the child with the given filename */
57 public StoreFile getChild(String filename) {
58 return (StoreFile) children.get(filename);
59 }
60
61 /*** Returns an array of the files in this container */
62 public StoreFile[] listFiles() {
63 return (StoreFile[]) (children.values().toArray(new StoreFile[] {}));
64 }
65
66 /*** Returns path on server */
67 public String getPath() {
68 if (isRoot) {
69 return "";
70 }
71 else
72 {
73 return getParent().getPath()+getName()+"/";
74 }
75 }
76
77 /*** Returns the file path */
78 public String toString() { return getPath(); }
79
80 /*** Returns parent folder of this file/folder */
81 public StoreFile getParent() { return this.parentFolder; }
82
83 /*** Returns true - this is a container */
84 public boolean isFolder() { return true; }
85
86 /*** Returns false - this is a container */
87 public boolean isFile() { return false; }
88
89 /*** Returns the filename/foldername/tablename/etc */
90 public String getName() { return this.name; }
91
92 /*** Returns true if this represents the same file as the given one */
93 public boolean equals(StoreFile anotherFile) {
94 if (anotherFile instanceof MySpaceFolder) {
95 return name.equals( ((MySpaceFolder) anotherFile).name) &&
96 parentFolder.equals(((MySpaceFolder) anotherFile).parentFolder);
97 }
98 return false;
99 }
100
101 /***
102 * Returns the folder or file matching the given path in the *children* of
103 * this folder. So if the path is '/famous/stuff/main/' the returned StoreFile
104 * will be the MySpaceFolder instance representing the 'main' directory
105 * @todo test
106 */
107 public StoreFile findFile(String path) throws FileNotFoundException {
108
109
110 StringTokenizer dirTokens = new StringTokenizer(path, "/");
111 MySpaceFolder folder = this;
112 StoreFile child = null;
113 while (dirTokens.hasMoreTokens())
114 {
115 String token = dirTokens.nextToken();
116 child = folder.getChild(token);
117 if (child == null) {
118 throw new FileNotFoundException("No such token '"+token+"' in path "+path+" from "+this);
119 }
120 else {
121 if (child.isFolder()) {
122 folder = (MySpaceFolder) child;
123 }
124 }
125 }
126
127 if (dirTokens.hasMoreTokens()) {
128 throw new FileNotFoundException("path "+path+" only partly found from "+this);
129 }
130
131 return child;
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
191
192
193
194