Ticket #7027: preferences.xml2.patch

File preferences.xml2.patch, 7.3 KB (added by bastiK, 14 years ago)

improved patch

  • src/org/openstreetmap/josm/data/Preferences.java

     
    7474    protected final SortedMap<String, String> defaults = new TreeMap<String, String>();
    7575    protected final SortedMap<String, String> colornames = new TreeMap<String, String>();
    7676
    77     /* NOTE: FIXME: Remove when saving XML enabled */
    78     private boolean loadedXML = true;
    79 
    8077    public interface PreferenceChangeEvent{
    8178        String getKey();
    8279        String getOldValue();
     
    163160    }
    164161
    165162    public File getPreferenceFile() {
     163        return new File(getPreferencesDirFile(), "preferences.xml");
     164    }
     165
     166    public File getOldPreferenceFile() {
    166167        return new File(getPreferencesDirFile(), "preferences");
    167168    }
    168169
     
    387388
    388389        final PrintWriter out = new PrintWriter(new OutputStreamWriter(
    389390                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));
    402392        out.close();
    403393
    404394        File tmpFile = new File(prefFile + "_tmp");
     
    445435        }
    446436    }
    447437
     438    public void loadOld() throws Exception {
     439        load(true);
     440    }
     441
    448442    public void load() throws Exception {
     443        load(false);
     444    }
     445
     446    private void load(boolean old) throws Exception {
    449447        properties.clear();
    450448        if(!Main.applet) {
    451449            final BufferedReader in = new BufferedReader(new InputStreamReader(
    452                     new FileInputStream(getPreferencesDir()+"preferences"), "utf-8"));
     450                    new FileInputStream(old ? getOldPreferenceFile() : getPreferenceFile()), "utf-8"));
    453451            /* FIXME: TODO: remove old style config file end of 2012 */
    454452            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                            }
    469473                        }
    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));
    475476                    }
    476                     if (!errLines.isEmpty())
    477                         throw new IOException(tr("Malformed config file at lines {0}", errLines));
     477                } else {
     478                    fromXML(in);
    478479                }
    479480            } finally {
    480481                in.close();
     
    525526        File preferenceFile = getPreferenceFile();
    526527        try {
    527528            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                }
    531557            } else if (reset) {
    532558                System.out.println(tr("Warning: Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile()));
    533559                resetToDefault();
     
    547573            load();
    548574        } catch (Exception e) {
    549575            e.printStackTrace();
    550             File backupFile = new File(prefDir,"preferences.bak");
     576            File backupFile = new File(prefDir,"preferences.xml.bak");
    551577            JOptionPane.showMessageDialog(
    552578                    Main.parent,
    553579                    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()),