|
From: <tr...@us...> - 2003-07-01 12:48:11
|
Update of /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core
In directory sc8-pr-cvs1:/tmp/cvs-serv1905
Added Files:
TieredConfigurationHelper.java
Log Message:
--- NEW FILE: TieredConfigurationHelper.java ---
package com.babeldoc.core;
import com.babeldoc.core.config.IConfig;
import java.util.*;
/**
*/
public class TieredConfigurationHelper {
private IConfig config;
private Map configs;
public TieredConfigurationHelper(IConfig config) {
this.config = config;
}
public Map getNamedConfigs() {
if(configs==null) {
extractedTieredConfigs();
}
return configs;
}
protected void extractedTieredConfigs() {
configs = new HashMap();
Set keys = config.keys();
for(Iterator i = keys.iterator(); i.hasNext();) {
String key = (String)i.next();
int dotIndex = key.indexOf('.');
if(dotIndex>0) {
String name = key.substring(0, dotIndex);
String rest = key.substring(dotIndex+1);
String value = config.getString(key);
Map tconfig = (Map)configs.get(name);
if(tconfig==null) {
tconfig = new HashMap();
configs.put(name, tconfig);
}
tconfig.put(rest, value);
}
}
}
public static void main(String[] args) {
class TestConfig extends Named implements IConfig {
Properties props;
TestConfig(String name, Properties props) {
super(name);
this.props = props;
}
public boolean isDirty() {
return false;
}
public void setString(String key, String value) {
props.setProperty(key, value);
}
public String getString(String name) {
return props.getProperty(name);
}
public boolean hasKey(String name) {
return props.getProperty(name)!=null;
}
public Set keys() {
return props.keySet();
}
}
TestConfig cfg = new TestConfig("test", new Properties());
cfg.setString("roger.this", "that");
cfg.setString("roger.one", "1");
cfg.setString("roger.two", "2");
cfg.setString("roger.three", "3");
cfg.setString("roger.four", "4");
cfg.setString("roger.five", "5");
cfg.setString("mike.two", "2");
cfg.setString("mike.three", "3");
cfg.setString("mike.four", "4");
cfg.setString("mike.five", "5");
cfg.setString("peter.four.alpha", "4");
cfg.setString("peter.five.beta", "5");
cfg.setString(".info.broken", "aaa");
Map map = new TieredConfigurationHelper(cfg).getNamedConfigs();
for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
String name = (String) iterator.next();
System.out.println("Map name: "+name);
Map inner = (Map)map.get(name);
for (Iterator iterator1 = inner.keySet().iterator(); iterator1.hasNext();) {
String s = (String) iterator1.next();
String v = (String)inner.get(s);
System.out.println(" "+s+"="+v);
}
}
}
}
|