samer@0
|
1 /*
|
samer@0
|
2 * Properties.java
|
samer@0
|
3 *
|
samer@0
|
4 * Copyright (c) 2001, Samer Abdallah, King's College London.
|
samer@0
|
5 * All rights reserved.
|
samer@0
|
6 *
|
samer@0
|
7 * This software is provided AS IS and WITHOUT ANY WARRANTY;
|
samer@0
|
8 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
9 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
10 */
|
samer@0
|
11
|
samer@0
|
12 package samer.core.util;
|
samer@0
|
13 import samer.core.*;
|
samer@0
|
14 import java.io.*;
|
samer@0
|
15
|
samer@0
|
16 public class Properties extends Environment
|
samer@0
|
17 {
|
samer@0
|
18 protected java.util.Properties props;
|
samer@0
|
19 protected boolean writable;
|
samer@0
|
20
|
samer@0
|
21 public Properties(Environment parent) {
|
samer@0
|
22 this(parent,parent.node(),new java.util.Properties());
|
samer@0
|
23 }
|
samer@0
|
24 public Properties(Environment parent, java.util.Properties pr) {
|
samer@0
|
25 this(parent,parent.node(),pr);
|
samer@0
|
26 }
|
samer@0
|
27 public Properties(Environment parent, Node n) {
|
samer@0
|
28 this(parent,n,new java.util.Properties());
|
samer@0
|
29 }
|
samer@0
|
30 public Properties(Environment parent, Node n, java.util.Properties pr) {
|
samer@0
|
31 super(parent,n); props=pr; writable=true;
|
samer@0
|
32 }
|
samer@0
|
33
|
samer@0
|
34 public void save(OutputStream out) throws Exception { props.store(out,"properties"); }
|
samer@0
|
35 public void load(InputStream in) throws Exception { props.load(in); }
|
samer@0
|
36 public void setWritable(boolean f) { writable=f; }
|
samer@0
|
37 protected class Property implements Binding
|
samer@0
|
38 {
|
samer@0
|
39 String nm;
|
samer@0
|
40 String vl;
|
samer@0
|
41 int sc;
|
samer@0
|
42
|
samer@0
|
43 Property(String n, String v, int s) {
|
samer@0
|
44 nm=n; sc=s;
|
samer@0
|
45 vl = v.equals("~default") ? null : v;
|
samer@0
|
46 }
|
samer@0
|
47
|
samer@0
|
48 public String name() { return abs(nm); }
|
samer@0
|
49 public int score() { return sc; }
|
samer@0
|
50
|
samer@0
|
51 public Object get() { return vl; }
|
samer@0
|
52 public Object get(Codec c, Object def) { return c.decode(vl==null?def:vl); }
|
samer@0
|
53 public void get(Autocoder obj) { obj.decode(vl); }
|
samer@0
|
54
|
samer@0
|
55 public void set(Object obj) { props.put(nm,(String)obj); }
|
samer@0
|
56 public void remove() { props.remove(nm); }
|
samer@0
|
57 }
|
samer@0
|
58
|
samer@0
|
59 public void store(String name, Autocoder o) {
|
samer@0
|
60 if (writable && belongs(name)) props.put(rel(name),o.string());
|
samer@0
|
61 else parent.store(name,o);
|
samer@0
|
62 }
|
samer@0
|
63 public void store(String name, Object o, Codec c) {
|
samer@0
|
64 if (writable && belongs(name)) props.put(rel(name),c.string(o));
|
samer@0
|
65 else parent.store(name,o,c);
|
samer@0
|
66 }
|
samer@0
|
67
|
samer@0
|
68 public Datum datum(String name)
|
samer@0
|
69 {
|
samer@0
|
70 Datum local=localFind(rel(name)); // localised name
|
samer@0
|
71
|
samer@0
|
72 if (local.score()>0) {
|
samer@0
|
73 // if match is less than perfect, get parent's best match
|
samer@0
|
74 Datum inherited=parent.datum(abs(name));
|
samer@0
|
75 if (inherited.score()<local.score()) return inherited;
|
samer@0
|
76 }
|
samer@0
|
77 return local;
|
samer@0
|
78 }
|
samer@0
|
79
|
samer@0
|
80 public Binding binding(String name)
|
samer@0
|
81 {
|
samer@0
|
82 Binding local=localFind(rel(name)); // localised name
|
samer@0
|
83
|
samer@0
|
84 if (local.score()>0) {
|
samer@0
|
85 Binding inherited=parent.binding(abs(name));
|
samer@0
|
86 if (inherited.score()<local.score()) return inherited;
|
samer@0
|
87 }
|
samer@0
|
88 return local;
|
samer@0
|
89 }
|
samer@0
|
90
|
samer@0
|
91 public Iterator data()
|
samer@0
|
92 {
|
samer@0
|
93 return new CompoundIterator( new Iterator() {
|
samer@0
|
94 java.util.Enumeration enu=props.keys();
|
samer@0
|
95 public boolean hasNext() { return enu.hasMoreElements(); }
|
samer@0
|
96 public Datum next() {
|
samer@0
|
97 String key=(String)enu.nextElement();
|
samer@0
|
98 return new Property(key,props.getProperty(key),0);
|
samer@0
|
99 } }, parent.data() );
|
samer@0
|
100 }
|
samer@0
|
101
|
samer@0
|
102 public Binding localFind(String key)
|
samer@0
|
103 {
|
samer@0
|
104 String val, pname = '.'+key;
|
samer@0
|
105
|
samer@0
|
106 for (int i=0, s=0; i>=0; s++) {
|
samer@0
|
107 pname = pname.substring(i+1);
|
samer@0
|
108 val=props.getProperty(pname);
|
samer@0
|
109 if (val!=null) return new Property(pname,val,s);
|
samer@0
|
110 i = pname.indexOf('.');
|
samer@0
|
111 }
|
samer@0
|
112 return Null;
|
samer@0
|
113 }
|
samer@0
|
114 }
|
samer@0
|
115
|