Ignore:
Timestamp:
2019-02-24T20:25:48+01:00 (5 years ago)
Author:
Don-vip
Message:

fix #17376 - allows multiple values in URL validator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/InternetTags.java

    r13489 r14803  
    55import static org.openstreetmap.josm.tools.I18n.tr;
    66
     7import java.util.ArrayList;
     8import java.util.List;
    79import java.util.function.Supplier;
    810
     
    6567        for (String i : keys) {
    6668            if (i.equals(k)) {
    67                 TestError error = validateTag(p, k, validator, code);
    68                 if (error != null) {
    69                     errors.add(error);
    70                 }
    71                 break;
     69                return errors.addAll(validateTag(p, k, validator, code));
    7270            }
    7371        }
     
    8381     * @return The error if the validation fails, {@code null} otherwise
    8482     * @since 7824
     83     * @since 14803 (return type)
    8584     */
    86     public TestError validateTag(OsmPrimitive p, String k, AbstractValidator validator, int code) {
     85    public List<TestError> validateTag(OsmPrimitive p, String k, AbstractValidator validator, int code) {
    8786        return doValidateTag(p, k, null, validator, code);
    8887    }
     
    9796     * @return The error if the validation fails, {@code null} otherwise
    9897     */
    99     private TestError doValidateTag(OsmPrimitive p, String k, String v, AbstractValidator validator, int code) {
    100         TestError error = null;
    101         String value = v != null ? v : p.get(k);
    102         if (!validator.isValid(value)) {
    103             Supplier<Command> fix = null;
    104             String errMsg = validator.getErrorMessage();
    105             if (tr("URL contains an invalid protocol: {0}", (String) null).equals(errMsg)) {
    106                 // Special treatment to allow URLs without protocol. See UrlValidator#isValid
    107                 String proto = validator instanceof EmailValidator ? "mailto://" : "http://";
    108                 return doValidateTag(p, k, proto+value, validator, code);
    109             } else if (tr("URL contains an invalid authority: {0}", (String) null).equals(errMsg)
    110                     && value.contains("\\") && validator.isValid(value.replaceAll("\\\\", "/"))) {
    111                 // Special treatment to autofix URLs with backslashes. See UrlValidator#isValid
    112                 errMsg = tr("URL contains backslashes instead of slashes");
    113                 fix = () -> new ChangePropertyCommand(p, k, value.replaceAll("\\\\", "/"));
     98    private List<TestError> doValidateTag(OsmPrimitive p, String k, String v, AbstractValidator validator, int code) {
     99        List<TestError> errors = new ArrayList<>();
     100        String values = v != null ? v : p.get(k);
     101        for (String value : values.split(";")) {
     102            if (!validator.isValid(value)) {
     103                Supplier<Command> fix = null;
     104                String errMsg = validator.getErrorMessage();
     105                if (tr("URL contains an invalid protocol: {0}", (String) null).equals(errMsg)) {
     106                    // Special treatment to allow URLs without protocol. See UrlValidator#isValid
     107                    String proto = validator instanceof EmailValidator ? "mailto://" : "http://";
     108                    return doValidateTag(p, k, proto+value, validator, code);
     109                } else if (tr("URL contains an invalid authority: {0}", (String) null).equals(errMsg)
     110                        && value.contains("\\") && validator.isValid(value.replaceAll("\\\\", "/"))) {
     111                    // Special treatment to autofix URLs with backslashes. See UrlValidator#isValid
     112                    errMsg = tr("URL contains backslashes instead of slashes");
     113                    fix = () -> new ChangePropertyCommand(p, k, value.replaceAll("\\\\", "/"));
     114                }
     115                errors.add(TestError.builder(this, Severity.WARNING, code)
     116                            .message(validator.getValidatorName(), marktr("''{0}'': {1}"), k, errMsg)
     117                            .primitives(p)
     118                            .fix(fix)
     119                            .build());
    114120            }
    115             error = TestError.builder(this, Severity.WARNING, code)
    116                     .message(validator.getValidatorName(), marktr("''{0}'': {1}"), k, errMsg)
    117                     .primitives(p)
    118                     .fix(fix)
    119                     .build();
    120121        }
    121         return error;
     122        return errors;
    122123    }
    123124
Note: See TracChangeset for help on using the changeset viewer.