- Notifications
You must be signed in to change notification settings - Fork 7k
/
Copy pathPreferencesMap.java
343 lines (312 loc) · 8.78 KB
/
PreferencesMap.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
PreferencesMap - A Map<String, String> with some useful features
to handle preferences.
Part of the Arduino project - http://www.arduino.cc/
Copyright (c) 2014 Cristian Maglie
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
packageprocessing.app.helpers;
importorg.apache.commons.compress.utils.IOUtils;
importprocessing.app.legacy.PApplet;
importjava.io.*;
importjava.util.LinkedHashMap;
importjava.util.Map;
importjava.util.SortedSet;
importjava.util.TreeSet;
@SuppressWarnings("serial")
publicclassPreferencesMapextendsLinkedHashMap<String, String> {
publicPreferencesMap(Map<String, String> table) {
super(table);
}
/**
* Create a PreferencesMap and load the content of the file passed as
* argument.
*
* Is equivalent to:
*
* <pre>
* PreferencesMap map = new PreferencesMap();
* map.load(file);
* </pre>
*
* @param file
* @throws IOException
*/
publicPreferencesMap(Filefile) throwsIOException {
super();
load(file);
}
publicPreferencesMap() {
super();
}
/**
* Parse a property list file and put kev/value pairs into the Map
*
* @param file
* @throws FileNotFoundException
* @throws IOException
*/
publicvoidload(Filefile) throwsIOException {
FileInputStreamfileInputStream = null;
try {
fileInputStream = newFileInputStream(file);
load(fileInputStream);
} finally {
IOUtils.closeQuietly(fileInputStream);
}
}
protectedStringprocessPlatformSuffix(Stringkey, Stringsuffix, booleanisCurrentPlatform) {
if (key == null)
returnnull;
// Key does not end with the given suffix? Process as normal
if (!key.endsWith(suffix))
returnkey;
// Not the current platform? Ignore this key
if (!isCurrentPlatform)
returnnull;
// Strip the suffix from the key
returnkey.substring(0, key.length() - suffix.length());
}
/**
* Parse a property list stream and put key/value pairs into the Map
*
* @param input
* @throws IOException
*/
publicvoidload(InputStreaminput) throwsIOException {
String[] lines = PApplet.loadStrings(input);
for (Stringline : lines) {
if (line.length() == 0 || line.charAt(0) == '#')
continue;
intequals = line.indexOf('=');
if (equals != -1) {
Stringkey = line.substring(0, equals).trim();
Stringvalue = line.substring(equals + 1).trim();
key = processPlatformSuffix(key, ".linux", OSUtils.isLinux());
key = processPlatformSuffix(key, ".windows", OSUtils.isWindows());
key = processPlatformSuffix(key, ".macosx", OSUtils.isMacOS());
if (key != null)
put(key, value);
}
}
}
/**
* Create a new PreferenceMap that contains all the top level pairs of the
* current mapping. E.g. the folowing mapping:<br />
*
* <pre>
* Map (
* alpha = Alpha
* alpha.some.keys = v1
* alpha.other.keys = v2
* beta = Beta
* beta.some.keys = v3
* )
* </pre>
*
* will generate the following result:
*
* <pre>
* Map (
* alpha = Alpha
* beta = Beta
* )
* </pre>
*
* @return
*/
publicPreferencesMaptopLevelMap() {
PreferencesMapres = newPreferencesMap();
for (Stringkey : keySet()) {
if (key.contains("."))
continue;
res.put(key, get(key));
}
returnres;
}
/**
* Create a new Map<String, PreferenceMap> where keys are the first level of
* the current mapping. Top level pairs are discarded. E.g. the folowing
* mapping:<br />
*
* <pre>
* Map (
* alpha = Alpha
* alpha.some.keys = v1
* alpha.other.keys = v2
* beta = Beta
* beta.some.keys = v3
* )
* </pre>
*
* will generate the following result:
*
* <pre>
* alpha = Map(
* some.keys = v1
* other.keys = v2
* )
* beta = Map(
* some.keys = v3
* )
* </pre>
*
* @return
*/
publicMap<String, PreferencesMap> firstLevelMap() {
Map<String, PreferencesMap> res = newLinkedHashMap<>();
for (Stringkey : keySet()) {
intdot = key.indexOf('.');
if (dot == -1)
continue;
Stringparent = key.substring(0, dot);
Stringchild = key.substring(dot + 1);
if (!res.containsKey(parent))
res.put(parent, newPreferencesMap());
res.get(parent).put(child, get(key));
}
returnres;
}
/**
* Create a new PreferenceMap using a subtree of the current mapping. Top
* level pairs are ignored. E.g. with the following mapping:<br />
*
* <pre>
* Map (
* alpha = Alpha
* alpha.some.keys = v1
* alpha.other.keys = v2
* beta = Beta
* beta.some.keys = v3
* )
* </pre>
*
* a call to createSubTree("alpha") will generate the following result:
*
* <pre>
* Map(
* some.keys = v1
* other.keys = v2
* )
* </pre>
*
* @param parent
* @return
*/
publicPreferencesMapsubTree(Stringparent) {
returnsubTree(parent, -1);
}
publicPreferencesMapsubTree(Stringparent, intsublevels) {
PreferencesMapres = newPreferencesMap();
parent += ".";
intparentLen = parent.length();
for (Stringkey : keySet()) {
if (key.startsWith(parent)) {
StringnewKey = key.substring(parentLen);
intkeySubLevels = newKey.split("\\.").length;
if (sublevels == -1 || keySubLevels == sublevels) {
res.put(newKey, get(key));
}
}
}
returnres;
}
publicStringtoString(Stringindent) {
Stringres = indent + "{\n";
SortedSet<String> treeSet = newTreeSet<>(keySet());
for (Stringk : treeSet)
res += indent + " " + k + " = " + get(k) + "\n";
res += indent + "}\n";
returnres;
}
/**
* Returns the value to which the specified key is mapped, or throws a
* PreferencesMapException if not found
*
* @param k
* the key whose associated value is to be returned
* @return the value to which the specified key is mapped
* @throws PreferencesMapException
*/
publicStringgetOrExcept(Stringk) throwsPreferencesMapException {
Stringr = get(k);
if (r == null)
thrownewPreferencesMapException(k);
returnr;
}
@Override
publicStringtoString() {
returntoString("");
}
/**
* Creates a new File instance by converting the value of the key into an
* abstract pathname. If the the given key doesn't exists or his value is the
* empty string, the result is <b>null</b>.
*
* @param key
* @return
*/
publicFilegetFile(Stringkey) {
if (!containsKey(key))
returnnull;
Stringpath = get(key).trim();
if (path.length() == 0)
returnnull;
returnnewFile(path);
}
/**
* Creates a new File instance by converting the value of the key into an
* abstract pathname with the specified sub folder. If the the given key
* doesn't exists or his value is the empty string, the result is <b>null</b>.
*
* @param key
* @param subFolder
* @return
*/
publicFilegetFile(Stringkey, StringsubFolder) {
Filefile = getFile(key);
if (file == null)
returnnull;
returnnewFile(file, subFolder);
}
/**
* Return the value of the specified key as boolean.
*
* @param key
* @return <b>true</b> if the value of the key is the string "true" (case
* insensitive compared), <b>false</b> in any other case
*/
publicbooleangetBoolean(Stringkey) {
returnBoolean.valueOf(get(key));
}
/**
* Sets the value of the specified key to the string <b>"true"</b> or
* <b>"false"</b> based on value of the boolean parameter
*
* @param key
* @param value
* @return <b>true</b> if the previous value of the key was the string "true"
* (case insensitive compared), <b>false</b> in any other case
*/
publicbooleanputBoolean(Stringkey, booleanvalue) {
Stringprev = put(key, value ? "true" : "false");
returnnewBoolean(prev);
}
publicStringget(Stringkey, StringdefaultValue) {
Stringvalue = get(key);
if (value != null) {
returnvalue;
}
returndefaultValue;
}
}