source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/InternetTags.java@ 12539

Last change on this file since 12539 was 12539, checked in by Don-vip, 7 years ago

PMD - enable naming rules

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import org.openstreetmap.josm.data.osm.Node;
8import org.openstreetmap.josm.data.osm.OsmPrimitive;
9import org.openstreetmap.josm.data.osm.Relation;
10import org.openstreetmap.josm.data.osm.Way;
11import org.openstreetmap.josm.data.validation.Severity;
12import org.openstreetmap.josm.data.validation.Test;
13import org.openstreetmap.josm.data.validation.TestError;
14import org.openstreetmap.josm.data.validation.routines.AbstractValidator;
15import org.openstreetmap.josm.data.validation.routines.EmailValidator;
16import org.openstreetmap.josm.data.validation.routines.UrlValidator;
17
18/**
19 * Performs validation tests on internet-related tags (websites, e-mail addresses, etc.).
20 * @since 7489
21 */
22public class InternetTags extends Test {
23
24 /** Error code for an invalid URL */
25 public static final int INVALID_URL = 3301;
26 /** Error code for an invalid e-mail */
27 public static final int INVALID_EMAIL = 3302;
28
29 /**
30 * List of keys subject to URL validation.
31 */
32 private static final String[] URL_KEYS = new String[] {
33 "url", "source:url",
34 "website", "contact:website", "heritage:website", "source:website"
35 };
36
37 /**
38 * List of keys subject to email validation.
39 */
40 private static final String[] EMAIL_KEYS = new String[] {
41 "email", "contact:email"
42 };
43
44 /**
45 * Constructs a new {@code InternetTags} test.
46 */
47 public InternetTags() {
48 super(tr("Internet tags"), tr("Checks for errors in internet-related tags."));
49 }
50
51 /**
52 * Potentially validates a given primitive key against a given validator.
53 * @param p The OSM primitive to test
54 * @param k The key to validate
55 * @param keys The list of keys to check. If {@code k} is not inside this collection, do nothing
56 * @param validator The validator to run if {@code k} is inside {@code keys}
57 * @param code The error code to set if the validation fails
58 * @return {@code true} if the validation fails. In this case, a new error has been created.
59 */
60 private boolean doTest(OsmPrimitive p, String k, String[] keys, AbstractValidator validator, int code) {
61 for (String i : keys) {
62 if (i.equals(k)) {
63 TestError error = validateTag(p, k, validator, code);
64 if (error != null) {
65 errors.add(error);
66 }
67 break;
68 }
69 }
70 return false;
71 }
72
73 /**
74 * Validates a given primitive tag against a given validator.
75 * @param p The OSM primitive to test
76 * @param k The key to validate
77 * @param validator The validator to run
78 * @param code The error code to set if the validation fails
79 * @return The error if the validation fails, {@code null} otherwise
80 * @since 7824
81 */
82 public TestError validateTag(OsmPrimitive p, String k, AbstractValidator validator, int code) {
83 return doValidateTag(p, k, null, validator, code);
84 }
85
86 /**
87 * Validates a given primitive tag against a given validator.
88 * @param p The OSM primitive to test
89 * @param k The key to validate
90 * @param v The value to validate. May be {@code null} to use {@code p.get(k)}
91 * @param validator The validator to run
92 * @param code The error code to set if the validation fails
93 * @return The error if the validation fails, {@code null} otherwise
94 */
95 private TestError doValidateTag(OsmPrimitive p, String k, String v, AbstractValidator validator, int code) {
96 TestError error = null;
97 String value = v != null ? v : p.get(k);
98 if (!validator.isValid(value)) {
99 String errMsg = validator.getErrorMessage();
100 // Special treatment to allow URLs without protocol. See UrlValidator#isValid
101 if (tr("URL contains an invalid protocol: {0}", (String) null).equals(errMsg)) {
102 String proto = validator instanceof EmailValidator ? "mailto://" : "http://";
103 return doValidateTag(p, k, proto+value, validator, code);
104 }
105 error = TestError.builder(this, Severity.WARNING, code)
106 .message(validator.getValidatorName(), marktr("''{0}'': {1}"), k, errMsg)
107 .primitives(p)
108 .build();
109 }
110 return error;
111 }
112
113 private void test(OsmPrimitive p) {
114 for (String k : p.keySet()) {
115 // Test key against URL validator
116 if (!doTest(p, k, URL_KEYS, UrlValidator.getInstance(), INVALID_URL)) {
117 // Test key against e-mail validator only if the URL validator did not fail
118 doTest(p, k, EMAIL_KEYS, EmailValidator.getInstance(), INVALID_EMAIL);
119 }
120 }
121 }
122
123 @Override
124 public void visit(Node n) {
125 test(n);
126 }
127
128 @Override
129 public void visit(Way w) {
130 test(w);
131 }
132
133 @Override
134 public void visit(Relation r) {
135 test(r);
136 }
137}
Note: See TracBrowser for help on using the repository browser.