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

Last change on this file since 4806 was 4806, checked in by stoecker, 12 years ago

unify texts for validator tests, move command where it belongs

File size: 6.5 KB
Line 
1package org.openstreetmap.josm.data.validation.tests;
2
3import org.openstreetmap.josm.data.osm.Node;
4import org.openstreetmap.josm.data.osm.Relation;
5import org.openstreetmap.josm.data.osm.Way;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.util.Collection;
9import java.util.LinkedList;
10import java.util.List;
11import org.openstreetmap.josm.command.ChangePropertyCommand;
12import org.openstreetmap.josm.command.Command;
13import org.openstreetmap.josm.command.SequenceCommand;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.Tag;
16import org.openstreetmap.josm.data.validation.Severity;
17import org.openstreetmap.josm.data.validation.Test;
18import org.openstreetmap.josm.data.validation.TestError;
19import org.openstreetmap.josm.tools.Utils;
20
21public class DeprecatedTags extends Test {
22
23 private List<DeprecationCheck> checks = new LinkedList<DeprecationCheck>();
24
25 public DeprecatedTags() {
26 super(tr("Deprecated Tags"), tr("Checks and corrects deprecated tags."));
27 checks.add(new DeprecationCheck(2101).
28 testAndRemove("barrier", "wire_fence").
29 add("barrier", "fence").
30 add("fence_type", "chain"));
31 checks.add(new DeprecationCheck(2102).
32 testAndRemove("barrier", "wood_fence").
33 add("barrier", "fence").
34 add("fence_type", "wood"));
35 checks.add(new DeprecationCheck(2103).
36 testAndRemove("highway", "ford").
37 add("highway", "road").
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 }
75
76 public void visit(OsmPrimitive p) {
77 for (DeprecationCheck check : checks) {
78 if (check.matchesPrimitive(p)) {
79 errors.add(new DeprecationError(p, check));
80 }
81 }
82 }
83
84 @Override
85 public void visit(Node n) {
86 visit((OsmPrimitive) n);
87 }
88
89 @Override
90 public void visit(Way w) {
91 visit((OsmPrimitive) w);
92 }
93
94 @Override
95 public void visit(Relation r) {
96 visit((OsmPrimitive) r);
97 }
98
99 private class DeprecationCheck {
100
101 int code;
102 List<Tag> test = new LinkedList<Tag>();
103 List<Tag> change = new LinkedList<Tag>();
104 List<Tag> alternatives = new LinkedList<Tag>();
105
106 public DeprecationCheck(int code) {
107 this.code = code;
108 }
109
110 DeprecationCheck test(String key, String value) {
111 test.add(new Tag(key, value));
112 return this;
113 }
114
115 DeprecationCheck test(String key) {
116 return test(key, null);
117 }
118
119 DeprecationCheck add(String key, String value) {
120 change.add(new Tag(key, value));
121 return this;
122 }
123
124 DeprecationCheck remove(String key) {
125 change.add(new Tag(key));
126 return this;
127 }
128
129 DeprecationCheck testAndRemove(String key, String value) {
130 return test(key, value).remove(key);
131 }
132
133 DeprecationCheck testAndRemove(String key) {
134 return test(key).remove(key);
135 }
136
137 DeprecationCheck alternative(String key, String value) {
138 alternatives.add(new Tag(key, value));
139 return this;
140 }
141
142 DeprecationCheck alternative(String key) {
143 return alternative(key, null);
144 }
145
146 boolean matchesPrimitive(OsmPrimitive p) {
147 for (Tag tag : test) {
148 String key = tag.getKey();
149 String value = tag.getValue();
150 if (value.isEmpty() && !p.hasKey(key)) {
151 return false;
152 }
153 if (!value.isEmpty() && !value.equals(p.get(key))) {
154 return false;
155 }
156 }
157 return true;
158 }
159
160 Command fixPrimitive(OsmPrimitive p) {
161 Collection<Command> cmds = new LinkedList<Command>();
162 for (Tag tag : change) {
163 cmds.add(new ChangePropertyCommand(p, tag.getKey(), tag.getValue()));
164 }
165 return new SequenceCommand(tr("Deprecation fix of {0}", Utils.join(", ", test)), cmds);
166 }
167
168 String getDescription() {
169 if (alternatives.isEmpty()) {
170 return tr("{0} is deprecated", Utils.join(", ", test));
171 } else {
172 return tr("{0} is deprecated, use {1} instead", Utils.join(", ", test), Utils.join(tr(" or "), alternatives));
173 }
174 }
175 }
176
177 private class DeprecationError extends TestError {
178
179 OsmPrimitive p;
180 DeprecationCheck check;
181
182 DeprecationError(OsmPrimitive p, DeprecationCheck check) {
183 super(DeprecatedTags.this, Severity.WARNING, check.getDescription(), check.code, p);
184 this.p = p;
185 this.check = check;
186 }
187
188 @Override
189 public boolean isFixable() {
190 return !check.change.isEmpty();
191 }
192
193 @Override
194 public Command getFix() {
195 return check.fixPrimitive(p);
196 }
197 }
198}
Note: See TracBrowser for help on using the repository browser.