1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
4 import org.ibex.util.*;
8 // FEATURE: support for move
9 // FEATURE: support for bytestreams
10 // FEATURE: cache directories so we can do equality checking on them?
11 // FEATURE: autoconvert "true" to true and "0.3" to 0.3 on readback
14 * A crude mechanism for using a filesystem as object storage.
16 * This object represents a directory; writing a string, number, or
17 * boolean to any of its properties will create a file with the
18 * (encoded) property name as its filename and the "stringified"
19 * value as its contents.
21 * Writing 'null' to one of this object's properties will
22 * [recursively if necessary] delete the corresponding directory
25 * Writing any other object to one of this object's properties will
26 * create a new Directory object and copy the other object's keys()
27 * into the new Directory. This means that assigning one directory
28 * to a property of another directory will <i>copy</i> the directory,
29 * not move it. There is currently no way to move directories.
31 * If an object is written to a property that already has an entry,
32 * the old one is deleted (equivalent to writing 'null') first.
34 * WARNING: when instantiating a Directory object with a file
35 * argument that points to a non-directory File, this class will
36 * delete that file and create a directory!
38 public class Directory extends JS {
43 * Create the directory object. Existing directories will be
44 * preserved; if a file is present it will be obliterated.
47 public Directory(File f) throws IOException {
49 if (!f.exists()) new Directory(new File(f.getParent()));
50 if (!f.isDirectory()) destroy(f);
54 private static void destroy(File f) throws IOException {
55 if (!f.exists()) return;
56 if (f.isDirectory()) {
57 String[] entries = f.list();
58 for(int i=0; i<entries.length; i++) destroy(new File(f.getAbsolutePath() + File.separatorChar + entries[i]));
63 public void put(JS key0, JS val) throws JSExn {
65 if (key0 == null) return;
66 String key = toString(key0);
67 File f2 = new File(f.getAbsolutePath() + File.separatorChar + FileNameEncoder.encode(key));
69 if (val == null) return;
70 if (val instanceof JSPrimitive) {
71 OutputStream out = new FileOutputStream(f2);
72 Writer w = new OutputStreamWriter(out);
73 w.write(toString(val));
77 Directory d2 = new Directory(f2);
78 JS.Enumeration e = val.keys();
79 while(e.hasMoreElements()) {
80 JS k = e.nextElement();
85 } catch (IOException ioe) {
86 throw new JSExn.IO(ioe);
90 public JS get(JS key0) throws JSExn {
92 if (key0 == null) return null;
93 String key = toString(key0);
94 File f2 = new File(f.getAbsolutePath() + File.separatorChar + FileNameEncoder.encode(key));
95 if (!f2.exists()) return null;
96 if (f2.isDirectory()) return new Directory(f2);
97 char[] chars = new char[((int)f2.length()) * 2];
99 Reader r = new InputStreamReader(new FileInputStream(f2));
101 int numread = r.read(chars, numchars, chars.length - numchars);
102 if (numread == -1) return JS.S(new String(chars, 0, numchars));
105 } catch (IOException ioe) {
106 throw new JSExn.IO(ioe);
110 public Enumeration keys() {
111 final String[] elements = f.list();
112 return new Enumeration(null) {
114 public boolean _hasMoreElements() { return i < elements.length; }
115 public JS _nextElement() { return JS.S(FileNameEncoder.decode(elements[i++])); }