This commit is contained in:
Michael Bayne
2007-04-13 20:47:04 +00:00
parent 424aa5cd24
commit 573314875b
@@ -37,18 +37,17 @@ import com.samskivert.util.StringUtil;
import com.threerings.getdown.Log; import com.threerings.getdown.Log;
/** /**
* Parses a file containing key/value pairs and returns a {@link HashMap} * Parses a file containing key/value pairs and returns a {@link HashMap} with the values. Keys may
* with the values. Keys may be repeated, in which case they will be made * be repeated, in which case they will be made to reference an array of values.
* to reference an array of values.
*/ */
public class ConfigUtil public class ConfigUtil
{ {
/** /**
* Parses a configuration file containing key/value pairs. The file * Parses a configuration file containing key/value pairs. The file must be in the UTF-8
* must be in the UTF-8 encoding. * encoding.
* *
* @return a list of <code>String[]</code> instances containing the * @return a list of <code>String[]</code> instances containing the key/value pairs in the
* key/value pairs in the order they were parsed from the file. * order they were parsed from the file.
*/ */
public static List<String[]> parsePairs (File config, boolean checkPlatform) public static List<String[]> parsePairs (File config, boolean checkPlatform)
throws IOException throws IOException
@@ -61,8 +60,7 @@ public class ConfigUtil
FileInputStream fin = null; FileInputStream fin = null;
try { try {
fin = new FileInputStream(config); fin = new FileInputStream(config);
BufferedReader bin = BufferedReader bin = new BufferedReader(new InputStreamReader(fin, "UTF-8"));
new BufferedReader(new InputStreamReader(fin, "UTF-8"));
String line = null; String line = null;
while ((line = bin.readLine()) != null) { while ((line = bin.readLine()) != null) {
// nix comments // nix comments
@@ -103,14 +101,12 @@ public class ConfigUtil
platform = platform.substring(1); platform = platform.substring(1);
if (osname.indexOf(platform) != -1) { if (osname.indexOf(platform) != -1) {
Log.info("Skipping [platform=!" + platform + Log.info("Skipping [platform=!" + platform +
", key=" + pair[0] + ", key=" + pair[0] + ", value=" + pair[1] + "].");
", value=" + pair[1] + "].");
continue; continue;
} }
} else if (osname.indexOf(platform) == -1) { } else if (osname.indexOf(platform) == -1) {
Log.info("Skipping [platform=" + platform + Log.info("Skipping [platform=" + platform +
", key=" + pair[0] + ", key=" + pair[0] + ", value=" + pair[1] + "].");
", value=" + pair[1] + "].");
continue; continue;
} }
} }
@@ -128,23 +124,21 @@ public class ConfigUtil
} }
/** /**
* Parses a configuration file containing key/value pairs. The file * Parses a configuration file containing key/value pairs. The file must be in the UTF-8
* must be in the UTF-8 encoding. * encoding.
* *
* @return a map from keys to values, where a value will be an array * @return a map from keys to values, where a value will be an array of strings if more than
* of strings if more than one key/value pair in the config file was * one key/value pair in the config file was associated with the same key.
* associated with the same key.
*/ */
public static HashMap<String, Object> parseConfig (File config, public static HashMap<String, Object> parseConfig (File config, boolean checkPlatform)
boolean checkPlatform)
throws IOException throws IOException
{ {
List<String[]> pairs = parsePairs(config, checkPlatform); List<String[]> pairs = parsePairs(config, checkPlatform);
HashMap<String, Object> data = new HashMap<String, Object>(); HashMap<String, Object> data = new HashMap<String, Object>();
// I thought that we could use HashMap<String, String[]> and put // I thought that we could use HashMap<String, String[]> and put new String[] {pair[1]} for
// new String[] {pair[1]} for the null case, but it mysteriously dies // the null case, but it mysteriously dies on launch, so leaving it as HashMap<String,
// on launch, so leaving it as HashMap<String, Object> for now // Object> for now
for (Iterator iter = pairs.iterator(); iter.hasNext(); ) { for (Iterator iter = pairs.iterator(); iter.hasNext(); ) {
String[] pair = (String[])iter.next(); String[] pair = (String[])iter.next();
Object value = data.get(pair[0]); Object value = data.get(pair[0]);
@@ -165,9 +159,8 @@ public class ConfigUtil
} }
/** /**
* Massages a single string into an array and leaves existing array * Massages a single string into an array and leaves existing array values as is. Simplifies
* values as is. Simplifies access to parameters that are expected to * access to parameters that are expected to be arrays.
* be arrays.
*/ */
public static String[] getMultiValue (HashMap data, String name) public static String[] getMultiValue (HashMap data, String name)
{ {