Browse Source

Allow ConfigFile to work on Paths.

Apply a bunch of style fixes.

Change-Id: I314e09dcebabcce452881754ef370c2495692030
Reviewed-on: http://gerrit.dmdirc.com/4000
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
changes/00/4000/2
Chris Smith 9 years ago
parent
commit
5b7f3a9358
1 changed files with 25 additions and 18 deletions
  1. 25
    18
      src/com/dmdirc/util/io/ConfigFile.java

+ 25
- 18
src/com/dmdirc/util/io/ConfigFile.java View File

28
 import java.io.IOException;
28
 import java.io.IOException;
29
 import java.io.InputStream;
29
 import java.io.InputStream;
30
 import java.nio.charset.Charset;
30
 import java.nio.charset.Charset;
31
+import java.nio.file.Path;
31
 import java.util.ArrayList;
32
 import java.util.ArrayList;
33
+import java.util.Collection;
32
 import java.util.GregorianCalendar;
34
 import java.util.GregorianCalendar;
33
 import java.util.HashMap;
35
 import java.util.HashMap;
34
 import java.util.List;
36
 import java.util.List;
40
 public class ConfigFile extends TextFile {
42
 public class ConfigFile extends TextFile {
41
 
43
 
42
     /** A list of domains in this config file. */
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
     /** The values associated with each flat domain. */
47
     /** The values associated with each flat domain. */
46
     private final MapList<String, String> flatdomains = new MapList<>();
48
     private final MapList<String, String> flatdomains = new MapList<>();
78
         this(new File(filename));
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
      * Sets the "automake" value of this config file. If automake is set to
93
      * Sets the "automake" value of this config file. If automake is set to
83
      * true, any calls to getKeyDomain will automatically create the domain
94
      * true, any calls to getKeyDomain will automatically create the domain
96
      * @throws InvalidConfigFileException if the config file isn't valid
107
      * @throws InvalidConfigFileException if the config file isn't valid
97
      */
108
      */
98
     public void read() throws IOException, InvalidConfigFileException {
109
     public void read() throws IOException, InvalidConfigFileException {
99
-        String domain = null;
100
-        boolean keydomain = false;
101
-        int offset;
102
-
103
         keydomains.clear();
110
         keydomains.clear();
104
         flatdomains.clear();
111
         flatdomains.clear();
105
         domains.clear();
112
         domains.clear();
106
 
113
 
107
         readLines();
114
         readLines();
108
 
115
 
116
+        String domain = null;
117
+        boolean keydomain = false;
109
         for (String line : getLines()) {
118
         for (String line : getLines()) {
110
             String tline = line;
119
             String tline = line;
111
 
120
 
116
 
125
 
117
             if (tline.indexOf('#') == 0 || tline.isEmpty()) {
126
             if (tline.indexOf('#') == 0 || tline.isEmpty()) {
118
                 continue;
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
                 domain = unescape(tline.substring(0, tline.length() - 1));
132
                 domain = unescape(tline.substring(0, tline.length() - 1));
123
 
133
 
124
                 domains.add(domain);
134
                 domains.add(domain);
160
         final List<String> lines = new ArrayList<>();
170
         final List<String> lines = new ArrayList<>();
161
 
171
 
162
         lines.add("# This is a DMDirc configuration file.");
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
         writeMeta(lines);
175
         writeMeta(lines);
167
 
176
 
181
             } else {
190
             } else {
182
                 for (Map.Entry<String, String> entry : keydomains.get(domain)
191
                 for (Map.Entry<String, String> entry : keydomains.get(domain)
183
                         .entrySet()) {
192
                         .entrySet()) {
184
-                    lines.add("  " + escape(entry.getKey()) + "="
193
+                    lines.add("  " + escape(entry.getKey()) + '='
185
                             + escape(entry.getValue()));
194
                             + escape(entry.getValue()));
186
                 }
195
                 }
187
             }
196
             }
195
      *
204
      *
196
      * @param lines The set of lines to be appended to
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
         lines.add("");
208
         lines.add("");
200
         lines.add("# This section indicates which sections below take "
209
         lines.add("# This section indicates which sections below take "
201
                 + "key/value");
210
                 + "key/value");
205
         lines.add("keysections:");
214
         lines.add("keysections:");
206
 
215
 
207
         for (String domain : domains) {
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
                 lines.add("  " + domain);
218
                 lines.add("  " + domain);
212
             }
219
             }
213
         }
220
         }
286
      * @param name The name of the domain to be added
293
      * @param name The name of the domain to be added
287
      * @param data The content of the domain
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
         domains.add(name);
297
         domains.add(name);
291
         flatdomains.add(name, data);
298
         flatdomains.add(name, data);
292
     }
299
     }
308
      * @param input The string to unescape
315
      * @param input The string to unescape
309
      * @return The string with all escape chars (\) resolved
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
         boolean escaped = false;
319
         boolean escaped = false;
313
         final StringBuilder temp = new StringBuilder();
320
         final StringBuilder temp = new StringBuilder();
314
 
321
 
354
      * @param input The string to be searched
361
      * @param input The string to be searched
355
      * @return The offset of the first non-escaped instance of '=', or -1.
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
         boolean escaped = false;
365
         boolean escaped = false;
359
 
366
 
360
         for (int i = 0; i < input.length(); i++) {
367
         for (int i = 0; i < input.length(); i++) {

Loading…
Cancel
Save