Changeset 9780 in josm
- Timestamp:
- 2016-02-10T21:42:20+01:00 (8 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r9759 r9780 8 8 import java.awt.GraphicsEnvironment; 9 9 import java.awt.Toolkit; 10 import java.io.BufferedReader;11 10 import java.io.File; 12 11 import java.io.FileOutputStream; 13 12 import java.io.IOException; 14 import java.io.InputStream;15 13 import java.io.OutputStreamWriter; 16 14 import java.io.PrintWriter; … … 22 20 import java.lang.reflect.Field; 23 21 import java.nio.charset.StandardCharsets; 24 import java.nio.file.Files;25 22 import java.util.ArrayList; 26 23 import java.util.Collection; … … 53 50 import javax.json.JsonWriter; 54 51 import javax.swing.JOptionPane; 55 import javax.xml.XMLConstants;56 import javax.xml.stream.XMLInputFactory;57 import javax.xml.stream.XMLStreamConstants;58 52 import javax.xml.stream.XMLStreamException; 59 import javax.xml.stream.XMLStreamReader;60 import javax.xml.transform.stream.StreamSource;61 import javax.xml.validation.Schema;62 import javax.xml.validation.SchemaFactory;63 import javax.xml.validation.Validator;64 53 65 54 import org.openstreetmap.josm.Main; … … 68 57 import org.openstreetmap.josm.data.preferences.ListSetting; 69 58 import org.openstreetmap.josm.data.preferences.MapListSetting; 59 import org.openstreetmap.josm.data.preferences.PreferencesReader; 70 60 import org.openstreetmap.josm.data.preferences.Setting; 71 61 import org.openstreetmap.josm.data.preferences.SettingVisitor; 72 62 import org.openstreetmap.josm.data.preferences.StringSetting; 73 import org.openstreetmap.josm.io.CachedFile;74 63 import org.openstreetmap.josm.io.OfflineAccessException; 75 64 import org.openstreetmap.josm.io.OnlineResource; … … 121 110 122 111 /** 123 * Version of the loaded data file, required for updates124 */125 private int loadedVersion = 0;126 127 /**128 112 * Internal storage for the cache directory. 129 113 */ … … 524 508 try (PrintWriter out = new PrintWriter(new OutputStreamWriter( 525 509 new FileOutputStream(prefFile + "_tmp"), StandardCharsets.UTF_8), false)) { 526 out.print(toXML( false));510 out.print(toXML(settingsMap, false)); 527 511 } 528 512 … … 560 544 */ 561 545 protected void load() throws IOException, SAXException, XMLStreamException { 546 File pref = getPreferenceFile(); 547 PreferencesReader.validateXML(pref); 548 PreferencesReader reader = new PreferencesReader(); 549 reader.fromXML(pref); 562 550 settingsMap.clear(); 563 File pref = getPreferenceFile(); 564 try (BufferedReader in = Files.newBufferedReader(pref.toPath(), StandardCharsets.UTF_8)) { 565 validateXML(in); 566 } 567 try (BufferedReader in = Files.newBufferedReader(pref.toPath(), StandardCharsets.UTF_8)) { 568 fromXML(in); 569 } 551 settingsMap.putAll(reader.getSettings()); 570 552 updateSystemProperties(); 571 removeObsolete(); 553 removeObsolete(reader.getVersion()); 554 } 555 556 public void fromXML(Reader in) throws XMLStreamException { 557 PreferencesReader reader = new PreferencesReader(); 558 reader.fromXML(in); 559 settingsMap.clear(); 560 settingsMap.putAll(reader.getSettings()); 572 561 } 573 562 … … 1336 1325 } 1337 1326 1338 protected XMLStreamReader parser; 1339 1340 public static void validateXML(Reader in) throws IOException, SAXException { 1341 try (InputStream xsdStream = new CachedFile("resource://data/preferences.xsd").getInputStream()) { 1342 Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(xsdStream)); 1343 Validator validator = schema.newValidator(); 1344 validator.validate(new StreamSource(in)); 1345 } 1346 } 1347 1348 protected void fromXML(Reader in) throws XMLStreamException { 1349 this.parser = XMLInputFactory.newInstance().createXMLStreamReader(in); 1350 parse(); 1351 } 1352 1353 private void parse() throws XMLStreamException { 1354 int event = parser.getEventType(); 1355 while (true) { 1356 if (event == XMLStreamConstants.START_ELEMENT) { 1357 try { 1358 loadedVersion = Integer.parseInt(parser.getAttributeValue(null, "version")); 1359 } catch (NumberFormatException e) { 1360 if (Main.isDebugEnabled()) { 1361 Main.debug(e.getMessage()); 1362 } 1363 } 1364 parseRoot(); 1365 } else if (event == XMLStreamConstants.END_ELEMENT) { 1366 return; 1367 } 1368 if (parser.hasNext()) { 1369 event = parser.next(); 1370 } else { 1371 break; 1372 } 1373 } 1374 parser.close(); 1375 } 1376 1377 private void parseRoot() throws XMLStreamException { 1378 while (true) { 1379 int event = parser.next(); 1380 if (event == XMLStreamConstants.START_ELEMENT) { 1381 String localName = parser.getLocalName(); 1382 switch(localName) { 1383 case "tag": 1384 settingsMap.put(parser.getAttributeValue(null, "key"), new StringSetting(parser.getAttributeValue(null, "value"))); 1385 jumpToEnd(); 1386 break; 1387 case "list": 1388 case "collection": 1389 case "lists": 1390 case "maps": 1391 parseToplevelList(); 1392 break; 1393 default: 1394 throwException("Unexpected element: "+localName); 1395 } 1396 } else if (event == XMLStreamConstants.END_ELEMENT) { 1397 return; 1398 } 1399 } 1400 } 1401 1402 private void jumpToEnd() throws XMLStreamException { 1403 while (true) { 1404 int event = parser.next(); 1405 if (event == XMLStreamConstants.START_ELEMENT) { 1406 jumpToEnd(); 1407 } else if (event == XMLStreamConstants.END_ELEMENT) { 1408 return; 1409 } 1410 } 1411 } 1412 1413 private void parseToplevelList() throws XMLStreamException { 1414 String key = parser.getAttributeValue(null, "key"); 1415 String name = parser.getLocalName(); 1416 1417 List<String> entries = null; 1418 List<List<String>> lists = null; 1419 List<Map<String, String>> maps = null; 1420 while (true) { 1421 int event = parser.next(); 1422 if (event == XMLStreamConstants.START_ELEMENT) { 1423 String localName = parser.getLocalName(); 1424 switch(localName) { 1425 case "entry": 1426 if (entries == null) { 1427 entries = new ArrayList<>(); 1428 } 1429 entries.add(parser.getAttributeValue(null, "value")); 1430 jumpToEnd(); 1431 break; 1432 case "list": 1433 if (lists == null) { 1434 lists = new ArrayList<>(); 1435 } 1436 lists.add(parseInnerList()); 1437 break; 1438 case "map": 1439 if (maps == null) { 1440 maps = new ArrayList<>(); 1441 } 1442 maps.add(parseMap()); 1443 break; 1444 default: 1445 throwException("Unexpected element: "+localName); 1446 } 1447 } else if (event == XMLStreamConstants.END_ELEMENT) { 1448 break; 1449 } 1450 } 1451 if (entries != null) { 1452 settingsMap.put(key, new ListSetting(Collections.unmodifiableList(entries))); 1453 } else if (lists != null) { 1454 settingsMap.put(key, new ListListSetting(Collections.unmodifiableList(lists))); 1455 } else if (maps != null) { 1456 settingsMap.put(key, new MapListSetting(Collections.unmodifiableList(maps))); 1457 } else { 1458 if ("lists".equals(name)) { 1459 settingsMap.put(key, new ListListSetting(Collections.<List<String>>emptyList())); 1460 } else if ("maps".equals(name)) { 1461 settingsMap.put(key, new MapListSetting(Collections.<Map<String, String>>emptyList())); 1462 } else { 1463 settingsMap.put(key, new ListSetting(Collections.<String>emptyList())); 1464 } 1465 } 1466 } 1467 1468 private List<String> parseInnerList() throws XMLStreamException { 1469 List<String> entries = new ArrayList<>(); 1470 while (true) { 1471 int event = parser.next(); 1472 if (event == XMLStreamConstants.START_ELEMENT) { 1473 if ("entry".equals(parser.getLocalName())) { 1474 entries.add(parser.getAttributeValue(null, "value")); 1475 jumpToEnd(); 1476 } else { 1477 throwException("Unexpected element: "+parser.getLocalName()); 1478 } 1479 } else if (event == XMLStreamConstants.END_ELEMENT) { 1480 break; 1481 } 1482 } 1483 return Collections.unmodifiableList(entries); 1484 } 1485 1486 private Map<String, String> parseMap() throws XMLStreamException { 1487 Map<String, String> map = new LinkedHashMap<>(); 1488 while (true) { 1489 int event = parser.next(); 1490 if (event == XMLStreamConstants.START_ELEMENT) { 1491 if ("tag".equals(parser.getLocalName())) { 1492 map.put(parser.getAttributeValue(null, "key"), parser.getAttributeValue(null, "value")); 1493 jumpToEnd(); 1494 } else { 1495 throwException("Unexpected element: "+parser.getLocalName()); 1496 } 1497 } else if (event == XMLStreamConstants.END_ELEMENT) { 1498 break; 1499 } 1500 } 1501 return Collections.unmodifiableMap(map); 1502 } 1503 1504 protected void throwException(String msg) { 1505 throw new RuntimeException(msg + tr(" (at line {0}, column {1})", 1506 parser.getLocation().getLineNumber(), parser.getLocation().getColumnNumber())); 1507 } 1327 1508 1328 1509 1329 private class SettingToXml implements SettingVisitor { … … 1579 1399 1580 1400 public String toXML(boolean nopass) { 1401 return toXML(settingsMap, nopass); 1402 } 1403 1404 public String toXML(Map<String, Setting<?>> settings, boolean nopass) { 1581 1405 StringBuilder b = new StringBuilder( 1582 1406 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<preferences xmlns=\"") … … 1584 1408 .append(Version.getInstance().getVersion()).append("\">\n"); 1585 1409 SettingToXml toXml = new SettingToXml(b, nopass); 1586 for (Entry<String, Setting<?>> e : settings Map.entrySet()) {1410 for (Entry<String, Setting<?>> e : settings.entrySet()) { 1587 1411 toXml.setKey(e.getKey()); 1588 1412 e.getValue().visit(toXml); … … 1597 1421 * see something with an expiry date in the past, remove it from the list. 1598 1422 */ 1599 p ublic void removeObsolete() {1423 private void removeObsolete(int loadedVersion) { 1600 1424 // drop this block march 2016 1601 1425 // update old style JOSM server links to use zip now, see #10581, #12189
Note:
See TracChangeset
for help on using the changeset viewer.