All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.vpro.validation.TwitterRefValidator Maven / Gradle / Ivy

Go to download

The basic domain classes for 'media', the core of POMS. Also, the 'update' XML bindings for it. It also contains some closely related domain classes like the enum to contain NICAM kijkwijzer settings.

There is a newer version: 8.3.1
Show newest version
/*
 * Copyright (C) 2011 Licensed under the Apache License, Version 2.0
 * VPRO The Netherlands
 */
package nl.vpro.validation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class TwitterRefValidator implements ConstraintValidator {

    public static final Pattern PATTERN = Pattern.compile("^@\\w{1,50}|#\\w{1,279}$", Pattern.CASE_INSENSITIVE);

    @Override
    public boolean isValid(nl.vpro.domain.media.TwitterRef twitterRef, ConstraintValidatorContext context) {
        context.disableDefaultConstraintViolation();

        String validatedValue = twitterRef.getValue();
        if(validatedValue == null) {
            context.buildConstraintViolationWithTemplate("{nl.vpro.constraints.NotNull}")
                .addPropertyNode("value")
                .addConstraintViolation();
            return false;
        }


        final Matcher matcher = PATTERN.matcher(validatedValue);
        if(!matcher.find()) {
            context.buildConstraintViolationWithTemplate("{nl.vpro.constraints.twitterRefs.Pattern}")
                .addPropertyNode("value")
                .addConstraintViolation();
            return false;
        }

        if(twitterRef.getType() == null) {
            context.buildConstraintViolationWithTemplate("{nl.vpro.constraints.NotNull}")
                .addPropertyNode("type")
                .addConstraintViolation();
            return false;
        }

        return true;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy