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

Last change on this file since 7053 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

File size: 6.9 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 public 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 if(!e.getKey().equals(k)) {
83 boolean drop = k.isEmpty() || v.isEmpty();
84 if(drop || !keys.containsKey(k)) {
85 newKeys.remove(e.getKey());
86 if(!drop)
87 newKeys.put(k, v);
88 }
89 } else if(!e.getValue().equals(v)) {
90 if(v.isEmpty())
91 newKeys.remove(k);
92 else
93 newKeys.put(k, v);
94 }
95 }
96 boolean changed = !keys.equals(newKeys);
97 if (changed) {
98 keys.clear();
99 keys.putAll(newKeys);
100 }
101 return changed;
102 }
103 }
104
105 /**
106 * Data fix to cleanup wrong spelled keys
107 */
108 public static class FixDataKey implements FixData {
109 /** key of wrong data */
110 String oldKey;
111 /** key of correct data */
112 String newKey;
113
114 /**
115 * Setup key check for wrong spelled keys
116 *
117 * @param oldKey wrong spelled key
118 * @param newKey correct replacement
119 */
120 public FixDataKey(String oldKey, String newKey) {
121 this.oldKey = oldKey;
122 this.newKey = newKey;
123 }
124
125 @Override
126 public boolean fixKeys(Map<String, String> keys, OsmPrimitive osm) {
127 if(keys.containsKey(oldKey) && !keys.containsKey(newKey)) {
128 keys.put(newKey, keys.get(oldKey));
129 keys.remove(oldKey);
130 return true;
131 }
132 return false;
133 }
134 }
135
136 /**
137 * Data fix to cleanup wrong spelled tags
138 */
139 public static class FixDataTag implements FixData {
140 /** key of wrong data */
141 String oldKey;
142 /** value of wrong data */
143 String oldValue;
144 /** key of correct data */
145 String newKey;
146 /** value of correct data */
147 String newValue;
148
149 /**
150 * Setup key check for wrong spelled keys
151 *
152 * @param oldKey wrong or old key
153 * @param oldValue wrong or old value
154 * @param newKey correct key replacement
155 * @param newValue correct value replacement
156 */
157 public FixDataTag(String oldKey, String oldValue, String newKey, String newValue) {
158 this.oldKey = oldKey;
159 this.oldValue = oldValue;
160 this.newKey = newKey;
161 this.newValue = newValue;
162 }
163
164 @Override
165 public boolean fixKeys(Map<String, String> keys, OsmPrimitive osm) {
166 if(oldValue.equals(keys.get(oldKey)) && (newKey.equals(oldKey) || !keys.containsKey(newKey))) {
167 keys.put(newKey, newValue);
168 if(!newKey.equals(oldKey))
169 keys.remove(oldKey);
170 return true;
171 }
172 return false;
173 }
174 }
175
176 /**
177 * Checks the upload for deprecated or wrong tags.
178 * @param apiDataSet the data to upload
179 */
180 @Override
181 public boolean checkUpload(APIDataSet apiDataSet) {
182 if(!Main.pref.getBoolean("fix.data.on.upload", true))
183 return true;
184
185 List<OsmPrimitive> objectsToUpload = apiDataSet.getPrimitives();
186 Collection<Command> cmds = new LinkedList<>();
187
188 for (OsmPrimitive osm : objectsToUpload) {
189 Map<String, String> keys = osm.getKeys();
190 if(!keys.isEmpty()) {
191 boolean modified = false;
192 for (FixData fix : deprecated) {
193 if(fix.fixKeys(keys, osm))
194 modified = true;
195 }
196 if(modified)
197 cmds.add(new ChangePropertyCommand(Collections.singleton(osm), new HashMap<>(keys)));
198 }
199 }
200
201 if(!cmds.isEmpty())
202 Main.main.undoRedo.add(new SequenceCommand(tr("Fix deprecated tags"), cmds));
203 return true;
204 }
205}
Note: See TracBrowser for help on using the repository browser.