1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.io.BufferedReader;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.FileOutputStream;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.InputStreamReader;
|
---|
13 | import java.io.OutputStreamWriter;
|
---|
14 | import java.io.PrintWriter;
|
---|
15 | import java.io.Reader;
|
---|
16 | import java.lang.annotation.Retention;
|
---|
17 | import java.lang.annotation.RetentionPolicy;
|
---|
18 | import java.lang.reflect.Field;
|
---|
19 | import java.nio.channels.FileChannel;
|
---|
20 | import java.util.ArrayList;
|
---|
21 | import java.util.Arrays;
|
---|
22 | import java.util.Collection;
|
---|
23 | import java.util.Collections;
|
---|
24 | import java.util.Iterator;
|
---|
25 | import java.util.LinkedHashMap;
|
---|
26 | import java.util.LinkedList;
|
---|
27 | import java.util.List;
|
---|
28 | import java.util.Map;
|
---|
29 | import java.util.Map.Entry;
|
---|
30 | import java.util.Properties;
|
---|
31 | import java.util.SortedMap;
|
---|
32 | import java.util.TreeMap;
|
---|
33 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
34 | import java.util.regex.Matcher;
|
---|
35 | import java.util.regex.Pattern;
|
---|
36 |
|
---|
37 | import javax.swing.JOptionPane;
|
---|
38 | import javax.swing.UIManager;
|
---|
39 | import javax.xml.parsers.SAXParserFactory;
|
---|
40 | import javax.xml.stream.XMLInputFactory;
|
---|
41 | import javax.xml.stream.XMLStreamConstants;
|
---|
42 | import javax.xml.stream.XMLStreamException;
|
---|
43 | import javax.xml.stream.XMLStreamReader;
|
---|
44 | import javax.xml.transform.stax.StAXSource;
|
---|
45 | import javax.xml.transform.stream.StreamSource;
|
---|
46 | import javax.xml.validation.Schema;
|
---|
47 | import javax.xml.validation.SchemaFactory;
|
---|
48 | import javax.xml.validation.Validator;
|
---|
49 |
|
---|
50 | import org.openstreetmap.josm.Main;
|
---|
51 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
52 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
53 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
54 | import org.openstreetmap.josm.tools.Utils;
|
---|
55 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * This class holds all preferences for JOSM.
|
---|
59 | *
|
---|
60 | * Other classes can register their beloved properties here. All properties will be
|
---|
61 | * saved upon set-access.
|
---|
62 | *
|
---|
63 | * Each property is a simple key=value pair of Strings.
|
---|
64 | * In addition, each key has a unique default value that is set when the value is first
|
---|
65 | * accessed using one of the get...() methods. You can use the same preference
|
---|
66 | * key in different parts of the code, but the default value must be the same
|
---|
67 | * everywhere. null is a legitimate default value.
|
---|
68 | *
|
---|
69 | * At the moment, there is no such thing as an empty value.
|
---|
70 | * If you put "" or null as value, the property is removed.
|
---|
71 | *
|
---|
72 | * @author imi
|
---|
73 | */
|
---|
74 | public class Preferences {
|
---|
75 | /**
|
---|
76 | * Internal storage for the preference directory.
|
---|
77 | * Do not access this variable directly!
|
---|
78 | * @see #getPreferencesDirFile()
|
---|
79 | */
|
---|
80 | private File preferencesDirFile = null;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Map the property name to the property object. Does not contain null or "" values.
|
---|
84 | */
|
---|
85 | protected final SortedMap<String, String> properties = new TreeMap<String, String>();
|
---|
86 | protected final SortedMap<String, String> defaults = new TreeMap<String, String>();
|
---|
87 | protected final SortedMap<String, String> colornames = new TreeMap<String, String>();
|
---|
88 |
|
---|
89 | protected final SortedMap<String, List<String>> collectionProperties = new TreeMap<String, List<String>>();
|
---|
90 | protected final SortedMap<String, List<String>> collectionDefaults = new TreeMap<String, List<String>>();
|
---|
91 |
|
---|
92 | protected final SortedMap<String, List<List<String>>> arrayProperties = new TreeMap<String, List<List<String>>>();
|
---|
93 | protected final SortedMap<String, List<List<String>>> arrayDefaults = new TreeMap<String, List<List<String>>>();
|
---|
94 |
|
---|
95 | protected final SortedMap<String, List<Map<String,String>>> listOfStructsProperties = new TreeMap<String, List<Map<String,String>>>();
|
---|
96 | protected final SortedMap<String, List<Map<String,String>>> listOfStructsDefaults = new TreeMap<String, List<Map<String,String>>>();
|
---|
97 |
|
---|
98 | public interface Setting<T> {
|
---|
99 | T getValue();
|
---|
100 | void visit(SettingVisitor visitor);
|
---|
101 | }
|
---|
102 |
|
---|
103 | abstract public static class AbstractSetting<T> implements Setting<T> {
|
---|
104 | private T value;
|
---|
105 | public AbstractSetting(T value) {
|
---|
106 | this.value = value;
|
---|
107 | }
|
---|
108 | public T getValue() {
|
---|
109 | return value;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static class StringSetting extends AbstractSetting<String> {
|
---|
114 | public StringSetting(String value) {
|
---|
115 | super(value);
|
---|
116 | }
|
---|
117 | public void visit(SettingVisitor visitor) {
|
---|
118 | visitor.visit(this);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public static class ListSetting extends AbstractSetting<List<String>> {
|
---|
123 | public ListSetting(List<String> value) {
|
---|
124 | super(value);
|
---|
125 | }
|
---|
126 | public void visit(SettingVisitor visitor) {
|
---|
127 | visitor.visit(this);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | public static class ListListSetting extends AbstractSetting<List<List<String>>> {
|
---|
132 | public ListListSetting(List<List<String>> value) {
|
---|
133 | super(value);
|
---|
134 | }
|
---|
135 | public void visit(SettingVisitor visitor) {
|
---|
136 | visitor.visit(this);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | public static class MapListSetting extends AbstractSetting<List<Map<String, String>>> {
|
---|
141 | public MapListSetting(List<Map<String, String>> value) {
|
---|
142 | super(value);
|
---|
143 | }
|
---|
144 | public void visit(SettingVisitor visitor) {
|
---|
145 | visitor.visit(this);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public interface SettingVisitor {
|
---|
150 | void visit(StringSetting setting);
|
---|
151 | void visit(ListSetting value);
|
---|
152 | void visit(ListListSetting value);
|
---|
153 | void visit(MapListSetting value);
|
---|
154 | }
|
---|
155 |
|
---|
156 | public interface PreferenceChangeEvent<T> {
|
---|
157 | String getKey();
|
---|
158 | Setting<T> getOldValue();
|
---|
159 | Setting<T> getNewValue();
|
---|
160 | }
|
---|
161 |
|
---|
162 | public interface PreferenceChangedListener {
|
---|
163 | void preferenceChanged(PreferenceChangeEvent e);
|
---|
164 | }
|
---|
165 |
|
---|
166 | private static class DefaultPreferenceChangeEvent<T> implements PreferenceChangeEvent<T> {
|
---|
167 | private final String key;
|
---|
168 | private final Setting<T> oldValue;
|
---|
169 | private final Setting<T> newValue;
|
---|
170 |
|
---|
171 | public DefaultPreferenceChangeEvent(String key, Setting<T> oldValue, Setting<T> newValue) {
|
---|
172 | this.key = key;
|
---|
173 | this.oldValue = oldValue;
|
---|
174 | this.newValue = newValue;
|
---|
175 | }
|
---|
176 |
|
---|
177 | public String getKey() {
|
---|
178 | return key;
|
---|
179 | }
|
---|
180 | public Setting<T> getOldValue() {
|
---|
181 | return oldValue;
|
---|
182 | }
|
---|
183 | public Setting<T> getNewValue() {
|
---|
184 | return newValue;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | public interface ColorKey {
|
---|
189 | String getColorName();
|
---|
190 | String getSpecialName();
|
---|
191 | Color getDefault();
|
---|
192 | }
|
---|
193 |
|
---|
194 | private final CopyOnWriteArrayList<PreferenceChangedListener> listeners = new CopyOnWriteArrayList<PreferenceChangedListener>();
|
---|
195 |
|
---|
196 | public void addPreferenceChangeListener(PreferenceChangedListener listener) {
|
---|
197 | if (listener != null) {
|
---|
198 | listeners.addIfAbsent(listener);
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | public void removePreferenceChangeListener(PreferenceChangedListener listener) {
|
---|
203 | listeners.remove(listener);
|
---|
204 | }
|
---|
205 |
|
---|
206 | protected <T> void firePreferenceChanged(String key, Setting<T> oldValue, Setting<T> newValue) {
|
---|
207 | PreferenceChangeEvent<T> evt = new DefaultPreferenceChangeEvent<T>(key, oldValue, newValue);
|
---|
208 | for (PreferenceChangedListener l : listeners) {
|
---|
209 | l.preferenceChanged(evt);
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Return the location of the user defined preferences file
|
---|
215 | */
|
---|
216 | public String getPreferencesDir() {
|
---|
217 | final String path = getPreferencesDirFile().getPath();
|
---|
218 | if (path.endsWith(File.separator))
|
---|
219 | return path;
|
---|
220 | return path + File.separator;
|
---|
221 | }
|
---|
222 |
|
---|
223 | public File getPreferencesDirFile() {
|
---|
224 | if (preferencesDirFile != null)
|
---|
225 | return preferencesDirFile;
|
---|
226 | String path;
|
---|
227 | path = System.getProperty("josm.home");
|
---|
228 | if (path != null) {
|
---|
229 | preferencesDirFile = new File(path);
|
---|
230 | } else {
|
---|
231 | path = System.getenv("APPDATA");
|
---|
232 | if (path != null) {
|
---|
233 | preferencesDirFile = new File(path, "JOSM");
|
---|
234 | } else {
|
---|
235 | preferencesDirFile = new File(System.getProperty("user.home"), ".josm");
|
---|
236 | }
|
---|
237 | }
|
---|
238 | return preferencesDirFile;
|
---|
239 | }
|
---|
240 |
|
---|
241 | public File getPreferenceFile() {
|
---|
242 | return new File(getPreferencesDirFile(), "preferences.xml");
|
---|
243 | }
|
---|
244 |
|
---|
245 | public File getOldPreferenceFile() {
|
---|
246 | return new File(getPreferencesDirFile(), "preferences");
|
---|
247 | }
|
---|
248 |
|
---|
249 | public File getPluginsDirectory() {
|
---|
250 | return new File(getPreferencesDirFile(), "plugins");
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * @return A list of all existing directories where resources could be stored.
|
---|
255 | */
|
---|
256 | public Collection<String> getAllPossiblePreferenceDirs() {
|
---|
257 | LinkedList<String> locations = new LinkedList<String>();
|
---|
258 | locations.add(Main.pref.getPreferencesDir());
|
---|
259 | String s;
|
---|
260 | if ((s = System.getenv("JOSM_RESOURCES")) != null) {
|
---|
261 | if (!s.endsWith(File.separator)) {
|
---|
262 | s = s + File.separator;
|
---|
263 | }
|
---|
264 | locations.add(s);
|
---|
265 | }
|
---|
266 | if ((s = System.getProperty("josm.resources")) != null) {
|
---|
267 | if (!s.endsWith(File.separator)) {
|
---|
268 | s = s + File.separator;
|
---|
269 | }
|
---|
270 | locations.add(s);
|
---|
271 | }
|
---|
272 | String appdata = System.getenv("APPDATA");
|
---|
273 | if (System.getenv("ALLUSERSPROFILE") != null && appdata != null
|
---|
274 | && appdata.lastIndexOf(File.separator) != -1) {
|
---|
275 | appdata = appdata.substring(appdata.lastIndexOf(File.separator));
|
---|
276 | locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
|
---|
277 | appdata), "JOSM").getPath());
|
---|
278 | }
|
---|
279 | locations.add("/usr/local/share/josm/");
|
---|
280 | locations.add("/usr/local/lib/josm/");
|
---|
281 | locations.add("/usr/share/josm/");
|
---|
282 | locations.add("/usr/lib/josm/");
|
---|
283 | return locations;
|
---|
284 | }
|
---|
285 |
|
---|
286 | synchronized public boolean hasKey(final String key) {
|
---|
287 | return properties.containsKey(key);
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Get settings value for a certain key.
|
---|
292 | * @param key the identifier for the setting
|
---|
293 | * @return "" if there is nothing set for the preference key,
|
---|
294 | * the corresponding value otherwise. The result is not null.
|
---|
295 | */
|
---|
296 | synchronized public String get(final String key) {
|
---|
297 | putDefault(key, null);
|
---|
298 | if (!properties.containsKey(key))
|
---|
299 | return "";
|
---|
300 | return properties.get(key);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Get settings value for a certain key and provide default a value.
|
---|
305 | * @param key the identifier for the setting
|
---|
306 | * @param def the default value. For each call of get() with a given key, the
|
---|
307 | * default value must be the same.
|
---|
308 | * @return the corresponding value if the property has been set before,
|
---|
309 | * def otherwise
|
---|
310 | */
|
---|
311 | synchronized public String get(final String key, final String def) {
|
---|
312 | putDefault(key, def);
|
---|
313 | final String prop = properties.get(key);
|
---|
314 | if (prop == null || prop.equals(""))
|
---|
315 | return def;
|
---|
316 | return prop;
|
---|
317 | }
|
---|
318 |
|
---|
319 | synchronized public Map<String, String> getAllPrefix(final String prefix) {
|
---|
320 | final Map<String,String> all = new TreeMap<String,String>();
|
---|
321 | for (final Entry<String,String> e : properties.entrySet()) {
|
---|
322 | if (e.getKey().startsWith(prefix)) {
|
---|
323 | all.put(e.getKey(), e.getValue());
|
---|
324 | }
|
---|
325 | }
|
---|
326 | return all;
|
---|
327 | }
|
---|
328 |
|
---|
329 | synchronized private Map<String, String> getAllPrefixDefault(final String prefix) {
|
---|
330 | final Map<String,String> all = new TreeMap<String,String>();
|
---|
331 | for (final Entry<String,String> e : defaults.entrySet()) {
|
---|
332 | if (e.getKey().startsWith(prefix)) {
|
---|
333 | all.put(e.getKey(), e.getValue());
|
---|
334 | }
|
---|
335 | }
|
---|
336 | return all;
|
---|
337 | }
|
---|
338 |
|
---|
339 | synchronized public TreeMap<String, String> getAllColors() {
|
---|
340 | final TreeMap<String,String> all = new TreeMap<String,String>();
|
---|
341 | for (final Entry<String,String> e : defaults.entrySet()) {
|
---|
342 | if (e.getKey().startsWith("color.") && e.getValue() != null) {
|
---|
343 | all.put(e.getKey().substring(6), e.getValue());
|
---|
344 | }
|
---|
345 | }
|
---|
346 | for (final Entry<String,String> e : properties.entrySet()) {
|
---|
347 | if (e.getKey().startsWith("color.")) {
|
---|
348 | all.put(e.getKey().substring(6), e.getValue());
|
---|
349 | }
|
---|
350 | }
|
---|
351 | return all;
|
---|
352 | }
|
---|
353 |
|
---|
354 | synchronized public Map<String, String> getDefaults() {
|
---|
355 | return defaults;
|
---|
356 | }
|
---|
357 |
|
---|
358 | synchronized public void putDefault(final String key, final String def) {
|
---|
359 | if(!defaults.containsKey(key) || defaults.get(key) == null) {
|
---|
360 | defaults.put(key, def);
|
---|
361 | } else if(def != null && !defaults.get(key).equals(def)) {
|
---|
362 | System.out.println("Defaults for " + key + " differ: " + def + " != " + defaults.get(key));
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | synchronized public boolean getBoolean(final String key) {
|
---|
367 | putDefault(key, null);
|
---|
368 | return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : false;
|
---|
369 | }
|
---|
370 |
|
---|
371 | synchronized public boolean getBoolean(final String key, final boolean def) {
|
---|
372 | putDefault(key, Boolean.toString(def));
|
---|
373 | return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
|
---|
374 | }
|
---|
375 |
|
---|
376 | synchronized public boolean getBoolean(final String key, final String specName, final boolean def) {
|
---|
377 | putDefault(key, Boolean.toString(def));
|
---|
378 | String skey = key+"."+specName;
|
---|
379 | if(properties.containsKey(skey))
|
---|
380 | return Boolean.parseBoolean(properties.get(skey));
|
---|
381 | return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Set a value for a certain setting. The changed setting is saved
|
---|
386 | * to the preference file immediately. Due to caching mechanisms on modern
|
---|
387 | * operating systems and hardware, this shouldn't be a performance problem.
|
---|
388 | * @param key the unique identifier for the setting
|
---|
389 | * @param value the value of the setting. Can be null or "" wich both removes
|
---|
390 | * the key-value entry.
|
---|
391 | * @return if true, something has changed (i.e. value is different than before)
|
---|
392 | */
|
---|
393 | public boolean put(final String key, String value) {
|
---|
394 | boolean changed = false;
|
---|
395 | String oldValue = null;
|
---|
396 |
|
---|
397 | synchronized (this) {
|
---|
398 | oldValue = properties.get(key);
|
---|
399 | if(value != null && value.length() == 0) {
|
---|
400 | value = null;
|
---|
401 | }
|
---|
402 | // value is the same as before - no need to save anything
|
---|
403 | boolean equalValue = oldValue != null && oldValue.equals(value);
|
---|
404 | // The setting was previously unset and we are supposed to put a
|
---|
405 | // value that equals the default value. This is not necessary because
|
---|
406 | // the default value is the same throughout josm. In addition we like
|
---|
407 | // to have the possibility to change the default value from version
|
---|
408 | // to version, which would not work if we wrote it to the preference file.
|
---|
409 | boolean unsetIsDefault = oldValue == null && (value == null || value.equals(defaults.get(key)));
|
---|
410 |
|
---|
411 | if (!(equalValue || unsetIsDefault)) {
|
---|
412 | if (value == null) {
|
---|
413 | properties.remove(key);
|
---|
414 | } else {
|
---|
415 | properties.put(key, value);
|
---|
416 | }
|
---|
417 | try {
|
---|
418 | save();
|
---|
419 | } catch(IOException e){
|
---|
420 | System.out.println(tr("Warning: failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()));
|
---|
421 | }
|
---|
422 | changed = true;
|
---|
423 | }
|
---|
424 | }
|
---|
425 | if (changed) {
|
---|
426 | // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
|
---|
427 | firePreferenceChanged(key, new StringSetting(oldValue), new StringSetting(value));
|
---|
428 | }
|
---|
429 | return changed;
|
---|
430 | }
|
---|
431 |
|
---|
432 | public boolean put(final String key, final boolean value) {
|
---|
433 | return put(key, Boolean.toString(value));
|
---|
434 | }
|
---|
435 |
|
---|
436 | public boolean putInteger(final String key, final Integer value) {
|
---|
437 | return put(key, Integer.toString(value));
|
---|
438 | }
|
---|
439 |
|
---|
440 | public boolean putDouble(final String key, final Double value) {
|
---|
441 | return put(key, Double.toString(value));
|
---|
442 | }
|
---|
443 |
|
---|
444 | public boolean putLong(final String key, final Long value) {
|
---|
445 | return put(key, Long.toString(value));
|
---|
446 | }
|
---|
447 |
|
---|
448 | /**
|
---|
449 | * Called after every put. In case of a problem, do nothing but output the error
|
---|
450 | * in log.
|
---|
451 | */
|
---|
452 | public void save() throws IOException {
|
---|
453 | /* currently unused, but may help to fix configuration issues in future */
|
---|
454 | putInteger("josm.version", Version.getInstance().getVersion());
|
---|
455 |
|
---|
456 | updateSystemProperties();
|
---|
457 | if(Main.applet)
|
---|
458 | return;
|
---|
459 |
|
---|
460 | File prefFile = getPreferenceFile();
|
---|
461 | File backupFile = new File(prefFile + "_backup");
|
---|
462 |
|
---|
463 | // Backup old preferences if there are old preferences
|
---|
464 | if(prefFile.exists()) {
|
---|
465 | copyFile(prefFile, backupFile);
|
---|
466 | }
|
---|
467 |
|
---|
468 | final PrintWriter out = new PrintWriter(new OutputStreamWriter(
|
---|
469 | new FileOutputStream(prefFile + "_tmp"), "utf-8"), false);
|
---|
470 | out.print(toXML(false));
|
---|
471 | out.close();
|
---|
472 |
|
---|
473 | File tmpFile = new File(prefFile + "_tmp");
|
---|
474 | copyFile(tmpFile, prefFile);
|
---|
475 | tmpFile.delete();
|
---|
476 |
|
---|
477 | setCorrectPermissions(prefFile);
|
---|
478 | setCorrectPermissions(backupFile);
|
---|
479 | }
|
---|
480 |
|
---|
481 |
|
---|
482 | private void setCorrectPermissions(File file) {
|
---|
483 | file.setReadable(false, false);
|
---|
484 | file.setWritable(false, false);
|
---|
485 | file.setExecutable(false, false);
|
---|
486 | file.setReadable(true, true);
|
---|
487 | file.setWritable(true, true);
|
---|
488 | }
|
---|
489 |
|
---|
490 | /**
|
---|
491 | * Simple file copy function that will overwrite the target file
|
---|
492 | * Taken from http://www.rgagnon.com/javadetails/java-0064.html (CC-NC-BY-SA)
|
---|
493 | * @param in
|
---|
494 | * @param out
|
---|
495 | * @throws IOException
|
---|
496 | */
|
---|
497 | public static void copyFile(File in, File out) throws IOException {
|
---|
498 | FileChannel inChannel = new FileInputStream(in).getChannel();
|
---|
499 | FileChannel outChannel = new FileOutputStream(out).getChannel();
|
---|
500 | try {
|
---|
501 | inChannel.transferTo(0, inChannel.size(),
|
---|
502 | outChannel);
|
---|
503 | }
|
---|
504 | catch (IOException e) {
|
---|
505 | throw e;
|
---|
506 | }
|
---|
507 | finally {
|
---|
508 | if (inChannel != null) {
|
---|
509 | inChannel.close();
|
---|
510 | }
|
---|
511 | if (outChannel != null) {
|
---|
512 | outChannel.close();
|
---|
513 | }
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | public void loadOld() throws Exception {
|
---|
518 | load(true);
|
---|
519 | }
|
---|
520 |
|
---|
521 | public void load() throws Exception {
|
---|
522 | load(false);
|
---|
523 | }
|
---|
524 |
|
---|
525 | private void load(boolean old) throws Exception {
|
---|
526 | properties.clear();
|
---|
527 | if (!Main.applet) {
|
---|
528 | File pref = old ? getOldPreferenceFile() : getPreferenceFile();
|
---|
529 | BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), "utf-8"));
|
---|
530 | /* FIXME: TODO: remove old style config file end of 2012 */
|
---|
531 | try {
|
---|
532 | if (old) {
|
---|
533 | in.mark(1);
|
---|
534 | int v = in.read();
|
---|
535 | in.reset();
|
---|
536 | if(v == '<') {
|
---|
537 | validateXML(in);
|
---|
538 | Utils.close(in);
|
---|
539 | in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), "utf-8"));
|
---|
540 | fromXML(in);
|
---|
541 | } else {
|
---|
542 | int lineNumber = 0;
|
---|
543 | ArrayList<Integer> errLines = new ArrayList<Integer>();
|
---|
544 | for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) {
|
---|
545 | final int i = line.indexOf('=');
|
---|
546 | if (i == -1 || i == 0) {
|
---|
547 | errLines.add(lineNumber);
|
---|
548 | continue;
|
---|
549 | }
|
---|
550 | String key = line.substring(0,i);
|
---|
551 | String value = line.substring(i+1);
|
---|
552 | if (!value.isEmpty()) {
|
---|
553 | properties.put(key, value);
|
---|
554 | }
|
---|
555 | }
|
---|
556 | if (!errLines.isEmpty())
|
---|
557 | throw new IOException(tr("Malformed config file at lines {0}", errLines));
|
---|
558 | }
|
---|
559 | } else {
|
---|
560 | validateXML(in);
|
---|
561 | Utils.close(in);
|
---|
562 | in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), "utf-8"));
|
---|
563 | fromXML(in);
|
---|
564 | }
|
---|
565 | } finally {
|
---|
566 | in.close();
|
---|
567 | }
|
---|
568 | }
|
---|
569 | updateSystemProperties();
|
---|
570 | /* FIXME: TODO: remove special version check end of 2012 */
|
---|
571 | if(!properties.containsKey("expert")) {
|
---|
572 | try {
|
---|
573 | String v = get("josm.version");
|
---|
574 | if(v.isEmpty() || Integer.parseInt(v) <= 4511)
|
---|
575 | properties.put("expert", "true");
|
---|
576 | } catch(Exception e) {
|
---|
577 | properties.put("expert", "true");
|
---|
578 | }
|
---|
579 | }
|
---|
580 | }
|
---|
581 |
|
---|
582 | public void init(boolean reset){
|
---|
583 | if(Main.applet)
|
---|
584 | return;
|
---|
585 | // get the preferences.
|
---|
586 | File prefDir = getPreferencesDirFile();
|
---|
587 | if (prefDir.exists()) {
|
---|
588 | if(!prefDir.isDirectory()) {
|
---|
589 | System.err.println(tr("Warning: Failed to initialize preferences. Preference directory ''{0}'' is not a directory.", prefDir.getAbsoluteFile()));
|
---|
590 | JOptionPane.showMessageDialog(
|
---|
591 | Main.parent,
|
---|
592 | tr("<html>Failed to initialize preferences.<br>Preference directory ''{0}'' is not a directory.</html>", prefDir.getAbsoluteFile()),
|
---|
593 | tr("Error"),
|
---|
594 | JOptionPane.ERROR_MESSAGE
|
---|
595 | );
|
---|
596 | return;
|
---|
597 | }
|
---|
598 | } else {
|
---|
599 | if (! prefDir.mkdirs()) {
|
---|
600 | System.err.println(tr("Warning: Failed to initialize preferences. Failed to create missing preference directory: {0}", prefDir.getAbsoluteFile()));
|
---|
601 | JOptionPane.showMessageDialog(
|
---|
602 | Main.parent,
|
---|
603 | tr("<html>Failed to initialize preferences.<br>Failed to create missing preference directory: {0}</html>",prefDir.getAbsoluteFile()),
|
---|
604 | tr("Error"),
|
---|
605 | JOptionPane.ERROR_MESSAGE
|
---|
606 | );
|
---|
607 | return;
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | File preferenceFile = getPreferenceFile();
|
---|
612 | try {
|
---|
613 | if (!preferenceFile.exists()) {
|
---|
614 | File oldPreferenceFile = getOldPreferenceFile();
|
---|
615 | if (!oldPreferenceFile.exists()) {
|
---|
616 | System.out.println(tr("Warning: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
|
---|
617 | resetToDefault();
|
---|
618 | save();
|
---|
619 | } else {
|
---|
620 | try {
|
---|
621 | loadOld();
|
---|
622 | } catch (Exception e) {
|
---|
623 | e.printStackTrace();
|
---|
624 | File backupFile = new File(prefDir,"preferences.bak");
|
---|
625 | JOptionPane.showMessageDialog(
|
---|
626 | Main.parent,
|
---|
627 | tr("<html>Preferences file had errors.<br> Making backup of old one to <br>{0}<br> and creating a new default preference file.</html>", backupFile.getAbsoluteFile()),
|
---|
628 | tr("Error"),
|
---|
629 | JOptionPane.ERROR_MESSAGE
|
---|
630 | );
|
---|
631 | Main.platform.rename(oldPreferenceFile, backupFile);
|
---|
632 | try {
|
---|
633 | resetToDefault();
|
---|
634 | save();
|
---|
635 | } catch(IOException e1) {
|
---|
636 | e1.printStackTrace();
|
---|
637 | System.err.println(tr("Warning: Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile()));
|
---|
638 | }
|
---|
639 | }
|
---|
640 | return;
|
---|
641 | }
|
---|
642 | } else if (reset) {
|
---|
643 | System.out.println(tr("Warning: Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile()));
|
---|
644 | resetToDefault();
|
---|
645 | save();
|
---|
646 | }
|
---|
647 | } catch(IOException e) {
|
---|
648 | e.printStackTrace();
|
---|
649 | JOptionPane.showMessageDialog(
|
---|
650 | Main.parent,
|
---|
651 | tr("<html>Failed to initialize preferences.<br>Failed to reset preference file to default: {0}</html>",getPreferenceFile().getAbsoluteFile()),
|
---|
652 | tr("Error"),
|
---|
653 | JOptionPane.ERROR_MESSAGE
|
---|
654 | );
|
---|
655 | return;
|
---|
656 | }
|
---|
657 | try {
|
---|
658 | load();
|
---|
659 | } catch (Exception e) {
|
---|
660 | e.printStackTrace();
|
---|
661 | File backupFile = new File(prefDir,"preferences.xml.bak");
|
---|
662 | JOptionPane.showMessageDialog(
|
---|
663 | Main.parent,
|
---|
664 | tr("<html>Preferences file had errors.<br> Making backup of old one to <br>{0}<br> and creating a new default preference file.</html>", backupFile.getAbsoluteFile()),
|
---|
665 | tr("Error"),
|
---|
666 | JOptionPane.ERROR_MESSAGE
|
---|
667 | );
|
---|
668 | Main.platform.rename(preferenceFile, backupFile);
|
---|
669 | try {
|
---|
670 | resetToDefault();
|
---|
671 | save();
|
---|
672 | } catch(IOException e1) {
|
---|
673 | e1.printStackTrace();
|
---|
674 | System.err.println(tr("Warning: Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile()));
|
---|
675 | }
|
---|
676 | }
|
---|
677 | }
|
---|
678 |
|
---|
679 | public final void resetToDefault(){
|
---|
680 | properties.clear();
|
---|
681 | }
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * Convenience method for accessing colour preferences.
|
---|
685 | *
|
---|
686 | * @param colName name of the colour
|
---|
687 | * @param def default value
|
---|
688 | * @return a Color object for the configured colour, or the default value if none configured.
|
---|
689 | */
|
---|
690 | synchronized public Color getColor(String colName, Color def) {
|
---|
691 | return getColor(colName, null, def);
|
---|
692 | }
|
---|
693 |
|
---|
694 | synchronized public Color getUIColor(String colName) {
|
---|
695 | return UIManager.getColor(colName);
|
---|
696 | }
|
---|
697 |
|
---|
698 | /* only for preferences */
|
---|
699 | synchronized public String getColorName(String o) {
|
---|
700 | try
|
---|
701 | {
|
---|
702 | Matcher m = Pattern.compile("mappaint\\.(.+?)\\.(.+)").matcher(o);
|
---|
703 | m.matches();
|
---|
704 | return tr("Paint style {0}: {1}", tr(m.group(1)), tr(m.group(2)));
|
---|
705 | }
|
---|
706 | catch (Exception e) {}
|
---|
707 | try
|
---|
708 | {
|
---|
709 | Matcher m = Pattern.compile("layer (.+)").matcher(o);
|
---|
710 | m.matches();
|
---|
711 | return tr("Layer: {0}", tr(m.group(1)));
|
---|
712 | }
|
---|
713 | catch (Exception e) {}
|
---|
714 | return tr(colornames.containsKey(o) ? colornames.get(o) : o);
|
---|
715 | }
|
---|
716 |
|
---|
717 | public Color getColor(ColorKey key) {
|
---|
718 | return getColor(key.getColorName(), key.getSpecialName(), key.getDefault());
|
---|
719 | }
|
---|
720 |
|
---|
721 | /**
|
---|
722 | * Convenience method for accessing colour preferences.
|
---|
723 | *
|
---|
724 | * @param colName name of the colour
|
---|
725 | * @param specName name of the special colour settings
|
---|
726 | * @param def default value
|
---|
727 | * @return a Color object for the configured colour, or the default value if none configured.
|
---|
728 | */
|
---|
729 | synchronized public Color getColor(String colName, String specName, Color def) {
|
---|
730 | String colKey = colName.toLowerCase().replaceAll("[^a-z0-9]+",".");
|
---|
731 | if(!colKey.equals(colName)) {
|
---|
732 | colornames.put(colKey, colName);
|
---|
733 | }
|
---|
734 | putDefault("color."+colKey, ColorHelper.color2html(def));
|
---|
735 | String colStr = specName != null ? get("color."+specName) : "";
|
---|
736 | if(colStr.equals("")) {
|
---|
737 | colStr = get("color."+colKey);
|
---|
738 | }
|
---|
739 | return colStr.equals("") ? def : ColorHelper.html2color(colStr);
|
---|
740 | }
|
---|
741 |
|
---|
742 | synchronized public Color getDefaultColor(String colName) {
|
---|
743 | String colStr = defaults.get("color."+colName);
|
---|
744 | return colStr == null || "".equals(colStr) ? null : ColorHelper.html2color(colStr);
|
---|
745 | }
|
---|
746 |
|
---|
747 | synchronized public boolean putColor(String colName, Color val) {
|
---|
748 | return put("color."+colName, val != null ? ColorHelper.color2html(val) : null);
|
---|
749 | }
|
---|
750 |
|
---|
751 | synchronized public int getInteger(String key, int def) {
|
---|
752 | putDefault(key, Integer.toString(def));
|
---|
753 | String v = get(key);
|
---|
754 | if(v.isEmpty())
|
---|
755 | return def;
|
---|
756 |
|
---|
757 | try {
|
---|
758 | return Integer.parseInt(v);
|
---|
759 | } catch(NumberFormatException e) {
|
---|
760 | // fall out
|
---|
761 | }
|
---|
762 | return def;
|
---|
763 | }
|
---|
764 |
|
---|
765 | synchronized public int getInteger(String key, String specName, int def) {
|
---|
766 | putDefault(key, Integer.toString(def));
|
---|
767 | String v = get(key+"."+specName);
|
---|
768 | if(v.isEmpty())
|
---|
769 | v = get(key);
|
---|
770 | if(v.isEmpty())
|
---|
771 | return def;
|
---|
772 |
|
---|
773 | try {
|
---|
774 | return Integer.parseInt(v);
|
---|
775 | } catch(NumberFormatException e) {
|
---|
776 | // fall out
|
---|
777 | }
|
---|
778 | return def;
|
---|
779 | }
|
---|
780 |
|
---|
781 | synchronized public long getLong(String key, long def) {
|
---|
782 | putDefault(key, Long.toString(def));
|
---|
783 | String v = get(key);
|
---|
784 | if(null == v)
|
---|
785 | return def;
|
---|
786 |
|
---|
787 | try {
|
---|
788 | return Long.parseLong(v);
|
---|
789 | } catch(NumberFormatException e) {
|
---|
790 | // fall out
|
---|
791 | }
|
---|
792 | return def;
|
---|
793 | }
|
---|
794 |
|
---|
795 | synchronized public double getDouble(String key, double def) {
|
---|
796 | putDefault(key, Double.toString(def));
|
---|
797 | String v = get(key);
|
---|
798 | if(null == v)
|
---|
799 | return def;
|
---|
800 |
|
---|
801 | try {
|
---|
802 | return Double.parseDouble(v);
|
---|
803 | } catch(NumberFormatException e) {
|
---|
804 | // fall out
|
---|
805 | }
|
---|
806 | return def;
|
---|
807 | }
|
---|
808 |
|
---|
809 | synchronized public double getDouble(String key, String def) {
|
---|
810 | putDefault(key, def);
|
---|
811 | String v = get(key);
|
---|
812 | if(v != null && v.length() != 0) {
|
---|
813 | try { return Double.parseDouble(v); } catch(NumberFormatException e) {}
|
---|
814 | }
|
---|
815 | try { return Double.parseDouble(def); } catch(NumberFormatException e) {}
|
---|
816 | return 0.0;
|
---|
817 | }
|
---|
818 |
|
---|
819 | /**
|
---|
820 | * Get a list of values for a certain key
|
---|
821 | * @param key the identifier for the setting
|
---|
822 | * @param def the default value.
|
---|
823 | * @return the corresponding value if the property has been set before,
|
---|
824 | * def otherwise
|
---|
825 | */
|
---|
826 | public Collection<String> getCollection(String key, Collection<String> def) {
|
---|
827 | if (def != null) {
|
---|
828 | putCollectionDefault(key, new ArrayList<String>(def));
|
---|
829 | }
|
---|
830 | Collection<String> prop = getCollectionInternal(key);
|
---|
831 | if (prop != null)
|
---|
832 | return prop;
|
---|
833 | else
|
---|
834 | return def;
|
---|
835 | }
|
---|
836 |
|
---|
837 | /**
|
---|
838 | * Get a list of values for a certain key
|
---|
839 | * @param key the identifier for the setting
|
---|
840 | * @return the corresponding value if the property has been set before,
|
---|
841 | * an empty Collection otherwise.
|
---|
842 | */
|
---|
843 | public Collection<String> getCollection(String key) {
|
---|
844 | putCollectionDefault(key, null);
|
---|
845 | Collection<String> prop = getCollectionInternal(key);
|
---|
846 | if (prop != null)
|
---|
847 | return prop;
|
---|
848 | else
|
---|
849 | return Collections.emptyList();
|
---|
850 | }
|
---|
851 |
|
---|
852 | synchronized private List<String> getCollectionInternal(String key) {
|
---|
853 | List<String> prop = collectionProperties.get(key);
|
---|
854 | if (prop != null)
|
---|
855 | return prop;
|
---|
856 | else {
|
---|
857 | String s = properties.get(key);
|
---|
858 | if(s != null) {
|
---|
859 | prop = Arrays.asList(s.split("\u001e", -1));
|
---|
860 | collectionProperties.put(key, Collections.unmodifiableList(prop));
|
---|
861 | properties.remove(key);
|
---|
862 | defaults.remove(key);
|
---|
863 | return prop;
|
---|
864 | }
|
---|
865 | }
|
---|
866 | return null;
|
---|
867 | }
|
---|
868 |
|
---|
869 | synchronized public void removeFromCollection(String key, String value) {
|
---|
870 | List<String> a = new ArrayList<String>(getCollection(key, Collections.<String>emptyList()));
|
---|
871 | a.remove(value);
|
---|
872 | putCollection(key, a);
|
---|
873 | }
|
---|
874 |
|
---|
875 | public boolean putCollection(String key, Collection<String> value) {
|
---|
876 | List<String> oldValue = null;
|
---|
877 | List<String> valueCopy = null;
|
---|
878 |
|
---|
879 | synchronized (this) {
|
---|
880 | if (value == null) {
|
---|
881 | oldValue = collectionProperties.remove(key);
|
---|
882 | boolean changed = oldValue != null;
|
---|
883 | changed |= properties.remove(key) != null;
|
---|
884 | if (!changed) return false;
|
---|
885 | } else {
|
---|
886 | oldValue = getCollectionInternal(key);
|
---|
887 | if (equalCollection(value, oldValue)) return false;
|
---|
888 | Collection<String> defValue = collectionDefaults.get(key);
|
---|
889 | if (oldValue == null && equalCollection(value, defValue)) return false;
|
---|
890 |
|
---|
891 | valueCopy = new ArrayList<String>(value);
|
---|
892 | if (valueCopy.contains(null)) throw new RuntimeException("Error: Null as list element in preference setting (key '"+key+"')");
|
---|
893 | collectionProperties.put(key, Collections.unmodifiableList(valueCopy));
|
---|
894 | try {
|
---|
895 | save();
|
---|
896 | } catch(IOException e){
|
---|
897 | System.out.println(tr("Warning: failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()));
|
---|
898 | }
|
---|
899 | }
|
---|
900 | }
|
---|
901 | // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
|
---|
902 | firePreferenceChanged(key, new ListSetting(oldValue), new ListSetting(valueCopy));
|
---|
903 | return true;
|
---|
904 | }
|
---|
905 |
|
---|
906 | private static boolean equalCollection(Collection<String> a, Collection<String> b) {
|
---|
907 | if (a == null) return b == null;
|
---|
908 | if (b == null) return false;
|
---|
909 | if (a.size() != b.size()) return false;
|
---|
910 | Iterator<String> itA = a.iterator();
|
---|
911 | Iterator<String> itB = b.iterator();
|
---|
912 | while (itA.hasNext()) {
|
---|
913 | String aStr = itA.next();
|
---|
914 | String bStr = itB.next();
|
---|
915 | if (!Utils.equal(aStr,bStr)) return false;
|
---|
916 | }
|
---|
917 | return true;
|
---|
918 | }
|
---|
919 |
|
---|
920 | /**
|
---|
921 | * Saves at most {@code maxsize} items of collection {@code val}.
|
---|
922 | */
|
---|
923 | public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
|
---|
924 | Collection<String> newCollection = new ArrayList<String>(Math.min(maxsize, val.size()));
|
---|
925 | for (String i : val) {
|
---|
926 | if (newCollection.size() >= maxsize) {
|
---|
927 | break;
|
---|
928 | }
|
---|
929 | newCollection.add(i);
|
---|
930 | }
|
---|
931 | return putCollection(key, newCollection);
|
---|
932 | }
|
---|
933 |
|
---|
934 | synchronized private void putCollectionDefault(String key, List<String> val) {
|
---|
935 | collectionDefaults.put(key, val);
|
---|
936 | }
|
---|
937 |
|
---|
938 | /**
|
---|
939 | * Used to read a 2-dimensional array of strings from the preference file.
|
---|
940 | * If not a single entry could be found, def is returned.
|
---|
941 | */
|
---|
942 | synchronized public Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def) {
|
---|
943 | if (def != null) {
|
---|
944 | List<List<String>> defCopy = new ArrayList<List<String>>(def.size());
|
---|
945 | for (Collection<String> lst : def) {
|
---|
946 | defCopy.add(Collections.unmodifiableList(new ArrayList<String>(lst)));
|
---|
947 | }
|
---|
948 | putArrayDefault(key, Collections.unmodifiableList(defCopy));
|
---|
949 | }
|
---|
950 | List<List<String>> prop = getArrayInternal(key);
|
---|
951 | if (prop != null) {
|
---|
952 | @SuppressWarnings("unchecked")
|
---|
953 | Collection<Collection<String>> prop_cast = (Collection) prop;
|
---|
954 | return prop_cast;
|
---|
955 | } else
|
---|
956 | return def;
|
---|
957 | }
|
---|
958 |
|
---|
959 | synchronized private List<List<String>> getArrayInternal(String key) {
|
---|
960 | List<List<String>> prop = arrayProperties.get(key);
|
---|
961 | if (prop != null)
|
---|
962 | return prop;
|
---|
963 | else {
|
---|
964 | String keyDot = key + ".";
|
---|
965 | int num = 0;
|
---|
966 | List<List<String>> col = new ArrayList<List<String>>();
|
---|
967 | while (true) {
|
---|
968 | List<String> c = getCollectionInternal(keyDot+num);
|
---|
969 | if (c == null) {
|
---|
970 | break;
|
---|
971 | }
|
---|
972 | col.add(c);
|
---|
973 | collectionProperties.remove(keyDot+num);
|
---|
974 | collectionDefaults.remove(keyDot+num);
|
---|
975 | num++;
|
---|
976 | }
|
---|
977 | if (num > 0) {
|
---|
978 | arrayProperties.put(key, Collections.unmodifiableList(col));
|
---|
979 | return col;
|
---|
980 | }
|
---|
981 | }
|
---|
982 | return null;
|
---|
983 | }
|
---|
984 |
|
---|
985 | public boolean putArray(String key, Collection<Collection<String>> value) {
|
---|
986 | boolean changed = false;
|
---|
987 |
|
---|
988 | List<List<String>> oldValue = null;
|
---|
989 | List<List<String>> valueCopy = null;
|
---|
990 |
|
---|
991 | synchronized (this) {
|
---|
992 | if (value == null) {
|
---|
993 | oldValue = getArrayInternal(key);
|
---|
994 | if (arrayProperties.remove(key) != null) return false;
|
---|
995 | } else {
|
---|
996 | oldValue = getArrayInternal(key);
|
---|
997 | if (equalArray(value, oldValue)) return false;
|
---|
998 |
|
---|
999 | List<List<String>> defValue = arrayDefaults.get(key);
|
---|
1000 | if (oldValue == null && equalArray(value, defValue)) return false;
|
---|
1001 |
|
---|
1002 | valueCopy = new ArrayList<List<String>>(value.size());
|
---|
1003 | if (valueCopy.contains(null)) throw new RuntimeException("Error: Null as list element in preference setting (key '"+key+"')");
|
---|
1004 | for (Collection<String> lst : value) {
|
---|
1005 | List<String> lstCopy = new ArrayList<String>(lst);
|
---|
1006 | if (lstCopy.contains(null)) throw new RuntimeException("Error: Null as inner list element in preference setting (key '"+key+"')");
|
---|
1007 | valueCopy.add(Collections.unmodifiableList(lstCopy));
|
---|
1008 | }
|
---|
1009 | arrayProperties.put(key, Collections.unmodifiableList(valueCopy));
|
---|
1010 | try {
|
---|
1011 | save();
|
---|
1012 | } catch(IOException e){
|
---|
1013 | System.out.println(tr("Warning: failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()));
|
---|
1014 | }
|
---|
1015 | }
|
---|
1016 | }
|
---|
1017 | // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
|
---|
1018 | firePreferenceChanged(key, new ListListSetting(oldValue), new ListListSetting(valueCopy));
|
---|
1019 | return true;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | private static boolean equalArray(Collection<Collection<String>> a, Collection<List<String>> b) {
|
---|
1023 | if (a == null) return b == null;
|
---|
1024 | if (b == null) return false;
|
---|
1025 | if (a.size() != b.size()) return false;
|
---|
1026 | Iterator<Collection<String>> itA = a.iterator();
|
---|
1027 | Iterator<List<String>> itB = b.iterator();
|
---|
1028 | while (itA.hasNext()) {
|
---|
1029 | if (!equalCollection(itA.next(), itB.next())) return false;
|
---|
1030 | }
|
---|
1031 | return true;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | synchronized private void putArrayDefault(String key, List<List<String>> val) {
|
---|
1035 | arrayDefaults.put(key, val);
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | public Collection<Map<String, String>> getListOfStructs(String key, Collection<Map<String, String>> def) {
|
---|
1039 | if (def != null) {
|
---|
1040 | List<Map<String, String>> defCopy = new ArrayList<Map<String, String>>(def.size());
|
---|
1041 | for (Map<String, String> map : def) {
|
---|
1042 | defCopy.add(Collections.unmodifiableMap(new LinkedHashMap<String,String>(map)));
|
---|
1043 | }
|
---|
1044 | putListOfStructsDefault(key, Collections.unmodifiableList(defCopy));
|
---|
1045 | }
|
---|
1046 | Collection<Map<String, String>> prop = getListOfStructsInternal(key);
|
---|
1047 | if (prop != null)
|
---|
1048 | return prop;
|
---|
1049 | else
|
---|
1050 | return def;
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | private synchronized List<Map<String, String>> getListOfStructsInternal(String key) {
|
---|
1054 | List<Map<String, String>> prop = listOfStructsProperties.get(key);
|
---|
1055 | if (prop != null)
|
---|
1056 | return prop;
|
---|
1057 | else {
|
---|
1058 | List<List<String>> array = getArrayInternal(key);
|
---|
1059 | if (array == null) return null;
|
---|
1060 | prop = new ArrayList<Map<String, String>>(array.size());
|
---|
1061 | for (Collection<String> mapStr : array) {
|
---|
1062 | Map<String, String> map = new LinkedHashMap<String, String>();
|
---|
1063 | for (String key_value : mapStr) {
|
---|
1064 | final int i = key_value.indexOf(':');
|
---|
1065 | if (i == -1 || i == 0) {
|
---|
1066 | continue;
|
---|
1067 | }
|
---|
1068 | String k = key_value.substring(0,i);
|
---|
1069 | String v = key_value.substring(i+1);
|
---|
1070 | map.put(k, v);
|
---|
1071 | }
|
---|
1072 | prop.add(Collections.unmodifiableMap(map));
|
---|
1073 | }
|
---|
1074 | arrayProperties.remove(key);
|
---|
1075 | arrayDefaults.remove(key);
|
---|
1076 | listOfStructsProperties.put(key, Collections.unmodifiableList(prop));
|
---|
1077 | return prop;
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | public boolean putListOfStructs(String key, Collection<Map<String, String>> value) {
|
---|
1082 | boolean changed = false;
|
---|
1083 |
|
---|
1084 | List<Map<String, String>> oldValue;
|
---|
1085 | List<Map<String, String>> valueCopy = null;
|
---|
1086 |
|
---|
1087 | synchronized (this) {
|
---|
1088 | if (value == null) {
|
---|
1089 | oldValue = getListOfStructsInternal(key);
|
---|
1090 | if (listOfStructsProperties.remove(key) != null) return false;
|
---|
1091 | } else {
|
---|
1092 | oldValue = getListOfStructsInternal(key);
|
---|
1093 | if (equalListOfStructs(oldValue, value)) return false;
|
---|
1094 |
|
---|
1095 | List<Map<String, String>> defValue = listOfStructsDefaults.get(key);
|
---|
1096 | if (oldValue == null && equalListOfStructs(value, defValue)) return false;
|
---|
1097 |
|
---|
1098 | valueCopy = new ArrayList<Map<String, String>>(value.size());
|
---|
1099 | if (valueCopy.contains(null)) throw new RuntimeException("Error: Null as list element in preference setting (key '"+key+"')");
|
---|
1100 | for (Map<String, String> map : value) {
|
---|
1101 | Map<String, String> mapCopy = new LinkedHashMap<String,String>(map);
|
---|
1102 | if (mapCopy.keySet().contains(null)) throw new RuntimeException("Error: Null as map key in preference setting (key '"+key+"')");
|
---|
1103 | if (mapCopy.values().contains(null)) throw new RuntimeException("Error: Null as map value in preference setting (key '"+key+"')");
|
---|
1104 | valueCopy.add(Collections.unmodifiableMap(mapCopy));
|
---|
1105 | }
|
---|
1106 | listOfStructsProperties.put(key, Collections.unmodifiableList(valueCopy));
|
---|
1107 | try {
|
---|
1108 | save();
|
---|
1109 | } catch(IOException e){
|
---|
1110 | System.out.println(tr("Warning: failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()));
|
---|
1111 | }
|
---|
1112 | }
|
---|
1113 | }
|
---|
1114 | // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
|
---|
1115 | firePreferenceChanged(key, new MapListSetting(oldValue), new MapListSetting(valueCopy));
|
---|
1116 | return true;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | private static boolean equalListOfStructs(Collection<Map<String, String>> a, Collection<Map<String, String>> b) {
|
---|
1120 | if (a == null) return b == null;
|
---|
1121 | if (b == null) return false;
|
---|
1122 | if (a.size() != b.size()) return false;
|
---|
1123 | Iterator<Map<String, String>> itA = a.iterator();
|
---|
1124 | Iterator<Map<String, String>> itB = b.iterator();
|
---|
1125 | while (itA.hasNext()) {
|
---|
1126 | if (!equalMap(itA.next(), itB.next())) return false;
|
---|
1127 | }
|
---|
1128 | return true;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | private static boolean equalMap(Map<String, String> a, Map<String, String> b) {
|
---|
1132 | if (a == null) return b == null;
|
---|
1133 | if (b == null) return false;
|
---|
1134 | if (a.size() != b.size()) return false;
|
---|
1135 | for (Entry<String, String> e : a.entrySet()) {
|
---|
1136 | if (!Utils.equal(e.getValue(), b.get(e.getKey()))) return false;
|
---|
1137 | }
|
---|
1138 | return true;
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | synchronized private void putListOfStructsDefault(String key, List<Map<String, String>> val) {
|
---|
1142 | listOfStructsDefaults.put(key, val);
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | @Retention(RetentionPolicy.RUNTIME) public @interface pref { }
|
---|
1146 | @Retention(RetentionPolicy.RUNTIME) public @interface writeExplicitly { }
|
---|
1147 |
|
---|
1148 | /**
|
---|
1149 | * Get a list of hashes which are represented by a struct-like class.
|
---|
1150 | * It reads lines of the form
|
---|
1151 | * > key.0=prop:val \u001e prop:val \u001e ... \u001e prop:val
|
---|
1152 | * > ...
|
---|
1153 | * > key.N=prop:val \u001e prop:val \u001e ... \u001e prop:val
|
---|
1154 | * Possible properties are given by fields of the class klass that have
|
---|
1155 | * the @pref annotation.
|
---|
1156 | * Default constructor is used to initialize the struct objects, properties
|
---|
1157 | * then override some of these default values.
|
---|
1158 | * @param key main preference key
|
---|
1159 | * @param klass The struct class
|
---|
1160 | * @return a list of objects of type T or an empty list if nothing was found
|
---|
1161 | */
|
---|
1162 | public <T> List<T> getListOfStructs(String key, Class<T> klass) {
|
---|
1163 | List<T> r = getListOfStructs(key, null, klass);
|
---|
1164 | if (r == null)
|
---|
1165 | return Collections.emptyList();
|
---|
1166 | else
|
---|
1167 | return r;
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | /**
|
---|
1171 | * same as above, but returns def if nothing was found
|
---|
1172 | */
|
---|
1173 | public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) {
|
---|
1174 | Collection<Map<String,String>> prop =
|
---|
1175 | getListOfStructs(key, def == null ? null : serializeListOfStructs(def, klass));
|
---|
1176 | if (prop == null)
|
---|
1177 | return def == null ? null : new ArrayList<T>(def);
|
---|
1178 | List<T> lst = new ArrayList<T>();
|
---|
1179 | for (Map<String,String> entries : prop) {
|
---|
1180 | T struct = deserializeStruct(entries, klass);
|
---|
1181 | lst.add(struct);
|
---|
1182 | }
|
---|
1183 | return lst;
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | /**
|
---|
1187 | * Save a list of hashes represented by a struct-like class.
|
---|
1188 | * Considers only fields that have the @pref annotation.
|
---|
1189 | * In addition it does not write fields with null values. (Thus they are cleared)
|
---|
1190 | * Default values are given by the field values after default constructor has
|
---|
1191 | * been called.
|
---|
1192 | * Fields equal to the default value are not written unless the field has
|
---|
1193 | * the @writeExplicitly annotation.
|
---|
1194 | * @param key main preference key
|
---|
1195 | * @param val the list that is supposed to be saved
|
---|
1196 | * @param klass The struct class
|
---|
1197 | * @return true if something has changed
|
---|
1198 | */
|
---|
1199 | public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) {
|
---|
1200 | return putListOfStructs(key, serializeListOfStructs(val, klass));
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | private <T> Collection<Map<String,String>> serializeListOfStructs(Collection<T> l, Class<T> klass) {
|
---|
1204 | if (l == null)
|
---|
1205 | return null;
|
---|
1206 | Collection<Map<String,String>> vals = new ArrayList<Map<String,String>>();
|
---|
1207 | for (T struct : l) {
|
---|
1208 | if (struct == null) {
|
---|
1209 | continue;
|
---|
1210 | }
|
---|
1211 | vals.add(serializeStruct(struct, klass));
|
---|
1212 | }
|
---|
1213 | return vals;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | private <T> Map<String,String> serializeStruct(T struct, Class<T> klass) {
|
---|
1217 | T structPrototype;
|
---|
1218 | try {
|
---|
1219 | structPrototype = klass.newInstance();
|
---|
1220 | } catch (InstantiationException ex) {
|
---|
1221 | throw new RuntimeException(ex);
|
---|
1222 | } catch (IllegalAccessException ex) {
|
---|
1223 | throw new RuntimeException(ex);
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | Map<String,String> hash = new LinkedHashMap<String,String>();
|
---|
1227 | for (Field f : klass.getDeclaredFields()) {
|
---|
1228 | if (f.getAnnotation(pref.class) == null) {
|
---|
1229 | continue;
|
---|
1230 | }
|
---|
1231 | f.setAccessible(true);
|
---|
1232 | try {
|
---|
1233 | Object fieldValue = f.get(struct);
|
---|
1234 | Object defaultFieldValue = f.get(structPrototype);
|
---|
1235 | if (fieldValue != null) {
|
---|
1236 | if (f.getAnnotation(writeExplicitly.class) != null || !Utils.equal(fieldValue, defaultFieldValue)) {
|
---|
1237 | hash.put(f.getName().replace("_", "-"), fieldValue.toString());
|
---|
1238 | }
|
---|
1239 | }
|
---|
1240 | } catch (IllegalArgumentException ex) {
|
---|
1241 | throw new RuntimeException();
|
---|
1242 | } catch (IllegalAccessException ex) {
|
---|
1243 | throw new RuntimeException();
|
---|
1244 | }
|
---|
1245 | }
|
---|
1246 | return hash;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | private <T> T deserializeStruct(Map<String,String> hash, Class<T> klass) {
|
---|
1250 | T struct = null;
|
---|
1251 | try {
|
---|
1252 | struct = klass.newInstance();
|
---|
1253 | } catch (InstantiationException ex) {
|
---|
1254 | throw new RuntimeException();
|
---|
1255 | } catch (IllegalAccessException ex) {
|
---|
1256 | throw new RuntimeException();
|
---|
1257 | }
|
---|
1258 | for (Entry<String,String> key_value : hash.entrySet()) {
|
---|
1259 | Object value = null;
|
---|
1260 | Field f;
|
---|
1261 | try {
|
---|
1262 | f = klass.getDeclaredField(key_value.getKey().replace("-", "_"));
|
---|
1263 | } catch (NoSuchFieldException ex) {
|
---|
1264 | continue;
|
---|
1265 | } catch (SecurityException ex) {
|
---|
1266 | throw new RuntimeException();
|
---|
1267 | }
|
---|
1268 | if (f.getAnnotation(pref.class) == null) {
|
---|
1269 | continue;
|
---|
1270 | }
|
---|
1271 | f.setAccessible(true);
|
---|
1272 | if (f.getType() == Boolean.class || f.getType() == boolean.class) {
|
---|
1273 | value = Boolean.parseBoolean(key_value.getValue());
|
---|
1274 | } else if (f.getType() == Integer.class || f.getType() == int.class) {
|
---|
1275 | try {
|
---|
1276 | value = Integer.parseInt(key_value.getValue());
|
---|
1277 | } catch (NumberFormatException nfe) {
|
---|
1278 | continue;
|
---|
1279 | }
|
---|
1280 | } else if (f.getType() == Double.class || f.getType() == double.class) {
|
---|
1281 | try {
|
---|
1282 | value = Double.parseDouble(key_value.getValue());
|
---|
1283 | } catch (NumberFormatException nfe) {
|
---|
1284 | continue;
|
---|
1285 | }
|
---|
1286 | } else if (f.getType() == String.class) {
|
---|
1287 | value = key_value.getValue();
|
---|
1288 | } else
|
---|
1289 | throw new RuntimeException("unsupported preference primitive type");
|
---|
1290 |
|
---|
1291 | try {
|
---|
1292 | f.set(struct, value);
|
---|
1293 | } catch (IllegalArgumentException ex) {
|
---|
1294 | throw new AssertionError();
|
---|
1295 | } catch (IllegalAccessException ex) {
|
---|
1296 | throw new RuntimeException();
|
---|
1297 | }
|
---|
1298 | }
|
---|
1299 | return struct;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | /**
|
---|
1303 | * Updates system properties with the current values in the preferences.
|
---|
1304 | *
|
---|
1305 | */
|
---|
1306 | public void updateSystemProperties() {
|
---|
1307 | Properties sysProp = System.getProperties();
|
---|
1308 | sysProp.put("http.agent", Version.getInstance().getAgentString());
|
---|
1309 | System.setProperties(sysProp);
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | /**
|
---|
1313 | * The default plugin site
|
---|
1314 | */
|
---|
1315 | private final static String[] DEFAULT_PLUGIN_SITE = {
|
---|
1316 | "http://josm.openstreetmap.de/plugin%<?plugins=>"};
|
---|
1317 |
|
---|
1318 | /**
|
---|
1319 | * Replies the collection of plugin site URLs from where plugin lists can be downloaded
|
---|
1320 | *
|
---|
1321 | * @return
|
---|
1322 | */
|
---|
1323 | public Collection<String> getPluginSites() {
|
---|
1324 | return getCollection("pluginmanager.sites", Arrays.asList(DEFAULT_PLUGIN_SITE));
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | /**
|
---|
1328 | * Sets the collection of plugin site URLs.
|
---|
1329 | *
|
---|
1330 | * @param sites the site URLs
|
---|
1331 | */
|
---|
1332 | public void setPluginSites(Collection<String> sites) {
|
---|
1333 | putCollection("pluginmanager.sites", sites);
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 | protected XMLStreamReader parser;
|
---|
1337 |
|
---|
1338 | public void validateXML(Reader in) throws Exception {
|
---|
1339 | SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
|
---|
1340 | Schema schema = factory.newSchema(new StreamSource(new MirroredInputStream("resource://data/preferences.xsd")));
|
---|
1341 | XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(in);
|
---|
1342 | Validator validator = schema.newValidator();
|
---|
1343 | validator.validate(new StAXSource(parser));
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | public void fromXML(Reader in) throws XMLStreamException {
|
---|
1347 | XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(in);
|
---|
1348 | this.parser = parser;
|
---|
1349 | parse();
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | public void parse() throws XMLStreamException {
|
---|
1353 | int event = parser.getEventType();
|
---|
1354 | while (true) {
|
---|
1355 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
1356 | parseRoot();
|
---|
1357 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
1358 | return;
|
---|
1359 | }
|
---|
1360 | if (parser.hasNext()) {
|
---|
1361 | event = parser.next();
|
---|
1362 | } else {
|
---|
1363 | break;
|
---|
1364 | }
|
---|
1365 | }
|
---|
1366 | parser.close();
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | public void parseRoot() throws XMLStreamException {
|
---|
1370 | while (true) {
|
---|
1371 | int event = parser.next();
|
---|
1372 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
1373 | if (parser.getLocalName().equals("tag")) {
|
---|
1374 | properties.put(parser.getAttributeValue(null, "key"), parser.getAttributeValue(null, "value"));
|
---|
1375 | jumpToEnd();
|
---|
1376 | } else if (parser.getLocalName().equals("list") || parser.getLocalName().equals("collection")) {
|
---|
1377 | parseToplevelList();
|
---|
1378 | } else {
|
---|
1379 | throwException("Unexpected element: "+parser.getLocalName());
|
---|
1380 | }
|
---|
1381 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
1382 | return;
|
---|
1383 | }
|
---|
1384 | }
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | private void jumpToEnd() throws XMLStreamException {
|
---|
1388 | while (true) {
|
---|
1389 | int event = parser.next();
|
---|
1390 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
1391 | jumpToEnd();
|
---|
1392 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
1393 | return;
|
---|
1394 | }
|
---|
1395 | }
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | protected void parseToplevelList() throws XMLStreamException {
|
---|
1399 | String key = parser.getAttributeValue(null, "key");
|
---|
1400 | List<String> entries = null;
|
---|
1401 | List<List<String>> lists = null;
|
---|
1402 | List<Map<String, String>> maps = null;
|
---|
1403 | while (true) {
|
---|
1404 | int event = parser.next();
|
---|
1405 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
1406 | if (parser.getLocalName().equals("entry")) {
|
---|
1407 | if (entries == null) {
|
---|
1408 | entries = new ArrayList<String>();
|
---|
1409 | }
|
---|
1410 | entries.add(parser.getAttributeValue(null, "value"));
|
---|
1411 | jumpToEnd();
|
---|
1412 | } else if (parser.getLocalName().equals("list")) {
|
---|
1413 | if (lists == null) {
|
---|
1414 | lists = new ArrayList<List<String>>();
|
---|
1415 | }
|
---|
1416 | lists.add(parseInnerList());
|
---|
1417 | } else if (parser.getLocalName().equals("map")) {
|
---|
1418 | if (maps == null) {
|
---|
1419 | maps = new ArrayList<Map<String, String>>();
|
---|
1420 | }
|
---|
1421 | maps.add(parseMap());
|
---|
1422 | } else {
|
---|
1423 | throwException("Unexpected element: "+parser.getLocalName());
|
---|
1424 | }
|
---|
1425 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
1426 | break;
|
---|
1427 | }
|
---|
1428 | }
|
---|
1429 | if (entries != null) {
|
---|
1430 | collectionProperties.put(key, Collections.unmodifiableList(entries));
|
---|
1431 | }
|
---|
1432 | if (lists != null) {
|
---|
1433 | arrayProperties.put(key, Collections.unmodifiableList(lists));
|
---|
1434 | }
|
---|
1435 | if (maps != null) {
|
---|
1436 | listOfStructsProperties.put(key, Collections.unmodifiableList(maps));
|
---|
1437 | }
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | protected List<String> parseInnerList() throws XMLStreamException {
|
---|
1441 | List<String> entries = new ArrayList<String>();
|
---|
1442 | while (true) {
|
---|
1443 | int event = parser.next();
|
---|
1444 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
1445 | if (parser.getLocalName().equals("entry")) {
|
---|
1446 | entries.add(parser.getAttributeValue(null, "value"));
|
---|
1447 | jumpToEnd();
|
---|
1448 | } else {
|
---|
1449 | throwException("Unexpected element: "+parser.getLocalName());
|
---|
1450 | }
|
---|
1451 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
1452 | break;
|
---|
1453 | }
|
---|
1454 | }
|
---|
1455 | return Collections.unmodifiableList(entries);
|
---|
1456 | }
|
---|
1457 |
|
---|
1458 | protected Map<String, String> parseMap() throws XMLStreamException {
|
---|
1459 | Map<String, String> map = new LinkedHashMap<String, String>();
|
---|
1460 | while (true) {
|
---|
1461 | int event = parser.next();
|
---|
1462 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
1463 | if (parser.getLocalName().equals("tag")) {
|
---|
1464 | map.put(parser.getAttributeValue(null, "key"), parser.getAttributeValue(null, "value"));
|
---|
1465 | jumpToEnd();
|
---|
1466 | } else {
|
---|
1467 | throwException("Unexpected element: "+parser.getLocalName());
|
---|
1468 | }
|
---|
1469 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
1470 | break;
|
---|
1471 | }
|
---|
1472 | }
|
---|
1473 | return Collections.unmodifiableMap(map);
|
---|
1474 | }
|
---|
1475 |
|
---|
1476 | protected void throwException(String msg) {
|
---|
1477 | throw new RuntimeException(msg + tr(" (at line {0}, column {1})", parser.getLocation().getLineNumber(), parser.getLocation().getColumnNumber()));
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | private class SettingToXml implements SettingVisitor {
|
---|
1481 | private StringBuilder b;
|
---|
1482 | private boolean noPassword;
|
---|
1483 | private String key;
|
---|
1484 |
|
---|
1485 | public SettingToXml(StringBuilder b, boolean noPassword) {
|
---|
1486 | this.b = b;
|
---|
1487 | this.noPassword = noPassword;
|
---|
1488 | }
|
---|
1489 |
|
---|
1490 | public void setKey(String key) {
|
---|
1491 | this.key = key;
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | public void visit(StringSetting setting) {
|
---|
1495 | if (noPassword && key.equals("osm-server.password"))
|
---|
1496 | return; // do not store plain password.
|
---|
1497 | String r = setting.getValue();
|
---|
1498 | String s = defaults.get(key);
|
---|
1499 | /* don't save default values */
|
---|
1500 | if(s == null || !s.equals(r)) {
|
---|
1501 | if(r.contains("\u001e"))
|
---|
1502 | {
|
---|
1503 | b.append(" <list key='");
|
---|
1504 | b.append(XmlWriter.encode(key));
|
---|
1505 | b.append("'>\n");
|
---|
1506 | for (String val : r.split("\u001e", -1))
|
---|
1507 | {
|
---|
1508 | b.append(" <entry value='");
|
---|
1509 | b.append(XmlWriter.encode(val));
|
---|
1510 | b.append("'/>\n");
|
---|
1511 | }
|
---|
1512 | b.append(" </list>\n");
|
---|
1513 | }
|
---|
1514 | else
|
---|
1515 | {
|
---|
1516 | b.append(" <tag key='");
|
---|
1517 | b.append(XmlWriter.encode(key));
|
---|
1518 | b.append("' value='");
|
---|
1519 | b.append(XmlWriter.encode(setting.getValue()));
|
---|
1520 | b.append("'/>\n");
|
---|
1521 | }
|
---|
1522 | }
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | public void visit(ListSetting setting) {
|
---|
1526 | b.append(" <list key='").append(XmlWriter.encode(key)).append("'>\n");
|
---|
1527 | for (String s : setting.getValue()) {
|
---|
1528 | b.append(" <entry value='").append(XmlWriter.encode(s)).append("'/>\n");
|
---|
1529 | }
|
---|
1530 | b.append(" </list>\n");
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | public void visit(ListListSetting setting) {
|
---|
1534 | b.append(" <list key='").append(XmlWriter.encode(key)).append("'>\n");
|
---|
1535 | for (List<String> list : setting.getValue()) {
|
---|
1536 | b.append(" <list>\n");
|
---|
1537 | for (String s : list) {
|
---|
1538 | b.append(" <entry value='").append(XmlWriter.encode(s)).append("'/>\n");
|
---|
1539 | }
|
---|
1540 | b.append(" </list>\n");
|
---|
1541 | }
|
---|
1542 | b.append(" </list>\n");
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | public void visit(MapListSetting setting) {
|
---|
1546 | b.append(" <list key='").append(XmlWriter.encode(key)).append("'>\n");
|
---|
1547 | for (Map<String, String> struct : setting.getValue()) {
|
---|
1548 | b.append(" <map>\n");
|
---|
1549 | for (Entry<String, String> e : struct.entrySet()) {
|
---|
1550 | b.append(" <tag key='").append(XmlWriter.encode(e.getKey())).append("' value='").append(XmlWriter.encode(e.getValue())).append("'/>\n");
|
---|
1551 | }
|
---|
1552 | b.append(" </map>\n");
|
---|
1553 | }
|
---|
1554 | b.append(" </list>\n");
|
---|
1555 | }
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | public String toXML(boolean nopass) {
|
---|
1559 | StringBuilder b = new StringBuilder(
|
---|
1560 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
---|
1561 | "<preferences xmlns=\"http://josm.openstreetmap.de/preferences-1.0\" version=\""+
|
---|
1562 | Version.getInstance().getVersion() + "\">\n");
|
---|
1563 | SettingToXml toXml = new SettingToXml(b, nopass);
|
---|
1564 | Map<String, Setting> settings = new TreeMap<String, Setting>();
|
---|
1565 |
|
---|
1566 | for (Entry<String, String> e : properties.entrySet()) {
|
---|
1567 | settings.put(e.getKey(), new StringSetting(e.getValue()));
|
---|
1568 | }
|
---|
1569 | for (Entry<String, List<String>> e : collectionProperties.entrySet()) {
|
---|
1570 | settings.put(e.getKey(), new ListSetting(e.getValue()));
|
---|
1571 | }
|
---|
1572 | for (Entry<String, List<List<String>>> e : arrayProperties.entrySet()) {
|
---|
1573 | settings.put(e.getKey(), new ListListSetting(e.getValue()));
|
---|
1574 | }
|
---|
1575 | for (Entry<String, List<Map<String, String>>> e : listOfStructsProperties.entrySet()) {
|
---|
1576 | settings.put(e.getKey(), new MapListSetting(e.getValue()));
|
---|
1577 | }
|
---|
1578 | for (Entry<String, Setting> e : settings.entrySet()) {
|
---|
1579 | toXml.setKey(e.getKey());
|
---|
1580 | e.getValue().visit(toXml);
|
---|
1581 | }
|
---|
1582 | b.append("</preferences>\n");
|
---|
1583 | return b.toString();
|
---|
1584 | }
|
---|
1585 | }
|
---|