Debian Codegraph @

arduino-core/src/processing/app/helpers/PreferencesMap.java

raw symbols SCIP
1 /*
2 PreferencesMap - A Map<String, String> with some useful features
3 to handle preferences.
4 Part of the Arduino project - http://www.arduino.cc/
5
6 Copyright (c) 2014 Cristian Maglie
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 package processing.app.helpers;
23
24 import org.apache.commons.compress.utils.IOUtils;
25 import processing.app.legacy.PApplet;
26
27 import java.io.*;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.SortedSet;
31 import java.util.TreeSet;
32
33 @SuppressWarnings("serial")
34 public class PreferencesMap extends LinkedHashMap<String, String> {
35
36 public PreferencesMap(Map<String, String> table) {
37 super(table);
38 }
39
40 /**
41 * Create a PreferencesMap and load the content of the file passed as
42 * argument.
43 *
44 * Is equivalent to:
45 *
46 * <pre>
47 * PreferencesMap map = new PreferencesMap();
48 * map.load(file);
49 * </pre>
50 *
51 * @param file
52 * @throws IOException
53 */
54 public PreferencesMap(File file) throws IOException {
55 super();
56 load(file);
57 }
58
59 public PreferencesMap() {
60 super();
61 }
62
63 /**
64 * Parse a property list file and put kev/value pairs into the Map
65 *
66 * @param file
67 * @throws FileNotFoundException
68 * @throws IOException
69 */
70 public void load(File file) throws IOException {
71 FileInputStream fileInputStream = null;
72 try {
73 fileInputStream = new FileInputStream(file);
74 load(fileInputStream);
75 } finally {
76 IOUtils.closeQuietly(fileInputStream);
77 }
78 }
79
80 protected String processPlatformSuffix(String key, String suffix, boolean isCurrentPlatform) {
81 if (key == null)
82 return null;
83 // Key does not end with the given suffix? Process as normal
84 if (!key.endsWith(suffix))
85 return key;
86 // Not the current platform? Ignore this key
87 if (!isCurrentPlatform)
88 return null;
89 // Strip the suffix from the key
90 return key.substring(0, key.length() - suffix.length());
91 }
92
93 /**
94 * Parse a property list stream and put key/value pairs into the Map
95 *
96 * @param input
97 * @throws IOException
98 */
99 public void load(InputStream input) throws IOException {
100 String[] lines = PApplet.loadStrings(input);
101 for (String line : lines) {
102 if (line.length() == 0 || line.charAt(0) == '#')
103 continue;
104
105 int equals = line.indexOf('=');
106 if (equals != -1) {
107 String key = line.substring(0, equals).trim();
108 String value = line.substring(equals + 1).trim();
109
110 key = processPlatformSuffix(key, ".linux", OSUtils.isLinux());
111 key = processPlatformSuffix(key, ".windows", OSUtils.isWindows());
112 key = processPlatformSuffix(key, ".macosx", OSUtils.isMacOS());
113
114 if (key != null)
115 put(key, value);
116 }
117 }
118 }
119
120 /**
121 * Create a new PreferenceMap that contains all the top level pairs of the
122 * current mapping. E.g. the folowing mapping:<br />
123 *
124 * <pre>
125 * Map (
126 * alpha = Alpha
127 * alpha.some.keys = v1
128 * alpha.other.keys = v2
129 * beta = Beta
130 * beta.some.keys = v3
131 * )
132 * </pre>
133 *
134 * will generate the following result:
135 *
136 * <pre>
137 * Map (
138 * alpha = Alpha
139 * beta = Beta
140 * )
141 * </pre>
142 *
143 * @return
144 */
145 public PreferencesMap topLevelMap() {
146 PreferencesMap res = new PreferencesMap();
147 for (String key : keySet()) {
148 if (key.contains("."))
149 continue;
150 res.put(key, get(key));
151 }
152 return res;
153 }
154
155 /**
156 * Create a new Map<String, PreferenceMap> where keys are the first level of
157 * the current mapping. Top level pairs are discarded. E.g. the folowing
158 * mapping:<br />
159 *
160 * <pre>
161 * Map (
162 * alpha = Alpha
163 * alpha.some.keys = v1
164 * alpha.other.keys = v2
165 * beta = Beta
166 * beta.some.keys = v3
167 * )
168 * </pre>
169 *
170 * will generate the following result:
171 *
172 * <pre>
173 * alpha = Map(
174 * some.keys = v1
175 * other.keys = v2
176 * )
177 * beta = Map(
178 * some.keys = v3
179 * )
180 * </pre>
181 *
182 * @return
183 */
184 public Map<String, PreferencesMap> firstLevelMap() {
185 Map<String, PreferencesMap> res = new LinkedHashMap<>();
186 for (String key : keySet()) {
187 int dot = key.indexOf('.');
188 if (dot == -1)
189 continue;
190
191 String parent = key.substring(0, dot);
192 String child = key.substring(dot + 1);
193
194 if (!res.containsKey(parent))
195 res.put(parent, new PreferencesMap());
196 res.get(parent).put(child, get(key));
197 }
198 return res;
199 }
200
201 /**
202 * Create a new PreferenceMap using a subtree of the current mapping. Top
203 * level pairs are ignored. E.g. with the following mapping:<br />
204 *
205 * <pre>
206 * Map (
207 * alpha = Alpha
208 * alpha.some.keys = v1
209 * alpha.other.keys = v2
210 * beta = Beta
211 * beta.some.keys = v3
212 * )
213 * </pre>
214 *
215 * a call to createSubTree("alpha") will generate the following result:
216 *
217 * <pre>
218 * Map(
219 * some.keys = v1
220 * other.keys = v2
221 * )
222 * </pre>
223 *
224 * @param parent
225 * @return
226 */
227 public PreferencesMap subTree(String parent) {
228 return subTree(parent, -1);
229 }
230
231 public PreferencesMap subTree(String parent, int sublevels) {
232 PreferencesMap res = new PreferencesMap();
233 parent += ".";
234 int parentLen = parent.length();
235 for (String key : keySet()) {
236 if (key.startsWith(parent)) {
237 String newKey = key.substring(parentLen);
238 int keySubLevels = newKey.split("\\.").length;
239 if (sublevels == -1 || keySubLevels == sublevels) {
240 res.put(newKey, get(key));
241 }
242 }
243 }
244 return res;
245 }
246
247 public String toString(String indent) {
248 String res = indent + "{\n";
249 SortedSet<String> treeSet = new TreeSet<>(keySet());
250 for (String k : treeSet)
251 res += indent + " " + k + " = " + get(k) + "\n";
252 res += indent + "}\n";
253 return res;
254 }
255
256 /**
257 * Returns the value to which the specified key is mapped, or throws a
258 * PreferencesMapException if not found
259 *
260 * @param k
261 * the key whose associated value is to be returned
262 * @return the value to which the specified key is mapped
263 * @throws PreferencesMapException
264 */
265 public String getOrExcept(String k) throws PreferencesMapException {
266 String r = get(k);
267 if (r == null)
268 throw new PreferencesMapException(k);
269 return r;
270 }
271
272 @Override
273 public String toString() {
274 return toString("");
275 }
276
277 /**
278 * Creates a new File instance by converting the value of the key into an
279 * abstract pathname. If the the given key doesn't exists or his value is the
280 * empty string, the result is <b>null</b>.
281 *
282 * @param key
283 * @return
284 */
285 public File getFile(String key) {
286 if (!containsKey(key))
287 return null;
288 String path = get(key).trim();
289 if (path.length() == 0)
290 return null;
291 return new File(path);
292 }
293
294 /**
295 * Creates a new File instance by converting the value of the key into an
296 * abstract pathname with the specified sub folder. If the the given key
297 * doesn't exists or his value is the empty string, the result is <b>null</b>.
298 *
299 * @param key
300 * @param subFolder
301 * @return
302 */
303 public File getFile(String key, String subFolder) {
304 File file = getFile(key);
305 if (file == null)
306 return null;
307 return new File(file, subFolder);
308 }
309
310 /**
311 * Return the value of the specified key as boolean.
312 *
313 * @param key
314 * @return <b>true</b> if the value of the key is the string "true" (case
315 * insensitive compared), <b>false</b> in any other case
316 */
317 public boolean getBoolean(String key) {
318 return Boolean.valueOf(get(key));
319 }
320
321 /**
322 * Sets the value of the specified key to the string <b>"true"</b> or
323 * <b>"false"</b> based on value of the boolean parameter
324 *
325 * @param key
326 * @param value
327 * @return <b>true</b> if the previous value of the key was the string "true"
328 * (case insensitive compared), <b>false</b> in any other case
329 */
330 public boolean putBoolean(String key, boolean value) {
331 String prev = put(key, value ? "true" : "false");
332 return new Boolean(prev);
333 }
334
335 public String get(String key, String defaultValue) {
336 String value = get(key);
337 if (value != null) {
338 return value;
339 }
340 return defaultValue;
341 }
342
343 }