Adds removeProperty(String path) and getEmptyNode()

This commit is contained in:
Simon Rigby 2011-02-12 01:44:03 +00:00 committed by EvilSeph
parent 3cc8c0dae0
commit 5d60a1bc48
2 changed files with 40 additions and 2 deletions

View File

@ -120,4 +120,13 @@ public class Configuration extends ConfigurationNode {
throw new ConfigurationException("Root document must be an key-value structure"); throw new ConfigurationException("Root document must be an key-value structure");
} }
} }
/**
* This method returns an empty ConfigurationNode for using as a
* default in methods that select a node from a node list.
* @return
*/
public static ConfigurationNode getEmptyNode() {
return new ConfigurationNode(new HashMap<String, Object>());
}
} }

View File

@ -13,7 +13,7 @@ import java.util.Map;
public class ConfigurationNode { public class ConfigurationNode {
protected Map<String, Object> root; protected Map<String, Object> root;
ConfigurationNode(Map<String, Object> root) { protected ConfigurationNode(Map<String, Object> root) {
this.root = root; this.root = root;
} }
@ -478,4 +478,33 @@ public class ConfigurationNode {
return null; return null;
} }
} }
}
/**
* Remove the property at a location. This will override existing
* configuration data to have it conform to key/value mappings.
*
* @param path
*/
@SuppressWarnings("unchecked")
public void removeProperty(String path) {
if (!path.contains(".")) {
root.remove(path);
return;
}
String[] parts = path.split("\\.");
Map<String, Object> node = root;
for (int i = 0; i < parts.length; i++) {
Object o = node.get(parts[i]);
// Found our target!
if (i == parts.length - 1) {
node.remove(parts[i]);
return;
}
node = (Map<String, Object>)o;
}
}
}