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 void set(String path, Object value) { String[] split = path.split(Pattern.quote(Character.toString(getRoot().options().pathSeparator()))); ConfigurationSection section = this; if (path.length() == 0) { throw new IllegalArgumentException("Cannot set to an empty path"); } for (int i = 0; i < split.length - 1; i++) { ConfigurationSection last = section; section = last.getConfigurationSection(split[i]); if (section == null) { section = last.createSection(split[i]); } } String key = split[split.length - 1]; if (section == this) { if (value == null) { map.remove(key); } else { map.put(key, value); } } else { section.set(key, value); } }
public ConfigurationSection createSection(String path, Map<?, ?> map) { ConfigurationSection section = createSection(path); for (Map.Entry<?, ?> entry : map.entrySet()) { if (entry.getValue() instanceof Map) { section.createSection(entry.getKey().toString(), (Map<?, ?>) entry.getValue()); } else { section.set(entry.getKey().toString(), entry.getValue()); } } return section; }