Added a mechanism for retrieving a set of sub-properties (whose keys all
start with a particular prefix) from a configuration object. git-svn-id: https://samskivert.googlecode.com/svn/trunk@688 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: Config.java,v 1.10 2002/03/28 21:50:26 mdb Exp $
|
// $Id: Config.java,v 1.11 2002/03/28 22:21:06 mdb Exp $
|
||||||
//
|
//
|
||||||
// samskivert library - useful routines for java programs
|
// samskivert library - useful routines for java programs
|
||||||
// Copyright (C) 2001 Michael Bayne
|
// Copyright (C) 2001 Michael Bayne
|
||||||
@@ -305,6 +305,37 @@ public class Config
|
|||||||
return Class.forName(getValue(name, defcname)).newInstance();
|
return Class.forName(getValue(name, defcname)).newInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a properties file containing all configuration values that
|
||||||
|
* start with the supplied prefix (plus a trailing "." which will be
|
||||||
|
* added if it doesn't already exist). The keys in the sub-properties
|
||||||
|
* file will have had the prefix stripped off.
|
||||||
|
*/
|
||||||
|
public Properties getSubProperties (String prefix)
|
||||||
|
{
|
||||||
|
// slap a trailing dot on if necessary
|
||||||
|
if (!prefix.endsWith(".")) {
|
||||||
|
prefix = prefix + ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
// build the sub-properties
|
||||||
|
Properties props = new Properties();
|
||||||
|
Iterator iter = keys();
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
String key = (String)iter.next();
|
||||||
|
if (!key.startsWith(prefix)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String value = getValue(key, (String)null);
|
||||||
|
if (value == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
props.put(key.substring(prefix.length()), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return props;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an iterator that returns all of the configuration keys in
|
* Returns an iterator that returns all of the configuration keys in
|
||||||
* this config object.
|
* this config object.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#
|
#
|
||||||
# $Id: test.properties,v 1.1 2001/07/12 02:44:34 mdb Exp $
|
# $Id: test.properties,v 1.2 2002/03/28 22:21:06 mdb Exp $
|
||||||
#
|
#
|
||||||
# A test properties file
|
# A test properties file
|
||||||
|
|
||||||
@@ -8,3 +8,6 @@ prop2 = twenty five
|
|||||||
prop3 = 9, 8, 7, 6
|
prop3 = 9, 8, 7, 6
|
||||||
prop4 = one, two, three,, and a half, four
|
prop4 = one, two, three,, and a half, four
|
||||||
# prop5 = not defined
|
# prop5 = not defined
|
||||||
|
|
||||||
|
sub.sub1 = 5
|
||||||
|
sub.sub2 = whee!
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
//
|
//
|
||||||
// $Id: ConfigTest.java,v 1.2 2002/03/28 21:50:27 mdb Exp $
|
// $Id: ConfigTest.java,v 1.3 2002/03/28 22:21:06 mdb Exp $
|
||||||
|
|
||||||
package com.samskivert.util;
|
package com.samskivert.util;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
@@ -43,6 +44,12 @@ public class ConfigTest extends TestCase
|
|||||||
|
|
||||||
Iterator iter = config.keys();
|
Iterator iter = config.keys();
|
||||||
System.out.println("Keys: " + StringUtil.toString(iter));
|
System.out.println("Keys: " + StringUtil.toString(iter));
|
||||||
|
|
||||||
|
config.setValue("sub.sub3", "three");
|
||||||
|
|
||||||
|
Properties subprops = config.getSubProperties("sub");
|
||||||
|
System.out.println("Sub: " +
|
||||||
|
StringUtil.toString(subprops.propertyNames()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Test suite ()
|
public static Test suite ()
|
||||||
|
|||||||
Reference in New Issue
Block a user