001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.validation.tests; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import org.openstreetmap.josm.command.ChangePropertyCommand; 007import org.openstreetmap.josm.data.osm.Node; 008import org.openstreetmap.josm.data.osm.OsmPrimitive; 009import org.openstreetmap.josm.data.osm.Relation; 010import org.openstreetmap.josm.data.osm.Way; 011import org.openstreetmap.josm.data.validation.FixableTestError; 012import org.openstreetmap.josm.data.validation.Severity; 013import org.openstreetmap.josm.data.validation.Test; 014import org.openstreetmap.josm.data.validation.TestError; 015import org.openstreetmap.josm.data.validation.routines.AbstractValidator; 016import org.openstreetmap.josm.data.validation.routines.EmailValidator; 017import org.openstreetmap.josm.data.validation.routines.UrlValidator; 018 019/** 020 * Performs validation tests on internet-related tags (websites, e-mail addresses, etc.). 021 * @since 7489 022 */ 023public class InternetTags extends Test { 024 025 protected static final int INVALID_URL = 3301; 026 protected static final int INVALID_EMAIL = 3302; 027 028 /** 029 * List of keys subject to URL validation. 030 */ 031 public static String[] URL_KEYS = new String[] { 032 "url", "source:url", 033 "website", "contact:website", "heritage:website", "source:website" 034 }; 035 036 /** 037 * List of keys subject to email validation. 038 */ 039 public static String[] EMAIL_KEYS = new String[] { 040 "email", "contact:email" 041 }; 042 043 /** 044 * Constructs a new {@code InternetTags} test. 045 */ 046 public InternetTags() { 047 super(tr("Internet tags"), tr("Checks for errors in internet-related tags.")); 048 } 049 050 private boolean doTest(OsmPrimitive p, String k, String[] keys, AbstractValidator validator, int code) { 051 for (String i : keys) { 052 if (i.equals(k)) { 053 if (!validator.isValid(p.get(k))) { 054 TestError error; 055 String msg = tr("''{0}'': {1}", k, validator.getErrorMessage()); 056 String fix = validator.getFix(); 057 if (fix != null) { 058 error = new FixableTestError(this, Severity.WARNING, msg, code, p, 059 new ChangePropertyCommand(p, k, fix)); 060 } else { 061 error = new TestError(this, Severity.WARNING, msg, code, p); 062 } 063 return errors.add(error); 064 } 065 break; 066 } 067 } 068 return false; 069 } 070 071 private void test(OsmPrimitive p) { 072 for (String k : p.keySet()) { 073 if (!doTest(p, k, URL_KEYS, UrlValidator.getInstance(), INVALID_URL)) { 074 doTest(p, k, EMAIL_KEYS, EmailValidator.getInstance(), INVALID_EMAIL); 075 } 076 } 077 } 078 079 @Override 080 public void visit(Node n) { 081 test(n); 082 } 083 084 @Override 085 public void visit(Way w) { 086 test(w); 087 } 088 089 @Override 090 public void visit(Relation r) { 091 test(r); 092 } 093}