001: package pygmy.core;
002:
003: import java.util.*;
004: import java.io.PrintStream;
005: import java.io.PrintWriter;
006:
007: public class ChainableProperties extends Properties {
008:
009: Properties parent;
010:
011: public ChainableProperties() {
012: parent = null;
013: }
014:
015: public ChainableProperties(Properties parent) {
016: this .parent = parent;
017: }
018:
019: public Properties getParent() {
020: return parent;
021: }
022:
023: public String getProperty(String key) {
024: String value = super .getProperty(key);
025: if (value == null && parent != null) {
026: value = parent.getProperty(key);
027: }
028: value = resolveVariables(value, null);
029: return value;
030: }
031:
032: public String getProperty(String key, String defaultValue) {
033: String value = super .getProperty(key, defaultValue);
034: if (value == null && parent != null) {
035: value = parent.getProperty(key, defaultValue);
036: }
037: value = resolveVariables(value, defaultValue);
038: return value;
039: }
040:
041: private String resolveVariables(String value, String defaultValue) {
042: if (value != null) {
043: int start = value.indexOf("${"); //todo provide a mechanism to escape sequence the ${
044: StringBuffer buf = new StringBuffer(value.substring(0,
045: (start >= 0 ? start : value.length())));
046: while (start >= 0) {
047: int end = value.indexOf('}', start);
048: String tmp = getProperty(value
049: .substring(start + 2, end), defaultValue);
050: if (tmp != null) {
051: buf.append(tmp);
052: } else {
053: buf.append(value.substring(start, end + 1));
054: }
055: start = value.indexOf("${", end);
056: buf.append(value.substring(end + 1, (start >= 0 ? start
057: : value.length())));
058: }
059: return buf.toString();
060: } else {
061: return defaultValue;
062: }
063: }
064:
065: public Enumeration propertyNames() {
066: Vector set = new Vector(parent.keySet());
067: set.addAll(super .keySet());
068: return set.elements();
069: }
070:
071: public synchronized Object get(Object key) {
072: Object value = super .get(key);
073: if (value == null && parent != null) {
074: value = parent.get(key);
075: }
076: // if( value instanceof String ) {
077: // value = resolveVariables( (String)value, null );
078: // }
079: return value;
080: }
081:
082: public synchronized boolean containsKey(Object key) {
083: boolean contains = super .containsKey(key);
084: if (!contains && parent != null) {
085: contains = parent.containsKey(key);
086: }
087: return contains;
088: }
089:
090: public boolean containsValue(Object value) {
091: boolean contains = super .containsValue(value);
092: if (!contains && parent != null) {
093: contains = parent.containsValue(value);
094: }
095: return contains;
096: }
097:
098: public synchronized boolean contains(Object value) {
099: boolean contains = super .contains(value);
100: if (!contains && parent != null) {
101: contains = parent.contains(value);
102: }
103: return contains;
104: }
105:
106: public void list(PrintStream out) {
107: super .list(out);
108: parent.list(out);
109: }
110:
111: public void list(PrintWriter out) {
112: super.list(out);
113: parent.list(out);
114: }
115: }
|