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

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

fix #8459 - deprecate amenity=emergency_phone in favor of emergency=phone

File size: 7.1 KB
Line 
1package org.openstreetmap.josm.data.validation.tests;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.util.Collection;
6import java.util.LinkedList;
7import java.util.List;
8
9import org.openstreetmap.josm.command.ChangePropertyCommand;
10import org.openstreetmap.josm.command.Command;
11import org.openstreetmap.josm.command.SequenceCommand;
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.Relation;
15import org.openstreetmap.josm.data.osm.Tag;
16import org.openstreetmap.josm.data.osm.Way;
17import org.openstreetmap.josm.data.validation.Severity;
18import org.openstreetmap.josm.data.validation.Test;
19import org.openstreetmap.josm.data.validation.TestError;
20import org.openstreetmap.josm.tools.Utils;
21
22public class DeprecatedTags extends Test {
23
24 private List<DeprecationCheck> checks = new LinkedList<DeprecationCheck>();
25
26 public DeprecatedTags() {
27 super(tr("Deprecated Tags"), tr("Checks and corrects deprecated tags."));
28 checks.add(new DeprecationCheck(2101).
29 testAndRemove("barrier", "wire_fence").
30 add("barrier", "fence").
31 add("fence_type", "chain"));
32 checks.add(new DeprecationCheck(2102).
33 testAndRemove("barrier", "wood_fence").
34 add("barrier", "fence").
35 add("fence_type", "wood"));
36 checks.add(new DeprecationCheck(2103).
37 testAndRemove("highway", "ford").
38 add("ford", "yes"));
39 // from http://wiki.openstreetmap.org/wiki/Deprecated_features
40 checks.add(new DeprecationCheck(2104).
41 test("class").
42 alternative("highway"));
43 checks.add(new DeprecationCheck(2105).
44 testAndRemove("highway", "stile").
45 add("barrier", "stile"));
46 checks.add(new DeprecationCheck(2106).
47 testAndRemove("highway", "incline").
48 add("highway", "road").
49 add("incline", "up"));
50 checks.add(new DeprecationCheck(2107).
51 testAndRemove("highway", "incline_steep").
52 add("highway", "road").
53 add("incline", "up"));
54 checks.add(new DeprecationCheck(2108).
55 testAndRemove("highway", "unsurfaced").
56 add("highway", "road").
57 add("incline", "unpaved"));
58 checks.add(new DeprecationCheck(2109).
59 test("landuse", "wood").
60 alternative("landuse", "forest").
61 alternative("natural", "wood"));
62 checks.add(new DeprecationCheck(2110).
63 testAndRemove("natural", "marsh").
64 add("natural", "wetland").
65 add("wetland", "marsh"));
66 checks.add(new DeprecationCheck(2111).
67 test("highway", "byway"));
68 checks.add(new DeprecationCheck(2112).
69 test("power_source").
70 alternative("generator:source"));
71 checks.add(new DeprecationCheck(2113).
72 test("power_rating").
73 alternative("generator:output"));
74 // from http://wiki.openstreetmap.org/wiki/Tag:shop%3Dorganic
75 checks.add(new DeprecationCheck(2114).
76 testAndRemove("shop", "organic").
77 add("shop", "supermarket").
78 add("organic", "only"));
79 // from http://wiki.openstreetmap.org/wiki/Key:bicycle_parking
80 checks.add(new DeprecationCheck(2115).
81 testAndRemove("bicycle_parking", "sheffield").
82 add("bicycle_parking", "stands"));
83 // http://wiki.openstreetmap.org/wiki/Tag:emergency=phone
84 checks.add(new DeprecationCheck(2116).
85 testAndRemove("amenity", "emergency_phone").
86 add("emergency", "phone"));
87 }
88
89 public void visit(OsmPrimitive p) {
90 for (DeprecationCheck check : checks) {
91 if (check.matchesPrimitive(p)) {
92 errors.add(new DeprecationError(p, check));
93 }
94 }
95 }
96
97 @Override
98 public void visit(Node n) {
99 visit((OsmPrimitive) n);
100 }
101
102 @Override
103 public void visit(Way w) {
104 visit((OsmPrimitive) w);
105 }
106
107 @Override
108 public void visit(Relation r) {
109 visit((OsmPrimitive) r);
110 }
111
112 private static class DeprecationCheck {
113
114 int code;
115 List<Tag> test = new LinkedList<Tag>();
116 List<Tag> change = new LinkedList<Tag>();
117 List<Tag> alternatives = new LinkedList<Tag>();
118
119 public DeprecationCheck(int code) {
120 this.code = code;
121 }
122
123 DeprecationCheck test(String key, String value) {
124 test.add(new Tag(key, value));
125 return this;
126 }
127
128 DeprecationCheck test(String key) {
129 return test(key, null);
130 }
131
132 DeprecationCheck add(String key, String value) {
133 change.add(new Tag(key, value));
134 return this;
135 }
136
137 DeprecationCheck remove(String key) {
138 change.add(new Tag(key));
139 return this;
140 }
141
142 DeprecationCheck testAndRemove(String key, String value) {
143 return test(key, value).remove(key);
144 }
145
146 DeprecationCheck testAndRemove(String key) {
147 return test(key).remove(key);
148 }
149
150 DeprecationCheck alternative(String key, String value) {
151 alternatives.add(new Tag(key, value));
152 return this;
153 }
154
155 DeprecationCheck alternative(String key) {
156 return alternative(key, null);
157 }
158
159 boolean matchesPrimitive(OsmPrimitive p) {
160 for (Tag tag : test) {
161 String key = tag.getKey();
162 String value = tag.getValue();
163 if (value.isEmpty() && !p.hasKey(key))
164 return false;
165 if (!value.isEmpty() && !value.equals(p.get(key)))
166 return false;
167 }
168 return true;
169 }
170
171 Command fixPrimitive(OsmPrimitive p) {
172 Collection<Command> cmds = new LinkedList<Command>();
173 for (Tag tag : change) {
174 cmds.add(new ChangePropertyCommand(p, tag.getKey(), tag.getValue()));
175 }
176 return new SequenceCommand(tr("Deprecation fix of {0}", Utils.join(", ", test)), cmds);
177 }
178
179 String getDescription() {
180 if (alternatives.isEmpty())
181 return tr("{0} is deprecated", Utils.join(", ", test));
182 else
183 return tr("{0} is deprecated, use {1} instead", Utils.join(", ", test), Utils.join(tr(" or "), alternatives));
184 }
185 }
186
187 private class DeprecationError extends TestError {
188
189 OsmPrimitive p;
190 DeprecationCheck check;
191
192 DeprecationError(OsmPrimitive p, DeprecationCheck check) {
193 super(DeprecatedTags.this, Severity.WARNING, check.getDescription(), check.code, p);
194 this.p = p;
195 this.check = check;
196 }
197
198 @Override
199 public boolean isFixable() {
200 return !check.change.isEmpty();
201 }
202
203 @Override
204 public Command getFix() {
205 return check.fixPrimitive(p);
206 }
207 }
208}
Note: See TracBrowser for help on using the repository browser.