diff --git a/projects/samskivert/src/java/com/samskivert/util/Config.java b/projects/samskivert/src/java/com/samskivert/util/Config.java index 81c03894..a93d1e55 100644 --- a/projects/samskivert/src/java/com/samskivert/util/Config.java +++ b/projects/samskivert/src/java/com/samskivert/util/Config.java @@ -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 // Copyright (C) 2001 Michael Bayne @@ -305,6 +305,37 @@ public class Config 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 * this config object. diff --git a/projects/samskivert/tests/rsrc/util/test.properties b/projects/samskivert/tests/rsrc/util/test.properties index 0252ea9c..755c97e5 100644 --- a/projects/samskivert/tests/rsrc/util/test.properties +++ b/projects/samskivert/tests/rsrc/util/test.properties @@ -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 @@ -8,3 +8,6 @@ prop2 = twenty five prop3 = 9, 8, 7, 6 prop4 = one, two, three,, and a half, four # prop5 = not defined + +sub.sub1 = 5 +sub.sub2 = whee! diff --git a/projects/samskivert/tests/src/java/com/samskivert/util/ConfigTest.java b/projects/samskivert/tests/src/java/com/samskivert/util/ConfigTest.java index 43290cf3..9a2ea9a3 100644 --- a/projects/samskivert/tests/src/java/com/samskivert/util/ConfigTest.java +++ b/projects/samskivert/tests/src/java/com/samskivert/util/ConfigTest.java @@ -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; import java.util.Iterator; +import java.util.Properties; import junit.framework.Test; import junit.framework.TestCase; @@ -43,6 +44,12 @@ public class ConfigTest extends TestCase Iterator iter = config.keys(); 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 ()