1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.util.Locale;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.command.Command;
|
---|
10 | import org.openstreetmap.josm.data.osm.KeyValueVisitor;
|
---|
11 | import org.openstreetmap.josm.data.osm.Node;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
14 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
15 | import org.openstreetmap.josm.data.validation.Test;
|
---|
16 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Checks for nodes with uninteresting tags that are in no way
|
---|
20 | *
|
---|
21 | * @author frsantos
|
---|
22 | */
|
---|
23 | public class UntaggedNode extends Test implements KeyValueVisitor {
|
---|
24 |
|
---|
25 | protected static final int UNTAGGED_NODE_BLANK = 201;
|
---|
26 | protected static final int UNTAGGED_NODE_FIXME = 202;
|
---|
27 | protected static final int UNTAGGED_NODE_NOTE = 203;
|
---|
28 | protected static final int UNTAGGED_NODE_CREATED_BY = 204;
|
---|
29 | protected static final int UNTAGGED_NODE_WATCH = 205;
|
---|
30 | protected static final int UNTAGGED_NODE_SOURCE = 206;
|
---|
31 | protected static final int UNTAGGED_NODE_OTHER = 207;
|
---|
32 | protected static final String ERROR_MESSAGE = tr("Unconnected nodes without physical tags");
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructor
|
---|
36 | */
|
---|
37 | public UntaggedNode() {
|
---|
38 | super(tr("Untagged and unconnected nodes"),
|
---|
39 | tr("This test checks for untagged nodes that are not part of any way."));
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void visit(Node n) {
|
---|
44 | if (n.isUsable() && !n.isTagged() && n.getReferrers().isEmpty()) {
|
---|
45 |
|
---|
46 | if (!n.hasKeys() && IN_DOWNLOADED_AREA.test(n)) {
|
---|
47 | errors.add(TestError.builder(this, Severity.WARNING, UNTAGGED_NODE_BLANK)
|
---|
48 | .message(ERROR_MESSAGE, marktr("No tags"))
|
---|
49 | .primitives(n)
|
---|
50 | .build());
|
---|
51 | return;
|
---|
52 | }
|
---|
53 | n.visitKeys(this);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | private static OsmPrimitive[] castPrim(Tagged n) {
|
---|
58 | return n instanceof OsmPrimitive ? (new OsmPrimitive[]{(OsmPrimitive) n}) : (new OsmPrimitive[0]);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void visitKeyValue(Tagged n, String key, String value) {
|
---|
63 | if (key.toLowerCase(Locale.ENGLISH).contains("fixme") || value.toLowerCase(Locale.ENGLISH).contains("fixme")) {
|
---|
64 | /* translation note: don't translate quoted words */
|
---|
65 | errors.add(TestError.builder(this, Severity.WARNING, UNTAGGED_NODE_FIXME)
|
---|
66 | .message(ERROR_MESSAGE, marktr("Has tag containing ''fixme'' or ''FIXME''"))
|
---|
67 | .primitives(castPrim(n))
|
---|
68 | .build());
|
---|
69 | return;
|
---|
70 | }
|
---|
71 |
|
---|
72 | String msg = null;
|
---|
73 | int code = 0;
|
---|
74 | if (key.startsWith("note") || key.startsWith("comment") || key.startsWith("description")) {
|
---|
75 | /* translation note: don't translate quoted words */
|
---|
76 | msg = marktr("Has key ''note'' or ''comment'' or ''description''");
|
---|
77 | code = UNTAGGED_NODE_NOTE;
|
---|
78 | } else if (key.startsWith("created_by") || key.startsWith("converted_by")) {
|
---|
79 | /* translation note: don't translate quoted words */
|
---|
80 | msg = marktr("Has key ''created_by'' or ''converted_by''");
|
---|
81 | code = UNTAGGED_NODE_CREATED_BY;
|
---|
82 | } else if (key.startsWith("watch")) {
|
---|
83 | /* translation note: don't translate quoted words */
|
---|
84 | msg = marktr("Has key ''watch''");
|
---|
85 | code = UNTAGGED_NODE_WATCH;
|
---|
86 | } else if (key.startsWith("source")) {
|
---|
87 | /* translation note: don't translate quoted words */
|
---|
88 | msg = marktr("Has key ''source''");
|
---|
89 | code = UNTAGGED_NODE_SOURCE;
|
---|
90 | }
|
---|
91 | if (msg != null) {
|
---|
92 | errors.add(TestError.builder(this, Severity.WARNING, code)
|
---|
93 | .message(ERROR_MESSAGE, msg)
|
---|
94 | .primitives(castPrim(n))
|
---|
95 | .build());
|
---|
96 | return;
|
---|
97 | }
|
---|
98 | // Does not happen, but just to be sure. Maybe definition of uninteresting tags changes in future.
|
---|
99 | errors.add(TestError.builder(this, Severity.WARNING, UNTAGGED_NODE_OTHER)
|
---|
100 | .message(ERROR_MESSAGE, marktr("Other"))
|
---|
101 | .primitives(castPrim(n))
|
---|
102 | .build());
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public Command fixError(TestError testError) {
|
---|
107 | return deletePrimitivesIfNeeded(testError.getPrimitives());
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public boolean isFixable(TestError testError) {
|
---|
112 | if (testError.getTester() instanceof UntaggedNode) {
|
---|
113 | int code = testError.getCode();
|
---|
114 | switch (code) {
|
---|
115 | case UNTAGGED_NODE_BLANK:
|
---|
116 | case UNTAGGED_NODE_CREATED_BY:
|
---|
117 | case UNTAGGED_NODE_WATCH:
|
---|
118 | case UNTAGGED_NODE_SOURCE:
|
---|
119 | return true;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 | }
|
---|