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.tr;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
7 | import org.openstreetmap.josm.data.osm.Node;
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
10 | import org.openstreetmap.josm.data.osm.Way;
|
---|
11 | import org.openstreetmap.josm.data.validation.FixableTestError;
|
---|
12 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
13 | import org.openstreetmap.josm.data.validation.Test;
|
---|
14 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
15 | import org.openstreetmap.josm.data.validation.routines.AbstractValidator;
|
---|
16 | import org.openstreetmap.josm.data.validation.routines.EmailValidator;
|
---|
17 | import org.openstreetmap.josm.data.validation.routines.UrlValidator;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Performs validation tests on internet-related tags (websites, e-mail addresses, etc.).
|
---|
21 | * @since 7489
|
---|
22 | */
|
---|
23 | public class InternetTags extends Test {
|
---|
24 |
|
---|
25 | protected static final int INVALID_URL = 3301;
|
---|
26 | protected static final int INVALID_EMAIL = 3302;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * List of keys subject to URL validation.
|
---|
30 | */
|
---|
31 | public static String[] URL_KEYS = new String[] {
|
---|
32 | "url", "source:url",
|
---|
33 | "website", "contact:website", "heritage:website", "source:website"
|
---|
34 | };
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * List of keys subject to email validation.
|
---|
38 | */
|
---|
39 | public static String[] EMAIL_KEYS = new String[] {
|
---|
40 | "email", "contact:email"
|
---|
41 | };
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code InternetTags} test.
|
---|
45 | */
|
---|
46 | public InternetTags() {
|
---|
47 | super(tr("Internet tags"), tr("Checks for errors in internet-related tags."));
|
---|
48 | }
|
---|
49 |
|
---|
50 | private boolean doTest(OsmPrimitive p, String k, String[] keys, AbstractValidator validator, int code) {
|
---|
51 | for (String i : keys) {
|
---|
52 | if (i.equals(k)) {
|
---|
53 | if (!validator.isValid(p.get(k))) {
|
---|
54 | TestError error;
|
---|
55 | String msg = tr("''{0}'': {1}", k, validator.getErrorMessage());
|
---|
56 | String fix = validator.getFix();
|
---|
57 | if (fix != null) {
|
---|
58 | error = new FixableTestError(this, Severity.WARNING, msg, code, p,
|
---|
59 | new ChangePropertyCommand(p, k, fix));
|
---|
60 | } else {
|
---|
61 | error = new TestError(this, Severity.WARNING, msg, code, p);
|
---|
62 | }
|
---|
63 | return errors.add(error);
|
---|
64 | }
|
---|
65 | break;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 |
|
---|
71 | private void test(OsmPrimitive p) {
|
---|
72 | for (String k : p.keySet()) {
|
---|
73 | if (!doTest(p, k, URL_KEYS, UrlValidator.getInstance(), INVALID_URL)) {
|
---|
74 | doTest(p, k, EMAIL_KEYS, EmailValidator.getInstance(), INVALID_EMAIL);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public void visit(Node n) {
|
---|
81 | test(n);
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public void visit(Way w) {
|
---|
86 | test(w);
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public void visit(Relation r) {
|
---|
91 | test(r);
|
---|
92 | }
|
---|
93 | }
|
---|