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.LinkedList;
|
---|
25 | import java.util.List;
|
---|
26 | import java.util.Map;
|
---|
27 | import java.util.Map.Entry;
|
---|
28 | import java.util.Properties;
|
---|
29 | import java.util.SortedMap;
|
---|
30 | import java.util.TreeMap;
|
---|
31 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
32 | import java.util.regex.Matcher;
|
---|
33 | import java.util.regex.Pattern;
|
---|
34 |
|
---|
35 | import javax.swing.JOptionPane;
|
---|
36 | import javax.swing.UIManager;
|
---|
37 |
|
---|
38 | import org.openstreetmap.josm.Main;
|
---|
39 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
40 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
41 | import org.openstreetmap.josm.tools.Utils;
|
---|
42 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
43 | import org.xml.sax.SAXException;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * This class holds all preferences for JOSM.
|
---|
47 | *
|
---|
48 | * Other classes can register their beloved properties here. All properties will be
|
---|
49 | * saved upon set-access.
|
---|
50 | *
|
---|
51 | * Each property is a simple key=value pair of Strings.
|
---|
52 | * In addition, each key has a unique default value that is set when the value is first
|
---|
53 | * accessed using one of the get...() methods. You can use the same preference
|
---|
54 | * key in different parts of the code, but the default value must be the same
|
---|
55 | * everywhere. null is a legitimate default value.
|
---|
56 | *
|
---|
57 | * At the moment, there is no such thing as an empty value.
|
---|
58 | * If you put "" or null as value, the property is removed.
|
---|
59 | *
|
---|
60 | * @author imi
|
---|
61 | */
|
---|
62 | public class Preferences {
|
---|
63 | /**
|
---|
64 | * Internal storage for the preference directory.
|
---|
65 | * Do not access this variable directly!
|
---|
66 | * @see #getPreferencesDirFile()
|
---|
67 | */
|
---|
68 | private File preferencesDirFile = null;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Map the property name to the property object. Does not contain null or "" values.
|
---|
72 | */
|
---|
73 | protected final SortedMap<String, String> properties = new TreeMap<String, String>();
|
---|
74 | protected final SortedMap<String, String> defaults = new TreeMap<String, String>();
|
---|
75 | protected final SortedMap<String, String> colornames = new TreeMap<String, String>();
|
---|
76 |
|
---|
77 | public interface PreferenceChangeEvent{
|
---|
78 | String getKey();
|
---|
79 | String getOldValue();
|
---|
80 | String getNewValue();
|
---|
81 | }
|
---|
82 |
|
---|
83 | public interface PreferenceChangedListener {
|
---|
84 | void preferenceChanged(PreferenceChangeEvent e);
|
---|
85 | }
|
---|
86 |
|
---|
87 | private static class DefaultPreferenceChangeEvent implements PreferenceChangeEvent {
|
---|
88 | private final String key;
|
---|
89 | private final String oldValue;
|
---|
90 | private final String newValue;
|
---|
91 |
|
---|
92 | public DefaultPreferenceChangeEvent(String key, String oldValue, String newValue) {
|
---|
93 | this.key = key;
|
---|
94 | this.oldValue = oldValue;
|
---|
95 | this.newValue = newValue;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public String getKey() {
|
---|
99 | return key;
|
---|
100 | }
|
---|
101 | public String getOldValue() {
|
---|
102 | return oldValue;
|
---|
103 | }
|
---|
104 | public String getNewValue() {
|
---|
105 | return newValue;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public interface ColorKey {
|
---|
110 | String getColorName();
|
---|
111 | String getSpecialName();
|
---|
112 | Color getDefault();
|
---|
113 | }
|
---|
114 |
|
---|
115 | private final CopyOnWriteArrayList<PreferenceChangedListener> listeners = new CopyOnWriteArrayList<PreferenceChangedListener>();
|
---|
116 |
|
---|
117 | public void addPreferenceChangeListener(PreferenceChangedListener listener) {
|
---|
118 | if (listener != null) {
|
---|
119 | listeners.addIfAbsent(listener);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void removePreferenceChangeListener(PreferenceChangedListener listener) {
|
---|
124 | listeners.remove(listener);
|
---|
125 | }
|
---|
126 |
|
---|
127 | protected void firePreferenceChanged(String key, String oldValue, String newValue) {
|
---|
128 | PreferenceChangeEvent evt = new DefaultPreferenceChangeEvent(key, oldValue, newValue);
|
---|
129 | for (PreferenceChangedListener l : listeners) {
|
---|
130 | l.preferenceChanged(evt);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Return the location of the user defined preferences file
|
---|
136 | */
|
---|
137 | public String getPreferencesDir() {
|
---|
138 | final String path = getPreferencesDirFile().getPath();
|
---|
139 | if (path.endsWith(File.separator))
|
---|
140 | return path;
|
---|
141 | return path + File.separator;
|
---|
142 | }
|
---|
143 |
|
---|
144 | public File getPreferencesDirFile() {
|
---|
145 | if (preferencesDirFile != null)
|
---|
146 | return preferencesDirFile;
|
---|
147 | String path;
|
---|
148 | path = System.getProperty("josm.home");
|
---|
149 | if (path != null) {
|
---|
150 | preferencesDirFile = new File(path);
|
---|
151 | } else {
|
---|
152 | path = System.getenv("APPDATA");
|
---|
153 | if (path != null) {
|
---|
154 | preferencesDirFile = new File(path, "JOSM");
|
---|
155 | } else {
|
---|
156 | preferencesDirFile = new File(System.getProperty("user.home"), ".josm");
|
---|
157 | }
|
---|
158 | }
|
---|
159 | return preferencesDirFile;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public File getPreferenceFile() {
|
---|
163 | return new File(getPreferencesDirFile(), "preferences");
|
---|
164 | }
|
---|
165 |
|
---|
166 | public File getPluginsDirectory() {
|
---|
167 | return new File(getPreferencesDirFile(), "plugins");
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * @return A list of all existing directories where resources could be stored.
|
---|
172 | */
|
---|
173 | public Collection<String> getAllPossiblePreferenceDirs() {
|
---|
174 | LinkedList<String> locations = new LinkedList<String>();
|
---|
175 | locations.add(Main.pref.getPreferencesDir());
|
---|
176 | String s;
|
---|
177 | if ((s = System.getenv("JOSM_RESOURCES")) != null) {
|
---|
178 | if (!s.endsWith(File.separator)) {
|
---|
179 | s = s + File.separator;
|
---|
180 | }
|
---|
181 | locations.add(s);
|
---|
182 | }
|
---|
183 | if ((s = System.getProperty("josm.resources")) != null) {
|
---|
184 | if (!s.endsWith(File.separator)) {
|
---|
185 | s = s + File.separator;
|
---|
186 | }
|
---|
187 | locations.add(s);
|
---|
188 | }
|
---|
189 | String appdata = System.getenv("APPDATA");
|
---|
190 | if (System.getenv("ALLUSERSPROFILE") != null && appdata != null
|
---|
191 | && appdata.lastIndexOf(File.separator) != -1) {
|
---|
192 | appdata = appdata.substring(appdata.lastIndexOf(File.separator));
|
---|
193 | locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
|
---|
194 | appdata), "JOSM").getPath());
|
---|
195 | }
|
---|
196 | locations.add("/usr/local/share/josm/");
|
---|
197 | locations.add("/usr/local/lib/josm/");
|
---|
198 | locations.add("/usr/share/josm/");
|
---|
199 | locations.add("/usr/lib/josm/");
|
---|
200 | return locations;
|
---|
201 | }
|
---|
202 |
|
---|
203 | synchronized public boolean hasKey(final String key) {
|
---|
204 | return properties.containsKey(key);
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Get settings value for a certain key.
|
---|
209 | * @param key the identifier for the setting
|
---|
210 | * @return "" if there is nothing set for the preference key,
|
---|
211 | * the corresponding value otherwise. The result is not null.
|
---|
212 | */
|
---|
213 | synchronized public String get(final String key) {
|
---|
214 | putDefault(key, null);
|
---|
215 | if (!properties.containsKey(key))
|
---|
216 | return "";
|
---|
217 | return properties.get(key);
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Get settings value for a certain key and provide default a value.
|
---|
222 | * @param key the identifier for the setting
|
---|
223 | * @param def the default value. For each call of get() with a given key, the
|
---|
224 | * default value must be the same.
|
---|
225 | * @return the corresponding value if the property has been set before,
|
---|
226 | * def otherwise
|
---|
227 | */
|
---|
228 | synchronized public String get(final String key, final String def) {
|
---|
229 | putDefault(key, def);
|
---|
230 | final String prop = properties.get(key);
|
---|
231 | if (prop == null || prop.equals(""))
|
---|
232 | return def;
|
---|
233 | return prop;
|
---|
234 | }
|
---|
235 |
|
---|
236 | synchronized public Map<String, String> getAllPrefix(final String prefix) {
|
---|
237 | final Map<String,String> all = new TreeMap<String,String>();
|
---|
238 | for (final Entry<String,String> e : properties.entrySet()) {
|
---|
239 | if (e.getKey().startsWith(prefix)) {
|
---|
240 | all.put(e.getKey(), e.getValue());
|
---|
241 | }
|
---|
242 | }
|
---|
243 | return all;
|
---|
244 | }
|
---|
245 |
|
---|
246 | synchronized private Map<String, String> getAllPrefixDefault(final String prefix) {
|
---|
247 | final Map<String,String> all = new TreeMap<String,String>();
|
---|
248 | for (final Entry<String,String> e : defaults.entrySet()) {
|
---|
249 | if (e.getKey().startsWith(prefix)) {
|
---|
250 | all.put(e.getKey(), e.getValue());
|
---|
251 | }
|
---|
252 | }
|
---|
253 | return all;
|
---|
254 | }
|
---|
255 |
|
---|
256 | synchronized public TreeMap<String, String> getAllColors() {
|
---|
257 | final TreeMap<String,String> all = new TreeMap<String,String>();
|
---|
258 | for (final Entry<String,String> e : defaults.entrySet()) {
|
---|
259 | if (e.getKey().startsWith("color.") && e.getValue() != null) {
|
---|
260 | all.put(e.getKey().substring(6), e.getValue());
|
---|
261 | }
|
---|
262 | }
|
---|
263 | for (final Entry<String,String> e : properties.entrySet()) {
|
---|
264 | if (e.getKey().startsWith("color.")) {
|
---|
265 | all.put(e.getKey().substring(6), e.getValue());
|
---|
266 | }
|
---|
267 | }
|
---|
268 | return all;
|
---|
269 | }
|
---|
270 |
|
---|
271 | synchronized public Map<String, String> getDefaults() {
|
---|
272 | return defaults;
|
---|
273 | }
|
---|
274 |
|
---|
275 | synchronized public void putDefault(final String key, final String def) {
|
---|
276 | if(!defaults.containsKey(key) || defaults.get(key) == null) {
|
---|
277 | defaults.put(key, def);
|
---|
278 | } else if(def != null && !defaults.get(key).equals(def)) {
|
---|
279 | System.out.println("Defaults for " + key + " differ: " + def + " != " + defaults.get(key));
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | synchronized public boolean getBoolean(final String key) {
|
---|
284 | putDefault(key, null);
|
---|
285 | return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : false;
|
---|
286 | }
|
---|
287 |
|
---|
288 | synchronized public boolean getBoolean(final String key, final boolean def) {
|
---|
289 | putDefault(key, Boolean.toString(def));
|
---|
290 | return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
|
---|
291 | }
|
---|
292 |
|
---|
293 | synchronized public boolean getBoolean(final String key, final String specName, final boolean def) {
|
---|
294 | putDefault(key, Boolean.toString(def));
|
---|
295 | String skey = key+"."+specName;
|
---|
296 | if(properties.containsKey(skey))
|
---|
297 | return Boolean.parseBoolean(properties.get(skey));
|
---|
298 | return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Set a value for a certain setting. The changed setting is saved
|
---|
303 | * to the preference file immediately. Due to caching mechanisms on modern
|
---|
304 | * operating systems and hardware, this shouldn't be a performance problem.
|
---|
305 | * @param key the unique identifier for the setting
|
---|
306 | * @param value the value of the setting. Can be null or "" wich both removes
|
---|
307 | * the key-value entry.
|
---|
308 | * @return if true, something has changed (i.e. value is different than before)
|
---|
309 | */
|
---|
310 | public boolean put(final String key, String value) {
|
---|
311 | boolean changed = false;
|
---|
312 | String oldValue = null;
|
---|
313 |
|
---|
314 | synchronized (this) {
|
---|
315 | oldValue = properties.get(key);
|
---|
316 | if(value != null && value.length() == 0) {
|
---|
317 | value = null;
|
---|
318 | }
|
---|
319 | // value is the same as before - no need to save anything
|
---|
320 | boolean equalValue = oldValue != null && oldValue.equals(value);
|
---|
321 | // The setting was previously unset and we are supposed to put a
|
---|
322 | // value that equals the default value. This is not necessary because
|
---|
323 | // the default value is the same throughout josm. In addition we like
|
---|
324 | // to have the possibility to change the default value from version
|
---|
325 | // to version, which would not work if we wrote it to the preference file.
|
---|
326 | boolean unsetIsDefault = oldValue == null && (value == null || value.equals(defaults.get(key)));
|
---|
327 |
|
---|
328 | if (!(equalValue || unsetIsDefault)) {
|
---|
329 | if (value == null) {
|
---|
330 | properties.remove(key);
|
---|
331 | } else {
|
---|
332 | properties.put(key, value);
|
---|
333 | }
|
---|
334 | try {
|
---|
335 | save();
|
---|
336 | } catch(IOException e){
|
---|
337 | System.out.println(tr("Warning: failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()));
|
---|
338 | }
|
---|
339 | changed = true;
|
---|
340 | }
|
---|
341 | }
|
---|
342 | if (changed) {
|
---|
343 | // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
|
---|
344 | firePreferenceChanged(key, oldValue, value);
|
---|
345 | }
|
---|
346 | return changed;
|
---|
347 | }
|
---|
348 |
|
---|
349 | public boolean put(final String key, final boolean value) {
|
---|
350 | return put(key, Boolean.toString(value));
|
---|
351 | }
|
---|
352 |
|
---|
353 | public boolean putInteger(final String key, final Integer value) {
|
---|
354 | return put(key, Integer.toString(value));
|
---|
355 | }
|
---|
356 |
|
---|
357 | public boolean putDouble(final String key, final Double value) {
|
---|
358 | return put(key, Double.toString(value));
|
---|
359 | }
|
---|
360 |
|
---|
361 | public boolean putLong(final String key, final Long value) {
|
---|
362 | return put(key, Long.toString(value));
|
---|
363 | }
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Called after every put. In case of a problem, do nothing but output the error
|
---|
367 | * in log.
|
---|
368 | */
|
---|
369 | public void save() throws IOException {
|
---|
370 | /* currently unused, but may help to fix configuration issues in future */
|
---|
371 | putInteger("josm.version", Version.getInstance().getVersion());
|
---|
372 |
|
---|
373 | updateSystemProperties();
|
---|
374 | if(Main.applet)
|
---|
375 | return;
|
---|
376 |
|
---|
377 | File prefFile = getPreferenceFile();
|
---|
378 | File backupFile = new File(prefFile + "_backup");
|
---|
379 |
|
---|
380 | // Backup old preferences if there are old preferences
|
---|
381 | if(prefFile.exists()) {
|
---|
382 | copyFile(prefFile, backupFile);
|
---|
383 | }
|
---|
384 |
|
---|
385 | final PrintWriter out = new PrintWriter(new OutputStreamWriter(
|
---|
386 | new FileOutputStream(prefFile + "_tmp"), "utf-8"), false);
|
---|
387 | for (final Entry<String, String> e : properties.entrySet()) {
|
---|
388 | String s = defaults.get(e.getKey());
|
---|
389 | /* don't save default values */
|
---|
390 | if(s == null || !s.equals(e.getValue())) {
|
---|
391 | out.println(e.getKey() + "=" + e.getValue());
|
---|
392 | }
|
---|
393 | }
|
---|
394 | out.close();
|
---|
395 |
|
---|
396 | File tmpFile = new File(prefFile + "_tmp");
|
---|
397 | copyFile(tmpFile, prefFile);
|
---|
398 | tmpFile.delete();
|
---|
399 |
|
---|
400 | setCorrectPermissions(prefFile);
|
---|
401 | setCorrectPermissions(backupFile);
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 | private void setCorrectPermissions(File file) {
|
---|
406 | file.setReadable(false, false);
|
---|
407 | file.setWritable(false, false);
|
---|
408 | file.setExecutable(false, false);
|
---|
409 | file.setReadable(true, true);
|
---|
410 | file.setWritable(true, true);
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * Simple file copy function that will overwrite the target file
|
---|
415 | * Taken from http://www.rgagnon.com/javadetails/java-0064.html (CC-NC-BY-SA)
|
---|
416 | * @param in
|
---|
417 | * @param out
|
---|
418 | * @throws IOException
|
---|
419 | */
|
---|
420 | public static void copyFile(File in, File out) throws IOException {
|
---|
421 | FileChannel inChannel = new FileInputStream(in).getChannel();
|
---|
422 | FileChannel outChannel = new FileOutputStream(out).getChannel();
|
---|
423 | try {
|
---|
424 | inChannel.transferTo(0, inChannel.size(),
|
---|
425 | outChannel);
|
---|
426 | }
|
---|
427 | catch (IOException e) {
|
---|
428 | throw e;
|
---|
429 | }
|
---|
430 | finally {
|
---|
431 | if (inChannel != null) {
|
---|
432 | inChannel.close();
|
---|
433 | }
|
---|
434 | if (outChannel != null) {
|
---|
435 | outChannel.close();
|
---|
436 | }
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | public void load() throws IOException {
|
---|
441 | properties.clear();
|
---|
442 | if(!Main.applet) {
|
---|
443 | final BufferedReader in = new BufferedReader(new InputStreamReader(
|
---|
444 | new FileInputStream(getPreferencesDir()+"preferences"), "utf-8"));
|
---|
445 | int lineNumber = 0;
|
---|
446 | ArrayList<Integer> errLines = new ArrayList<Integer>();
|
---|
447 | for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) {
|
---|
448 | final int i = line.indexOf('=');
|
---|
449 | if (i == -1 || i == 0) {
|
---|
450 | errLines.add(lineNumber);
|
---|
451 | continue;
|
---|
452 | }
|
---|
453 | String key = line.substring(0,i);
|
---|
454 | String value = line.substring(i+1);
|
---|
455 | if (!value.isEmpty()) {
|
---|
456 | properties.put(key, value);
|
---|
457 | }
|
---|
458 | }
|
---|
459 | if (!errLines.isEmpty())
|
---|
460 | throw new IOException(tr("Malformed config file at lines {0}", errLines));
|
---|
461 | }
|
---|
462 | updateSystemProperties();
|
---|
463 | }
|
---|
464 |
|
---|
465 | public void init(boolean reset){
|
---|
466 | if(Main.applet)
|
---|
467 | return;
|
---|
468 | // get the preferences.
|
---|
469 | File prefDir = getPreferencesDirFile();
|
---|
470 | if (prefDir.exists()) {
|
---|
471 | if(!prefDir.isDirectory()) {
|
---|
472 | System.err.println(tr("Warning: Failed to initialize preferences. Preference directory ''{0}'' is not a directory.", prefDir.getAbsoluteFile()));
|
---|
473 | JOptionPane.showMessageDialog(
|
---|
474 | Main.parent,
|
---|
475 | tr("<html>Failed to initialize preferences.<br>Preference directory ''{0}'' is not a directory.</html>", prefDir.getAbsoluteFile()),
|
---|
476 | tr("Error"),
|
---|
477 | JOptionPane.ERROR_MESSAGE
|
---|
478 | );
|
---|
479 | return;
|
---|
480 | }
|
---|
481 | } else {
|
---|
482 | if (! prefDir.mkdirs()) {
|
---|
483 | System.err.println(tr("Warning: Failed to initialize preferences. Failed to create missing preference directory: {0}", prefDir.getAbsoluteFile()));
|
---|
484 | JOptionPane.showMessageDialog(
|
---|
485 | Main.parent,
|
---|
486 | tr("<html>Failed to initialize preferences.<br>Failed to create missing preference directory: {0}</html>",prefDir.getAbsoluteFile()),
|
---|
487 | tr("Error"),
|
---|
488 | JOptionPane.ERROR_MESSAGE
|
---|
489 | );
|
---|
490 | return;
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 | File preferenceFile = getPreferenceFile();
|
---|
495 | try {
|
---|
496 | if (!preferenceFile.exists()) {
|
---|
497 | System.out.println(tr("Warning: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
|
---|
498 | resetToDefault();
|
---|
499 | save();
|
---|
500 | } else if (reset) {
|
---|
501 | System.out.println(tr("Warning: Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile()));
|
---|
502 | resetToDefault();
|
---|
503 | save();
|
---|
504 | }
|
---|
505 | } catch(IOException e) {
|
---|
506 | e.printStackTrace();
|
---|
507 | JOptionPane.showMessageDialog(
|
---|
508 | Main.parent,
|
---|
509 | tr("<html>Failed to initialize preferences.<br>Failed to reset preference file to default: {0}</html>",getPreferenceFile().getAbsoluteFile()),
|
---|
510 | tr("Error"),
|
---|
511 | JOptionPane.ERROR_MESSAGE
|
---|
512 | );
|
---|
513 | return;
|
---|
514 | }
|
---|
515 | try {
|
---|
516 | load();
|
---|
517 | } catch (IOException e) {
|
---|
518 | e.printStackTrace();
|
---|
519 | File backupFile = new File(prefDir,"preferences.bak");
|
---|
520 | JOptionPane.showMessageDialog(
|
---|
521 | Main.parent,
|
---|
522 | 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()),
|
---|
523 | tr("Error"),
|
---|
524 | JOptionPane.ERROR_MESSAGE
|
---|
525 | );
|
---|
526 | preferenceFile.renameTo(backupFile);
|
---|
527 | try {
|
---|
528 | resetToDefault();
|
---|
529 | save();
|
---|
530 | } catch(IOException e1) {
|
---|
531 | e1.printStackTrace();
|
---|
532 | System.err.println(tr("Warning: Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile()));
|
---|
533 | }
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | public final void resetToDefault(){
|
---|
538 | properties.clear();
|
---|
539 | }
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * Convenience method for accessing colour preferences.
|
---|
543 | *
|
---|
544 | * @param colName name of the colour
|
---|
545 | * @param def default value
|
---|
546 | * @return a Color object for the configured colour, or the default value if none configured.
|
---|
547 | */
|
---|
548 | synchronized public Color getColor(String colName, Color def) {
|
---|
549 | return getColor(colName, null, def);
|
---|
550 | }
|
---|
551 |
|
---|
552 | synchronized public Color getUIColor(String colName) {
|
---|
553 | return UIManager.getColor(colName);
|
---|
554 | }
|
---|
555 |
|
---|
556 | /* only for preferences */
|
---|
557 | synchronized public String getColorName(String o) {
|
---|
558 | try
|
---|
559 | {
|
---|
560 | Matcher m = Pattern.compile("mappaint\\.(.+?)\\.(.+)").matcher(o);
|
---|
561 | m.matches();
|
---|
562 | return tr("Paint style {0}: {1}", tr(m.group(1)), tr(m.group(2)));
|
---|
563 | }
|
---|
564 | catch (Exception e) {}
|
---|
565 | try
|
---|
566 | {
|
---|
567 | Matcher m = Pattern.compile("layer (.+)").matcher(o);
|
---|
568 | m.matches();
|
---|
569 | return tr("Layer: {0}", tr(m.group(1)));
|
---|
570 | }
|
---|
571 | catch (Exception e) {}
|
---|
572 | return tr(colornames.containsKey(o) ? colornames.get(o) : o);
|
---|
573 | }
|
---|
574 |
|
---|
575 | public Color getColor(ColorKey key) {
|
---|
576 | return getColor(key.getColorName(), key.getSpecialName(), key.getDefault());
|
---|
577 | }
|
---|
578 |
|
---|
579 | /**
|
---|
580 | * Convenience method for accessing colour preferences.
|
---|
581 | *
|
---|
582 | * @param colName name of the colour
|
---|
583 | * @param specName name of the special colour settings
|
---|
584 | * @param def default value
|
---|
585 | * @return a Color object for the configured colour, or the default value if none configured.
|
---|
586 | */
|
---|
587 | synchronized public Color getColor(String colName, String specName, Color def) {
|
---|
588 | String colKey = colName.toLowerCase().replaceAll("[^a-z0-9]+",".");
|
---|
589 | if(!colKey.equals(colName)) {
|
---|
590 | colornames.put(colKey, colName);
|
---|
591 | }
|
---|
592 | putDefault("color."+colKey, ColorHelper.color2html(def));
|
---|
593 | String colStr = specName != null ? get("color."+specName) : "";
|
---|
594 | if(colStr.equals("")) {
|
---|
595 | colStr = get("color."+colKey);
|
---|
596 | }
|
---|
597 | return colStr.equals("") ? def : ColorHelper.html2color(colStr);
|
---|
598 | }
|
---|
599 |
|
---|
600 | synchronized public Color getDefaultColor(String colName) {
|
---|
601 | String colStr = defaults.get("color."+colName);
|
---|
602 | return colStr == null || "".equals(colStr) ? null : ColorHelper.html2color(colStr);
|
---|
603 | }
|
---|
604 |
|
---|
605 | synchronized public boolean putColor(String colName, Color val) {
|
---|
606 | return put("color."+colName, val != null ? ColorHelper.color2html(val) : null);
|
---|
607 | }
|
---|
608 |
|
---|
609 | synchronized public int getInteger(String key, int def) {
|
---|
610 | putDefault(key, Integer.toString(def));
|
---|
611 | String v = get(key);
|
---|
612 | if(v.isEmpty())
|
---|
613 | return def;
|
---|
614 |
|
---|
615 | try {
|
---|
616 | return Integer.parseInt(v);
|
---|
617 | } catch(NumberFormatException e) {
|
---|
618 | // fall out
|
---|
619 | }
|
---|
620 | return def;
|
---|
621 | }
|
---|
622 |
|
---|
623 | synchronized public int getInteger(String key, String specName, int def) {
|
---|
624 | putDefault(key, Integer.toString(def));
|
---|
625 | String v = get(key+"."+specName);
|
---|
626 | if(v.isEmpty())
|
---|
627 | v = get(key);
|
---|
628 | if(v.isEmpty())
|
---|
629 | return def;
|
---|
630 |
|
---|
631 | try {
|
---|
632 | return Integer.parseInt(v);
|
---|
633 | } catch(NumberFormatException e) {
|
---|
634 | // fall out
|
---|
635 | }
|
---|
636 | return def;
|
---|
637 | }
|
---|
638 |
|
---|
639 | synchronized public long getLong(String key, long def) {
|
---|
640 | putDefault(key, Long.toString(def));
|
---|
641 | String v = get(key);
|
---|
642 | if(null == v)
|
---|
643 | return def;
|
---|
644 |
|
---|
645 | try {
|
---|
646 | return Long.parseLong(v);
|
---|
647 | } catch(NumberFormatException e) {
|
---|
648 | // fall out
|
---|
649 | }
|
---|
650 | return def;
|
---|
651 | }
|
---|
652 |
|
---|
653 | synchronized public double getDouble(String key, double def) {
|
---|
654 | putDefault(key, Double.toString(def));
|
---|
655 | String v = get(key);
|
---|
656 | if(null == v)
|
---|
657 | return def;
|
---|
658 |
|
---|
659 | try {
|
---|
660 | return Double.parseDouble(v);
|
---|
661 | } catch(NumberFormatException e) {
|
---|
662 | // fall out
|
---|
663 | }
|
---|
664 | return def;
|
---|
665 | }
|
---|
666 |
|
---|
667 | synchronized public double getDouble(String key, String def) {
|
---|
668 | putDefault(key, def);
|
---|
669 | String v = get(key);
|
---|
670 | if(v != null && v.length() != 0) {
|
---|
671 | try { return Double.parseDouble(v); } catch(NumberFormatException e) {}
|
---|
672 | }
|
---|
673 | try { return Double.parseDouble(def); } catch(NumberFormatException e) {}
|
---|
674 | return 0.0;
|
---|
675 | }
|
---|
676 |
|
---|
677 | synchronized public String getCollectionAsString(final String key) {
|
---|
678 | String s = get(key);
|
---|
679 | if(s != null && s.length() != 0) {
|
---|
680 | s = s.replaceAll("\u001e",",");
|
---|
681 | }
|
---|
682 | return s;
|
---|
683 | }
|
---|
684 |
|
---|
685 | public boolean isCollection(String key, boolean def) {
|
---|
686 | String s = get(key);
|
---|
687 | if (s != null && s.length() != 0)
|
---|
688 | return s.indexOf("\u001e") >= 0;
|
---|
689 | else
|
---|
690 | return def;
|
---|
691 | }
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * Get a list of values for a certain key
|
---|
695 | * @param key the identifier for the setting
|
---|
696 | * @param def the default value.
|
---|
697 | * @return the corresponding value if the property has been set before,
|
---|
698 | * def otherwise
|
---|
699 | */
|
---|
700 | synchronized public Collection<String> getCollection(String key, Collection<String> def) {
|
---|
701 | putCollectionDefault(key, def);
|
---|
702 | String s = get(key);
|
---|
703 | if(s != null && s.length() != 0)
|
---|
704 | return Arrays.asList(s.split("\u001e", -1));
|
---|
705 | return def;
|
---|
706 | }
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Get a list of values for a certain key
|
---|
710 | * @param key the identifier for the setting
|
---|
711 | * @return the corresponding value if the property has been set before,
|
---|
712 | * an empty Collection otherwise.
|
---|
713 | */
|
---|
714 | synchronized public Collection<String> getCollection(String key) {
|
---|
715 | putCollectionDefault(key, null);
|
---|
716 | String s = get(key);
|
---|
717 | if (s != null && s.length() != 0)
|
---|
718 | return Arrays.asList(s.split("\u001e", -1));
|
---|
719 | return Collections.emptyList();
|
---|
720 | }
|
---|
721 |
|
---|
722 | /* old style conversion, replace by above call after some transition time */
|
---|
723 | /* remove this function, when no more old-style preference collections in the code */
|
---|
724 | @Deprecated
|
---|
725 | synchronized public Collection<String> getCollectionOld(String key, String sep) {
|
---|
726 | putCollectionDefault(key, null);
|
---|
727 | String s = get(key);
|
---|
728 | if (s != null && s.length() != 0) {
|
---|
729 | if(!s.contains("\u001e") && s.contains(sep)) {
|
---|
730 | s = s.replace(sep, "\u001e");
|
---|
731 | put(key, s);
|
---|
732 | }
|
---|
733 | return Arrays.asList(s.split("\u001e", -1));
|
---|
734 | }
|
---|
735 | return Collections.emptyList();
|
---|
736 | }
|
---|
737 |
|
---|
738 | synchronized public void removeFromCollection(String key, String value) {
|
---|
739 | List<String> a = new ArrayList<String>(getCollection(key, Collections.<String>emptyList()));
|
---|
740 | a.remove(value);
|
---|
741 | putCollection(key, a);
|
---|
742 | }
|
---|
743 |
|
---|
744 | synchronized public boolean putCollection(String key, Collection<String> val) {
|
---|
745 | return put(key, Utils.join("\u001e", val));
|
---|
746 | }
|
---|
747 |
|
---|
748 | /**
|
---|
749 | * Saves at most {@code maxsize} items of collection {@code val}.
|
---|
750 | */
|
---|
751 | public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
|
---|
752 | Collection<String> newCollection = new ArrayList<String>(Math.min(maxsize, val.size()));
|
---|
753 | for (String i : val) {
|
---|
754 | if (newCollection.size() >= maxsize) {
|
---|
755 | break;
|
---|
756 | }
|
---|
757 | newCollection.add(i);
|
---|
758 | }
|
---|
759 | return putCollection(key, newCollection);
|
---|
760 | }
|
---|
761 |
|
---|
762 | synchronized private void putCollectionDefault(String key, Collection<String> val) {
|
---|
763 | putDefault(key, Utils.join("\u001e", val));
|
---|
764 | }
|
---|
765 |
|
---|
766 | /**
|
---|
767 | * Used to read a 2-dimensional array of strings from the preference file.
|
---|
768 | * If not a single entry could be found, def is returned.
|
---|
769 | */
|
---|
770 | synchronized public Collection<Collection<String>> getArray(String key,
|
---|
771 | Collection<Collection<String>> def)
|
---|
772 | {
|
---|
773 | if(def != null) {
|
---|
774 | putArrayDefault(key, def);
|
---|
775 | }
|
---|
776 | key += ".";
|
---|
777 | int num = 0;
|
---|
778 | Collection<Collection<String>> col = new LinkedList<Collection<String>>();
|
---|
779 | while(properties.containsKey(key+num)) {
|
---|
780 | col.add(getCollection(key+num++, null));
|
---|
781 | }
|
---|
782 | return num == 0 ? def : col;
|
---|
783 | }
|
---|
784 |
|
---|
785 | synchronized public boolean putArray(String key, Collection<Collection<String>> val) {
|
---|
786 | boolean changed = false;
|
---|
787 | key += ".";
|
---|
788 | Collection<String> keys = getAllPrefix(key).keySet();
|
---|
789 | if(val != null) {
|
---|
790 | int num = 0;
|
---|
791 | for(Collection<String> c : val) {
|
---|
792 | keys.remove(key+num);
|
---|
793 | changed |= putCollection(key+num++, c);
|
---|
794 | }
|
---|
795 | }
|
---|
796 | int l = key.length();
|
---|
797 | for(String k : keys) {
|
---|
798 | try {
|
---|
799 | Integer.valueOf(k.substring(l));
|
---|
800 | changed |= put(k, null);
|
---|
801 | } catch(NumberFormatException e) {
|
---|
802 | /* everything which does not end with a number should not be deleted */
|
---|
803 | }
|
---|
804 | }
|
---|
805 | return changed;
|
---|
806 | }
|
---|
807 |
|
---|
808 | synchronized private void putArrayDefault(String key, Collection<Collection<String>> val) {
|
---|
809 | key += ".";
|
---|
810 | Collection<String> keys = getAllPrefixDefault(key).keySet();
|
---|
811 | int num = 0;
|
---|
812 | for(Collection<String> c : val) {
|
---|
813 | keys.remove(key+num);
|
---|
814 | putCollectionDefault(key+num++, c);
|
---|
815 | }
|
---|
816 | int l = key.length();
|
---|
817 | for(String k : keys) {
|
---|
818 | try {
|
---|
819 | Integer.valueOf(k.substring(l));
|
---|
820 | defaults.remove(k);
|
---|
821 | } catch(Exception e) {
|
---|
822 | /* everything which does not end with a number should not be deleted */
|
---|
823 | }
|
---|
824 | }
|
---|
825 | }
|
---|
826 |
|
---|
827 | @Retention(RetentionPolicy.RUNTIME) public @interface pref { }
|
---|
828 | @Retention(RetentionPolicy.RUNTIME) public @interface writeExplicitly { }
|
---|
829 |
|
---|
830 | /**
|
---|
831 | * Get a list of hashes which are represented by a struct-like class.
|
---|
832 | * It reads lines of the form
|
---|
833 | * > key.0=prop:val \u001e prop:val \u001e ... \u001e prop:val
|
---|
834 | * > ...
|
---|
835 | * > key.N=prop:val \u001e prop:val \u001e ... \u001e prop:val
|
---|
836 | * Possible properties are given by fields of the class klass that have
|
---|
837 | * the @pref annotation.
|
---|
838 | * Default constructor is used to initialize the struct objects, properties
|
---|
839 | * then override some of these default values.
|
---|
840 | * @param key main preference key
|
---|
841 | * @param klass The struct class
|
---|
842 | * @return a list of objects of type T or an empty list if nothing was found
|
---|
843 | */
|
---|
844 | public <T> List<T> getListOfStructs(String key, Class<T> klass) {
|
---|
845 | List<T> r = getListOfStructs(key, null, klass);
|
---|
846 | if (r == null)
|
---|
847 | return Collections.emptyList();
|
---|
848 | else
|
---|
849 | return r;
|
---|
850 | }
|
---|
851 |
|
---|
852 | /**
|
---|
853 | * same as above, but returns def if nothing was found
|
---|
854 | */
|
---|
855 | public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) {
|
---|
856 | Collection<Collection<String>> array =
|
---|
857 | getArray(key, def == null ? null : serializeListOfStructs(def, klass));
|
---|
858 | if (array == null)
|
---|
859 | return def == null ? null : new ArrayList<T>(def);
|
---|
860 | List<T> lst = new ArrayList<T>();
|
---|
861 | for (Collection<String> entries : array) {
|
---|
862 | T struct = deserializeStruct(entries, klass);
|
---|
863 | lst.add(struct);
|
---|
864 | }
|
---|
865 | return lst;
|
---|
866 | }
|
---|
867 |
|
---|
868 | /**
|
---|
869 | * Save a list of hashes represented by a struct-like class.
|
---|
870 | * Considers only fields that have the @pref annotation.
|
---|
871 | * In addition it does not write fields with null values. (Thus they are cleared)
|
---|
872 | * Default values are given by the field values after default constructor has
|
---|
873 | * been called.
|
---|
874 | * Fields equal to the default value are not written unless the field has
|
---|
875 | * the @writeExplicitly annotation.
|
---|
876 | * @param key main preference key
|
---|
877 | * @param val the list that is supposed to be saved
|
---|
878 | * @param klass The struct class
|
---|
879 | * @return true if something has changed
|
---|
880 | */
|
---|
881 | public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) {
|
---|
882 | return putArray(key, serializeListOfStructs(val, klass));
|
---|
883 | }
|
---|
884 |
|
---|
885 | private <T> Collection<Collection<String>> serializeListOfStructs(Collection<T> l, Class<T> klass) {
|
---|
886 | if (l == null)
|
---|
887 | return null;
|
---|
888 | Collection<Collection<String>> vals = new ArrayList<Collection<String>>();
|
---|
889 | for (T struct : l) {
|
---|
890 | if (struct == null) {
|
---|
891 | continue;
|
---|
892 | }
|
---|
893 | vals.add(serializeStruct(struct, klass));
|
---|
894 | }
|
---|
895 | return vals;
|
---|
896 | }
|
---|
897 |
|
---|
898 | private <T> Collection<String> serializeStruct(T struct, Class<T> klass) {
|
---|
899 | T structPrototype;
|
---|
900 | try {
|
---|
901 | structPrototype = klass.newInstance();
|
---|
902 | } catch (InstantiationException ex) {
|
---|
903 | throw new RuntimeException();
|
---|
904 | } catch (IllegalAccessException ex) {
|
---|
905 | throw new RuntimeException();
|
---|
906 | }
|
---|
907 |
|
---|
908 | Collection<String> hash = new ArrayList<String>();
|
---|
909 | for (Field f : klass.getDeclaredFields()) {
|
---|
910 | if (f.getAnnotation(pref.class) == null) {
|
---|
911 | continue;
|
---|
912 | }
|
---|
913 | f.setAccessible(true);
|
---|
914 | try {
|
---|
915 | Object fieldValue = f.get(struct);
|
---|
916 | Object defaultFieldValue = f.get(structPrototype);
|
---|
917 | if (fieldValue != null) {
|
---|
918 | if (f.getAnnotation(writeExplicitly.class) != null || !Utils.equal(fieldValue, defaultFieldValue)) {
|
---|
919 | hash.add(String.format("%s:%s", f.getName().replace("_", "-"), fieldValue.toString()));
|
---|
920 | }
|
---|
921 | }
|
---|
922 | } catch (IllegalArgumentException ex) {
|
---|
923 | throw new RuntimeException();
|
---|
924 | } catch (IllegalAccessException ex) {
|
---|
925 | throw new RuntimeException();
|
---|
926 | }
|
---|
927 | }
|
---|
928 | return hash;
|
---|
929 | }
|
---|
930 |
|
---|
931 | private <T> T deserializeStruct(Collection<String> hash, Class<T> klass) {
|
---|
932 | T struct = null;
|
---|
933 | try {
|
---|
934 | struct = klass.newInstance();
|
---|
935 | } catch (InstantiationException ex) {
|
---|
936 | throw new RuntimeException();
|
---|
937 | } catch (IllegalAccessException ex) {
|
---|
938 | throw new RuntimeException();
|
---|
939 | }
|
---|
940 | for (String key_value : hash) {
|
---|
941 | final int i = key_value.indexOf(':');
|
---|
942 | if (i == -1 || i == 0) {
|
---|
943 | continue;
|
---|
944 | }
|
---|
945 | String key = key_value.substring(0,i);
|
---|
946 | String valueString = key_value.substring(i+1);
|
---|
947 |
|
---|
948 | Object value = null;
|
---|
949 | Field f;
|
---|
950 | try {
|
---|
951 | f = klass.getDeclaredField(key.replace("-", "_"));
|
---|
952 | } catch (NoSuchFieldException ex) {
|
---|
953 | continue;
|
---|
954 | } catch (SecurityException ex) {
|
---|
955 | throw new RuntimeException();
|
---|
956 | }
|
---|
957 | if (f.getAnnotation(pref.class) == null) {
|
---|
958 | continue;
|
---|
959 | }
|
---|
960 | f.setAccessible(true);
|
---|
961 | if (f.getType() == Boolean.class || f.getType() == boolean.class) {
|
---|
962 | value = Boolean.parseBoolean(valueString);
|
---|
963 | } else if (f.getType() == Integer.class || f.getType() == int.class) {
|
---|
964 | try {
|
---|
965 | value = Integer.parseInt(valueString);
|
---|
966 | } catch (NumberFormatException nfe) {
|
---|
967 | continue;
|
---|
968 | }
|
---|
969 | } else if (f.getType() == String.class) {
|
---|
970 | value = valueString;
|
---|
971 | } else
|
---|
972 | throw new RuntimeException("unsupported preference primitive type");
|
---|
973 |
|
---|
974 | try {
|
---|
975 | f.set(struct, value);
|
---|
976 | } catch (IllegalArgumentException ex) {
|
---|
977 | throw new AssertionError();
|
---|
978 | } catch (IllegalAccessException ex) {
|
---|
979 | throw new RuntimeException();
|
---|
980 | }
|
---|
981 | }
|
---|
982 | return struct;
|
---|
983 | }
|
---|
984 |
|
---|
985 | /**
|
---|
986 | * Updates system properties with the current values in the preferences.
|
---|
987 | *
|
---|
988 | */
|
---|
989 | public void updateSystemProperties() {
|
---|
990 | Properties sysProp = System.getProperties();
|
---|
991 | sysProp.put("http.agent", Version.getInstance().getAgentString());
|
---|
992 | System.setProperties(sysProp);
|
---|
993 | }
|
---|
994 |
|
---|
995 | /**
|
---|
996 | * The default plugin site
|
---|
997 | */
|
---|
998 | private final static String[] DEFAULT_PLUGIN_SITE = {
|
---|
999 | "http://josm.openstreetmap.de/plugin%<?plugins=>"};
|
---|
1000 |
|
---|
1001 | /**
|
---|
1002 | * Replies the collection of plugin site URLs from where plugin lists can be downloaded
|
---|
1003 | *
|
---|
1004 | * @return
|
---|
1005 | */
|
---|
1006 | public Collection<String> getPluginSites() {
|
---|
1007 | return getCollection("pluginmanager.sites", Arrays.asList(DEFAULT_PLUGIN_SITE));
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | /**
|
---|
1011 | * Sets the collection of plugin site URLs.
|
---|
1012 | *
|
---|
1013 | * @param sites the site URLs
|
---|
1014 | */
|
---|
1015 | public void setPluginSites(Collection<String> sites) {
|
---|
1016 | putCollection("pluginmanager.sites", sites);
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | public static class XMLTag {
|
---|
1020 | public String key;
|
---|
1021 | public String value;
|
---|
1022 | }
|
---|
1023 | public static class XMLCollection {
|
---|
1024 | public String key;
|
---|
1025 | }
|
---|
1026 | public static class XMLEntry {
|
---|
1027 | public String value;
|
---|
1028 | }
|
---|
1029 | public void fromXML(Reader in) throws SAXException {
|
---|
1030 | XmlObjectParser parser = new XmlObjectParser();
|
---|
1031 | parser.map("tag", XMLTag.class);
|
---|
1032 | parser.map("entry", XMLEntry.class);
|
---|
1033 | parser.map("collection", XMLCollection.class);
|
---|
1034 | parser.startWithValidation(in,
|
---|
1035 | "http://josm.openstreetmap.de/preferences-1.0", "resource://data/preferences.xsd");
|
---|
1036 | LinkedList<String> vals = new LinkedList<String>();
|
---|
1037 | while(parser.hasNext()) {
|
---|
1038 | Object o = parser.next();
|
---|
1039 | if(o instanceof XMLTag) {
|
---|
1040 | properties.put(((XMLTag)o).key, ((XMLTag)o).value);
|
---|
1041 | } else if (o instanceof XMLEntry) {
|
---|
1042 | vals.add(((XMLEntry)o).value);
|
---|
1043 | } else if (o instanceof XMLCollection) {
|
---|
1044 | properties.put(((XMLCollection)o).key, Utils.join("\u001e", vals));
|
---|
1045 | vals = new LinkedList<String>();
|
---|
1046 | }
|
---|
1047 | }
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | public String toXML(boolean nopass) {
|
---|
1051 | StringBuilder b = new StringBuilder(
|
---|
1052 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
---|
1053 | "<preferences xmlns=\"http://josm.openstreetmap.de/preferences-1.0\">\n");
|
---|
1054 | for (Entry<String, String> p : properties.entrySet()) {
|
---|
1055 | if (nopass && p.getKey().equals("osm-server.password")) {
|
---|
1056 | continue; // do not store plain password.
|
---|
1057 | }
|
---|
1058 | String r = p.getValue();
|
---|
1059 | if(r.contains("\u001e"))
|
---|
1060 | {
|
---|
1061 | b.append(" <collection key='");
|
---|
1062 | b.append(XmlWriter.encode(p.getKey()));
|
---|
1063 | b.append("'>\n");
|
---|
1064 | for (String val : r.split("\u001e", -1))
|
---|
1065 | {
|
---|
1066 | b.append(" <entry value='");
|
---|
1067 | b.append(XmlWriter.encode(val));
|
---|
1068 | b.append("' />\n");
|
---|
1069 | }
|
---|
1070 | b.append(" </collection>\n");
|
---|
1071 | }
|
---|
1072 | else
|
---|
1073 | {
|
---|
1074 | b.append(" <tag key='");
|
---|
1075 | b.append(XmlWriter.encode(p.getKey()));
|
---|
1076 | b.append("' value='");
|
---|
1077 | b.append(XmlWriter.encode(p.getValue()));
|
---|
1078 | b.append("' />\n");
|
---|
1079 | }
|
---|
1080 | }
|
---|
1081 | b.append("</preferences>");
|
---|
1082 | return b.toString();
|
---|
1083 | }
|
---|
1084 | }
|
---|