Ticket #7027: preferences.xml2.patch
| File preferences.xml2.patch, 7.3 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; 462 int lineNumber = 0; 463 ArrayList<Integer> errLines = new ArrayList<Integer>(); 464 for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) { 465 final int i = line.indexOf('='); 466 if (i == -1 || i == 0) { 467 errLines.add(lineNumber); 468 continue; 453 if (old) { 454 in.mark(1); 455 int v = in.read(); 456 in.reset(); 457 if(v == '<') { 458 fromXML(in); 459 } else { 460 int lineNumber = 0; 461 ArrayList<Integer> errLines = new ArrayList<Integer>(); 462 for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) { 463 final int i = line.indexOf('='); 464 if (i == -1 || i == 0) { 465 errLines.add(lineNumber); 466 continue; 467 } 468 String key = line.substring(0,i); 469 String value = line.substring(i+1); 470 if (!value.isEmpty()) { 471 properties.put(key, value); 472 } 469 473 } 470 String key = line.substring(0,i); 471 String value = line.substring(i+1); 472 if (!value.isEmpty()) { 473 properties.put(key, value); 474 } 474 if (!errLines.isEmpty()) 475 throw new IOException(tr("Malformed config file at lines {0}", errLines)); 475 476 } 476 if (!errLines.isEmpty())477 throw new IOException(tr("Malformed config file at lines {0}", errLines));477 } else { 478 fromXML(in); 478 479 } 479 480 } finally { 480 481 in.close(); … … 525 526 File preferenceFile = getPreferenceFile(); 526 527 try { 527 528 if (!preferenceFile.exists()) { 528 System.out.println(tr("Warning: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile())); 529 resetToDefault(); 530 save(); 529 File oldPreferenceFile = getOldPreferenceFile(); 530 if (!oldPreferenceFile.exists()) { 531 System.out.println(tr("Warning: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile())); 532 resetToDefault(); 533 save(); 534 } else { 535 try { 536 loadOld(); 537 } catch (Exception e) { 538 e.printStackTrace(); 539 File backupFile = new File(prefDir,"preferences.bak"); 540 JOptionPane.showMessageDialog( 541 Main.parent, 542 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()), 543 tr("Error"), 544 JOptionPane.ERROR_MESSAGE 545 ); 546 Main.platform.rename(oldPreferenceFile, backupFile); 547 try { 548 resetToDefault(); 549 save(); 550 } catch(IOException e1) { 551 e1.printStackTrace(); 552 System.err.println(tr("Warning: Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile())); 553 } 554 } 555 return; 556 } 531 557 } else if (reset) { 532 558 System.out.println(tr("Warning: Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile())); 533 559 resetToDefault(); … … 547 573 load(); 548 574 } catch (Exception e) { 549 575 e.printStackTrace(); 550 File backupFile = new File(prefDir,"preferences. bak");576 File backupFile = new File(prefDir,"preferences.xml.bak"); 551 577 JOptionPane.showMessageDialog( 552 578 Main.parent, 553 579 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()),
