1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
7 import org.ibex.util.*;
11 // FEATURE: support for move
12 // FEATURE: support for bytestreams
13 // FEATURE: cache directories so we can do equality checking on them?
14 // FEATURE: autoconvert "true" to true and "0.3" to 0.3 on readback
17 * A crude mechanism for using a filesystem as object storage.
19 * This object represents a directory; writing a string, number, or
20 * boolean to any of its properties will create a file with the
21 * (encoded) property name as its filename and the "stringified"
22 * value as its contents.
24 * Writing 'null' to one of this object's properties will
25 * [recursively if necessary] delete the corresponding directory
28 * Writing any other object to one of this object's properties will
29 * create a new Directory object and copy the other object's keys()
30 * into the new Directory. This means that assigning one directory
31 * to a property of another directory will <i>copy</i> the directory,
32 * not move it. There is currently no way to move directories.
34 * If an object is written to a property that already has an entry,
35 * the old one is deleted (equivalent to writing 'null') first.
37 * WARNING: when instantiating a Directory object with a file
38 * argument that points to a non-directory File, this class will
39 * delete that file and create a directory!
41 public class Directory extends JS {
46 * Create the directory object. Existing directories will be
47 * preserved; if a file is present it will be obliterated.
50 public Directory(File f) throws IOException {
52 if (!f.exists()) new Directory(new File(f.getParent()));
53 if (!f.isDirectory()) destroy(f);
57 private static void destroy(File f) throws IOException {
58 if (!f.exists()) return;
59 if (f.isDirectory()) {
60 String[] entries = f.list();
61 for(int i=0; i<entries.length; i++) destroy(new File(f.getAbsolutePath() + File.separatorChar + entries[i]));
66 public void put(JS key0, JS val) throws JSExn {
68 if (key0 == null) return;
69 String key = toString(key0);
70 File f2 = new File(f.getAbsolutePath() + File.separatorChar + FileNameEncoder.encode(key));
72 if (val == null) return;
73 if (val instanceof JSPrimitive) {
74 OutputStream out = new FileOutputStream(f2);
75 Writer w = new OutputStreamWriter(out);
76 w.write(toString(val));
80 Directory d2 = new Directory(f2);
81 JS.Enumeration e = val.keys();
82 while(e.hasMoreElements()) {
83 JS k = e.nextElement();
88 } catch (IOException ioe) {
89 throw new JSExn.IO(ioe);
93 public JS get(JS key0) throws JSExn {
95 if (key0 == null) return null;
96 String key = toString(key0);
97 File f2 = new File(f.getAbsolutePath() + File.separatorChar + FileNameEncoder.encode(key));
98 if (!f2.exists()) return null;
99 if (f2.isDirectory()) return new Directory(f2);
100 char[] chars = new char[((int)f2.length()) * 2];
102 Reader r = new InputStreamReader(new FileInputStream(f2));
104 int numread = r.read(chars, numchars, chars.length - numchars);
105 if (numread == -1) return Script.S(new String(chars, 0, numchars));
108 } catch (IOException ioe) {
109 throw new JSExn.IO(ioe);
113 public Enumeration keys() {
114 final String[] elements = f.list();
115 return new Enumeration(null) {
117 public boolean _hasMoreElements() { return i < elements.length; }
118 public JS _nextElement() { return Script.S(FileNameEncoder.decode(elements[i++])); }