-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
YamlConfiguration.java
377 lines (341 loc) · 12.5 KB
/
YamlConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.denizenscript.denizencore.utilities;
import com.denizenscript.denizencore.scripts.ScriptBuilder;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;
import org.yaml.snakeyaml.scanner.ScannerImpl;
import java.io.InputStream;
import java.util.*;
/**
* Represents a YAML file.
*/
public class YamlConfiguration {
static {
ScannerImpl.ESCAPE_REPLACEMENTS.put('/', "/");
}
public static class CustomResolver extends Resolver {
@Override
protected void addImplicitResolvers() {
}
}
private static volatile boolean hasModernYaml = true; // Note: can be called async
public static Yaml createBaseYaml(boolean useCustomResolver) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setAllowUnicode(true);
LoaderOptions loaderOptions = new LoaderOptions();
try {
if (hasModernYaml) {
loaderOptions.setCodePointLimit(Integer.MAX_VALUE);
}
}
catch (NoSuchMethodError ignored) {
hasModernYaml = false; // pre-1.32 snakeyaml
}
return new Yaml(new SafeConstructor(), new Representer(), options, loaderOptions, useCustomResolver ? new CustomResolver() : new Resolver());
}
public static YamlConfiguration load(String data) {
return load(data, true);
}
public static YamlConfiguration load(String data, boolean useCustomResolver) {
Object obj = createBaseYaml(useCustomResolver).load(data);
return loadRaw(obj);
}
public static YamlConfiguration load(InputStream inputStream) {
return load(inputStream, true);
}
public static YamlConfiguration load(InputStream inputStream, boolean useCustomResolver) {
Object obj = createBaseYaml(useCustomResolver).load(inputStream);
return loadRaw(obj);
}
/**
* Loads a raw object into a YamlConfiguration. For internal use.
*/
public static YamlConfiguration loadRaw(Object obj) { // Note: can be called async
YamlConfiguration config = new YamlConfiguration();
if (obj == null) {
return null;
}
else if (obj instanceof String) {
config.contents = new LinkedHashMap<>();
config.contents.put(null, obj);
}
else if (obj instanceof Map) {
config.contents = (Map<StringHolder, Object>) obj;
}
else {
Debug.echoError("Invalid YAML object type: " + obj + " is " + obj.getClass().getSimpleName()); // Note: intentionally no opti classname call (async safety)
return null;
}
switchKeys(config.contents);
return config;
}
public Map<StringHolder, Object> contents;
boolean dirty;
/**
* Use StringHolders instead of strings.
*/
public static void switchKeys(Map<StringHolder, Object> objs) {
for (Map.Entry entry : new ArrayList<>(objs.entrySet())) {
Object key = entry.getKey();
Object value = entry.getValue();
objs.remove(key);
objs.put(key == null ? null : new StringHolder(key.toString()), value);
}
for (Map.Entry<StringHolder, Object> entry : objs.entrySet()) {
if (entry.getValue() instanceof Map) {
Map map = (Map<StringHolder, Object>) entry.getValue();
switchKeys(map);
objs.put(entry.getKey(), map);
}
else if (entry.getValue() instanceof List) {
List list = (List) entry.getValue();
List outList = new ArrayList();
for (Object obj : list) {
if (obj instanceof Map) {
Map map = new LinkedHashMap((Map) obj);
switchKeys(map);
outList.add(map);
}
else {
outList.add(obj);
}
}
objs.put(entry.getKey(), outList);
}
}
}
public static List reverseList(List vals, boolean patchLines) {
List output = new ArrayList<>(vals.size());
for (Object val : vals) {
if (val == null) {
continue;
}
if (val instanceof Map) {
output.add(reverse((Map) val, patchLines));
}
else if (val instanceof List) {
output.add(reverseList((List) val, patchLines));
}
else if (patchLines) {
output.add(ScriptBuilder.stripLinePrefix(val.toString()));
}
else {
output.add(val);
}
}
return output;
}
public static Map<String, Object> reverse(Map<StringHolder, Object> objs, boolean patchLines) {
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
for (Map.Entry<StringHolder, Object> obj : objs.entrySet()) {
if (obj.getValue() instanceof Map) {
map.put(obj.getKey().str, reverse((Map<StringHolder, Object>) obj.getValue(), patchLines));
}
else if (obj.getValue() instanceof List) {
List vals = (List) obj.getValue();
map.put(obj.getKey().str, reverseList(vals, patchLines));
}
else {
map.put(obj.getKey().str, obj.getValue());
}
}
return map;
}
public void forceLoweredRootKey(String keyName) {
StringHolder key = new StringHolder(CoreUtilities.toLowerCase(keyName));
Object obj = contents.get(key);
if (obj != null) {
contents.remove(key);
contents.put(key, obj);
}
}
public static List<String> patchListNonsense(List<Object> objs) {
List<String> list = new ArrayList<>();
for (Object o : objs) {
if (o == null) {
list.add("null");
}
else {
list.add(o.toString());
}
}
return list;
}
public YamlConfiguration() {
contents = new LinkedHashMap<>();
dirty = false;
}
public Set<StringHolder> getKeys(boolean deep) {
if (!deep) {
return new LinkedHashSet<>(contents.keySet());
}
else {
return getKeysDeep(contents, "");
}
}
public Map<StringHolder, Object> getMap() {
return new LinkedHashMap<>(contents);
}
public void addAll(Map<StringHolder, Object> newContents) {
contents.putAll(newContents);
}
private Set<StringHolder> getKeysDeep(Map<StringHolder, Object> objs, String base) {
Set<StringHolder> strings = new LinkedHashSet<>();
for (Map.Entry<StringHolder, Object> obj : objs.entrySet()) {
strings.add(new StringHolder(base + obj.getKey()));
if (obj.getValue() instanceof Map) {
strings.addAll(getKeysDeep((Map<StringHolder, Object>) obj.getValue(), base + obj.getKey() + "."));
}
}
return strings;
}
public String saveToString(boolean patchLines) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setAllowUnicode(true);
Yaml yaml = new Yaml(options);
String dumped = yaml.dump(reverse(contents, patchLines));
if (CoreConfiguration.debugVerbose) {
Debug.log("Outputting " + dumped);
}
return dumped;
}
public Object get(String path) {
if (path.isEmpty()) {
return contents;
}
List<String> parts = CoreUtilities.split(path, '.');
Map<StringHolder, Object> portion = contents;
for (int i = 0; i < parts.size(); i++) {
Object oPortion = portion.get(new StringHolder(parts.get(i)));
if (oPortion == null) {
return null;
}
else if (parts.size() == i + 1) {
return oPortion;
}
else if (oPortion instanceof Map) {
portion = (Map<StringHolder, Object>) oPortion;
}
else {
return null;
}
}
return null;
}
public void set(String path, Object o) {
if (o instanceof YamlConfiguration) {
o = new LinkedHashMap<>(((YamlConfiguration) o).contents);
}
List<String> parts = CoreUtilities.split(path, '.');
Map<StringHolder, Object> portion = contents;
for (int i = 0; i < parts.size(); i++) {
Object oPortion = portion.get(new StringHolder(parts.get(i)));
if (parts.size() == i + 1) {
if (o == null) {
portion.remove(new StringHolder(parts.get(i)));
emptyEmptyMaps(parts);
}
else {
portion.put(new StringHolder(parts.get(i)), o);
}
dirty = true;
return;
}
else if (oPortion == null) {
Map<StringHolder, Object> map = new LinkedHashMap<>();
portion.put(new StringHolder(parts.get(i)), map);
portion = map;
}
else if (oPortion instanceof Map) {
portion = (Map<StringHolder, Object>) oPortion;
}
else {
Map<StringHolder, Object> map = new LinkedHashMap<>();
portion.put(new StringHolder(parts.get(i)), map);
portion = map;
}
}
Debug.echoError("Failed to set somehow?");
}
void emptyEmptyMaps(List<String> parts) {
Map<StringHolder, Object> portion = contents;
for (int i = 0; i < parts.size(); i++) {
Object oPortion = portion.get(new StringHolder(parts.get(i)));
if (oPortion == null) {
return;
}
else if (oPortion instanceof Map) {
if (((Map<StringHolder, Object>) oPortion).isEmpty()) {
portion.remove(new StringHolder(parts.get(i)));
emptyEmptyMaps(parts);
return;
}
portion = (Map<StringHolder, Object>) oPortion;
}
else {
return;
}
}
}
public boolean contains(String path) {
return get(path) != null;
}
public String getString(String path) {
Object o = get(path);
if (o == null) {
return null;
}
return o.toString();
}
public String getString(String path, String def) {
Object o = get(path);
if (o == null) {
return def;
}
return o.toString();
}
public boolean isList(String path) {
Object o = get(path);
if (!(o instanceof List)) {
return false;
}
return true;
}
public List<Object> getList(String path) {
Object o = get(path);
if (!(o instanceof List)) {
return null;
}
return (List<Object>) o;
}
public List<String> getStringList(String path) {
Object o = get(path);
if (!(o instanceof List)) {
return null;
}
return patchListNonsense((List<Object>) o);
}
public YamlConfiguration getConfigurationSection(String path) {
Object o = get(path);
if (!(o instanceof Map)) {
return null;
}
YamlConfiguration configuration = new YamlConfiguration();
configuration.contents = (Map<StringHolder, Object>) o;
return configuration;
}
public boolean isDirty() {
return dirty;
}
public void setDirty(boolean dirty) {
this.dirty = dirty;
}
}