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