|
| 1 | +package com.bencodez.simpleapi.file.config.configurate; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.LinkedHashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +import org.spongepowered.configurate.ConfigurationNode; |
| 12 | + |
| 13 | +import com.bencodez.simpleapi.file.config.ConfigView; |
| 14 | + |
| 15 | +/** |
| 16 | + * Read-only view of a Configurate section. Scalar numbers/booleans are read |
| 17 | + * strictly, rather than using Configurate's more permissive string coercions. |
| 18 | + * This matches the Bukkit scalar/list rules used by the existing binder. |
| 19 | + * |
| 20 | + * <p>Paths use '.' by default; at(String...) uses literal segments for identifiers |
| 21 | + * containing dots. An externally supplied node is a live view and the caller must |
| 22 | + * coordinate its mutation. YamlConfigDocument supplies detached snapshots.</p> |
| 23 | + * |
| 24 | + * <p>Getter/annotation defaults are supported. Bukkit's mutable default-section |
| 25 | + * overlay and native serialized Bukkit objects are deliberately not emulated.</p> |
| 26 | + */ |
| 27 | +public class ConfigurateConfigView implements ConfigView { |
| 28 | + protected final ConfigurationNode node; |
| 29 | + private final char separator; |
| 30 | + |
| 31 | + public ConfigurateConfigView(ConfigurationNode node) { this(node, '.'); } |
| 32 | + |
| 33 | + public ConfigurateConfigView(ConfigurationNode node, char separator) { |
| 34 | + this.node = Objects.requireNonNull(node, "node"); |
| 35 | + this.separator = separator; |
| 36 | + } |
| 37 | + |
| 38 | + protected final String[] segments(String path) { |
| 39 | + Objects.requireNonNull(path, "path"); |
| 40 | + return path.isEmpty() ? new String[0] : path.split(Pattern.quote(String.valueOf(separator)), -1); |
| 41 | + } |
| 42 | + |
| 43 | + protected final ConfigurationNode resolve(String... keys) { |
| 44 | + Objects.requireNonNull(keys, "keys"); |
| 45 | + ConfigurationNode current = node; |
| 46 | + for (String key : keys) { |
| 47 | + Objects.requireNonNull(key, "key"); |
| 48 | + ConfigurationNode match = null; |
| 49 | + // YAML numeric keys remain addressable as strings, without replacing |
| 50 | + // them with a second, differently typed key when an editor writes. |
| 51 | + for (Map.Entry<Object, ? extends ConfigurationNode> entry : current.childrenMap().entrySet()) { |
| 52 | + if (String.valueOf(entry.getKey()).equals(key)) { |
| 53 | + if (match != null) throw new IllegalArgumentException("Ambiguous configuration key"); |
| 54 | + match = entry.getValue(); |
| 55 | + } |
| 56 | + } |
| 57 | + current = match == null ? current.node(key) : match; |
| 58 | + } |
| 59 | + return current; |
| 60 | + } |
| 61 | + |
| 62 | + /** Returns a section using literal key segments, or null for an absent/non-map node. */ |
| 63 | + public ConfigurateConfigView at(String... keys) { |
| 64 | + ConfigurationNode child = resolve(keys); |
| 65 | + return child.isMap() || child == node && child.isNull() |
| 66 | + ? new ConfigurateConfigView(child, separator) : null; |
| 67 | + } |
| 68 | + |
| 69 | + @Override public boolean contains(String path) { |
| 70 | + return path.isEmpty() || !resolve(segments(path)).isNull(); |
| 71 | + } |
| 72 | + @Override public String getString(String path, String fallback) { |
| 73 | + ConfigurationNode child = resolve(segments(path)); |
| 74 | + if (child.isMap()) return fallback; // no Bukkit section toString emulation |
| 75 | + Object value = child.raw(); |
| 76 | + return value == null ? fallback : value.toString(); |
| 77 | + } |
| 78 | + @Override public boolean getBoolean(String path, boolean fallback) { |
| 79 | + Object value = resolve(segments(path)).rawScalar(); |
| 80 | + return value instanceof Boolean bool ? bool : fallback; |
| 81 | + } |
| 82 | + @Override public int getInt(String path, int fallback) { |
| 83 | + Object value = resolve(segments(path)).rawScalar(); |
| 84 | + return value instanceof Number number ? number.intValue() : fallback; |
| 85 | + } |
| 86 | + @Override public long getLong(String path, long fallback) { |
| 87 | + Object value = resolve(segments(path)).rawScalar(); |
| 88 | + return value instanceof Number number ? number.longValue() : fallback; |
| 89 | + } |
| 90 | + @Override public double getDouble(String path, double fallback) { |
| 91 | + Object value = resolve(segments(path)).rawScalar(); |
| 92 | + return value instanceof Number number ? number.doubleValue() : fallback; |
| 93 | + } |
| 94 | + @Override public List<String> getStringList(String path) { |
| 95 | + List<String> result = new ArrayList<>(); |
| 96 | + for (ConfigurationNode child : resolve(segments(path)).childrenList()) { |
| 97 | + Object value = child.rawScalar(); |
| 98 | + if (value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof Character) |
| 99 | + result.add(value.toString()); |
| 100 | + } |
| 101 | + return result; |
| 102 | + } |
| 103 | + @Override public List<Integer> getIntegerList(String path) { |
| 104 | + List<Integer> result = new ArrayList<>(); |
| 105 | + for (ConfigurationNode child : resolve(segments(path)).childrenList()) { |
| 106 | + Object value = child.rawScalar(); |
| 107 | + if (value instanceof Number number) result.add(number.intValue()); |
| 108 | + else if (value instanceof Character character) result.add((int) character); |
| 109 | + else if (value instanceof String string) { |
| 110 | + try { result.add(Integer.parseInt(string)); } catch (NumberFormatException ignored) { } |
| 111 | + } |
| 112 | + } |
| 113 | + return result; |
| 114 | + } |
| 115 | + @Override public boolean isConfigurationSection(String path) { return at(segments(path)) != null; } |
| 116 | + @Override public ConfigurateConfigView getConfigurationSection(String path) { return at(segments(path)); } |
| 117 | + @Override public Set<String> getKeys(boolean deep) { |
| 118 | + LinkedHashSet<String> result = new LinkedHashSet<>(); |
| 119 | + collectKeys(node, "", deep, result, 0); |
| 120 | + return result; |
| 121 | + } |
| 122 | + private void collectKeys(ConfigurationNode parent, String prefix, boolean deep, Set<String> keys, int depth) { |
| 123 | + if (depth > 64) throw new IllegalArgumentException("Configuration nesting is too deep"); |
| 124 | + Set<String> siblings = new LinkedHashSet<>(); |
| 125 | + for (Map.Entry<Object, ? extends ConfigurationNode> entry : parent.childrenMap().entrySet()) { |
| 126 | + String key = String.valueOf(entry.getKey()); |
| 127 | + if (!siblings.add(key)) throw new IllegalArgumentException("Ambiguous configuration key"); |
| 128 | + String path = prefix + key; |
| 129 | + keys.add(path); |
| 130 | + if (deep && entry.getValue().isMap()) collectKeys(entry.getValue(), path + separator, true, keys, depth + 1); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments