[9823] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data.preferences;
|
---|
| 3 |
|
---|
| 4 | import java.io.PrintWriter;
|
---|
| 5 | import java.util.Collection;
|
---|
| 6 | import java.util.List;
|
---|
| 7 | import java.util.Map;
|
---|
| 8 |
|
---|
| 9 | import org.openstreetmap.josm.Main;
|
---|
| 10 | import org.openstreetmap.josm.data.Version;
|
---|
| 11 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * Write preferences to XML.
|
---|
[9828] | 15 | * @since 9823
|
---|
[9823] | 16 | */
|
---|
| 17 | public class PreferencesWriter extends XmlWriter implements SettingVisitor {
|
---|
| 18 | private final boolean noPassword;
|
---|
| 19 | private final boolean defaults;
|
---|
| 20 | private String key;
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Construct new {@code PreferencesWriter}.
|
---|
| 24 | * @param out the {@link PrintWriter}
|
---|
| 25 | * @param noPassword if password must be excluded
|
---|
[9828] | 26 | * @param defaults true, if default values are converted to XML, false for regular preferences
|
---|
[9823] | 27 | */
|
---|
| 28 | public PreferencesWriter(PrintWriter out, boolean noPassword, boolean defaults) {
|
---|
| 29 | super(out);
|
---|
| 30 | this.noPassword = noPassword;
|
---|
| 31 | this.defaults = defaults;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Write preferences.
|
---|
| 36 | *
|
---|
| 37 | * @param settings preferences settings to write
|
---|
| 38 | */
|
---|
| 39 | public void write(Collection<Map.Entry<String, Setting<?>>> settings) {
|
---|
[9828] | 40 | out.write(String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n"));
|
---|
[9823] | 41 | String rootElement = defaults ? "preferences-defaults" : "preferences";
|
---|
| 42 | out.write(String.format("<%s xmlns='%s/preferences-1.0'", rootElement, Main.getXMLBase()));
|
---|
| 43 | if (defaults) {
|
---|
| 44 | out.write(" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'");
|
---|
| 45 | }
|
---|
[9828] | 46 | out.write(String.format(" version='%d'>%n", Version.getInstance().getVersion()));
|
---|
[9823] | 47 | for (Map.Entry<String, Setting<?>> e : settings) {
|
---|
| 48 | setKey(e.getKey());
|
---|
| 49 | e.getValue().visit(this);
|
---|
| 50 | }
|
---|
[9828] | 51 | out.write(String.format("</%s>%n", rootElement));
|
---|
[9823] | 52 | }
|
---|
| 53 |
|
---|
| 54 | private void setKey(String key) {
|
---|
| 55 | this.key = key;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[9828] | 58 | private void addTime(Setting<?> setting) {
|
---|
[9823] | 59 | if (defaults) {
|
---|
| 60 | Long time = setting.getTime();
|
---|
[9828] | 61 | if (time == null)
|
---|
| 62 | throw new IllegalStateException();
|
---|
[9823] | 63 | out.write("' time='" + time);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[9828] | 67 | private void addDefaults() {
|
---|
| 68 | out.write(String.format("' xsi:nil='true'/>%n"));
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[9823] | 71 | @Override
|
---|
| 72 | public void visit(StringSetting setting) {
|
---|
| 73 | if (noPassword && "osm-server.password".equals(key))
|
---|
| 74 | return; // do not store plain password.
|
---|
[9828] | 75 | out.write(" <tag key='" + XmlWriter.encode(key));
|
---|
[9823] | 76 | addTime(setting);
|
---|
| 77 | if (setting.getValue() != null) {
|
---|
[9828] | 78 | out.write(String.format("' value='%s'/>%n", XmlWriter.encode(setting.getValue())));
|
---|
[9823] | 79 | } else if (defaults) {
|
---|
[9828] | 80 | addDefaults();
|
---|
[9823] | 81 | } else {
|
---|
| 82 | throw new NullPointerException();
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | @Override
|
---|
| 87 | public void visit(ListSetting setting) {
|
---|
| 88 | out.write(" <list key='" + XmlWriter.encode(key));
|
---|
| 89 | addTime(setting);
|
---|
| 90 | if (setting.getValue() != null) {
|
---|
[9828] | 91 | out.write(String.format("'>%n"));
|
---|
[9823] | 92 | for (String s : setting.getValue()) {
|
---|
[9828] | 93 | out.write(String.format(" <entry value='%s'/>%n", XmlWriter.encode(s)));
|
---|
[9823] | 94 | }
|
---|
[9828] | 95 | out.write(String.format(" </list>%n"));
|
---|
[9823] | 96 | } else if (defaults) {
|
---|
[9828] | 97 | addDefaults();
|
---|
[9823] | 98 | } else {
|
---|
| 99 | throw new NullPointerException();
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | @Override
|
---|
| 104 | public void visit(ListListSetting setting) {
|
---|
| 105 | out.write(" <lists key='" + XmlWriter.encode(key));
|
---|
| 106 | addTime(setting);
|
---|
| 107 | if (setting.getValue() != null) {
|
---|
[9828] | 108 | out.write(String.format("'>%n"));
|
---|
[9823] | 109 | for (List<String> list : setting.getValue()) {
|
---|
[9828] | 110 | out.write(String.format(" <list>%n"));
|
---|
[9823] | 111 | for (String s : list) {
|
---|
[9828] | 112 | out.write(String.format(" <entry value='%s'/>%n", encode(s)));
|
---|
[9823] | 113 | }
|
---|
[9828] | 114 | out.write(String.format(" </list>%n"));
|
---|
[9823] | 115 | }
|
---|
[9828] | 116 | out.write(String.format(" </lists>%n"));
|
---|
[9823] | 117 | } else if (defaults) {
|
---|
[9828] | 118 | addDefaults();
|
---|
[9823] | 119 | } else {
|
---|
| 120 | throw new NullPointerException();
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | @Override
|
---|
| 125 | public void visit(MapListSetting setting) {
|
---|
| 126 | out.write(" <maps key='" + encode(key));
|
---|
| 127 | addTime(setting);
|
---|
| 128 | if (setting.getValue() != null) {
|
---|
[9828] | 129 | out.write(String.format("'>%n"));
|
---|
[9823] | 130 | for (Map<String, String> struct : setting.getValue()) {
|
---|
[9828] | 131 | out.write(String.format(" <map>%n"));
|
---|
[9823] | 132 | for (Map.Entry<String, String> e : struct.entrySet()) {
|
---|
[9828] | 133 | out.write(String.format(" <tag key='%s' value='%s'/>%n", encode(e.getKey()), encode(e.getValue())));
|
---|
[9823] | 134 | }
|
---|
[9828] | 135 | out.write(String.format(" </map>%n"));
|
---|
[9823] | 136 | }
|
---|
[9828] | 137 | out.write(String.format(" </maps>%n"));
|
---|
[9823] | 138 | } else if (defaults) {
|
---|
[9828] | 139 | addDefaults();
|
---|
[9823] | 140 | } else {
|
---|
| 141 | throw new NullPointerException();
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|