/
YamlConfiguration.java
291 lines (267 loc) · 9.33 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
package com.denizenscript.denizencore.utilities;
import com.denizenscript.denizencore.utilities.debugging.dB;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.util.*;
/**
* Represents a YAML file.
*/
public class YamlConfiguration {
public static YamlConfiguration load(String data) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setAllowUnicode(true);
Yaml yaml = new Yaml(options);
Object obj = yaml.load(data);
YamlConfiguration config = new YamlConfiguration();
if (obj == null) {
return null;
}
else if (obj instanceof String) {
config.contents = new HashMap<>();
config.contents.put(null, obj);
}
else if (obj instanceof Map) {
config.contents = (Map<StringHolder, Object>) obj;
}
else {
dB.echoError("Invalid YAML object type: " + obj.toString() + " is " + obj.getClass().getSimpleName());
return null;
}
switchKeys(config.contents);
return config;
}
Map<StringHolder, Object> contents = null;
/**
* Use StringHolders instead of strings.
*/
private static void switchKeys(Map<StringHolder, Object> objs) {
for (Object o : new HashSet<Object>(objs.keySet())) {
Object got = objs.get(o);
objs.remove(o);
objs.put(new StringHolder(o.toString()), got);
}
for (Map.Entry<StringHolder, Object> str : objs.entrySet()) {
if (str.getValue() instanceof Map) {
Map map = (Map<StringHolder, Object>) str.getValue();
switchKeys(map);
objs.remove(map);
objs.put(str.getKey(), map);
}
}
}
private static Map<String, Object> reverse(Map<StringHolder, Object> objs) {
HashMap<String, Object> map = new HashMap<>();
for (Map.Entry<StringHolder, Object> obj : objs.entrySet()) {
if (obj.getValue() instanceof Map) {
map.put(obj.getKey().str, reverse((Map<StringHolder, Object>) obj.getValue()));
}
else {
map.put(obj.getKey().str, obj.getValue());
}
}
return map;
}
private 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 HashMap<>();
}
public Set<StringHolder> getKeys(boolean deep) {
if (!deep) {
return new HashSet<>(contents.keySet());
}
else {
return getKeysDeep(contents, "");
}
}
public Map<StringHolder, Object> getMap() {
return new HashMap<>(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 HashSet<>();
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() {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setAllowUnicode(true);
Yaml yaml = new Yaml(options);
String dumped = yaml.dump(reverse(contents));
if (dB.verbose) {
dB.log("Outputting " + dumped);
}
return dumped;
}
public Object get(String path) {
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 HashMap<>(((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);
}
return;
}
else if (oPortion == null) {
Map<StringHolder, Object> map = new HashMap<>();
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 HashMap<>();
portion.put(new StringHolder(parts.get(i)), map);
portion = map;
}
}
dB.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).size() == 0) {
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 == null) {
return false;
}
if (!(o instanceof List)) {
return false;
}
return true;
}
public List<Object> getList(String path) {
Object o = get(path);
if (o == null) {
return null;
}
if (!(o instanceof List)) {
return null;
}
return (List<Object>) o;
}
public List<String> getStringList(String path) {
Object o = get(path);
if (o == null) {
return null;
}
if (!(o instanceof List)) {
return null;
}
return patchListNonsense((List<Object>) o);
}
public YamlConfiguration getConfigurationSection(String path) {
try {
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) {
YamlConfiguration configuration = new YamlConfiguration();
if (!(oPortion instanceof Map)) {
return null;
}
configuration.contents = (Map<StringHolder, Object>) oPortion;
return configuration;
}
else if (oPortion instanceof Map) {
portion = (Map<StringHolder, Object>) oPortion;
}
else {
return null;
}
}
}
catch (Exception e) {
dB.echoError(e);
}
return null;
}
}