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