licensing update to APSL 2.0
[org.ibex.js.git] / src / org / ibex / js / PropertyFile.java
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.
4
5 package org.ibex.js; 
6
7 import org.ibex.util.*; 
8 import java.util.*;
9 import java.io.*;
10
11 // FEATURE: Update for new api
12
13 /** A JS interface to a Java '.properties' file; very crude */
14 public class PropertyFile extends JS {
15
16     /*private final Properties p = new Properties();
17     private final Hash cache = new Hash(10, 3);
18     private File f;
19
20     private class Minion extends JS {
21         private final String prefix;
22         Minion(String prefix) { this.prefix = prefix; }
23         public Number coerceToNumber()  { return N(coerceToString()); }
24         public Boolean coerceToBoolean() { return B(coerceToString().equals("true")); }
25         public String coerceToString()  { return (String)p.get(prefix.substring(0, prefix.length() - 1)); }
26         public Enumeration keys() throws JSExn { throw new JSExn("PropertyFile.keys() not supported"); }
27         public Object get(Object key) throws JSExn {
28             if (toString(key).equals("")) return coerceToString();
29             Object ret = p.get(prefix + escape(toString(key)));
30             if (ret != null) return ret;
31             return new Minion(prefix + escape(toString(key)) + ".");
32         }
33         public void put(Object key, Object val) throws JSExn {
34             try {
35                 p.put(prefix + (prefix.equals("") ? "" : ".") + escape(toString(key)), toString(val));
36                 File fnew = new File(f.getName() + ".new");
37                 FileOutputStream fo = new FileOutputStream(fnew);
38                 p.save(fo, "");
39                 fo.close();
40                 fnew.renameTo(f);
41                 f = fnew;
42             } catch (IOException e) { throw new JSExn(e); }
43         }
44     }
45
46     public static String escape(String s) {
47         return s.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\.", "\\\\.").replaceAll("=","\\\\="); }
48     public PropertyFile(File f) throws IOException { this.f = f; this.p.load(new FileInputStream(f)); }
49     public void put(Object key, Object val) throws JSExn { new Minion("").put(key, val); }
50     public Enumeration keys() throws JSExn { return new Minion("").keys(); }
51     public Object get(Object key) throws JSExn {
52         Object ret = p.get(toString(key));
53         if (ret != null) return ret;
54         return new Minion(escape(toString(key)));
55     }*/
56 }