public static String createPath( ConfigurationSection section, String key, ConfigurationSection relativeTo) { Configuration root = section.getRoot(); if (root == null) { throw new IllegalStateException("Cannot create path without a root"); } char separator = root.options().pathSeparator(); StringBuilder builder = new StringBuilder(); if (section != null) { for (ConfigurationSection parent = section; (parent != null) && (parent != relativeTo); parent = parent.getParent()) { if (builder.length() > 0) { builder.insert(0, separator); } builder.insert(0, parent.getName()); } } if ((key != null) && (key.length() > 0)) { if (builder.length() > 0) { builder.append(separator); } builder.append(key); } return builder.toString(); }
public ConfigurationSection createSection(String path) { Configuration root = getRoot(); if (root == null) { throw new IllegalStateException("Cannot create section without a root"); } final char separator = root.options().pathSeparator(); // i1 is the leading (higher) index // i2 is the trailing (lower) index int i1 = -1, i2; ConfigurationSection section = this; while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) { String node = path.substring(i2, i1); ConfigurationSection subSection = section.getConfigurationSection(node); if (subSection == null) { section = section.createSection(node); } else { section = subSection; } } String key = path.substring(i2); if (section == this) { ConfigurationSection result = new MemorySection(this, key); map.put(key, result); return result; } return section.createSection(key); }
public Object get(String path, Object def) { if (path.length() == 0) { return this; } Configuration root = getRoot(); if (root == null) { throw new IllegalStateException("Cannot access section without a root"); } final char separator = root.options().pathSeparator(); // i1 is the leading (higher) index // i2 is the trailing (lower) index int i1 = -1, i2; ConfigurationSection section = this; while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) { section = section.getConfigurationSection(path.substring(i2, i1)); if (section == null) { return def; } } String key = path.substring(i2); if (section == this) { Object result = map.get(key); return (result == null) ? def : result; } return section.get(key, def); }
public void set(String path, Object value) { Configuration root = getRoot(); if (root == null) { throw new IllegalStateException("Cannot use section without a root"); } final char separator = root.options().pathSeparator(); // i1 is the leading (higher) index // i2 is the trailing (lower) index int i1 = -1, i2; ConfigurationSection section = this; while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) { String node = path.substring(i2, i1); ConfigurationSection subSection = section.getConfigurationSection(node); if (subSection == null) { section = section.createSection(node); } else { section = subSection; } } String key = path.substring(i2); if (section == this) { if (value == null) { map.remove(key); } else { map.put(key, value); } } else { section.set(key, value); } }
public boolean isSet(String path) { Configuration root = getRoot(); if (root == null) { return false; } if (root.options().copyDefaults()) { return contains(path); } return get(path, null) != null; }
public Map<String, Object> getValues(boolean deep) { Map<String, Object> result = new LinkedHashMap<String, Object>(); Configuration root = getRoot(); if (root != null && root.options().copyDefaults()) { ConfigurationSection defaults = getDefaultSection(); if (defaults != null) { result.putAll(defaults.getValues(deep)); } } mapChildrenValues(result, this, deep); return result; }
public Set<String> getKeys(boolean deep) { Set<String> result = new LinkedHashSet<String>(); Configuration root = getRoot(); if (root != null && root.options().copyDefaults()) { ConfigurationSection defaults = getDefaultSection(); if (defaults != null) { result.addAll(defaults.getKeys(deep)); } } mapChildrenKeys(result, this, deep); return result; }