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.
|
---|
15 | * @since 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
|
---|
26 | * @param defaults true, if default values are converted to XML, false for regular preferences
|
---|
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) {
|
---|
40 | out.write(String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n"));
|
---|
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 | }
|
---|
46 | out.write(String.format(" version='%d'>%n", Version.getInstance().getVersion()));
|
---|
47 | for (Map.Entry<String, Setting<?>> e : settings) {
|
---|
48 | setKey(e.getKey());
|
---|
49 | e.getValue().visit(this);
|
---|
50 | }
|
---|
51 | out.write(String.format("</%s>%n", rootElement));
|
---|
52 | }
|
---|
53 |
|
---|
54 | private void setKey(String key) {
|
---|
55 | this.key = key;
|
---|
56 | }
|
---|
57 |
|
---|
58 | private void addTime(Setting<?> setting) {
|
---|
59 | if (defaults) {
|
---|
60 | Long time = setting.getTime();
|
---|
61 | if (time == null)
|
---|
62 | throw new IllegalStateException();
|
---|
63 | out.write("' time='" + time);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | private void addDefaults() {
|
---|
68 | out.write(String.format("' xsi:nil='true'/>%n"));
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public void visit(StringSetting setting) {
|
---|
73 | if (noPassword && "osm-server.password".equals(key))
|
---|
74 | return; // do not store plain password.
|
---|
75 | out.write(" <tag key='" + XmlWriter.encode(key));
|
---|
76 | addTime(setting);
|
---|
77 | if (setting.getValue() != null) {
|
---|
78 | out.write(String.format("' value='%s'/>%n", XmlWriter.encode(setting.getValue())));
|
---|
79 | } else if (defaults) {
|
---|
80 | addDefaults();
|
---|
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) {
|
---|
91 | out.write(String.format("'>%n"));
|
---|
92 | for (String s : setting.getValue()) {
|
---|
93 | out.write(String.format(" <entry value='%s'/>%n", XmlWriter.encode(s)));
|
---|
94 | }
|
---|
95 | out.write(String.format(" </list>%n"));
|
---|
96 | } else if (defaults) {
|
---|
97 | addDefaults();
|
---|
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) {
|
---|
108 | out.write(String.format("'>%n"));
|
---|
109 | for (List<String> list : setting.getValue()) {
|
---|
110 | out.write(String.format(" <list>%n"));
|
---|
111 | for (String s : list) {
|
---|
112 | out.write(String.format(" <entry value='%s'/>%n", encode(s)));
|
---|
113 | }
|
---|
114 | out.write(String.format(" </list>%n"));
|
---|
115 | }
|
---|
116 | out.write(String.format(" </lists>%n"));
|
---|
117 | } else if (defaults) {
|
---|
118 | addDefaults();
|
---|
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) {
|
---|
129 | out.write(String.format("'>%n"));
|
---|
130 | for (Map<String, String> struct : setting.getValue()) {
|
---|
131 | out.write(String.format(" <map>%n"));
|
---|
132 | for (Map.Entry<String, String> e : struct.entrySet()) {
|
---|
133 | out.write(String.format(" <tag key='%s' value='%s'/>%n", encode(e.getKey()), encode(e.getValue())));
|
---|
134 | }
|
---|
135 | out.write(String.format(" </map>%n"));
|
---|
136 | }
|
---|
137 | out.write(String.format(" </maps>%n"));
|
---|
138 | } else if (defaults) {
|
---|
139 | addDefaults();
|
---|
140 | } else {
|
---|
141 | throw new NullPointerException();
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|