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