Adds removeProperty(String path) and getEmptyNode()

By: Simon Rigby <rigby@rigbydev.co.uk>
This commit is contained in:
Bukkit/Spigot 2011-02-12 01:44:03 +00:00
parent 204952eed0
commit 13a3b24c47
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");
}
}
/**
* 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 {
protected Map<String, Object> root;
ConfigurationNode(Map<String, Object> root) {
protected ConfigurationNode(Map<String, Object> root) {
this.root = root;
}
@ -478,4 +478,33 @@ public class ConfigurationNode {
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;
}
}
}