Ticket #7027: preferences.xml.patch
| File preferences.xml.patch, 5.7 KB (added by , 14 years ago) |
|---|
-
src/org/openstreetmap/josm/data/Preferences.java
74 74 protected final SortedMap<String, String> defaults = new TreeMap<String, String>(); 75 75 protected final SortedMap<String, String> colornames = new TreeMap<String, String>(); 76 76 77 /* NOTE: FIXME: Remove when saving XML enabled */78 private boolean loadedXML = true;79 80 77 public interface PreferenceChangeEvent{ 81 78 String getKey(); 82 79 String getOldValue(); … … 163 160 } 164 161 165 162 public File getPreferenceFile() { 163 return new File(getPreferencesDirFile(), "preferences.xml"); 164 } 165 166 public File getOldPreferenceFile() { 166 167 return new File(getPreferencesDirFile(), "preferences"); 167 168 } 168 169 … … 387 388 388 389 final PrintWriter out = new PrintWriter(new OutputStreamWriter( 389 390 new FileOutputStream(prefFile + "_tmp"), "utf-8"), false); 390 /* FIXME: NOTE: loadedXML - removed 01.12.2011 */ 391 if(loadedXML) { 392 out.print(toXML(false)); 393 } else { 394 for (final Entry<String, String> e : properties.entrySet()) { 395 String s = defaults.get(e.getKey()); 396 /* don't save default values */ 397 if(s == null || !s.equals(e.getValue())) { 398 out.println(e.getKey() + "=" + e.getValue()); 399 } 400 } 401 } 391 out.print(toXML(false)); 402 392 out.close(); 403 393 404 394 File tmpFile = new File(prefFile + "_tmp"); … … 445 435 } 446 436 } 447 437 438 public void loadOld() throws Exception { 439 load(true); 440 } 441 448 442 public void load() throws Exception { 443 load(false); 444 } 445 446 private void load(boolean old) throws Exception { 449 447 properties.clear(); 450 448 if(!Main.applet) { 451 449 final BufferedReader in = new BufferedReader(new InputStreamReader( 452 new FileInputStream( getPreferencesDir()+"preferences"), "utf-8"));450 new FileInputStream(old ? getOldPreferenceFile() : getPreferenceFile()), "utf-8")); 453 451 /* FIXME: TODO: remove old style config file end of 2012 */ 454 452 try { 455 in.mark(1); 456 int v = in.read(); 457 in.reset(); 458 if(v == '<') { 459 fromXML(in); 460 } else { 461 loadedXML = false; 453 if (old) { 462 454 int lineNumber = 0; 463 455 ArrayList<Integer> errLines = new ArrayList<Integer>(); 464 456 for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) { … … 475 467 } 476 468 if (!errLines.isEmpty()) 477 469 throw new IOException(tr("Malformed config file at lines {0}", errLines)); 470 } else { 471 fromXML(in); 478 472 } 479 473 } finally { 480 474 in.close(); … … 525 519 File preferenceFile = getPreferenceFile(); 526 520 try { 527 521 if (!preferenceFile.exists()) { 528 System.out.println(tr("Warning: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile())); 529 resetToDefault(); 530 save(); 522 File oldPreferenceFile = getOldPreferenceFile(); 523 if (!oldPreferenceFile.exists()) { 524 System.out.println(tr("Warning: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile())); 525 resetToDefault(); 526 save(); 527 } else { 528 try { 529 loadOld(); 530 } catch (Exception e) { 531 e.printStackTrace(); 532 File backupFile = new File(prefDir,"preferences.bak"); 533 JOptionPane.showMessageDialog( 534 Main.parent, 535 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()), 536 tr("Error"), 537 JOptionPane.ERROR_MESSAGE 538 ); 539 Main.platform.rename(oldPreferenceFile, backupFile); 540 try { 541 resetToDefault(); 542 save(); 543 } catch(IOException e1) { 544 e1.printStackTrace(); 545 System.err.println(tr("Warning: Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile())); 546 } 547 } 548 return; 549 } 531 550 } else if (reset) { 532 551 System.out.println(tr("Warning: Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile())); 533 552 resetToDefault(); … … 547 566 load(); 548 567 } catch (Exception e) { 549 568 e.printStackTrace(); 550 File backupFile = new File(prefDir,"preferences. bak");569 File backupFile = new File(prefDir,"preferences.xml.bak"); 551 570 JOptionPane.showMessageDialog( 552 571 Main.parent, 553 572 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()),
