| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public PreferencesMap(File file) throws IOException { |
| 55 |
super(); |
| 56 |
load(file); |
| 57 |
} |
| 58 |
|
| 59 |
public PreferencesMap() { |
| 60 |
super(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 84 |
if (!key.endsWith(suffix)) |
| 85 |
return key; |
| 86 |
|
| 87 |
if (!isCurrentPlatform) |
| 88 |
return null; |
| 89 |
|
| 90 |
return key.substring(0, key.length() - suffix.length()); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 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 |
|
| 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 |
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 |
|
| 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 |
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 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 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 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 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 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 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 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
public boolean getBoolean(String key) { |
| 318 |
return Boolean.valueOf(get(key)); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 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 |
} |