1 | // License: GPL. See LICENSE file for details.
|
---|
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.Collection;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.command.Command;
|
---|
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 {
|
---|
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 |
|
---|
32 | /**
|
---|
33 | * Constructor
|
---|
34 | */
|
---|
35 | public UntaggedNode() {
|
---|
36 | super(tr("Untagged and unconnected nodes"),
|
---|
37 | tr("This test checks for untagged nodes that are not part of any way."));
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public void visit(Collection<OsmPrimitive> selection) {
|
---|
42 | for (OsmPrimitive p : selection) {
|
---|
43 | if (p.isUsable() && p instanceof Node) {
|
---|
44 | p.accept(this);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void visit(Node n) {
|
---|
51 | if (n.isUsable() && !n.isTagged() && n.getReferrers().isEmpty()) {
|
---|
52 | String errorMessage = tr("Unconnected nodes without physical tags");
|
---|
53 | if (!n.hasKeys()) {
|
---|
54 | String msg = marktr("No tags");
|
---|
55 | errors.add(new TestError(this, Severity.WARNING, errorMessage, tr(msg), msg, UNTAGGED_NODE_BLANK, n));
|
---|
56 | return;
|
---|
57 | }
|
---|
58 | for (Map.Entry<String, String> tag : n.getKeys().entrySet()) {
|
---|
59 | String key = tag.getKey();
|
---|
60 | if (contains(tag, "fixme") || contains(tag, "FIXME")) {
|
---|
61 | /* translation note: don't translate quoted words */
|
---|
62 | String msg = marktr("Has tag containing ''fixme'' or ''FIXME''");
|
---|
63 | errors.add(new TestError(this, Severity.WARNING, errorMessage, tr(msg), msg, UNTAGGED_NODE_FIXME, n));
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | String msg = null;
|
---|
68 | int code = 0;
|
---|
69 | if (key.startsWith("note") || key.startsWith("comment") || key.startsWith("description")) {
|
---|
70 | /* translation note: don't translate quoted words */
|
---|
71 | msg = marktr("Has key ''note'' or ''comment'' or ''description''");
|
---|
72 | code = UNTAGGED_NODE_NOTE;
|
---|
73 | } else if (key.startsWith("created_by") || key.startsWith("converted_by")) {
|
---|
74 | /* translation note: don't translate quoted words */
|
---|
75 | msg = marktr("Has key ''created_by'' or ''converted_by''");
|
---|
76 | code = UNTAGGED_NODE_CREATED_BY;
|
---|
77 | } else if (key.startsWith("watch")) {
|
---|
78 | /* translation note: don't translate quoted words */
|
---|
79 | msg = marktr("Has key ''watch''");
|
---|
80 | code = UNTAGGED_NODE_WATCH;
|
---|
81 | } else if (key.startsWith("source")) {
|
---|
82 | /* translation note: don't translate quoted words */
|
---|
83 | msg = marktr("Has key ''source''");
|
---|
84 | code = UNTAGGED_NODE_SOURCE;
|
---|
85 | }
|
---|
86 | if (msg != null) {
|
---|
87 | errors.add(new TestError(this, Severity.WARNING, errorMessage, tr(msg), msg, code, n));
|
---|
88 | return;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | // Does not happen, but just to be sure. Maybe definition of uninteresting tags changes in future.
|
---|
92 | errors.add(new TestError(this, Severity.WARNING, errorMessage, tr("Other"), "Other", UNTAGGED_NODE_OTHER, n));
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private boolean contains(Map.Entry<String, String> tag, String s) {
|
---|
97 | return tag.getKey().indexOf(s) != -1 || tag.getValue().indexOf(s) != -1;
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public Command fixError(TestError testError) {
|
---|
102 | return deletePrimitivesIfNeeded(testError.getPrimitives());
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public boolean isFixable(TestError testError) {
|
---|
107 | if (testError.getTester() instanceof UntaggedNode) {
|
---|
108 | int code = testError.getCode();
|
---|
109 | switch (code) {
|
---|
110 | case UNTAGGED_NODE_BLANK:
|
---|
111 | case UNTAGGED_NODE_CREATED_BY:
|
---|
112 | case UNTAGGED_NODE_WATCH:
|
---|
113 | case UNTAGGED_NODE_SOURCE:
|
---|
114 | return true;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | return false;
|
---|
118 | }
|
---|
119 | }
|
---|