-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
FileConfiguration.java
243 lines (215 loc) · 9.39 KB
/
FileConfiguration.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
package org.simpleyaml.configuration.file;
import org.simpleyaml.configuration.Configuration;
import org.simpleyaml.configuration.LoadableConfiguration;
import org.simpleyaml.configuration.MemoryConfiguration;
import org.simpleyaml.configuration.comments.format.CommentFormatter;
import org.simpleyaml.exceptions.InvalidConfigurationException;
import org.simpleyaml.utils.Validate;
import java.io.*;
/**
* This is a base class for all File based implementations of {@link Configuration}
*
* @author Bukkit
* @author Carleslc
* @see <a href="https://github.com/Bukkit/Bukkit/tree/master/src/main/java/org/bukkit/configuration/file/FileConfiguration.java">Bukkit Source</a>
*/
public abstract class FileConfiguration extends MemoryConfiguration implements LoadableConfiguration {
/**
* Creates an empty {@link FileConfiguration} with no default values.
*/
public FileConfiguration() {
super();
}
/**
* Creates an empty {@link FileConfiguration} using the specified
* {@link Configuration} as a source for all default values.
*
* @param defaults Default value provider
*/
public FileConfiguration(final Configuration defaults) {
super(defaults);
}
/**
* Saves this {@link FileConfiguration} to the specified location.
* <p>
* If the file does not exist, it will be created. If already exists, it
* will be overwritten. If it cannot be overwritten or created, an
* exception will be thrown.
* <p>
* This method will use the {@link #options()} {@link FileConfigurationOptions#charset() charset} encoding,
* which defaults to UTF8.
*
* @param file File to save to.
* @throws IOException Thrown when the given file cannot be written to for any reason.
* @throws IllegalArgumentException Thrown when file is null.
*/
public void save(final File file) throws IOException {
Validate.notNull(file, "File cannot be null");
final File parents = file.getParentFile();
if (parents != null && !parents.exists() && !parents.mkdirs()) {
throw new IOException("Cannot create successfully all needed parent directories!");
}
this.save(new OutputStreamWriter(new FileOutputStream(file), this.options().charset()));
}
/**
* Saves this {@link FileConfiguration} to the specified location.
* <p>
* If the file does not exist, it will be created. If already exists, it
* will be overwritten. If it cannot be overwritten or created, an
* exception will be thrown.
* <p>
* This method will use the {@link #options()} {@link FileConfigurationOptions#charset() charset} encoding,
* which defaults to UTF8.
*
* @param file File to save to.
* @throws IOException Thrown when the given file cannot be written to for any reason.
* @throws IllegalArgumentException Thrown when file is null.
*/
public void save(final String file) throws IOException {
Validate.notNull(file, "File cannot be null");
this.save(new File(file));
}
@Override
public void save(final Writer writer) throws IOException {
Validate.notNull(writer, "Writer cannot be null");
try {
writer.write(this.saveToString());
} finally {
writer.close();
}
}
/**
* Loads this configuration from the specified file path.
* <p>
* All the values contained within this configuration will be removed,
* leaving only settings and defaults, and the new values will be loaded
* from the given file.
* <p>
* If the file cannot be loaded for any reason, an exception will be
* thrown.
* <p>
* This method will use the {@link #options()} {@link FileConfigurationOptions#charset() charset} encoding,
* which defaults to UTF8.
*
* @param file File to load from.
* @throws FileNotFoundException Thrown when the given file cannot be opened.
* @throws IOException Thrown when the given file cannot be read.
* @throws InvalidConfigurationException Thrown when the given file is not a valid Configuration.
* @throws IllegalArgumentException Thrown when file is null.
*/
public void load(final String file) throws FileNotFoundException, IOException, InvalidConfigurationException {
Validate.notNull(file, "File cannot be null");
this.load(new File(file));
}
/**
* Loads this configuration from the specified file.
* <p>
* All the values contained within this configuration will be removed,
* leaving only settings and defaults, and the new values will be loaded
* from the given file.
* <p>
* If the file cannot be loaded for any reason, an exception will be
* thrown.
* <p>
* This method will use the {@link #options()} {@link FileConfigurationOptions#charset() charset} encoding,
* which defaults to UTF8.
*
* @param file File to load from.
* @throws FileNotFoundException Thrown when the given file cannot be opened.
* @throws IOException Thrown when the given file cannot be read.
* @throws InvalidConfigurationException Thrown when the given file is not a valid Configuration.
* @throws IllegalArgumentException Thrown when file is null.
*/
public void load(final File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
Validate.notNull(file, "File cannot be null");
load(new FileInputStream(file));
}
/**
* Loads this configuration from the specified stream.
* <p>
* All the values contained within this configuration will be removed,
* leaving only settings and defaults, and the new values will be loaded
* from the given stream.
* <p>
* If the file cannot be loaded for any reason, an exception will be
* thrown.
* <p>
* This method will use the {@link #options()} {@link FileConfigurationOptions#charset() charset} encoding,
* which defaults to UTF8.
*
* @param stream Stream to load from
* @throws IOException Thrown when the given file cannot be read.
* @throws InvalidConfigurationException Thrown when the given file is not a valid Configuration.
* @throws IllegalArgumentException Thrown when stream is null.
* @see #load(Reader)
*/
public void load(final InputStream stream) throws IOException, InvalidConfigurationException {
Validate.notNull(stream, "Stream cannot be null");
load(new InputStreamReader(stream, this.options().charset()));
}
/**
* Loads this configuration from the specified reader.
* <p>
* All the values contained within this configuration will be removed,
* leaving only settings and defaults, and the new values will be loaded
* from the given stream.
* <p>
* If the file cannot be loaded for any reason, an exception will be thrown.
*
* @param reader the reader to load from
* @throws IOException Thrown when underlying reader throws an IOException.
* @throws InvalidConfigurationException Thrown when the reader does not represent a valid Configuration.
* @throws IllegalArgumentException Thrown when reader is null.
*/
@Override
public void load(final Reader reader) throws IOException, InvalidConfigurationException {
Validate.notNull(reader, "Reader cannot be null");
try (final BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader)) {
final StringBuilder builder = new StringBuilder();
String line;
while ((line = input.readLine()) != null) {
builder.append(line);
builder.append('\n');
}
this.loadFromString(builder.toString());
}
}
@Override
public FileConfigurationOptions options() {
if (this.options == null) {
this.options = new FileConfigurationOptions(this);
}
return (FileConfigurationOptions) this.options;
}
/**
* Compiles the header for this {@link FileConfiguration} and returns the
* result.
* <p>
* This will use the header from {@link #options()} {@link FileConfigurationOptions#header()},
* respecting the rules of {@link FileConfigurationOptions#copyHeader()}
* and {@link FileConfigurationOptions#headerFormatter()} if set.
*
* @return Compiled header
*/
public String buildHeader() {
final FileConfigurationOptions options = this.options();
if (!options.copyHeader()) {
return "";
}
final Configuration def = this.getDefaults();
if (def instanceof FileConfiguration) {
final FileConfiguration defaults = (FileConfiguration) def;
final String defaultsHeader = defaults.buildHeader();
if (defaultsHeader != null && !defaultsHeader.isEmpty()) {
return defaultsHeader;
}
}
final String header = options.header();
final CommentFormatter headerFormatter = options.headerFormatter();
if (headerFormatter != null) {
final String headerDump = headerFormatter.dump(header);
return headerDump != null ? headerDump : "";
}
return header != null && !header.isEmpty() ? header + '\n' : "";
}
}