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 java.util.ArrayList; 007import java.util.Arrays; 008import java.util.Collection; 009import java.util.HashSet; 010import java.util.List; 011import java.util.Set; 012import java.util.regex.Matcher; 013import java.util.regex.Pattern; 014 015import org.openstreetmap.josm.data.osm.OsmPrimitive; 016import org.openstreetmap.josm.data.validation.Severity; 017import org.openstreetmap.josm.data.validation.Test; 018import org.openstreetmap.josm.data.validation.TestError; 019import org.openstreetmap.josm.tools.Predicates; 020import org.openstreetmap.josm.tools.Utils; 021 022/** 023 * Checks for <a href="http://wiki.openstreetmap.org/wiki/Conditional_restrictions">conditional restrictions</a> 024 * @since 6605 025 */ 026public class ConditionalKeys extends Test.TagTest { 027 028 final OpeningHourTest openingHourTest = new OpeningHourTest(); 029 static final Set<String> RESTRICTION_TYPES = new HashSet<>(Arrays.asList("oneway", "toll", "noexit", "maxspeed", "minspeed", "maxstay", 030 "maxweight", "maxaxleload", "maxheight", "maxwidth", "maxlength", "overtaking", "maxgcweight", "maxgcweightrating", "fee")); 031 static final Set<String> RESTRICTION_VALUES = new HashSet<>(Arrays.asList("yes", "official", "designated", "destination", 032 "delivery", "permissive", "private", "agricultural", "forestry", "no")); 033 static final Set<String> TRANSPORT_MODES = new HashSet<>(Arrays.asList("access", "foot", "ski", "inline_skates", "ice_skates", 034 "horse", "vehicle", "bicycle", "carriage", "trailer", "caravan", "motor_vehicle", "motorcycle", "moped", "mofa", 035 "motorcar", "motorhome", "psv", "bus", "taxi", "tourist_bus", "goods", "hgv", "agricultural", "atv", "snowmobile" 036 /*,"hov","emergency","hazmat","disabled"*/)); 037 038 /** 039 * Constructs a new {@code ConditionalKeys}. 040 */ 041 public ConditionalKeys() { 042 super(tr("Conditional Keys"), tr("Tests for the correct usage of ''*:conditional'' tags.")); 043 } 044 045 @Override 046 public void initialize() throws Exception { 047 super.initialize(); 048 openingHourTest.initialize(); 049 } 050 051 public static boolean isRestrictionType(String part) { 052 return RESTRICTION_TYPES.contains(part); 053 } 054 055 public static boolean isRestrictionValue(String part) { 056 return RESTRICTION_VALUES.contains(part); 057 } 058 059 public static boolean isTransportationMode(String part) { 060 // http://wiki.openstreetmap.org/wiki/Key:access#Transport_mode_restrictions 061 return TRANSPORT_MODES.contains(part); 062 } 063 064 public static boolean isDirection(String part) { 065 return "forward".equals(part) || "backward".equals(part); 066 } 067 068 public boolean isKeyValid(String key) { 069 // <restriction-type>[:<transportation mode>][:<direction>]:conditional 070 // -- or -- <transportation mode> [:<direction>]:conditional 071 if (!key.endsWith(":conditional")) { 072 return false; 073 } 074 final String[] parts = key.replaceAll(":conditional", "").split(":"); 075 return parts.length == 3 && isRestrictionType(parts[0]) && isTransportationMode(parts[1]) && isDirection(parts[2]) 076 || parts.length == 1 && (isRestrictionType(parts[0]) || isTransportationMode(parts[0])) 077 || parts.length == 2 && ( 078 isRestrictionType(parts[0]) && (isTransportationMode(parts[1]) || isDirection(parts[1])) 079 || isTransportationMode(parts[0]) && isDirection(parts[1])); 080 } 081 082 public boolean isValueValid(String key, String value) { 083 return validateValue(key, value) == null; 084 } 085 086 static class ConditionalParsingException extends RuntimeException { 087 ConditionalParsingException(String message) { 088 super(message); 089 } 090 } 091 092 public static class ConditionalValue { 093 public final String restrictionValue; 094 public final Collection<String> conditions; 095 096 public ConditionalValue(String restrictionValue, Collection<String> conditions) { 097 this.restrictionValue = restrictionValue; 098 this.conditions = conditions; 099 } 100 101 public static List<ConditionalValue> parse(String value) throws ConditionalParsingException { 102 // <restriction-value> @ <condition>[;<restriction-value> @ <condition>] 103 final List<ConditionalValue> r = new ArrayList<>(); 104 final Pattern part = Pattern.compile("([^@\\p{Space}][^@]*?)" + "\\s*@\\s*" + "(\\([^)\\p{Space}][^)]+?\\)|[^();\\p{Space}][^();]*?)\\s*"); 105 final Matcher m = Pattern.compile("(" + part + ")(;\\s*" + part + ")*").matcher(value); 106 if (!m.matches()) { 107 throw new ConditionalParsingException(tr("Does not match pattern ''restriction value @ condition''")); 108 } else { 109 int i = 2; 110 while (i + 1 <= m.groupCount() && m.group(i + 1) != null) { 111 final String restrictionValue = m.group(i); 112 final String[] conditions = m.group(i + 1).replace("(", "").replace(")", "").split("\\s+(AND|and)\\s+"); 113 r.add(new ConditionalValue(restrictionValue, Arrays.asList(conditions))); 114 i += 3; 115 } 116 } 117 return r; 118 } 119 } 120 121 public String validateValue(String key, String value) { 122 try { 123 for (final ConditionalValue conditional : ConditionalValue.parse(value)) { 124 // validate restriction value 125 if (isTransportationMode(key.split(":")[0]) && !isRestrictionValue(conditional.restrictionValue)) { 126 return tr("{0} is not a valid restriction value", conditional.restrictionValue); 127 } 128 // validate opening hour if the value contains an hour (heuristic) 129 for (final String condition : conditional.conditions) { 130 if (condition.matches(".*[0-9]:[0-9]{2}.*")) { 131 final List<OpeningHourTest.OpeningHoursTestError> errors = openingHourTest.checkOpeningHourSyntax( 132 "", condition, OpeningHourTest.CheckMode.TIME_RANGE, true); 133 if (!errors.isEmpty()) { 134 return errors.get(0).getMessage(); 135 } 136 } 137 } 138 } 139 } catch (ConditionalParsingException ex) { 140 return ex.getMessage(); 141 } 142 return null; 143 } 144 145 public List<TestError> validatePrimitive(OsmPrimitive p) { 146 final List<TestError> errors = new ArrayList<>(); 147 for (final String key : Utils.filter(p.keySet(), Predicates.stringMatchesPattern(Pattern.compile(".*:conditional(:.*)?$")))) { 148 if (!isKeyValid(key)) { 149 errors.add(new TestError(this, Severity.WARNING, tr("Wrong syntax in {0} key", key), 3201, p)); 150 continue; 151 } 152 final String value = p.get(key); 153 final String error = validateValue(key, value); 154 if (error != null) { 155 errors.add(new TestError(this, Severity.WARNING, tr("Error in {0} value: {1}", key, error), 3202, p)); 156 } 157 } 158 return errors; 159 } 160 161 @Override 162 public void check(OsmPrimitive p) { 163 errors.addAll(validatePrimitive(p)); 164 } 165}