Changeset 5761 in josm
- Timestamp:
- 2013-03-07T20:53:08+01:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r5698 r5761 296 296 } 297 297 298 /* remove end of 2012 */299 public File getOldPreferenceFile() {300 return new File(getPreferencesDirFile(), "preferences");301 }302 303 298 public File getPluginsDirectory() { 304 299 return new File(getPreferencesDirFile(), "plugins"); … … 609 604 } 610 605 611 public void loadOld() throws Exception {612 load(true);613 }614 615 606 public void load() throws Exception { 616 load(false);617 }618 619 private void load(boolean old) throws Exception {620 607 properties.clear(); 621 608 if (!Main.applet) { 622 File pref = old ? getOldPreferenceFile() :getPreferenceFile();609 File pref = getPreferenceFile(); 623 610 BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), "utf-8")); 624 /* FIXME: TODO: remove old style config file end of 2012 */625 611 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); 659 616 } finally { 660 617 in.close(); … … 662 619 } 663 620 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 }674 621 removeObsolete(); 675 622 } … … 707 654 try { 708 655 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(); 737 659 } else if (reset) { 738 660 System.out.println(tr("Warning: Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile())); … … 921 843 public Collection<String> getCollection(String key, Collection<String> def) { 922 844 putCollectionDefault(key, def == null ? null : new ArrayList<String>(def)); 923 Collection<String> prop = getCollectionInternal(key);845 Collection<String> prop = collectionProperties.get(key); 924 846 if (prop != null) 925 847 return prop; … … 936 858 public Collection<String> getCollection(String key) { 937 859 putCollectionDefault(key, null); 938 Collection<String> prop = getCollectionInternal(key);860 Collection<String> prop = collectionProperties.get(key); 939 861 if (prop != null) 940 862 return prop; 941 863 else 942 864 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;961 865 } 962 866 … … 978 882 if (!changed) return false; 979 883 } else { 980 oldValue = getCollectionInternal(key);884 oldValue = collectionProperties.get(key); 981 885 if (equalCollection(value, oldValue)) return false; 982 886 Collection<String> defValue = collectionDefaults.get(key); … … 1044 948 putArrayDefault(key, null); 1045 949 } 1046 List<List<String>> prop = getArrayInternal(key);950 List<List<String>> prop = arrayProperties.get(key); 1047 951 if (prop != null) { 1048 952 @SuppressWarnings("unchecked") … … 1055 959 public Collection<Collection<String>> getArray(String key) { 1056 960 putArrayDefault(key, null); 1057 List<List<String>> prop = getArrayInternal(key);961 List<List<String>> prop = arrayProperties.get(key); 1058 962 if (prop != null) { 1059 963 @SuppressWarnings("unchecked") … … 1064 968 } 1065 969 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 1093 970 public boolean putArray(String key, Collection<Collection<String>> value) { 1094 971 boolean changed = false; … … 1098 975 1099 976 synchronized (this) { 977 oldValue = arrayProperties.get(key); 1100 978 if (value == null) { 1101 oldValue = getArrayInternal(key);1102 979 if (arrayProperties.remove(key) != null) return false; 1103 980 } else { 1104 oldValue = getArrayInternal(key);1105 981 if (equalArray(value, oldValue)) return false; 1106 982 … … 1154 1030 putListOfStructsDefault(key, null); 1155 1031 } 1156 Collection<Map<String, String>> prop = getListOfStructsInternal(key);1032 Collection<Map<String, String>> prop = listOfStructsProperties.get(key); 1157 1033 if (prop != null) 1158 1034 return prop; … … 1161 1037 } 1162 1038 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 1192 1039 public boolean putListOfStructs(String key, Collection<Map<String, String>> value) { 1193 1040 boolean changed = false; … … 1197 1044 1198 1045 synchronized (this) { 1046 oldValue = listOfStructsProperties.get(key); 1199 1047 if (value == null) { 1200 oldValue = getListOfStructsInternal(key);1201 1048 if (listOfStructsProperties.remove(key) != null) return false; 1202 1049 } else { 1203 oldValue = getListOfStructsInternal(key);1204 1050 if (equalListOfStructs(oldValue, value)) return false; 1205 1051 … … 1519 1365 1520 1366 public void validateXML(Reader in) throws Exception { 1521 SchemaFactory factory = 1367 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 1522 1368 Schema schema = factory.newSchema(new StreamSource(new MirroredInputStream("resource://data/preferences.xsd"))); 1523 1369 Validator validator = schema.newValidator(); … … 1692 1538 /* don't save default values */ 1693 1539 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"); 1716 1545 } 1717 1546 } … … 1785 1614 public void removeObsolete() { 1786 1615 String[] obsolete = { 1787 "gui.combobox.maximum-row-count", // 08/2012 - briefly introduced with #7917, can be removed end 20121788 1616 "color.Imagery fade", // 08/2012 - wrong property caused by #6723, can be removed mid-2013 1789 1617 };
Note:
See TracChangeset
for help on using the changeset viewer.