-
Notifications
You must be signed in to change notification settings - Fork 0
/
yamltemp
261 lines (209 loc) · 5.87 KB
/
yamltemp
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
package com.booksaw.corruption.configuration;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.yaml.snakeyaml.Yaml;
public class YamlConfiguration {
private Map<String, Object> obj;
private File f;
private Yaml yaml;
public YamlConfiguration(File f) {
this.f = f;
yaml = new Yaml();
InputStream inputStream = null;
try {
inputStream = new FileInputStream(f);
} catch (FileNotFoundException e) {
return;
}
obj = yaml.load(inputStream);
}
@SuppressWarnings("unchecked")
public Object getSetting(String ref) {
String[] split = ref.split("\\.");
Object toReturn = null;
for (String temp : split) {
if (toReturn == null) {
try {
toReturn = obj.get(temp);
} catch (Exception e) {
return null;
}
continue;
}
if (toReturn instanceof HashMap<?, ?>)
toReturn = ((HashMap<String, Object>) toReturn).get(temp);
}
return toReturn;
}
public void set(String ref, Object value) {
System.out.println("setting " + ref + ": " + value);
if (ref.contains(".")) {
dotSet(ref, value);
} else {
obj.put(ref, value);
}
}
private HashMap<String, Object> getHash(String path, String[] args, int loc, Object toSet) {
HashMap<String, Object> map = new HashMap<>();
if (args.length - 1 > loc) {
path = path + ((path.equals("") ? "" : ".") + args[loc]);
map.put(args[loc], getHash(path, args, loc + 1, toSet));
} else {
map.put(args[loc], toSet);
}
return map;
}
@SuppressWarnings("unchecked")
private void dotSet(String ref, Object toSet) {
String[] split = ref.split("\\.");
String path = "";
Map<String, Object> map = null, older = null;
for (int i = 0; i < split.length; i++) {
// adding to the path
path = path + ((path.equals("") ? "" : ".") + split[i]);
Object temp = getSetting(path);
System.out.println("path = " + path + " temp = " + temp);
if (temp != null && (temp instanceof Map<?, ?> && !path.equals(""))) {
// if there is a sub map which is not null
older = map;
map = (HashMap<String, Object>) temp;
continue;
} else {
// checking if there is a prior path
if (map != null) {
// removing the old values
if (older != null)
older.remove(split[i - 1]);
else
obj.remove(split[0]);
// getting the hash as there is no more valid subhashes
HashMap<String, Object> test = getHash(path, split, i, toSet);
System.out.println("path = " + path);
System.out.println("test = " + test);
System.out.println("map = " + map);
// merging
// for (Entry<String, Object> toMerge : map.entrySet()) {
// if (!test.containsKey(toMerge.getKey())) {
// test.put(toMerge.getKey(), toMerge.getValue());
// }
// }
if (older != null) {
for (Entry<String, Object> toMerge : older.entrySet()) {
if (!test.containsKey(toMerge.getKey())) {
test.put(toMerge.getKey(), toMerge.getValue());
}
}
older.put(split[i - 1], test);
} else {
obj.put(split[0], test);
}
} else {
// if there is no map so it is just the base ref
System.out.println("YAML setting base");
obj.put(split[0], getHash(path, split, i, toSet));
}
return;
}
}
}
public void saveConfiguration() {
FileWriter pw;
try {
pw = new FileWriter(f);
pw.write(saveCon(obj, 0, ""));
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Uses recursion to generate a final string to be printed which represents the
* file
*
* @param obj - The map to generate the output for
* @param level - The indentation level (usually leave at 0 and leave the
* recursion to change
* @param priortree - The prior reference ie (foo.bar)
* @return - The output for that map
*/
@SuppressWarnings("unchecked")
private String saveCon(Map<String, Object> obj, int level, String priortree) {
if (obj == null) {
return "";
}
String indent = "";
for (int i = 0; i < level; i++) {
indent = indent + " ";
}
String toReturn = "";
for (Entry<String, Object> temp : obj.entrySet()) {
String ref = (priortree.equals("")) ? temp.getKey() : priortree + "." + temp.getKey();
Object out = getSetting(ref);
if (out instanceof ArrayList<?>) {
toReturn = toReturn + indent + temp.getKey() + ":\n";
for (String r : ((ArrayList<String>) out)) {
toReturn = toReturn + indent + "- " + r + "\n";
}
continue;
}
if (!(out instanceof Map<?, ?>)) {
String[] split = temp.getKey().split("\\.");
toReturn = toReturn + indent + split[split.length - 1] + ": " + out + "\n";
continue;
}
toReturn = toReturn + indent + temp.getKey() + ":\n";
toReturn = toReturn + saveCon(((Map<String, Object>) out), level + 1, ref);
}
return toReturn;
}
@SuppressWarnings("unchecked")
public List<String> getStringList(String ref) {
Object o = getSetting(ref);
if (o instanceof ArrayList<?>) {
return (ArrayList<String>) o;
}
return new ArrayList<>();
}
public boolean getBoolean(String ref) {
Object o = getSetting(ref);
if (o instanceof Boolean) {
return (Boolean) o;
}
return false;
}
public String getString(String ref) {
Object o = getSetting(ref);
if (o != null) {
return o.toString();
}
return "";
}
public int getInteger(String ref) {
Object o = getSetting(ref);
try {
return Integer.parseInt(o.toString());
} catch (Exception e) {
return 0;
}
}
public double getDouble(String ref) {
Object o = getSetting(ref);
try {
return Double.parseDouble(o.toString());
} catch (Exception e) {
return 0;
}
}
public boolean isNull() {
return obj == null || obj.size() == 0;
}
}