source: josm/trunk/src/org/openstreetmap/josm/data/Preferences.java@ 12495

Last change on this file since 12495 was 12409, checked in by Don-vip, 7 years ago

see #11924 - fix javadoc warnings new in 9-ea+173

  • Property svn:eol-style set to native
File size: 66.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Color;
8import java.awt.GraphicsEnvironment;
9import java.awt.Toolkit;
10import java.io.File;
11import java.io.IOException;
12import java.io.PrintWriter;
13import java.io.Reader;
14import java.io.StringReader;
15import java.io.StringWriter;
16import java.lang.annotation.Retention;
17import java.lang.annotation.RetentionPolicy;
18import java.lang.reflect.Field;
19import java.nio.charset.StandardCharsets;
20import java.util.AbstractMap;
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
24import java.util.HashMap;
25import java.util.HashSet;
26import java.util.Iterator;
27import java.util.LinkedHashMap;
28import java.util.LinkedList;
29import java.util.List;
30import java.util.Map;
31import java.util.Map.Entry;
32import java.util.Objects;
33import java.util.Optional;
34import java.util.ResourceBundle;
35import java.util.Set;
36import java.util.SortedMap;
37import java.util.TreeMap;
38import java.util.concurrent.TimeUnit;
39import java.util.function.Predicate;
40import java.util.regex.Matcher;
41import java.util.regex.Pattern;
42import java.util.stream.Collectors;
43import java.util.stream.Stream;
44
45import javax.json.Json;
46import javax.json.JsonArray;
47import javax.json.JsonArrayBuilder;
48import javax.json.JsonObject;
49import javax.json.JsonObjectBuilder;
50import javax.json.JsonReader;
51import javax.json.JsonString;
52import javax.json.JsonValue;
53import javax.json.JsonWriter;
54import javax.swing.JOptionPane;
55import javax.xml.stream.XMLStreamException;
56
57import org.openstreetmap.josm.Main;
58import org.openstreetmap.josm.data.preferences.BooleanProperty;
59import org.openstreetmap.josm.data.preferences.ColorProperty;
60import org.openstreetmap.josm.data.preferences.DoubleProperty;
61import org.openstreetmap.josm.data.preferences.IntegerProperty;
62import org.openstreetmap.josm.data.preferences.ListListSetting;
63import org.openstreetmap.josm.data.preferences.ListSetting;
64import org.openstreetmap.josm.data.preferences.LongProperty;
65import org.openstreetmap.josm.data.preferences.MapListSetting;
66import org.openstreetmap.josm.data.preferences.PreferencesReader;
67import org.openstreetmap.josm.data.preferences.PreferencesWriter;
68import org.openstreetmap.josm.data.preferences.Setting;
69import org.openstreetmap.josm.data.preferences.StringSetting;
70import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
71import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
72import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference.RulePrefHelper;
73import org.openstreetmap.josm.io.OfflineAccessException;
74import org.openstreetmap.josm.io.OnlineResource;
75import org.openstreetmap.josm.tools.CheckParameterUtil;
76import org.openstreetmap.josm.tools.ColorHelper;
77import org.openstreetmap.josm.tools.I18n;
78import org.openstreetmap.josm.tools.JosmRuntimeException;
79import org.openstreetmap.josm.tools.ListenerList;
80import org.openstreetmap.josm.tools.MultiMap;
81import org.openstreetmap.josm.tools.Utils;
82import org.xml.sax.SAXException;
83
84/**
85 * This class holds all preferences for JOSM.
86 *
87 * Other classes can register their beloved properties here. All properties will be
88 * saved upon set-access.
89 *
90 * Each property is a key=setting pair, where key is a String and setting can be one of
91 * 4 types:
92 * string, list, list of lists and list of maps.
93 * In addition, each key has a unique default value that is set when the value is first
94 * accessed using one of the get...() methods. You can use the same preference
95 * key in different parts of the code, but the default value must be the same
96 * everywhere. A default value of null means, the setting has been requested, but
97 * no default value was set. This is used in advanced preferences to present a list
98 * off all possible settings.
99 *
100 * At the moment, you cannot put the empty string for string properties.
101 * put(key, "") means, the property is removed.
102 *
103 * @author imi
104 * @since 74
105 */
106public class Preferences {
107
108 private static final String COLOR_PREFIX = "color.";
109
110 private static final String[] OBSOLETE_PREF_KEYS = {
111 "imagery.layers.addedIds", /* remove entry after June 2017 */
112 "projection", /* remove entry after Nov. 2017 */
113 "projection.sub", /* remove entry after Nov. 2017 */
114 };
115
116 private static final long MAX_AGE_DEFAULT_PREFERENCES = TimeUnit.DAYS.toSeconds(50);
117
118 /**
119 * Internal storage for the preference directory.
120 * Do not access this variable directly!
121 * @see #getPreferencesDirectory()
122 */
123 private File preferencesDir;
124
125 /**
126 * Internal storage for the cache directory.
127 */
128 private File cacheDir;
129
130 /**
131 * Internal storage for the user data directory.
132 */
133 private File userdataDir;
134
135 /**
136 * Determines if preferences file is saved each time a property is changed.
137 */
138 private boolean saveOnPut = true;
139
140 /**
141 * Maps the setting name to the current value of the setting.
142 * The map must not contain null as key or value. The mapped setting objects
143 * must not have a null value.
144 */
145 protected final SortedMap<String, Setting<?>> settingsMap = new TreeMap<>();
146
147 /**
148 * Maps the setting name to the default value of the setting.
149 * The map must not contain null as key or value. The value of the mapped
150 * setting objects can be null.
151 */
152 protected final SortedMap<String, Setting<?>> defaultsMap = new TreeMap<>();
153
154 private final Predicate<Entry<String, Setting<?>>> NO_DEFAULT_SETTINGS_ENTRY =
155 e -> !e.getValue().equals(defaultsMap.get(e.getKey()));
156
157 /**
158 * Maps color keys to human readable color name
159 */
160 protected final SortedMap<String, String> colornames = new TreeMap<>();
161
162 /**
163 * Indicates whether {@link #init(boolean)} completed successfully.
164 * Used to decide whether to write backup preference file in {@link #save()}
165 */
166 protected boolean initSuccessful;
167
168 /**
169 * Event triggered when a preference entry value changes.
170 */
171 public interface PreferenceChangeEvent {
172 /**
173 * Returns the preference key.
174 * @return the preference key
175 */
176 String getKey();
177
178 /**
179 * Returns the old preference value.
180 * @return the old preference value
181 */
182 Setting<?> getOldValue();
183
184 /**
185 * Returns the new preference value.
186 * @return the new preference value
187 */
188 Setting<?> getNewValue();
189 }
190
191 /**
192 * Listener to preference change events.
193 * @since 10600 (functional interface)
194 */
195 @FunctionalInterface
196 public interface PreferenceChangedListener {
197 /**
198 * Trigerred when a preference entry value changes.
199 * @param e the preference change event
200 */
201 void preferenceChanged(PreferenceChangeEvent e);
202 }
203
204 private static class DefaultPreferenceChangeEvent implements PreferenceChangeEvent {
205 private final String key;
206 private final Setting<?> oldValue;
207 private final Setting<?> newValue;
208
209 DefaultPreferenceChangeEvent(String key, Setting<?> oldValue, Setting<?> newValue) {
210 this.key = key;
211 this.oldValue = oldValue;
212 this.newValue = newValue;
213 }
214
215 @Override
216 public String getKey() {
217 return key;
218 }
219
220 @Override
221 public Setting<?> getOldValue() {
222 return oldValue;
223 }
224
225 @Override
226 public Setting<?> getNewValue() {
227 return newValue;
228 }
229 }
230
231 private final ListenerList<PreferenceChangedListener> listeners = ListenerList.create();
232
233 private final HashMap<String, ListenerList<PreferenceChangedListener>> keyListeners = new HashMap<>();
234
235 /**
236 * Adds a new preferences listener.
237 * @param listener The listener to add
238 */
239 public void addPreferenceChangeListener(PreferenceChangedListener listener) {
240 if (listener != null) {
241 listeners.addListener(listener);
242 }
243 }
244
245 /**
246 * Removes a preferences listener.
247 * @param listener The listener to remove
248 */
249 public void removePreferenceChangeListener(PreferenceChangedListener listener) {
250 listeners.removeListener(listener);
251 }
252
253 /**
254 * Adds a listener that only listens to changes in one preference
255 * @param key The preference key to listen to
256 * @param listener The listener to add.
257 * @since 10824
258 */
259 public void addKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
260 listenersForKey(key).addListener(listener);
261 }
262
263 /**
264 * Adds a weak listener that only listens to changes in one preference
265 * @param key The preference key to listen to
266 * @param listener The listener to add.
267 * @since 10824
268 */
269 public void addWeakKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
270 listenersForKey(key).addWeakListener(listener);
271 }
272
273 private ListenerList<PreferenceChangedListener> listenersForKey(String key) {
274 ListenerList<PreferenceChangedListener> keyListener = keyListeners.get(key);
275 if (keyListener == null) {
276 keyListener = ListenerList.create();
277 keyListeners.put(key, keyListener);
278 }
279 return keyListener;
280 }
281
282 /**
283 * Removes a listener that only listens to changes in one preference
284 * @param key The preference key to listen to
285 * @param listener The listener to add.
286 */
287 public void removeKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
288 Optional.ofNullable(keyListeners.get(key)).orElseThrow(
289 () -> new IllegalArgumentException("There are no listeners registered for " + key))
290 .removeListener(listener);
291 }
292
293 protected void firePreferenceChanged(String key, Setting<?> oldValue, Setting<?> newValue) {
294 final PreferenceChangeEvent evt = new DefaultPreferenceChangeEvent(key, oldValue, newValue);
295 listeners.fireEvent(listener -> listener.preferenceChanged(evt));
296
297 ListenerList<PreferenceChangedListener> forKey = keyListeners.get(key);
298 if (forKey != null) {
299 forKey.fireEvent(listener -> listener.preferenceChanged(evt));
300 }
301 }
302
303 /**
304 * Get the base name of the JOSM directories for preferences, cache and
305 * user data.
306 * Default value is "JOSM", unless overridden by system property "josm.dir.name".
307 * @return the base name of the JOSM directories for preferences, cache and
308 * user data
309 */
310 public String getJOSMDirectoryBaseName() {
311 String name = System.getProperty("josm.dir.name");
312 if (name != null)
313 return name;
314 else
315 return "JOSM";
316 }
317
318 /**
319 * Returns the user defined preferences directory, containing the preferences.xml file
320 * @return The user defined preferences directory, containing the preferences.xml file
321 * @since 7834
322 */
323 public File getPreferencesDirectory() {
324 if (preferencesDir != null)
325 return preferencesDir;
326 String path;
327 path = System.getProperty("josm.pref");
328 if (path != null) {
329 preferencesDir = new File(path).getAbsoluteFile();
330 } else {
331 path = System.getProperty("josm.home");
332 if (path != null) {
333 preferencesDir = new File(path).getAbsoluteFile();
334 } else {
335 preferencesDir = Main.platform.getDefaultPrefDirectory();
336 }
337 }
338 return preferencesDir;
339 }
340
341 /**
342 * Returns the user data directory, containing autosave, plugins, etc.
343 * Depending on the OS it may be the same directory as preferences directory.
344 * @return The user data directory, containing autosave, plugins, etc.
345 * @since 7834
346 */
347 public File getUserDataDirectory() {
348 if (userdataDir != null)
349 return userdataDir;
350 String path;
351 path = System.getProperty("josm.userdata");
352 if (path != null) {
353 userdataDir = new File(path).getAbsoluteFile();
354 } else {
355 path = System.getProperty("josm.home");
356 if (path != null) {
357 userdataDir = new File(path).getAbsoluteFile();
358 } else {
359 userdataDir = Main.platform.getDefaultUserDataDirectory();
360 }
361 }
362 return userdataDir;
363 }
364
365 /**
366 * Returns the user preferences file (preferences.xml).
367 * @return The user preferences file (preferences.xml)
368 */
369 public File getPreferenceFile() {
370 return new File(getPreferencesDirectory(), "preferences.xml");
371 }
372
373 /**
374 * Returns the cache file for default preferences.
375 * @return the cache file for default preferences
376 */
377 public File getDefaultsCacheFile() {
378 return new File(getCacheDirectory(), "default_preferences.xml");
379 }
380
381 /**
382 * Returns the user plugin directory.
383 * @return The user plugin directory
384 */
385 public File getPluginsDirectory() {
386 return new File(getUserDataDirectory(), "plugins");
387 }
388
389 /**
390 * Get the directory where cached content of any kind should be stored.
391 *
392 * If the directory doesn't exist on the file system, it will be created by this method.
393 *
394 * @return the cache directory
395 */
396 public File getCacheDirectory() {
397 if (cacheDir != null)
398 return cacheDir;
399 String path = System.getProperty("josm.cache");
400 if (path != null) {
401 cacheDir = new File(path).getAbsoluteFile();
402 } else {
403 path = System.getProperty("josm.home");
404 if (path != null) {
405 cacheDir = new File(path, "cache");
406 } else {
407 path = get("cache.folder", null);
408 if (path != null) {
409 cacheDir = new File(path).getAbsoluteFile();
410 } else {
411 cacheDir = Main.platform.getDefaultCacheDirectory();
412 }
413 }
414 }
415 if (!cacheDir.exists() && !cacheDir.mkdirs()) {
416 Main.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile()));
417 JOptionPane.showMessageDialog(
418 Main.parent,
419 tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()),
420 tr("Error"),
421 JOptionPane.ERROR_MESSAGE
422 );
423 }
424 return cacheDir;
425 }
426
427 private static void addPossibleResourceDir(Set<String> locations, String s) {
428 if (s != null) {
429 if (!s.endsWith(File.separator)) {
430 s += File.separator;
431 }
432 locations.add(s);
433 }
434 }
435
436 /**
437 * Returns a set of all existing directories where resources could be stored.
438 * @return A set of all existing directories where resources could be stored.
439 */
440 public Collection<String> getAllPossiblePreferenceDirs() {
441 Set<String> locations = new HashSet<>();
442 addPossibleResourceDir(locations, getPreferencesDirectory().getPath());
443 addPossibleResourceDir(locations, getUserDataDirectory().getPath());
444 addPossibleResourceDir(locations, System.getenv("JOSM_RESOURCES"));
445 addPossibleResourceDir(locations, System.getProperty("josm.resources"));
446 if (Main.isPlatformWindows()) {
447 String appdata = System.getenv("APPDATA");
448 if (appdata != null && System.getenv("ALLUSERSPROFILE") != null
449 && appdata.lastIndexOf(File.separator) != -1) {
450 appdata = appdata.substring(appdata.lastIndexOf(File.separator));
451 locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
452 appdata), "JOSM").getPath());
453 }
454 } else {
455 locations.add("/usr/local/share/josm/");
456 locations.add("/usr/local/lib/josm/");
457 locations.add("/usr/share/josm/");
458 locations.add("/usr/lib/josm/");
459 }
460 return locations;
461 }
462
463 /**
464 * Get settings value for a certain key.
465 * @param key the identifier for the setting
466 * @return "" if there is nothing set for the preference key, the corresponding value otherwise. The result is not null.
467 */
468 public synchronized String get(final String key) {
469 String value = get(key, null);
470 return value == null ? "" : value;
471 }
472
473 /**
474 * Get settings value for a certain key and provide default a value.
475 * @param key the identifier for the setting
476 * @param def the default value. For each call of get() with a given key, the default value must be the same.
477 * @return the corresponding value if the property has been set before, {@code def} otherwise
478 */
479 public synchronized String get(final String key, final String def) {
480 return getSetting(key, new StringSetting(def), StringSetting.class).getValue();
481 }
482
483 /**
484 * Gets all normal (string) settings that have a key starting with the prefix
485 * @param prefix The start of the key
486 * @return The key names of the settings
487 */
488 public synchronized Map<String, String> getAllPrefix(final String prefix) {
489 final Map<String, String> all = new TreeMap<>();
490 for (final Entry<String, Setting<?>> e : settingsMap.entrySet()) {
491 if (e.getKey().startsWith(prefix) && (e.getValue() instanceof StringSetting)) {
492 all.put(e.getKey(), ((StringSetting) e.getValue()).getValue());
493 }
494 }
495 return all;
496 }
497
498 /**
499 * Gets all list settings that have a key starting with the prefix
500 * @param prefix The start of the key
501 * @return The key names of the list settings
502 */
503 public synchronized List<String> getAllPrefixCollectionKeys(final String prefix) {
504 final List<String> all = new LinkedList<>();
505 for (Map.Entry<String, Setting<?>> entry : settingsMap.entrySet()) {
506 if (entry.getKey().startsWith(prefix) && entry.getValue() instanceof ListSetting) {
507 all.add(entry.getKey());
508 }
509 }
510 return all;
511 }
512
513 /**
514 * Gets all known colors (preferences starting with the color prefix)
515 * @return All colors
516 */
517 public synchronized Map<String, String> getAllColors() {
518 final Map<String, String> all = new TreeMap<>();
519 for (final Entry<String, Setting<?>> e : defaultsMap.entrySet()) {
520 if (e.getKey().startsWith(COLOR_PREFIX) && e.getValue() instanceof StringSetting) {
521 StringSetting d = (StringSetting) e.getValue();
522 if (d.getValue() != null) {
523 all.put(e.getKey().substring(6), d.getValue());
524 }
525 }
526 }
527 for (final Entry<String, Setting<?>> e : settingsMap.entrySet()) {
528 if (e.getKey().startsWith(COLOR_PREFIX) && (e.getValue() instanceof StringSetting)) {
529 all.put(e.getKey().substring(6), ((StringSetting) e.getValue()).getValue());
530 }
531 }
532 return all;
533 }
534
535 /**
536 * Gets a boolean preference
537 * @param key The preference key
538 * @return The boolean or <code>false</code> if it could not be parsed
539 * @see IntegerProperty#get()
540 */
541 public synchronized boolean getBoolean(final String key) {
542 String s = get(key, null);
543 return s != null && Boolean.parseBoolean(s);
544 }
545
546 /**
547 * Gets a boolean preference
548 * @param key The preference key
549 * @param def The default value to use
550 * @return The boolean, <code>false</code> if it could not be parsed, the default value if it is unset
551 * @see IntegerProperty#get()
552 */
553 public synchronized boolean getBoolean(final String key, final boolean def) {
554 return Boolean.parseBoolean(get(key, Boolean.toString(def)));
555 }
556
557 /**
558 * Gets an boolean that may be specialized
559 * @param key The basic key
560 * @param specName The sub-key to append to the key
561 * @param def The default value
562 * @return The boolean value or the default value if it could not be parsed
563 */
564 public synchronized boolean getBoolean(final String key, final String specName, final boolean def) {
565 boolean generic = getBoolean(key, def);
566 String skey = key+'.'+specName;
567 Setting<?> prop = settingsMap.get(skey);
568 if (prop instanceof StringSetting)
569 return Boolean.parseBoolean(((StringSetting) prop).getValue());
570 else
571 return generic;
572 }
573
574 /**
575 * Set a value for a certain setting.
576 * @param key the unique identifier for the setting
577 * @param value the value of the setting. Can be null or "" which both removes the key-value entry.
578 * @return {@code true}, if something has changed (i.e. value is different than before)
579 */
580 public boolean put(final String key, String value) {
581 return putSetting(key, value == null || value.isEmpty() ? null : new StringSetting(value));
582 }
583
584 /**
585 * Set a boolean value for a certain setting.
586 * @param key the unique identifier for the setting
587 * @param value The new value
588 * @return {@code true}, if something has changed (i.e. value is different than before)
589 * @see BooleanProperty
590 */
591 public boolean put(final String key, final boolean value) {
592 return put(key, Boolean.toString(value));
593 }
594
595 /**
596 * Set a boolean value for a certain setting.
597 * @param key the unique identifier for the setting
598 * @param value The new value
599 * @return {@code true}, if something has changed (i.e. value is different than before)
600 * @see IntegerProperty#put(Integer)
601 */
602 public boolean putInteger(final String key, final Integer value) {
603 return put(key, Integer.toString(value));
604 }
605
606 /**
607 * Set a boolean value for a certain setting.
608 * @param key the unique identifier for the setting
609 * @param value The new value
610 * @return {@code true}, if something has changed (i.e. value is different than before)
611 * @see DoubleProperty#put(Double)
612 */
613 public boolean putDouble(final String key, final Double value) {
614 return put(key, Double.toString(value));
615 }
616
617 /**
618 * Set a boolean value for a certain setting.
619 * @param key the unique identifier for the setting
620 * @param value The new value
621 * @return {@code true}, if something has changed (i.e. value is different than before)
622 * @see LongProperty#put(Long)
623 */
624 public boolean putLong(final String key, final Long value) {
625 return put(key, Long.toString(value));
626 }
627
628 /**
629 * Called after every put. In case of a problem, do nothing but output the error in log.
630 * @throws IOException if any I/O error occurs
631 */
632 public synchronized void save() throws IOException {
633 save(getPreferenceFile(), settingsMap.entrySet().stream().filter(NO_DEFAULT_SETTINGS_ENTRY), false);
634 }
635
636 /**
637 * Stores the defaults to the defaults file
638 * @throws IOException If the file could not be saved
639 */
640 public synchronized void saveDefaults() throws IOException {
641 save(getDefaultsCacheFile(), defaultsMap.entrySet().stream(), true);
642 }
643
644 protected void save(File prefFile, Stream<Entry<String, Setting<?>>> settings, boolean defaults) throws IOException {
645 if (!defaults) {
646 /* currently unused, but may help to fix configuration issues in future */
647 putInteger("josm.version", Version.getInstance().getVersion());
648
649 updateSystemProperties();
650 }
651
652 File backupFile = new File(prefFile + "_backup");
653
654 // Backup old preferences if there are old preferences
655 if (initSuccessful && prefFile.exists() && prefFile.length() > 0) {
656 Utils.copyFile(prefFile, backupFile);
657 }
658
659 try (PreferencesWriter writer = new PreferencesWriter(
660 new PrintWriter(new File(prefFile + "_tmp"), StandardCharsets.UTF_8.name()), false, defaults)) {
661 writer.write(settings);
662 }
663
664 File tmpFile = new File(prefFile + "_tmp");
665 Utils.copyFile(tmpFile, prefFile);
666 Utils.deleteFile(tmpFile, marktr("Unable to delete temporary file {0}"));
667
668 setCorrectPermissions(prefFile);
669 setCorrectPermissions(backupFile);
670 }
671
672 private static void setCorrectPermissions(File file) {
673 if (!file.setReadable(false, false) && Main.isDebugEnabled()) {
674 Main.debug(tr("Unable to set file non-readable {0}", file.getAbsolutePath()));
675 }
676 if (!file.setWritable(false, false) && Main.isDebugEnabled()) {
677 Main.debug(tr("Unable to set file non-writable {0}", file.getAbsolutePath()));
678 }
679 if (!file.setExecutable(false, false) && Main.isDebugEnabled()) {
680 Main.debug(tr("Unable to set file non-executable {0}", file.getAbsolutePath()));
681 }
682 if (!file.setReadable(true, true) && Main.isDebugEnabled()) {
683 Main.debug(tr("Unable to set file readable {0}", file.getAbsolutePath()));
684 }
685 if (!file.setWritable(true, true) && Main.isDebugEnabled()) {
686 Main.debug(tr("Unable to set file writable {0}", file.getAbsolutePath()));
687 }
688 }
689
690 /**
691 * Loads preferences from settings file.
692 * @throws IOException if any I/O error occurs while reading the file
693 * @throws SAXException if the settings file does not contain valid XML
694 * @throws XMLStreamException if an XML error occurs while parsing the file (after validation)
695 */
696 protected void load() throws IOException, SAXException, XMLStreamException {
697 File pref = getPreferenceFile();
698 PreferencesReader.validateXML(pref);
699 PreferencesReader reader = new PreferencesReader(pref, false);
700 reader.parse();
701 settingsMap.clear();
702 settingsMap.putAll(reader.getSettings());
703 updateSystemProperties();
704 removeObsolete(reader.getVersion());
705 }
706
707 /**
708 * Loads default preferences from default settings cache file.
709 *
710 * Discards entries older than {@link #MAX_AGE_DEFAULT_PREFERENCES}.
711 *
712 * @throws IOException if any I/O error occurs while reading the file
713 * @throws SAXException if the settings file does not contain valid XML
714 * @throws XMLStreamException if an XML error occurs while parsing the file (after validation)
715 */
716 protected void loadDefaults() throws IOException, XMLStreamException, SAXException {
717 File def = getDefaultsCacheFile();
718 PreferencesReader.validateXML(def);
719 PreferencesReader reader = new PreferencesReader(def, true);
720 reader.parse();
721 defaultsMap.clear();
722 long minTime = System.currentTimeMillis() / 1000 - MAX_AGE_DEFAULT_PREFERENCES;
723 for (Entry<String, Setting<?>> e : reader.getSettings().entrySet()) {
724 if (e.getValue().getTime() >= minTime) {
725 defaultsMap.put(e.getKey(), e.getValue());
726 }
727 }
728 }
729
730 /**
731 * Loads preferences from XML reader.
732 * @param in XML reader
733 * @throws XMLStreamException if any XML stream error occurs
734 * @throws IOException if any I/O error occurs
735 */
736 public void fromXML(Reader in) throws XMLStreamException, IOException {
737 PreferencesReader reader = new PreferencesReader(in, false);
738 reader.parse();
739 settingsMap.clear();
740 settingsMap.putAll(reader.getSettings());
741 }
742
743 /**
744 * Initializes preferences.
745 * @param reset if {@code true}, current settings file is replaced by the default one
746 */
747 public void init(boolean reset) {
748 initSuccessful = false;
749 // get the preferences.
750 File prefDir = getPreferencesDirectory();
751 if (prefDir.exists()) {
752 if (!prefDir.isDirectory()) {
753 Main.warn(tr("Failed to initialize preferences. Preference directory ''{0}'' is not a directory.",
754 prefDir.getAbsoluteFile()));
755 JOptionPane.showMessageDialog(
756 Main.parent,
757 tr("<html>Failed to initialize preferences.<br>Preference directory ''{0}'' is not a directory.</html>",
758 prefDir.getAbsoluteFile()),
759 tr("Error"),
760 JOptionPane.ERROR_MESSAGE
761 );
762 return;
763 }
764 } else {
765 if (!prefDir.mkdirs()) {
766 Main.warn(tr("Failed to initialize preferences. Failed to create missing preference directory: {0}",
767 prefDir.getAbsoluteFile()));
768 JOptionPane.showMessageDialog(
769 Main.parent,
770 tr("<html>Failed to initialize preferences.<br>Failed to create missing preference directory: {0}</html>",
771 prefDir.getAbsoluteFile()),
772 tr("Error"),
773 JOptionPane.ERROR_MESSAGE
774 );
775 return;
776 }
777 }
778
779 File preferenceFile = getPreferenceFile();
780 try {
781 if (!preferenceFile.exists()) {
782 Main.info(tr("Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
783 resetToDefault();
784 save();
785 } else if (reset) {
786 File backupFile = new File(prefDir, "preferences.xml.bak");
787 Main.platform.rename(preferenceFile, backupFile);
788 Main.warn(tr("Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile()));
789 resetToDefault();
790 save();
791 }
792 } catch (IOException e) {
793 Main.error(e);
794 JOptionPane.showMessageDialog(
795 Main.parent,
796 tr("<html>Failed to initialize preferences.<br>Failed to reset preference file to default: {0}</html>",
797 getPreferenceFile().getAbsoluteFile()),
798 tr("Error"),
799 JOptionPane.ERROR_MESSAGE
800 );
801 return;
802 }
803 try {
804 load();
805 initSuccessful = true;
806 } catch (IOException | SAXException | XMLStreamException e) {
807 Main.error(e);
808 File backupFile = new File(prefDir, "preferences.xml.bak");
809 JOptionPane.showMessageDialog(
810 Main.parent,
811 tr("<html>Preferences file had errors.<br> Making backup of old one to <br>{0}<br> " +
812 "and creating a new default preference file.</html>",
813 backupFile.getAbsoluteFile()),
814 tr("Error"),
815 JOptionPane.ERROR_MESSAGE
816 );
817 Main.platform.rename(preferenceFile, backupFile);
818 try {
819 resetToDefault();
820 save();
821 } catch (IOException e1) {
822 Main.error(e1);
823 Main.warn(tr("Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile()));
824 }
825 }
826 File def = getDefaultsCacheFile();
827 if (def.exists()) {
828 try {
829 loadDefaults();
830 } catch (IOException | XMLStreamException | SAXException e) {
831 Main.error(e);
832 Main.warn(tr("Failed to load defaults cache file: {0}", def));
833 defaultsMap.clear();
834 if (!def.delete()) {
835 Main.warn(tr("Failed to delete faulty defaults cache file: {0}", def));
836 }
837 }
838 }
839 }
840
841 /**
842 * Resets the preferences to their initial state. This resets all values and file associations.
843 * The default values and listeners are not removed.
844 * <p>
845 * It is meant to be called before {@link #init(boolean)}
846 * @since 10876
847 */
848 public void resetToInitialState() {
849 resetToDefault();
850 preferencesDir = null;
851 cacheDir = null;
852 userdataDir = null;
853 saveOnPut = true;
854 initSuccessful = false;
855 }
856
857 /**
858 * Reset all values stored in this map to the default values. This clears the preferences.
859 */
860 public final void resetToDefault() {
861 settingsMap.clear();
862 }
863
864 /**
865 * Convenience method for accessing colour preferences.
866 * <p>
867 * To be removed: end of 2016
868 *
869 * @param colName name of the colour
870 * @param def default value
871 * @return a Color object for the configured colour, or the default value if none configured.
872 * @deprecated Use a {@link ColorProperty} instead.
873 */
874 @Deprecated
875 public synchronized Color getColor(String colName, Color def) {
876 return getColor(colName, null, def);
877 }
878
879 /* only for preferences */
880 public synchronized String getColorName(String o) {
881 Matcher m = Pattern.compile("mappaint\\.(.+?)\\.(.+)").matcher(o);
882 if (m.matches()) {
883 return tr("Paint style {0}: {1}", tr(I18n.escape(m.group(1))), tr(I18n.escape(m.group(2))));
884 }
885 m = Pattern.compile("layer (.+)").matcher(o);
886 if (m.matches()) {
887 return tr("Layer: {0}", tr(I18n.escape(m.group(1))));
888 }
889 return tr(I18n.escape(colornames.containsKey(o) ? colornames.get(o) : o));
890 }
891
892 /**
893 * Convenience method for accessing colour preferences.
894 * <p>
895 * To be removed: end of 2016
896 * @param colName name of the colour
897 * @param specName name of the special colour settings
898 * @param def default value
899 * @return a Color object for the configured colour, or the default value if none configured.
900 * @deprecated Use a {@link ColorProperty} instead.
901 * You can replace this by: <code>new ColorProperty(colName, def).getChildColor(specName)</code>
902 */
903 @Deprecated
904 public synchronized Color getColor(String colName, String specName, Color def) {
905 String colKey = ColorProperty.getColorKey(colName);
906 registerColor(colKey, colName);
907 String colStr = specName != null ? get(COLOR_PREFIX+specName) : "";
908 if (colStr.isEmpty()) {
909 colStr = get(colKey, ColorHelper.color2html(def, true));
910 }
911 if (colStr != null && !colStr.isEmpty()) {
912 return ColorHelper.html2color(colStr);
913 } else {
914 return def;
915 }
916 }
917
918 /**
919 * Registers a color name conversion for the global color registry.
920 * @param colKey The key
921 * @param colName The name of the color.
922 * @since 10824
923 */
924 public void registerColor(String colKey, String colName) {
925 if (!colKey.equals(colName)) {
926 colornames.put(colKey, colName);
927 }
928 }
929
930 /**
931 * Gets the default color that was registered with the preference
932 * @param colKey The color name
933 * @return The color
934 */
935 public synchronized Color getDefaultColor(String colKey) {
936 StringSetting col = Utils.cast(defaultsMap.get(COLOR_PREFIX+colKey), StringSetting.class);
937 String colStr = col == null ? null : col.getValue();
938 return colStr == null || colStr.isEmpty() ? null : ColorHelper.html2color(colStr);
939 }
940
941 /**
942 * Stores a color
943 * @param colKey The color name
944 * @param val The color
945 * @return true if the setting was modified
946 * @see ColorProperty#put(Color)
947 */
948 public synchronized boolean putColor(String colKey, Color val) {
949 return put(COLOR_PREFIX+colKey, val != null ? ColorHelper.color2html(val, true) : null);
950 }
951
952 /**
953 * Gets an integer preference
954 * @param key The preference key
955 * @param def The default value to use
956 * @return The integer
957 * @see IntegerProperty#get()
958 */
959 public synchronized int getInteger(String key, int def) {
960 String v = get(key, Integer.toString(def));
961 if (v.isEmpty())
962 return def;
963
964 try {
965 return Integer.parseInt(v);
966 } catch (NumberFormatException e) {
967 // fall out
968 Main.trace(e);
969 }
970 return def;
971 }
972
973 /**
974 * Gets an integer that may be specialized
975 * @param key The basic key
976 * @param specName The sub-key to append to the key
977 * @param def The default value
978 * @return The integer value or the default value if it could not be parsed
979 */
980 public synchronized int getInteger(String key, String specName, int def) {
981 String v = get(key+'.'+specName);
982 if (v.isEmpty())
983 v = get(key, Integer.toString(def));
984 if (v.isEmpty())
985 return def;
986
987 try {
988 return Integer.parseInt(v);
989 } catch (NumberFormatException e) {
990 // fall out
991 Main.trace(e);
992 }
993 return def;
994 }
995
996 /**
997 * Gets a long preference
998 * @param key The preference key
999 * @param def The default value to use
1000 * @return The long value or the default value if it could not be parsed
1001 * @see LongProperty#get()
1002 */
1003 public synchronized long getLong(String key, long def) {
1004 String v = get(key, Long.toString(def));
1005 if (null == v)
1006 return def;
1007
1008 try {
1009 return Long.parseLong(v);
1010 } catch (NumberFormatException e) {
1011 // fall out
1012 Main.trace(e);
1013 }
1014 return def;
1015 }
1016
1017 /**
1018 * Gets a double preference
1019 * @param key The preference key
1020 * @param def The default value to use
1021 * @return The double value or the default value if it could not be parsed
1022 * @see LongProperty#get()
1023 */
1024 public synchronized double getDouble(String key, double def) {
1025 String v = get(key, Double.toString(def));
1026 if (null == v)
1027 return def;
1028
1029 try {
1030 return Double.parseDouble(v);
1031 } catch (NumberFormatException e) {
1032 // fall out
1033 Main.trace(e);
1034 }
1035 return def;
1036 }
1037
1038 /**
1039 * Get a list of values for a certain key
1040 * @param key the identifier for the setting
1041 * @param def the default value.
1042 * @return the corresponding value if the property has been set before, {@code def} otherwise
1043 */
1044 public Collection<String> getCollection(String key, Collection<String> def) {
1045 return getSetting(key, ListSetting.create(def), ListSetting.class).getValue();
1046 }
1047
1048 /**
1049 * Get a list of values for a certain key
1050 * @param key the identifier for the setting
1051 * @return the corresponding value if the property has been set before, an empty collection otherwise.
1052 */
1053 public Collection<String> getCollection(String key) {
1054 Collection<String> val = getCollection(key, null);
1055 return val == null ? Collections.<String>emptyList() : val;
1056 }
1057
1058 /**
1059 * Removes a value from a given String collection
1060 * @param key The preference key the collection is stored with
1061 * @param value The value that should be removed in the collection
1062 * @see #getCollection(String)
1063 */
1064 public synchronized void removeFromCollection(String key, String value) {
1065 List<String> a = new ArrayList<>(getCollection(key, Collections.<String>emptyList()));
1066 a.remove(value);
1067 putCollection(key, a);
1068 }
1069
1070 /**
1071 * Set a value for a certain setting. The changed setting is saved to the preference file immediately.
1072 * Due to caching mechanisms on modern operating systems and hardware, this shouldn't be a performance problem.
1073 * @param key the unique identifier for the setting
1074 * @param setting the value of the setting. In case it is null, the key-value entry will be removed.
1075 * @return {@code true}, if something has changed (i.e. value is different than before)
1076 */
1077 public boolean putSetting(final String key, Setting<?> setting) {
1078 CheckParameterUtil.ensureParameterNotNull(key);
1079 if (setting != null && setting.getValue() == null)
1080 throw new IllegalArgumentException("setting argument must not have null value");
1081 Setting<?> settingOld;
1082 Setting<?> settingCopy = null;
1083 synchronized (this) {
1084 if (setting == null) {
1085 settingOld = settingsMap.remove(key);
1086 if (settingOld == null)
1087 return false;
1088 } else {
1089 settingOld = settingsMap.get(key);
1090 if (setting.equals(settingOld))
1091 return false;
1092 if (settingOld == null && setting.equals(defaultsMap.get(key)))
1093 return false;
1094 settingCopy = setting.copy();
1095 settingsMap.put(key, settingCopy);
1096 }
1097 if (saveOnPut) {
1098 try {
1099 save();
1100 } catch (IOException e) {
1101 Main.warn(e, tr("Failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()));
1102 }
1103 }
1104 }
1105 // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
1106 firePreferenceChanged(key, settingOld, settingCopy);
1107 return true;
1108 }
1109
1110 /**
1111 * Get a setting of any type
1112 * @param key The key for the setting
1113 * @param def The default value to use if it was not found
1114 * @return The setting
1115 */
1116 public synchronized Setting<?> getSetting(String key, Setting<?> def) {
1117 return getSetting(key, def, Setting.class);
1118 }
1119
1120 /**
1121 * Get settings value for a certain key and provide default a value.
1122 * @param <T> the setting type
1123 * @param key the identifier for the setting
1124 * @param def the default value. For each call of getSetting() with a given key, the default value must be the same.
1125 * <code>def</code> must not be null, but the value of <code>def</code> can be null.
1126 * @param klass the setting type (same as T)
1127 * @return the corresponding value if the property has been set before, {@code def} otherwise
1128 */
1129 @SuppressWarnings("unchecked")
1130 public synchronized <T extends Setting<?>> T getSetting(String key, T def, Class<T> klass) {
1131 CheckParameterUtil.ensureParameterNotNull(key);
1132 CheckParameterUtil.ensureParameterNotNull(def);
1133 Setting<?> oldDef = defaultsMap.get(key);
1134 if (oldDef != null && oldDef.isNew() && oldDef.getValue() != null && def.getValue() != null && !def.equals(oldDef)) {
1135 Main.info("Defaults for " + key + " differ: " + def + " != " + defaultsMap.get(key));
1136 }
1137 if (def.getValue() != null || oldDef == null) {
1138 Setting<?> defCopy = def.copy();
1139 defCopy.setTime(System.currentTimeMillis() / 1000);
1140 defCopy.setNew(true);
1141 defaultsMap.put(key, defCopy);
1142 }
1143 Setting<?> prop = settingsMap.get(key);
1144 if (klass.isInstance(prop)) {
1145 return (T) prop;
1146 } else {
1147 return def;
1148 }
1149 }
1150
1151 /**
1152 * Put a collection.
1153 * @param key key
1154 * @param value value
1155 * @return {@code true}, if something has changed (i.e. value is different than before)
1156 */
1157 public boolean putCollection(String key, Collection<String> value) {
1158 return putSetting(key, value == null ? null : ListSetting.create(value));
1159 }
1160
1161 /**
1162 * Saves at most {@code maxsize} items of collection {@code val}.
1163 * @param key key
1164 * @param maxsize max number of items to save
1165 * @param val value
1166 * @return {@code true}, if something has changed (i.e. value is different than before)
1167 */
1168 public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
1169 Collection<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
1170 for (String i : val) {
1171 if (newCollection.size() >= maxsize) {
1172 break;
1173 }
1174 newCollection.add(i);
1175 }
1176 return putCollection(key, newCollection);
1177 }
1178
1179 /**
1180 * Used to read a 2-dimensional array of strings from the preference file.
1181 * If not a single entry could be found, <code>def</code> is returned.
1182 * @param key preference key
1183 * @param def default array value
1184 * @return array value
1185 */
1186 @SuppressWarnings({ "unchecked", "rawtypes" })
1187 public synchronized Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def) {
1188 ListListSetting val = getSetting(key, ListListSetting.create(def), ListListSetting.class);
1189 return (Collection) val.getValue();
1190 }
1191
1192 /**
1193 * Gets a collection of string collections for the given key
1194 * @param key The key
1195 * @return The collection of string collections or an empty collection as default
1196 */
1197 public Collection<Collection<String>> getArray(String key) {
1198 Collection<Collection<String>> res = getArray(key, null);
1199 return res == null ? Collections.<Collection<String>>emptyList() : res;
1200 }
1201
1202 /**
1203 * Put an array.
1204 * @param key key
1205 * @param value value
1206 * @return {@code true}, if something has changed (i.e. value is different than before)
1207 */
1208 public boolean putArray(String key, Collection<Collection<String>> value) {
1209 return putSetting(key, value == null ? null : ListListSetting.create(value));
1210 }
1211
1212 /**
1213 * Gets a collection of key/value maps.
1214 * @param key The key to search at
1215 * @param def The default value to use
1216 * @return The stored value or the default one if it could not be parsed
1217 */
1218 public Collection<Map<String, String>> getListOfStructs(String key, Collection<Map<String, String>> def) {
1219 return getSetting(key, new MapListSetting(def == null ? null : new ArrayList<>(def)), MapListSetting.class).getValue();
1220 }
1221
1222 /**
1223 * Stores a list of structs
1224 * @param key The key to store the list in
1225 * @param value A list of key/value maps
1226 * @return <code>true</code> if the value was changed
1227 * @see #getListOfStructs(String, Collection)
1228 */
1229 public boolean putListOfStructs(String key, Collection<Map<String, String>> value) {
1230 return putSetting(key, value == null ? null : new MapListSetting(new ArrayList<>(value)));
1231 }
1232
1233 /**
1234 * Annotation used for converting objects to String Maps and vice versa.
1235 * Indicates that a certain field should be considered in the conversion process. Otherwise it is ignored.
1236 *
1237 * @see #serializeStruct(java.lang.Object, java.lang.Class)
1238 * @see #deserializeStruct(java.util.Map, java.lang.Class)
1239 */
1240 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
1241 public @interface pref { }
1242
1243 /**
1244 * Annotation used for converting objects to String Maps.
1245 * Indicates that a certain field should be written to the map, even if the value is the same as the default value.
1246 *
1247 * @see #serializeStruct(java.lang.Object, java.lang.Class)
1248 */
1249 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
1250 public @interface writeExplicitly { }
1251
1252 /**
1253 * Get a list of hashes which are represented by a struct-like class.
1254 * Possible properties are given by fields of the class klass that have the @pref annotation.
1255 * Default constructor is used to initialize the struct objects, properties then override some of these default values.
1256 * @param <T> klass type
1257 * @param key main preference key
1258 * @param klass The struct class
1259 * @return a list of objects of type T or an empty list if nothing was found
1260 */
1261 public <T> List<T> getListOfStructs(String key, Class<T> klass) {
1262 return Optional.ofNullable(getListOfStructs(key, null, klass)).orElseGet(Collections::emptyList);
1263 }
1264
1265 /**
1266 * same as above, but returns def if nothing was found
1267 * @param <T> klass type
1268 * @param key main preference key
1269 * @param def default value
1270 * @param klass The struct class
1271 * @return a list of objects of type T or {@code def} if nothing was found
1272 */
1273 public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) {
1274 Collection<Map<String, String>> prop =
1275 getListOfStructs(key, def == null ? null : serializeListOfStructs(def, klass));
1276 if (prop == null)
1277 return def == null ? null : new ArrayList<>(def);
1278 return prop.stream().map(p -> deserializeStruct(p, klass)).collect(Collectors.toList());
1279 }
1280
1281 /**
1282 * Convenience method that saves a MapListSetting which is provided as a collection of objects.
1283 *
1284 * Each object is converted to a <code>Map&lt;String, String&gt;</code> using the fields with {@link pref} annotation.
1285 * The field name is the key and the value will be converted to a string.
1286 *
1287 * Considers only fields that have the @pref annotation.
1288 * In addition it does not write fields with null values. (Thus they are cleared)
1289 * Default values are given by the field values after default constructor has been called.
1290 * Fields equal to the default value are not written unless the field has the @writeExplicitly annotation.
1291 * @param <T> the class,
1292 * @param key main preference key
1293 * @param val the list that is supposed to be saved
1294 * @param klass The struct class
1295 * @return true if something has changed
1296 */
1297 public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) {
1298 return putListOfStructs(key, serializeListOfStructs(val, klass));
1299 }
1300
1301 private static <T> Collection<Map<String, String>> serializeListOfStructs(Collection<T> l, Class<T> klass) {
1302 if (l == null)
1303 return null;
1304 Collection<Map<String, String>> vals = new ArrayList<>();
1305 for (T struct : l) {
1306 if (struct != null) {
1307 vals.add(serializeStruct(struct, klass));
1308 }
1309 }
1310 return vals;
1311 }
1312
1313 @SuppressWarnings("rawtypes")
1314 private static String mapToJson(Map map) {
1315 StringWriter stringWriter = new StringWriter();
1316 try (JsonWriter writer = Json.createWriter(stringWriter)) {
1317 JsonObjectBuilder object = Json.createObjectBuilder();
1318 for (Object o: map.entrySet()) {
1319 Entry e = (Entry) o;
1320 Object evalue = e.getValue();
1321 object.add(e.getKey().toString(), evalue.toString());
1322 }
1323 writer.writeObject(object.build());
1324 }
1325 return stringWriter.toString();
1326 }
1327
1328 @SuppressWarnings({ "rawtypes", "unchecked" })
1329 private static Map mapFromJson(String s) {
1330 Map ret = null;
1331 try (JsonReader reader = Json.createReader(new StringReader(s))) {
1332 JsonObject object = reader.readObject();
1333 ret = new HashMap(object.size());
1334 for (Entry<String, JsonValue> e: object.entrySet()) {
1335 JsonValue value = e.getValue();
1336 if (value instanceof JsonString) {
1337 // in some cases, when JsonValue.toString() is called, then additional quotation marks are left in value
1338 ret.put(e.getKey(), ((JsonString) value).getString());
1339 } else {
1340 ret.put(e.getKey(), e.getValue().toString());
1341 }
1342 }
1343 }
1344 return ret;
1345 }
1346
1347 @SuppressWarnings("rawtypes")
1348 private static String multiMapToJson(MultiMap map) {
1349 StringWriter stringWriter = new StringWriter();
1350 try (JsonWriter writer = Json.createWriter(stringWriter)) {
1351 JsonObjectBuilder object = Json.createObjectBuilder();
1352 for (Object o: map.entrySet()) {
1353 Entry e = (Entry) o;
1354 Set evalue = (Set) e.getValue();
1355 JsonArrayBuilder a = Json.createArrayBuilder();
1356 for (Object evo: evalue) {
1357 a.add(evo.toString());
1358 }
1359 object.add(e.getKey().toString(), a.build());
1360 }
1361 writer.writeObject(object.build());
1362 }
1363 return stringWriter.toString();
1364 }
1365
1366 @SuppressWarnings({ "rawtypes", "unchecked" })
1367 private static MultiMap multiMapFromJson(String s) {
1368 MultiMap ret = null;
1369 try (JsonReader reader = Json.createReader(new StringReader(s))) {
1370 JsonObject object = reader.readObject();
1371 ret = new MultiMap(object.size());
1372 for (Entry<String, JsonValue> e: object.entrySet()) {
1373 JsonValue value = e.getValue();
1374 if (value instanceof JsonArray) {
1375 for (JsonString js: ((JsonArray) value).getValuesAs(JsonString.class)) {
1376 ret.put(e.getKey(), js.getString());
1377 }
1378 } else if (value instanceof JsonString) {
1379 // in some cases, when JsonValue.toString() is called, then additional quotation marks are left in value
1380 ret.put(e.getKey(), ((JsonString) value).getString());
1381 } else {
1382 ret.put(e.getKey(), e.getValue().toString());
1383 }
1384 }
1385 }
1386 return ret;
1387 }
1388
1389 /**
1390 * Convert an object to a String Map, by using field names and values as map key and value.
1391 *
1392 * The field value is converted to a String.
1393 *
1394 * Only fields with annotation {@link pref} are taken into account.
1395 *
1396 * Fields will not be written to the map if the value is null or unchanged
1397 * (compared to an object created with the no-arg-constructor).
1398 * The {@link writeExplicitly} annotation overrides this behavior, i.e. the default value will also be written.
1399 *
1400 * @param <T> the class of the object <code>struct</code>
1401 * @param struct the object to be converted
1402 * @param klass the class T
1403 * @return the resulting map (same data content as <code>struct</code>)
1404 */
1405 public static <T> Map<String, String> serializeStruct(T struct, Class<T> klass) {
1406 T structPrototype;
1407 try {
1408 structPrototype = klass.getConstructor().newInstance();
1409 } catch (ReflectiveOperationException ex) {
1410 throw new IllegalArgumentException(ex);
1411 }
1412
1413 Map<String, String> hash = new LinkedHashMap<>();
1414 for (Field f : klass.getDeclaredFields()) {
1415 if (f.getAnnotation(pref.class) == null) {
1416 continue;
1417 }
1418 Utils.setObjectsAccessible(f);
1419 try {
1420 Object fieldValue = f.get(struct);
1421 Object defaultFieldValue = f.get(structPrototype);
1422 if (fieldValue != null && (f.getAnnotation(writeExplicitly.class) != null || !Objects.equals(fieldValue, defaultFieldValue))) {
1423 String key = f.getName().replace('_', '-');
1424 if (fieldValue instanceof Map) {
1425 hash.put(key, mapToJson((Map<?, ?>) fieldValue));
1426 } else if (fieldValue instanceof MultiMap) {
1427 hash.put(key, multiMapToJson((MultiMap<?, ?>) fieldValue));
1428 } else {
1429 hash.put(key, fieldValue.toString());
1430 }
1431 }
1432 } catch (IllegalAccessException ex) {
1433 throw new JosmRuntimeException(ex);
1434 }
1435 }
1436 return hash;
1437 }
1438
1439 /**
1440 * Converts a String-Map to an object of a certain class, by comparing map keys to field names of the class and assigning
1441 * map values to the corresponding fields.
1442 *
1443 * The map value (a String) is converted to the field type. Supported types are: boolean, Boolean, int, Integer, double,
1444 * Double, String, Map&lt;String, String&gt; and Map&lt;String, List&lt;String&gt;&gt;.
1445 *
1446 * Only fields with annotation {@link pref} are taken into account.
1447 * @param <T> the class
1448 * @param hash the string map with initial values
1449 * @param klass the class T
1450 * @return an object of class T, initialized as described above
1451 */
1452 public static <T> T deserializeStruct(Map<String, String> hash, Class<T> klass) {
1453 T struct = null;
1454 try {
1455 struct = klass.getConstructor().newInstance();
1456 } catch (ReflectiveOperationException ex) {
1457 throw new IllegalArgumentException(ex);
1458 }
1459 for (Entry<String, String> key_value : hash.entrySet()) {
1460 Object value;
1461 Field f;
1462 try {
1463 f = klass.getDeclaredField(key_value.getKey().replace('-', '_'));
1464 } catch (NoSuchFieldException ex) {
1465 Main.trace(ex);
1466 continue;
1467 }
1468 if (f.getAnnotation(pref.class) == null) {
1469 continue;
1470 }
1471 Utils.setObjectsAccessible(f);
1472 if (f.getType() == Boolean.class || f.getType() == boolean.class) {
1473 value = Boolean.valueOf(key_value.getValue());
1474 } else if (f.getType() == Integer.class || f.getType() == int.class) {
1475 try {
1476 value = Integer.valueOf(key_value.getValue());
1477 } catch (NumberFormatException nfe) {
1478 continue;
1479 }
1480 } else if (f.getType() == Double.class || f.getType() == double.class) {
1481 try {
1482 value = Double.valueOf(key_value.getValue());
1483 } catch (NumberFormatException nfe) {
1484 continue;
1485 }
1486 } else if (f.getType() == String.class) {
1487 value = key_value.getValue();
1488 } else if (f.getType().isAssignableFrom(Map.class)) {
1489 value = mapFromJson(key_value.getValue());
1490 } else if (f.getType().isAssignableFrom(MultiMap.class)) {
1491 value = multiMapFromJson(key_value.getValue());
1492 } else
1493 throw new JosmRuntimeException("unsupported preference primitive type");
1494
1495 try {
1496 f.set(struct, value);
1497 } catch (IllegalArgumentException ex) {
1498 throw new AssertionError(ex);
1499 } catch (IllegalAccessException ex) {
1500 throw new JosmRuntimeException(ex);
1501 }
1502 }
1503 return struct;
1504 }
1505
1506 /**
1507 * Gets a map of all settings that are currently stored
1508 * @return The settings
1509 */
1510 public Map<String, Setting<?>> getAllSettings() {
1511 return new TreeMap<>(settingsMap);
1512 }
1513
1514 /**
1515 * Gets a map of all currently known defaults
1516 * @return The map (key/setting)
1517 */
1518 public Map<String, Setting<?>> getAllDefaults() {
1519 return new TreeMap<>(defaultsMap);
1520 }
1521
1522 /**
1523 * Updates system properties with the current values in the preferences.
1524 */
1525 public void updateSystemProperties() {
1526 if ("true".equals(get("prefer.ipv6", "auto")) && !"true".equals(Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true"))) {
1527 // never set this to false, only true!
1528 Main.info(tr("Try enabling IPv6 network, prefering IPv6 over IPv4 (only works on early startup)."));
1529 }
1530 Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString());
1531 Utils.updateSystemProperty("user.language", get("language"));
1532 // Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-6292739
1533 // Force AWT toolkit to update its internal preferences (fix #6345).
1534 // Does not work anymore with Java 9, to remove with Java 9 migration
1535 if (Utils.getJavaVersion() < 9 && !GraphicsEnvironment.isHeadless()) {
1536 try {
1537 Field field = Toolkit.class.getDeclaredField("resources");
1538 Utils.setObjectsAccessible(field);
1539 field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt"));
1540 } catch (ReflectiveOperationException | RuntimeException e) { // NOPMD
1541 // Catch RuntimeException in order to catch InaccessibleObjectException, new in Java 9
1542 Main.warn(e);
1543 }
1544 }
1545 // Possibility to disable SNI (not by default) in case of misconfigured https servers
1546 // See #9875 + http://stackoverflow.com/a/14884941/2257172
1547 // then https://josm.openstreetmap.de/ticket/12152#comment:5 for details
1548 if (getBoolean("jdk.tls.disableSNIExtension", false)) {
1549 Utils.updateSystemProperty("jsse.enableSNIExtension", "false");
1550 }
1551 }
1552
1553 /**
1554 * Replies the collection of plugin site URLs from where plugin lists can be downloaded.
1555 * @return the collection of plugin site URLs
1556 * @see #getOnlinePluginSites
1557 */
1558 public Collection<String> getPluginSites() {
1559 return getCollection("pluginmanager.sites", Collections.singleton(Main.getJOSMWebsite()+"/pluginicons%<?plugins=>"));
1560 }
1561
1562 /**
1563 * Returns the list of plugin sites available according to offline mode settings.
1564 * @return the list of available plugin sites
1565 * @since 8471
1566 */
1567 public Collection<String> getOnlinePluginSites() {
1568 Collection<String> pluginSites = new ArrayList<>(getPluginSites());
1569 for (Iterator<String> it = pluginSites.iterator(); it.hasNext();) {
1570 try {
1571 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(it.next(), Main.getJOSMWebsite());
1572 } catch (OfflineAccessException ex) {
1573 Main.warn(ex, false);
1574 it.remove();
1575 }
1576 }
1577 return pluginSites;
1578 }
1579
1580 /**
1581 * Sets the collection of plugin site URLs.
1582 *
1583 * @param sites the site URLs
1584 */
1585 public void setPluginSites(Collection<String> sites) {
1586 putCollection("pluginmanager.sites", sites);
1587 }
1588
1589 /**
1590 * Returns XML describing these preferences.
1591 * @param nopass if password must be excluded
1592 * @return XML
1593 */
1594 public String toXML(boolean nopass) {
1595 return toXML(settingsMap.entrySet(), nopass, false);
1596 }
1597
1598 /**
1599 * Returns XML describing the given preferences.
1600 * @param settings preferences settings
1601 * @param nopass if password must be excluded
1602 * @param defaults true, if default values are converted to XML, false for
1603 * regular preferences
1604 * @return XML
1605 */
1606 public String toXML(Collection<Entry<String, Setting<?>>> settings, boolean nopass, boolean defaults) {
1607 try (
1608 StringWriter sw = new StringWriter();
1609 PreferencesWriter prefWriter = new PreferencesWriter(new PrintWriter(sw), nopass, defaults)
1610 ) {
1611 prefWriter.write(settings);
1612 sw.flush();
1613 return sw.toString();
1614 } catch (IOException e) {
1615 Main.error(e);
1616 return null;
1617 }
1618 }
1619
1620 /**
1621 * Removes obsolete preference settings. If you throw out a once-used preference
1622 * setting, add it to the list here with an expiry date (written as comment). If you
1623 * see something with an expiry date in the past, remove it from the list.
1624 * @param loadedVersion JOSM version when the preferences file was written
1625 */
1626 private void removeObsolete(int loadedVersion) {
1627 // drop in March 2017
1628 removeUrlFromEntries(loadedVersion, 10063,
1629 "validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
1630 "resource://data/validator/power.mapcss");
1631 // drop in March 2017
1632 if (loadedVersion < 11058) {
1633 migrateOldColorKeys();
1634 }
1635 // drop in September 2017
1636 if (loadedVersion < 11424) {
1637 addNewerDefaultEntry(
1638 "validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
1639 "resource://data/validator/territories.mapcss");
1640 }
1641
1642 for (String key : OBSOLETE_PREF_KEYS) {
1643 if (settingsMap.containsKey(key)) {
1644 settingsMap.remove(key);
1645 Main.info(tr("Preference setting {0} has been removed since it is no longer used.", key));
1646 }
1647 }
1648 }
1649
1650 private void migrateOldColorKeys() {
1651 settingsMap.keySet().stream()
1652 .filter(key -> key.startsWith(COLOR_PREFIX))
1653 .flatMap(this::searchOldColorKey)
1654 .collect(Collectors.toList()) // to avoid ConcurrentModificationException
1655 .forEach(entry -> {
1656 final String oldKey = entry.getKey();
1657 final String newKey = entry.getValue();
1658 Main.info("Migrating old color key {0} => {1}", oldKey, newKey);
1659 put(newKey, get(oldKey));
1660 put(oldKey, null);
1661 });
1662 }
1663
1664 private Stream<AbstractMap.SimpleImmutableEntry<String, String>> searchOldColorKey(String key) {
1665 final String newKey = ColorProperty.getColorKey(key.substring(COLOR_PREFIX.length()));
1666 return key.equals(newKey) || settingsMap.containsKey(newKey)
1667 ? Stream.empty()
1668 : Stream.of(new AbstractMap.SimpleImmutableEntry<>(key, newKey));
1669 }
1670
1671 private void removeUrlFromEntries(int loadedVersion, int versionMax, String key, String urlPart) {
1672 if (loadedVersion < versionMax) {
1673 Setting<?> setting = settingsMap.get(key);
1674 if (setting instanceof MapListSetting) {
1675 List<Map<String, String>> l = new LinkedList<>();
1676 boolean modified = false;
1677 for (Map<String, String> map: ((MapListSetting) setting).getValue()) {
1678 String url = map.get("url");
1679 if (url != null && url.contains(urlPart)) {
1680 modified = true;
1681 } else {
1682 l.add(map);
1683 }
1684 }
1685 if (modified) {
1686 putListOfStructs(key, l);
1687 }
1688 }
1689 }
1690 }
1691
1692 private void addNewerDefaultEntry(String key, final String url) {
1693 Setting<?> setting = settingsMap.get(key);
1694 if (setting instanceof MapListSetting) {
1695 List<Map<String, String>> l = new ArrayList<>(((MapListSetting) setting).getValue());
1696 if (l.stream().noneMatch(x -> x.containsValue(url))) {
1697 RulePrefHelper helper = ValidatorTagCheckerRulesPreference.RulePrefHelper.INSTANCE;
1698 Optional<ExtendedSourceEntry> val = helper.getDefault().stream().filter(x -> url.equals(x.url)).findFirst();
1699 if (val.isPresent()) {
1700 l.add(helper.serialize(val.get()));
1701 }
1702 putListOfStructs(key, l);
1703 }
1704 }
1705 }
1706
1707 /**
1708 * Enables or not the preferences file auto-save mechanism (save each time a setting is changed).
1709 * This behaviour is enabled by default.
1710 * @param enable if {@code true}, makes JOSM save preferences file each time a setting is changed
1711 * @since 7085
1712 */
1713 public final void enableSaveOnPut(boolean enable) {
1714 synchronized (this) {
1715 saveOnPut = enable;
1716 }
1717 }
1718}
Note: See TracBrowser for help on using the repository browser.