Added node and writing functions to Configuration.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package org.bukkit.util.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -59,6 +60,42 @@ public class ConfigurationNode {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the property at a location. This will override existing
|
||||
* configuration data to have it conform to key/value mappings.
|
||||
*
|
||||
* @param path
|
||||
* @param value
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setProperty(String path, Object value) {
|
||||
if (!path.contains(".")) {
|
||||
root.put(path, value);
|
||||
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.put(parts[i], value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (o == null || !(o instanceof Map)) {
|
||||
// This will override existing configuration data!
|
||||
o = new HashMap<String, Object>();
|
||||
node.put(parts[i], o);
|
||||
}
|
||||
|
||||
node = (Map<String, Object>)o;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a string at a location. This will either return an String
|
||||
@@ -302,6 +339,81 @@ public class ConfigurationNode {
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of nodes. Non-valid entries will not be in the list.
|
||||
* There will be no null slots. If the list is not defined, the
|
||||
* default will be returned. 'null' can be passed for the default
|
||||
* and an empty list will be returned instead. The node must be
|
||||
* an actual node and cannot be just a boolean,
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @param def default value or null for an empty list as default
|
||||
* @return list of integers
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<ConfigurationNode> getNodeList(String path, List<ConfigurationNode> def) {
|
||||
List<Object> raw = getList(path);
|
||||
if (raw == null) {
|
||||
return def != null ? def : new ArrayList<ConfigurationNode>();
|
||||
}
|
||||
|
||||
List<ConfigurationNode> list = new ArrayList<ConfigurationNode>();
|
||||
for (Object o : raw) {
|
||||
if (o instanceof Map) {
|
||||
list.add(new ConfigurationNode((Map<String, Object>)o));
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a configuration node at a path. If the node doesn't exist or the
|
||||
* path does not lead to a node, null will be returned. A node has
|
||||
* key/value mappings.
|
||||
*
|
||||
* @param path
|
||||
* @return node or null
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public ConfigurationNode getNode(String path) {
|
||||
Object raw = getProperty(path);
|
||||
if (raw instanceof Map) {
|
||||
return new ConfigurationNode((Map<String, Object>)raw);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of nodes at a location. If the map at the particular location
|
||||
* does not exist or it is not a map, null will be returned.
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @return map of nodes
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, ConfigurationNode> getNodes(String path) {
|
||||
Object o = getProperty(path);
|
||||
if (o == null) {
|
||||
return null;
|
||||
} else if (o instanceof Map) {
|
||||
Map<String, ConfigurationNode> nodes =
|
||||
new HashMap<String, ConfigurationNode>();
|
||||
|
||||
for (Map.Entry<String, Object> entry : ((Map<String, Object>)o).entrySet()) {
|
||||
if (entry.getValue() instanceof Map) {
|
||||
nodes.put(entry.getKey(),
|
||||
new ConfigurationNode((Map<String, Object>) entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
return nodes;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts a value to an integer. May return null.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user