|
@@ -28,7 +28,9 @@ import java.io.File;
|
28
|
28
|
import java.io.IOException;
|
29
|
29
|
import java.io.InputStream;
|
30
|
30
|
import java.nio.charset.Charset;
|
|
31
|
+import java.nio.file.Path;
|
31
|
32
|
import java.util.ArrayList;
|
|
33
|
+import java.util.Collection;
|
32
|
34
|
import java.util.GregorianCalendar;
|
33
|
35
|
import java.util.HashMap;
|
34
|
36
|
import java.util.List;
|
|
@@ -40,7 +42,7 @@ import java.util.Map;
|
40
|
42
|
public class ConfigFile extends TextFile {
|
41
|
43
|
|
42
|
44
|
/** A list of domains in this config file. */
|
43
|
|
- private final List<String> domains = new ArrayList<>();
|
|
45
|
+ private final Collection<String> domains = new ArrayList<>();
|
44
|
46
|
|
45
|
47
|
/** The values associated with each flat domain. */
|
46
|
48
|
private final MapList<String, String> flatdomains = new MapList<>();
|
|
@@ -78,6 +80,15 @@ public class ConfigFile extends TextFile {
|
78
|
80
|
this(new File(filename));
|
79
|
81
|
}
|
80
|
82
|
|
|
83
|
+ /**
|
|
84
|
+ * Creates a new config file from the specified path.
|
|
85
|
+ *
|
|
86
|
+ * @param path The path to read/write.
|
|
87
|
+ */
|
|
88
|
+ public ConfigFile(final Path path) {
|
|
89
|
+ super(path, Charset.forName("UTF-8"));
|
|
90
|
+ }
|
|
91
|
+
|
81
|
92
|
/**
|
82
|
93
|
* Sets the "automake" value of this config file. If automake is set to
|
83
|
94
|
* true, any calls to getKeyDomain will automatically create the domain
|
|
@@ -96,16 +107,14 @@ public class ConfigFile extends TextFile {
|
96
|
107
|
* @throws InvalidConfigFileException if the config file isn't valid
|
97
|
108
|
*/
|
98
|
109
|
public void read() throws IOException, InvalidConfigFileException {
|
99
|
|
- String domain = null;
|
100
|
|
- boolean keydomain = false;
|
101
|
|
- int offset;
|
102
|
|
-
|
103
|
110
|
keydomains.clear();
|
104
|
111
|
flatdomains.clear();
|
105
|
112
|
domains.clear();
|
106
|
113
|
|
107
|
114
|
readLines();
|
108
|
115
|
|
|
116
|
+ String domain = null;
|
|
117
|
+ boolean keydomain = false;
|
109
|
118
|
for (String line : getLines()) {
|
110
|
119
|
String tline = line;
|
111
|
120
|
|
|
@@ -116,9 +125,10 @@ public class ConfigFile extends TextFile {
|
116
|
125
|
|
117
|
126
|
if (tline.indexOf('#') == 0 || tline.isEmpty()) {
|
118
|
127
|
continue;
|
119
|
|
- } else if (
|
120
|
|
- (tline.endsWith(":") && !tline.endsWith("\\:"))
|
121
|
|
- && findEquals(tline) == -1) {
|
|
128
|
+ }
|
|
129
|
+
|
|
130
|
+ final int offset;
|
|
131
|
+ if (tline.endsWith(":") && !tline.endsWith("\\:") && findEquals(tline) == -1) {
|
122
|
132
|
domain = unescape(tline.substring(0, tline.length() - 1));
|
123
|
133
|
|
124
|
134
|
domains.add(domain);
|
|
@@ -160,8 +170,7 @@ public class ConfigFile extends TextFile {
|
160
|
170
|
final List<String> lines = new ArrayList<>();
|
161
|
171
|
|
162
|
172
|
lines.add("# This is a DMDirc configuration file.");
|
163
|
|
- lines.add("# Written on: " + new GregorianCalendar().getTime()
|
164
|
|
- .toString());
|
|
173
|
+ lines.add("# Written on: " + new GregorianCalendar().getTime());
|
165
|
174
|
|
166
|
175
|
writeMeta(lines);
|
167
|
176
|
|
|
@@ -181,7 +190,7 @@ public class ConfigFile extends TextFile {
|
181
|
190
|
} else {
|
182
|
191
|
for (Map.Entry<String, String> entry : keydomains.get(domain)
|
183
|
192
|
.entrySet()) {
|
184
|
|
- lines.add(" " + escape(entry.getKey()) + "="
|
|
193
|
+ lines.add(" " + escape(entry.getKey()) + '='
|
185
|
194
|
+ escape(entry.getValue()));
|
186
|
195
|
}
|
187
|
196
|
}
|
|
@@ -195,7 +204,7 @@ public class ConfigFile extends TextFile {
|
195
|
204
|
*
|
196
|
205
|
* @param lines The set of lines to be appended to
|
197
|
206
|
*/
|
198
|
|
- private void writeMeta(final List<String> lines) {
|
|
207
|
+ private void writeMeta(final Collection<String> lines) {
|
199
|
208
|
lines.add("");
|
200
|
209
|
lines.add("# This section indicates which sections below take "
|
201
|
210
|
+ "key/value");
|
|
@@ -205,9 +214,7 @@ public class ConfigFile extends TextFile {
|
205
|
214
|
lines.add("keysections:");
|
206
|
215
|
|
207
|
216
|
for (String domain : domains) {
|
208
|
|
- if ("keysections".equals(domain)) {
|
209
|
|
- continue;
|
210
|
|
- } else if (keydomains.containsKey(domain)) {
|
|
217
|
+ if (!"keysections".equals(domain) && keydomains.containsKey(domain)) {
|
211
|
218
|
lines.add(" " + domain);
|
212
|
219
|
}
|
213
|
220
|
}
|
|
@@ -286,7 +293,7 @@ public class ConfigFile extends TextFile {
|
286
|
293
|
* @param name The name of the domain to be added
|
287
|
294
|
* @param data The content of the domain
|
288
|
295
|
*/
|
289
|
|
- public void addDomain(final String name, final List<String> data) {
|
|
296
|
+ public void addDomain(final String name, final Collection<String> data) {
|
290
|
297
|
domains.add(name);
|
291
|
298
|
flatdomains.add(name, data);
|
292
|
299
|
}
|
|
@@ -308,7 +315,7 @@ public class ConfigFile extends TextFile {
|
308
|
315
|
* @param input The string to unescape
|
309
|
316
|
* @return The string with all escape chars (\) resolved
|
310
|
317
|
*/
|
311
|
|
- protected static String unescape(final String input) {
|
|
318
|
+ protected static String unescape(final CharSequence input) {
|
312
|
319
|
boolean escaped = false;
|
313
|
320
|
final StringBuilder temp = new StringBuilder();
|
314
|
321
|
|
|
@@ -354,7 +361,7 @@ public class ConfigFile extends TextFile {
|
354
|
361
|
* @param input The string to be searched
|
355
|
362
|
* @return The offset of the first non-escaped instance of '=', or -1.
|
356
|
363
|
*/
|
357
|
|
- protected static int findEquals(final String input) {
|
|
364
|
+ protected static int findEquals(final CharSequence input) {
|
358
|
365
|
boolean escaped = false;
|
359
|
366
|
|
360
|
367
|
for (int i = 0; i < input.length(); i++) {
|