source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/DeprecatedTags.java@ 6346

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

fix #9076, fix #9213:

  • Presets: add internet_access preset, remove useless default board value for board_type (modified patch by skyper)
  • Validator: deprecates board_type=board
File size: 9.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7import java.util.LinkedList;
8import java.util.List;
9
10import org.openstreetmap.josm.command.ChangePropertyCommand;
11import org.openstreetmap.josm.command.ChangePropertyKeyCommand;
12import org.openstreetmap.josm.command.Command;
13import org.openstreetmap.josm.command.SequenceCommand;
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.data.osm.Tag;
18import org.openstreetmap.josm.data.osm.Way;
19import org.openstreetmap.josm.data.validation.Severity;
20import org.openstreetmap.josm.data.validation.Test;
21import org.openstreetmap.josm.data.validation.TestError;
22import org.openstreetmap.josm.tools.Utils;
23
24/**
25 * Checks and corrects deprecated tags.
26 * @since 4442
27 */
28public class DeprecatedTags extends Test {
29
30 private List<DeprecationCheck> checks = new LinkedList<DeprecationCheck>();
31
32 /**
33 * Constructs a new {@code DeprecatedTags} test.
34 */
35 public DeprecatedTags() {
36 super(tr("Deprecated Tags"), tr("Checks and corrects deprecated tags."));
37 checks.add(new DeprecationCheck(2101).
38 testAndRemove("barrier", "wire_fence").
39 add("barrier", "fence").
40 add("fence_type", "chain_link"));
41 checks.add(new DeprecationCheck(2102).
42 testAndRemove("barrier", "wood_fence").
43 add("barrier", "fence").
44 add("fence_type", "wood"));
45 checks.add(new DeprecationCheck(2103).
46 testAndRemove("highway", "ford").
47 add("ford", "yes"));
48 // from http://wiki.openstreetmap.org/wiki/Deprecated_features
49 checks.add(new DeprecationCheck(2104).
50 test("class").
51 alternative("highway"));
52 checks.add(new DeprecationCheck(2105).
53 testAndRemove("highway", "stile").
54 add("barrier", "stile"));
55 checks.add(new DeprecationCheck(2106).
56 testAndRemove("highway", "incline").
57 add("highway", "road").
58 add("incline", "up"));
59 checks.add(new DeprecationCheck(2107).
60 testAndRemove("highway", "incline_steep").
61 add("highway", "road").
62 add("incline", "up"));
63 checks.add(new DeprecationCheck(2108).
64 testAndRemove("highway", "unsurfaced").
65 add("highway", "road").
66 add("incline", "unpaved"));
67 checks.add(new DeprecationCheck(2109).
68 test("landuse", "wood").
69 alternative("landuse", "forest").
70 alternative("natural", "wood"));
71 checks.add(new DeprecationCheck(2110).
72 testAndRemove("natural", "marsh").
73 add("natural", "wetland").
74 add("wetland", "marsh"));
75 checks.add(new DeprecationCheck(2111).
76 test("highway", "byway"));
77 checks.add(new DeprecationCheck(2112).
78 test("power_source").
79 alternative("generator:source"));
80 checks.add(new DeprecationCheck(2113).
81 test("power_rating").
82 alternative("generator:output"));
83 // from http://wiki.openstreetmap.org/wiki/Tag:shop=organic
84 checks.add(new DeprecationCheck(2114).
85 testAndRemove("shop", "organic").
86 add("shop", "supermarket").
87 add("organic", "only"));
88 // from http://wiki.openstreetmap.org/wiki/Key:bicycle_parking
89 checks.add(new DeprecationCheck(2115).
90 testAndRemove("bicycle_parking", "sheffield").
91 add("bicycle_parking", "stands"));
92 // http://wiki.openstreetmap.org/wiki/Tag:emergency=phone
93 checks.add(new DeprecationCheck(2116).
94 testAndRemove("amenity", "emergency_phone").
95 add("emergency", "phone"));
96 // http://wiki.openstreetmap.org/wiki/Tag:sport=gaelic_football
97 // fix #8132
98 checks.add(new DeprecationCheck(2117).
99 testAndRemove("sport", "gaelic_football").
100 add("sport", "gaelic_games"));
101 // http://wiki.openstreetmap.org/wiki/Tag:power=station
102 // see #8847 / #8961
103 checks.add(new DeprecationCheck(2118).
104 test("power", "station").
105 alternative("power", "plant").
106 alternative("power", "sub_station"));
107 checks.add(new DeprecationCheck(2119).
108 testAndRemove("generator:method", "dam").
109 add("generator:method", "water-storage"));
110 checks.add(new DeprecationCheck(2120).
111 testAndRemove("generator:method", "pumped-storage").
112 add("generator:method", "water-pumped-storage"));
113 checks.add(new DeprecationCheck(2121).
114 testAndRemove("generator:method", "pumping").
115 add("generator:method", "water-pumped-storage"));
116 // http://wiki.openstreetmap.org/wiki/Key:fence_type
117 // see #8962
118 checks.add(new DeprecationCheck(2122).
119 test("fence_type", "chain").
120 alternative("barrier", "chain").
121 alternative("fence_type", "chain_link"));
122 // http://wiki.openstreetmap.org/wiki/Key:entrance
123 // see #9000
124 checks.add(new DeprecationCheck(2123).
125 test("building", "entrance").
126 alternative("entrance"));
127 // Useless tag proposed in internal preset for years
128 // see #9213
129 checks.add(new DeprecationCheck(2124).
130 testAndRemove("board_type", "board"));
131 }
132
133 /**
134 * Visiting call for primitives.
135 * @param p The primitive to inspect.
136 */
137 public void visit(OsmPrimitive p) {
138 for (DeprecationCheck check : checks) {
139 if (check.matchesPrimitive(p)) {
140 errors.add(new DeprecationError(p, check));
141 }
142 }
143 }
144
145 @Override
146 public void visit(Node n) {
147 visit((OsmPrimitive) n);
148 }
149
150 @Override
151 public void visit(Way w) {
152 visit((OsmPrimitive) w);
153 }
154
155 @Override
156 public void visit(Relation r) {
157 visit((OsmPrimitive) r);
158 }
159
160 private static class DeprecationCheck {
161
162 private int code;
163 private final List<Tag> test = new LinkedList<Tag>();
164 private final List<Tag> change = new LinkedList<Tag>();
165 private final List<Tag> alternatives = new LinkedList<Tag>();
166
167 public DeprecationCheck(int code) {
168 this.code = code;
169 }
170
171 DeprecationCheck test(String key, String value) {
172 test.add(new Tag(key, value));
173 return this;
174 }
175
176 DeprecationCheck test(String key) {
177 return test(key, null);
178 }
179
180 DeprecationCheck add(String key, String value) {
181 change.add(new Tag(key, value));
182 return this;
183 }
184
185 DeprecationCheck remove(String key) {
186 change.add(new Tag(key));
187 return this;
188 }
189
190 DeprecationCheck testAndRemove(String key, String value) {
191 return test(key, value).remove(key);
192 }
193
194 DeprecationCheck alternative(String key, String value) {
195 alternatives.add(new Tag(key, value));
196 return this;
197 }
198
199 DeprecationCheck alternative(String key) {
200 return alternative(key, null);
201 }
202
203 boolean matchesPrimitive(OsmPrimitive p) {
204 for (Tag tag : test) {
205 String key = tag.getKey();
206 String value = tag.getValue();
207 if (value.isEmpty() && !p.hasKey(key))
208 return false;
209 if (!value.isEmpty() && !value.equals(p.get(key)))
210 return false;
211 }
212 return true;
213 }
214
215 Command fixPrimitive(OsmPrimitive p) {
216 Collection<Command> cmds = new LinkedList<Command>();
217 for (Tag tag : change) {
218 cmds.add(new ChangePropertyCommand(p, tag.getKey(), tag.getValue()));
219 }
220 if (test.size() == 1 && alternatives.size() == 1) {
221 cmds.add(new ChangePropertyKeyCommand(p, test.get(0).getKey(), alternatives.get(0).getKey()));
222 }
223 return new SequenceCommand(tr("Deprecation fix of {0}", Utils.join(", ", test)), cmds);
224 }
225
226 String getDescription() {
227 if (alternatives.isEmpty())
228 return tr("{0} is deprecated", Utils.join(", ", test));
229 else
230 return tr("{0} is deprecated, use {1} instead", Utils.join(", ", test), Utils.join(tr(" or "), alternatives));
231 }
232 }
233
234 private class DeprecationError extends TestError {
235
236 private OsmPrimitive p;
237 private DeprecationCheck check;
238
239 public DeprecationError(OsmPrimitive p, DeprecationCheck check) {
240 super(DeprecatedTags.this, Severity.WARNING, check.getDescription(), check.code, p);
241 this.p = p;
242 this.check = check;
243 }
244
245 @Override
246 public boolean isFixable() {
247 return !check.change.isEmpty() || (check.test.size() == 1 && check.alternatives.size() == 1);
248 }
249
250 @Override
251 public Command getFix() {
252 return check.fixPrimitive(p);
253 }
254 }
255}
Note: See TracBrowser for help on using the repository browser.