/
YAMLSection.java
184 lines (162 loc) · 4.8 KB
/
YAMLSection.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
package net.ME1312.Galaxi.Library.Config;
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.Galaxi.Library.Util;
import org.json.JSONObject;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* YAML Config Section Class
*/
@SuppressWarnings({"unchecked", "unused", "rawtypes"})
public class YAMLSection extends ObjectMap<String> {
private Yaml yaml;
/**
* Creates an empty YAML Section
*/
public YAMLSection() {
this.yaml = new Yaml(YAMLConfig.getDumperOptions());
}
/**
* Creates a YAML Section from an Input Stream
*
* @param stream Input Stream
* @throws YAMLException
*/
public YAMLSection(InputStream stream) throws YAMLException {
if (Util.isNull(stream)) throw new NullPointerException();
setAll((LinkedHashMap<String, Object>) (this.yaml = new Yaml(YAMLConfig.getDumperOptions())).loadAs(stream, LinkedHashMap.class));
}
/**
* Creates a YAML Section from a Reader
*
* @param reader Reader
* @throws YAMLException
*/
public YAMLSection(Reader reader) throws YAMLException {
if (Util.isNull(reader)) throw new NullPointerException();
setAll((LinkedHashMap<String, Object>) (this.yaml = new Yaml(YAMLConfig.getDumperOptions())).loadAs(reader, LinkedHashMap.class));
}
/**
* Creates a YAML Section from a String
*
* @param str String
* @throws YAMLException
*/
public YAMLSection(String str) throws YAMLException {
if (Util.isNull(str)) throw new NullPointerException();
setAll((LinkedHashMap<String, Object>) (this.yaml = new Yaml(YAMLConfig.getDumperOptions())).loadAs(str, LinkedHashMap.class));
}
/**
* Creates a YAML Section from Map Contents
*
* @param map Map
*/
public YAMLSection(Map<? extends String, ?> map) {
this(map, new Yaml(YAMLConfig.getDumperOptions()));
}
/**
* Creates a YAML Section from Map Contents
*
* @param map Map
*/
public YAMLSection(ObjectMap<String> map) {
super(map);
this.yaml = (map instanceof YAMLSection) ? ((YAMLSection) map).yaml : new Yaml(YAMLConfig.getDumperOptions());
}
/**
* Creates a YAML Section from JSON Contents
*
* @param json JSON
*/
public YAMLSection(JSONObject json) {
this(json.toString(4));
}
YAMLSection(Map<? extends String, ?> map, Yaml yaml) {
super(map);
this.yaml = yaml;
}
@Override
public YAMLSection clone() {
return (YAMLSection) super.clone();
}
@Override
protected ObjectMap<String> constructMap(Map<? extends String, ?> map) {
return new YAMLSection(map, yaml);
}
/**
* Get a YAML Section by Handle
*
* @param handle Handle
* @return Object Map
*/
public YAMLSection getSection(String handle) {
return (YAMLSection) super.getMap(handle);
}
/**
* Get a YAML Section by Handle
*
* @param handle Handle
* @param def Default
* @return Object Map
*/
public YAMLSection getSection(String handle, Map<? extends String, ?> def) {
return (YAMLSection) super.getMap(handle, def);
}
/**
* Get a YAML Section by Handle
*
* @param handle Handle
* @param def Default
* @return Object Map
*/
public YAMLSection getSection(String handle, ObjectMap<? extends String> def) {
return (YAMLSection) super.getMap(handle, def);
}
/**
* Get a YAML Section List by Handle
*
* @param handle Handle
* @return Object Map List
*/
public List<YAMLSection> getSectionList(String handle) {
return (List<YAMLSection>) (List) super.getMapList(handle);
}
/**
* Get a YAML Section List by Handle
*
* @param handle Handle
* @param def Default
* @return Object Map List
*/
public List<YAMLSection> getSectionList(String handle, Collection<? extends Map<? extends String, ?>> def) {
return (List<YAMLSection>) (List) super.getMapList(handle, def);
}
/**
* Get a YAML Section List by Handle
*
* @param handle Handle
* @param def Default
* @return Object Map List
*/
public List<YAMLSection> getSectionList(String handle, List<? extends ObjectMap<? extends String>> def) {
return (List<YAMLSection>) (List) super.getMapList(handle, def);
}
@Override
public synchronized String toString() {
return yaml.dump(get());
}
/**
* Convert to JSON
*
* @return JSON
*/
public JSONObject toJSON() {
return new JSONObject(get());
}
}