source: josm/trunk/src/org/openstreetmap/josm/actions/upload/FixDataHook.java@ 8512

Last change on this file since 8512 was 8512, checked in by Don-vip, 9 years ago

checkstyle: redundant modifiers

  • Property svn:eol-style set to native
File size: 7.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.upload;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7import java.util.Collections;
8import java.util.HashMap;
9import java.util.LinkedList;
10import java.util.List;
11import java.util.Map;
12import java.util.Map.Entry;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.command.ChangePropertyCommand;
16import org.openstreetmap.josm.command.Command;
17import org.openstreetmap.josm.command.SequenceCommand;
18import org.openstreetmap.josm.data.APIDataSet;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.Relation;
21import org.openstreetmap.josm.data.osm.Tag;
22
23/**
24 * Fixes defective data entries for all modified objects before upload
25 * @since 5621
26 */
27public class FixDataHook implements UploadHook {
28
29 /**
30 * List of checks to run on data
31 */
32 private List<FixData> deprecated = new LinkedList<>();
33
34 /**
35 * Constructor for data initialization
36 */
37 public FixDataHook() {
38 deprecated.add(new FixDataSpace());
39 deprecated.add(new FixDataKey("color", "colour"));
40 deprecated.add(new FixDataTag("highway", "ford", "ford", "yes"));
41 deprecated.add(new FixDataTag("oneway", "false", "oneway", "no"));
42 deprecated.add(new FixDataTag("oneway", "0", "oneway", "no"));
43 deprecated.add(new FixDataTag("oneway", "true", "oneway", "yes"));
44 deprecated.add(new FixDataTag("oneway", "1", "oneway", "yes"));
45 deprecated.add(new FixDataTag("highway", "stile", "barrier", "stile"));
46 deprecated.add(new FixData() {
47 @Override
48 public boolean fixKeys(Map<String, String> keys, OsmPrimitive osm) {
49 if (osm instanceof Relation && "multipolygon".equals(keys.get("type")) && "administrative".equals(keys.get("boundary"))) {
50 keys.put("type", "boundary");
51 return true;
52 }
53 return false;
54 }
55 });
56 }
57
58 /**
59 * Common set of commands for data fixing
60 */
61 public interface FixData {
62 /**
63 * Checks if data needs to be fixed and change keys
64 *
65 * @param keys list of keys to be modified
66 * @param osm the object for type validation, don't use keys of it!
67 * @return <code>true</code> if keys have been modified
68 */
69 boolean fixKeys(Map<String, String> keys, OsmPrimitive osm);
70 }
71
72 /**
73 * Data fix to remove spaces at begin or end of tags
74 */
75 public static class FixDataSpace implements FixData {
76 @Override
77 public boolean fixKeys(Map<String, String> keys, OsmPrimitive osm) {
78 Map<String, String> newKeys = new HashMap<>(keys);
79 for (Entry<String, String> e : keys.entrySet()) {
80 String v = Tag.removeWhiteSpaces(e.getValue());
81 String k = Tag.removeWhiteSpaces(e.getKey());
82 boolean drop = k.isEmpty() || v.isEmpty();
83 if (!e.getKey().equals(k)) {
84 if (drop || !keys.containsKey(k)) {
85 newKeys.put(e.getKey(), null);
86 if (!drop)
87 newKeys.put(k, v);
88 }
89 } else if (!e.getValue().equals(v)) {
90 newKeys.put(k, v.isEmpty() ? null : v);
91 } else if (drop) {
92 newKeys.put(e.getKey(), null);
93 }
94 }
95 boolean changed = !keys.equals(newKeys);
96 if (changed) {
97 keys.clear();
98 keys.putAll(newKeys);
99 }
100 return changed;
101 }
102 }
103
104 /**
105 * Data fix to cleanup wrong spelled keys
106 */
107 public static class FixDataKey implements FixData {
108 /** key of wrong data */
109 private String oldKey;
110 /** key of correct data */
111 private String newKey;
112
113 /**
114 * Setup key check for wrong spelled keys
115 *
116 * @param oldKey wrong spelled key
117 * @param newKey correct replacement
118 */
119 public FixDataKey(String oldKey, String newKey) {
120 this.oldKey = oldKey;
121 this.newKey = newKey;
122 }
123
124 @Override
125 public boolean fixKeys(Map<String, String> keys, OsmPrimitive osm) {
126 if (keys.containsKey(oldKey) && !keys.containsKey(newKey)) {
127 keys.put(newKey, keys.get(oldKey));
128 keys.put(oldKey, null);
129 return true;
130 } else if (keys.containsKey(oldKey) && keys.containsKey(newKey) && keys.get(oldKey).equals(keys.get(newKey))) {
131 keys.put(oldKey, null);
132 return true;
133 }
134 return false;
135 }
136 }
137
138 /**
139 * Data fix to cleanup wrong spelled tags
140 */
141 public static class FixDataTag implements FixData {
142 /** key of wrong data */
143 private String oldKey;
144 /** value of wrong data */
145 private String oldValue;
146 /** key of correct data */
147 private String newKey;
148 /** value of correct data */
149 private String newValue;
150
151 /**
152 * Setup key check for wrong spelled keys
153 *
154 * @param oldKey wrong or old key
155 * @param oldValue wrong or old value
156 * @param newKey correct key replacement
157 * @param newValue correct value replacement
158 */
159 public FixDataTag(String oldKey, String oldValue, String newKey, String newValue) {
160 this.oldKey = oldKey;
161 this.oldValue = oldValue;
162 this.newKey = newKey;
163 this.newValue = newValue;
164 }
165
166 @Override
167 public boolean fixKeys(Map<String, String> keys, OsmPrimitive osm) {
168 if (oldValue.equals(keys.get(oldKey)) && (newKey.equals(oldKey)
169 || !keys.containsKey(newKey) || keys.get(newKey).equals(newValue))) {
170 keys.put(newKey, newValue);
171 if (!newKey.equals(oldKey))
172 keys.put(oldKey, null);
173 return true;
174 }
175 return false;
176 }
177 }
178
179 /**
180 * Checks the upload for deprecated or wrong tags.
181 * @param apiDataSet the data to upload
182 */
183 @Override
184 public boolean checkUpload(APIDataSet apiDataSet) {
185 if (!Main.pref.getBoolean("fix.data.on.upload", true))
186 return true;
187
188 List<OsmPrimitive> objectsToUpload = apiDataSet.getPrimitives();
189 Collection<Command> cmds = new LinkedList<>();
190
191 for (OsmPrimitive osm : objectsToUpload) {
192 Map<String, String> keys = new HashMap<>(osm.getKeys());
193 if (!keys.isEmpty()) {
194 boolean modified = false;
195 for (FixData fix : deprecated) {
196 if (fix.fixKeys(keys, osm))
197 modified = true;
198 }
199 if (modified)
200 cmds.add(new ChangePropertyCommand(Collections.singleton(osm), keys));
201 }
202 }
203
204 if (!cmds.isEmpty())
205 Main.main.undoRedo.add(new SequenceCommand(tr("Fix deprecated tags"), cmds));
206 return true;
207 }
208}
Note: See TracBrowser for help on using the repository browser.