Changeset 9823 in josm for trunk/src/org/openstreetmap/josm/data
- Timestamp:
- 2016-02-18T17:39:58+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r9822 r9823 58 58 import org.openstreetmap.josm.data.preferences.MapListSetting; 59 59 import org.openstreetmap.josm.data.preferences.PreferencesReader; 60 import org.openstreetmap.josm.data.preferences.PreferencesWriter; 60 61 import org.openstreetmap.josm.data.preferences.Setting; 61 62 import org.openstreetmap.josm.data.preferences.SettingVisitor; … … 538 539 try (PrintWriter out = new PrintWriter(new OutputStreamWriter( 539 540 new FileOutputStream(prefFile + "_tmp"), StandardCharsets.UTF_8), false)) { 540 out.print(toXML(settings, false, defaults)); 541 PreferencesWriter writer = new PreferencesWriter(out, false, defaults); 542 writer.write(settings); 541 543 } 542 544 … … 576 578 File pref = getPreferenceFile(); 577 579 PreferencesReader.validateXML(pref); 578 PreferencesReader reader = new PreferencesReader( false);579 reader. fromXML(pref);580 PreferencesReader reader = new PreferencesReader(pref, false); 581 reader.parse(); 580 582 settingsMap.clear(); 581 583 settingsMap.putAll(reader.getSettings()); … … 596 598 File def = getDefaultsCacheFile(); 597 599 PreferencesReader.validateXML(def); 598 PreferencesReader reader = new PreferencesReader( true);599 reader. fromXML(def);600 PreferencesReader reader = new PreferencesReader(def, true); 601 reader.parse(); 600 602 defaultsMap.clear(); 601 603 long minTime = System.currentTimeMillis() / 1000 - MAX_AGE_DEFAULT_PREFERENCES; … … 611 613 * @param in XML reader 612 614 * @throws XMLStreamException if any XML stream error occurs 613 */ 614 public void fromXML(Reader in) throws XMLStreamException { 615 PreferencesReader reader = new PreferencesReader(false); 616 reader.fromXML(in); 615 * @throws IOException if any I/O error occurs 616 */ 617 public void fromXML(Reader in) throws XMLStreamException, IOException { 618 PreferencesReader reader = new PreferencesReader(in, false); 619 reader.parse(); 617 620 settingsMap.clear(); 618 621 settingsMap.putAll(reader.getSettings()); … … 1399 1402 } 1400 1403 1401 private static class SettingToXml implements SettingVisitor {1402 private final StringBuilder b;1403 private final boolean noPassword;1404 private final boolean defaults;1405 private String key;1406 1407 SettingToXml(StringBuilder b, boolean noPassword, boolean defaults) {1408 this.b = b;1409 this.noPassword = noPassword;1410 this.defaults = defaults;1411 }1412 1413 public void setKey(String key) {1414 this.key = key;1415 }1416 1417 private void addTime(Setting setting) {1418 if (defaults) {1419 Long time = setting.getTime();1420 if (time == null) throw new IllegalStateException();1421 b.append("' time='").append(time);1422 }1423 }1424 1425 @Override1426 public void visit(StringSetting setting) {1427 if (noPassword && "osm-server.password".equals(key))1428 return; // do not store plain password.1429 b.append(" <tag key='");1430 b.append(XmlWriter.encode(key));1431 addTime(setting);1432 if (setting.getValue() != null) {1433 b.append("' value='");1434 b.append(XmlWriter.encode(setting.getValue()));1435 b.append("'/>\n");1436 } else if (defaults) {1437 b.append("' xsi:nil='true'/>\n");1438 } else {1439 throw new NullPointerException();1440 }1441 }1442 1443 @Override1444 public void visit(ListSetting setting) {1445 b.append(" <list key='").append(XmlWriter.encode(key));1446 addTime(setting);1447 if (setting.getValue() != null) {1448 b.append("'>\n");1449 for (String s : setting.getValue()) {1450 b.append(" <entry value='").append(XmlWriter.encode(s)).append("'/>\n");1451 }1452 b.append(" </list>\n");1453 } else if (defaults) {1454 b.append("' xsi:nil='true'/>\n");1455 } else {1456 throw new NullPointerException();1457 }1458 }1459 1460 @Override1461 public void visit(ListListSetting setting) {1462 b.append(" <lists key='").append(XmlWriter.encode(key));1463 addTime(setting);1464 if (setting.getValue() != null) {1465 b.append("'>\n");1466 for (List<String> list : setting.getValue()) {1467 b.append(" <list>\n");1468 for (String s : list) {1469 b.append(" <entry value='").append(XmlWriter.encode(s)).append("'/>\n");1470 }1471 b.append(" </list>\n");1472 }1473 b.append(" </lists>\n");1474 } else if (defaults) {1475 b.append("' xsi:nil='true'/>\n");1476 } else {1477 throw new NullPointerException();1478 }1479 }1480 1481 @Override1482 public void visit(MapListSetting setting) {1483 b.append(" <maps key='").append(XmlWriter.encode(key));1484 addTime(setting);1485 if (setting.getValue() != null) {1486 b.append("'>\n");1487 for (Map<String, String> struct : setting.getValue()) {1488 b.append(" <map>\n");1489 for (Entry<String, String> e : struct.entrySet()) {1490 b.append(" <tag key='").append(XmlWriter.encode(e.getKey()))1491 .append("' value='").append(XmlWriter.encode(e.getValue())).append("'/>\n");1492 }1493 b.append(" </map>\n");1494 }1495 b.append(" </maps>\n");1496 } else if (defaults) {1497 b.append("' xsi:nil='true'/>\n");1498 } else {1499 throw new NullPointerException();1500 }1501 }1502 }1503 1504 1404 /** 1505 1405 * Returns XML describing these preferences. … … 1520 1420 */ 1521 1421 public String toXML(Collection<Entry<String, Setting<?>>> settings, boolean nopass, boolean defaults) { 1522 String rootElement = defaults ? "preferences-defaults" : "preferences"; 1523 StringBuilder b = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<") 1524 .append(rootElement).append(" xmlns='") 1525 .append(Main.getXMLBase()).append("/preferences-1.0'"); 1526 if (defaults) { 1527 b.append(" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"); 1528 } 1529 b.append(" version='").append(Version.getInstance().getVersion()).append("'>\n"); 1530 SettingToXml toXml = new SettingToXml(b, nopass, defaults); 1531 for (Entry<String, Setting<?>> e : settings) { 1532 toXml.setKey(e.getKey()); 1533 e.getValue().visit(toXml); 1534 } 1535 b.append("</").append(rootElement).append(">\n"); 1536 return b.toString(); 1422 StringWriter sw = new StringWriter(); 1423 PrintWriter pw = new PrintWriter(sw); 1424 PreferencesWriter prefWriter = new PreferencesWriter(pw, nopass, defaults); 1425 prefWriter.write(settings); 1426 sw.flush(); 1427 StringBuffer sb = sw.getBuffer(); 1428 return sb.toString(); 1537 1429 } 1538 1430 -
trunk/src/org/openstreetmap/josm/data/preferences/PreferencesReader.java
r9822 r9823 41 41 42 42 private final SortedMap<String, Setting<?>> settings = new TreeMap<>(); 43 private XMLStreamReader parser; 43 44 private int version = 0; 44 private XMLStreamReader parser; 45 private Reader reader; 46 private File file; 45 47 46 48 private final boolean defaults; … … 48 50 /** 49 51 * Constructs a new {@code PreferencesReader}. 52 * @param file the file 50 53 * @param defaults true when reading from the cache file for default preferences, 51 54 * false for the regular preferences config file 52 */ 53 public PreferencesReader(boolean defaults) { 55 * @throws IOException if any I/O error occurs 56 * @throws XMLStreamException if any XML stream error occurs 57 */ 58 public PreferencesReader(File file, boolean defaults) throws IOException, XMLStreamException { 54 59 this.defaults = defaults; 60 this.reader = null; 61 this.file = file; 62 } 63 64 /** 65 * Constructs a new {@code PreferencesReader}. 66 * @param reader the {@link Reader} 67 * @param defaults true when reading from the cache file for default preferences, 68 * false for the regular preferences config file 69 * @throws XMLStreamException if any XML stream error occurs 70 */ 71 public PreferencesReader(Reader reader, boolean defaults) throws XMLStreamException { 72 this.defaults = defaults; 73 this.reader = reader; 74 this.file = null; 55 75 } 56 76 … … 82 102 83 103 /** 84 * Parse preferences XML.85 * @param f the file86 * @throws IOException if any I/O error occurs87 * @throws XMLStreamException if any XML stream error occurs88 */89 public void fromXML(File f) throws IOException, XMLStreamException {90 try (BufferedReader in = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8)) {91 fromXML(in);92 }93 }94 95 /**96 * Parse preferences XML.97 * @param in the {@link Reader}98 * @throws XMLStreamException if any XML stream error occurs99 */100 public void fromXML(Reader in) throws XMLStreamException {101 this.parser = XMLInputFactory.newInstance().createXMLStreamReader(in);102 parse();103 }104 105 /**106 104 * Return the parsed preferences as a settings map 107 105 * @return the parsed preferences as a settings map … … 120 118 } 121 119 122 private void parse() throws XMLStreamException { 120 121 public void parse() throws XMLStreamException, IOException { 122 if (reader != null) { 123 this.parser = XMLInputFactory.newInstance().createXMLStreamReader(reader); 124 doParse(); 125 } else { 126 try (BufferedReader in = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) { 127 this.parser = XMLInputFactory.newInstance().createXMLStreamReader(in); 128 doParse(); 129 } 130 } 131 } 132 133 private void doParse() throws XMLStreamException { 123 134 int event = parser.getEventType(); 124 135 while (true) {
Note:
See TracChangeset
for help on using the changeset viewer.