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: Update for new api
13 /** A JS interface to a Java '.properties' file; very crude */
14 public class PropertyFile extends JS.Immutable {
16 /*private final Properties p = new Properties();
17 private final Hash cache = new Hash(10, 3);
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)) + ".");
33 public void put(Object key, Object val) throws JSExn {
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);
42 } catch (IOException e) { throw new JSExn(e); }
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)));