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