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

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

PMD - Strict Exceptions

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