Changeset 5761 in josm


Ignore:
Timestamp:
2013-03-07T20:53:08+01:00 (12 years ago)
Author:
stoecker
Message:

drop outdated configfile handling routines

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r5698 r5761  
    296296    }
    297297
    298     /* remove end of 2012 */
    299     public File getOldPreferenceFile() {
    300         return new File(getPreferencesDirFile(), "preferences");
    301     }
    302 
    303298    public File getPluginsDirectory() {
    304299        return new File(getPreferencesDirFile(), "plugins");
     
    609604    }
    610605
    611     public void loadOld() throws Exception {
    612         load(true);
    613     }
    614 
    615606    public void load() throws Exception {
    616         load(false);
    617     }
    618 
    619     private void load(boolean old) throws Exception {
    620607        properties.clear();
    621608        if (!Main.applet) {
    622             File pref = old ? getOldPreferenceFile() : getPreferenceFile();
     609            File pref = getPreferenceFile();
    623610            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), "utf-8"));
    624             /* FIXME: TODO: remove old style config file end of 2012 */
    625611            try {
    626                 if (old) {
    627                     in.mark(1);
    628                     int v = in.read();
    629                     in.reset();
    630                     if(v == '<') {
    631                         validateXML(in);
    632                         Utils.close(in);
    633                         in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), "utf-8"));
    634                         fromXML(in);
    635                     } else {
    636                         int lineNumber = 0;
    637                         ArrayList<Integer> errLines = new ArrayList<Integer>();
    638                         for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) {
    639                             final int i = line.indexOf('=');
    640                             if (i == -1 || i == 0) {
    641                                 errLines.add(lineNumber);
    642                                 continue;
    643                             }
    644                             String key = line.substring(0,i);
    645                             String value = line.substring(i+1);
    646                             if (!value.isEmpty()) {
    647                                 properties.put(key, value);
    648                             }
    649                         }
    650                         if (!errLines.isEmpty())
    651                             throw new IOException(tr("Malformed config file at lines {0}", errLines));
    652                     }
    653                 } else {
    654                     validateXML(in);
    655                     Utils.close(in);
    656                     in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), "utf-8"));
    657                     fromXML(in);
    658                 }
     612                validateXML(in);
     613                Utils.close(in);
     614                in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), "utf-8"));
     615                fromXML(in);
    659616            } finally {
    660617                in.close();
     
    662619        }
    663620        updateSystemProperties();
    664         /* FIXME: TODO: remove special version check end of 2012 */
    665         if(!properties.containsKey("expert")) {
    666             try {
    667                 String v = get("josm.version");
    668                 if(v.isEmpty() || Integer.parseInt(v) <= 4511)
    669                     properties.put("expert", "true");
    670             } catch(Exception e) {
    671                 properties.put("expert", "true");
    672             }
    673         }
    674621        removeObsolete();
    675622    }
     
    707654        try {
    708655            if (!preferenceFile.exists()) {
    709                 File oldPreferenceFile = getOldPreferenceFile();
    710                 if (!oldPreferenceFile.exists()) {
    711                     System.out.println(tr("Info: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
    712                     resetToDefault();
    713                     save();
    714                 } else {
    715                     try {
    716                         loadOld();
    717                     } catch (Exception e) {
    718                         e.printStackTrace();
    719                         File backupFile = new File(prefDir,"preferences.bak");
    720                         JOptionPane.showMessageDialog(
    721                                 Main.parent,
    722                                 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()),
    723                                 tr("Error"),
    724                                 JOptionPane.ERROR_MESSAGE
    725                         );
    726                         Main.platform.rename(oldPreferenceFile, backupFile);
    727                         try {
    728                             resetToDefault();
    729                             save();
    730                         } catch(IOException e1) {
    731                             e1.printStackTrace();
    732                             System.err.println(tr("Warning: Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile()));
    733                         }
    734                     }
    735                     return;
    736                 }
     656                System.out.println(tr("Info: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
     657                resetToDefault();
     658                save();
    737659            } else if (reset) {
    738660                System.out.println(tr("Warning: Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile()));
     
    921843    public Collection<String> getCollection(String key, Collection<String> def) {
    922844        putCollectionDefault(key, def == null ? null : new ArrayList<String>(def));
    923         Collection<String> prop = getCollectionInternal(key);
     845        Collection<String> prop = collectionProperties.get(key);
    924846        if (prop != null)
    925847            return prop;
     
    936858    public Collection<String> getCollection(String key) {
    937859        putCollectionDefault(key, null);
    938         Collection<String> prop = getCollectionInternal(key);
     860        Collection<String> prop = collectionProperties.get(key);
    939861        if (prop != null)
    940862            return prop;
    941863        else
    942864            return Collections.emptyList();
    943     }
    944 
    945     /* remove this workaround end of 2012, replace by direct access to structure */
    946     synchronized private List<String> getCollectionInternal(String key) {
    947         List<String> prop = collectionProperties.get(key);
    948         if (prop != null)
    949             return prop;
    950         else {
    951             String s = properties.get(key);
    952             if(s != null) {
    953                 prop = Arrays.asList(s.split("\u001e", -1));
    954                 collectionProperties.put(key, Collections.unmodifiableList(prop));
    955                 properties.remove(key);
    956                 defaults.remove(key);
    957                 return prop;
    958             }
    959         }
    960         return null;
    961865    }
    962866
     
    978882                if (!changed) return false;
    979883            } else {
    980                 oldValue = getCollectionInternal(key);
     884                oldValue = collectionProperties.get(key);
    981885                if (equalCollection(value, oldValue)) return false;
    982886                Collection<String> defValue = collectionDefaults.get(key);
     
    1044948            putArrayDefault(key, null);
    1045949        }
    1046         List<List<String>> prop = getArrayInternal(key);
     950        List<List<String>> prop = arrayProperties.get(key);
    1047951        if (prop != null) {
    1048952            @SuppressWarnings("unchecked")
     
    1055959    public Collection<Collection<String>> getArray(String key) {
    1056960        putArrayDefault(key, null);
    1057         List<List<String>> prop = getArrayInternal(key);
     961        List<List<String>> prop = arrayProperties.get(key);
    1058962        if (prop != null) {
    1059963            @SuppressWarnings("unchecked")
     
    1064968    }
    1065969
    1066     /* remove this workaround end of 2012 and replace by direct array access */
    1067     synchronized private List<List<String>> getArrayInternal(String key) {
    1068         List<List<String>> prop = arrayProperties.get(key);
    1069         if (prop != null)
    1070             return prop;
    1071         else {
    1072             String keyDot = key + ".";
    1073             int num = 0;
    1074             List<List<String>> col = new ArrayList<List<String>>();
    1075             while (true) {
    1076                 List<String> c = getCollectionInternal(keyDot+num);
    1077                 if (c == null) {
    1078                     break;
    1079                 }
    1080                 col.add(c);
    1081                 collectionProperties.remove(keyDot+num);
    1082                 collectionDefaults.remove(keyDot+num);
    1083                 num++;
    1084             }
    1085             if (num > 0) {
    1086                 arrayProperties.put(key, Collections.unmodifiableList(col));
    1087                 return col;
    1088             }
    1089         }
    1090         return null;
    1091     }
    1092 
    1093970    public boolean putArray(String key, Collection<Collection<String>> value) {
    1094971        boolean changed = false;
     
    1098975
    1099976        synchronized (this) {
     977            oldValue = arrayProperties.get(key);
    1100978            if (value == null) {
    1101                 oldValue = getArrayInternal(key);
    1102979                if (arrayProperties.remove(key) != null) return false;
    1103980            } else {
    1104                 oldValue = getArrayInternal(key);
    1105981                if (equalArray(value, oldValue)) return false;
    1106982
     
    11541030            putListOfStructsDefault(key, null);
    11551031        }
    1156         Collection<Map<String, String>> prop = getListOfStructsInternal(key);
     1032        Collection<Map<String, String>> prop = listOfStructsProperties.get(key);
    11571033        if (prop != null)
    11581034            return prop;
     
    11611037    }
    11621038
    1163     /* remove this workaround end of 2012 and use direct access to proper variable */
    1164     private synchronized List<Map<String, String>> getListOfStructsInternal(String key) {
    1165         List<Map<String, String>> prop = listOfStructsProperties.get(key);
    1166         if (prop != null)
    1167             return prop;
    1168         else {
    1169             List<List<String>> array = getArrayInternal(key);
    1170             if (array == null) return null;
    1171             prop = new ArrayList<Map<String, String>>(array.size());
    1172             for (Collection<String> mapStr : array) {
    1173                 Map<String, String> map = new LinkedHashMap<String, String>();
    1174                 for (String key_value : mapStr) {
    1175                     final int i = key_value.indexOf(':');
    1176                     if (i == -1 || i == 0) {
    1177                         continue;
    1178                     }
    1179                     String k = key_value.substring(0,i);
    1180                     String v = key_value.substring(i+1);
    1181                     map.put(k, v);
    1182                 }
    1183                 prop.add(Collections.unmodifiableMap(map));
    1184             }
    1185             arrayProperties.remove(key);
    1186             arrayDefaults.remove(key);
    1187             listOfStructsProperties.put(key, Collections.unmodifiableList(prop));
    1188             return prop;
    1189         }
    1190     }
    1191 
    11921039    public boolean putListOfStructs(String key, Collection<Map<String, String>> value) {
    11931040        boolean changed = false;
     
    11971044
    11981045        synchronized (this) {
     1046            oldValue = listOfStructsProperties.get(key);
    11991047            if (value == null) {
    1200                 oldValue = getListOfStructsInternal(key);
    12011048                if (listOfStructsProperties.remove(key) != null) return false;
    12021049            } else {
    1203                 oldValue = getListOfStructsInternal(key);
    12041050                if (equalListOfStructs(oldValue, value)) return false;
    12051051
     
    15191365
    15201366    public void validateXML(Reader in) throws Exception {
    1521         SchemaFactory factory =  SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
     1367        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    15221368        Schema schema = factory.newSchema(new StreamSource(new MirroredInputStream("resource://data/preferences.xsd")));
    15231369        Validator validator = schema.newValidator();
     
    16921538            /* don't save default values */
    16931539            if(s == null || !s.equals(r)) {
    1694                 /* TODO: remove old format exception end of 2012 */
    1695                 if(r.contains("\u001e"))
    1696                 {
    1697                     b.append("  <list key='");
    1698                     b.append(XmlWriter.encode(key));
    1699                     b.append("'>\n");
    1700                     for (String val : r.split("\u001e", -1))
    1701                     {
    1702                         b.append("    <entry value='");
    1703                         b.append(XmlWriter.encode(val));
    1704                         b.append("'/>\n");
    1705                     }
    1706                     b.append("  </list>\n");
    1707                 }
    1708                 else
    1709                 {
    1710                     b.append("  <tag key='");
    1711                     b.append(XmlWriter.encode(key));
    1712                     b.append("' value='");
    1713                     b.append(XmlWriter.encode(setting.getValue()));
    1714                     b.append("'/>\n");
    1715                 }
     1540                b.append("  <tag key='");
     1541                b.append(XmlWriter.encode(key));
     1542                b.append("' value='");
     1543                b.append(XmlWriter.encode(setting.getValue()));
     1544                b.append("'/>\n");
    17161545            }
    17171546        }
     
    17851614    public void removeObsolete() {
    17861615        String[] obsolete = {
    1787                 "gui.combobox.maximum-row-count",  // 08/2012 - briefly introduced with #7917, can be removed end 2012
    17881616                "color.Imagery fade",              // 08/2012 - wrong property caused by #6723, can be removed mid-2013
    17891617        };
Note: See TracChangeset for help on using the changeset viewer.