/** * Construct a code validator with a specified regular * expression, minimum/maximum length and {@link CheckDigit} validation. * The RegexValidator validator is created to be case-sensitive * * @param regex The regular expression * @param minLength The minimum length of the code * @param maxLength The maximum length of the code * @param checkdigit The check digit validation routine */ public CodeValidator(String regex, int minLength, int maxLength, CheckDigit checkdigit) { if (regex != null && regex.length() > 0) { this.regexValidator = new RegexValidator(regex); } else { this.regexValidator = null; } this.minLength = minLength; this.maxLength = maxLength; this.checkdigit = checkdigit; }
/** * Test exceptions */ public void testExceptions() { String invalidRegex = "^([abCD12]*$"; try { new RegexValidator(invalidRegex); } catch (PatternSyntaxException e) { // expected } }
/** * Creates the validator * @param cc the country code * @param len the length of the IBAN * @param format the regex to use to check the format */ public Validator(String cc, int len, String format) { if (!(cc.length() == 2 && Character.isUpperCase(cc.charAt(0)) && Character.isUpperCase(cc.charAt(1)))) { throw new IllegalArgumentException("Invalid country Code; must be exactly 2 upper-case characters"); } if (len > MAX_LEN || len < MIN_LEN) { throw new IllegalArgumentException("Invalid length parameter, must be in range "+MIN_LEN+" to "+MAX_LEN+" inclusive: " +len); } if (!format.startsWith(cc)) { throw new IllegalArgumentException("countryCode '"+cc+"' does not agree with format: " + format); } this.countryCode = cc; this.lengthOfIBAN = len; this.validator = new RegexValidator(format); } }
/** * Test toString() method */ public void testToString() { RegexValidator single = new RegexValidator(REGEX); assertEquals("Single", "RegexValidator{" + REGEX + "}", single.toString()); RegexValidator multiple = new RegexValidator(new String[] {REGEX, REGEX}); assertEquals("Multiple", "RegexValidator{" + REGEX + "," + REGEX + "}", multiple.toString()); }
new RegexValidator((String)null); fail("Single Null - expected IllegalArgumentException"); } catch (IllegalArgumentException e) { new RegexValidator(""); fail("Single Zero Length - expected IllegalArgumentException"); } catch (IllegalArgumentException e) { new RegexValidator((String[])null); fail("Null Array - expected IllegalArgumentException"); } catch (IllegalArgumentException e) { new RegexValidator(new String[0]); fail("Zero Length Array - expected IllegalArgumentException"); } catch (IllegalArgumentException e) { new RegexValidator(expressions); fail("Array has Null - expected IllegalArgumentException"); } catch (IllegalArgumentException e) { new RegexValidator(expressions); fail("Array has Zero Length - expected IllegalArgumentException"); } catch (IllegalArgumentException e) {
/** * Test Null value */ public void testNullValue() { RegexValidator validator = new RegexValidator(REGEX); assertEquals("Instance isValid()", false, validator.isValid(null)); assertEquals("Instance validate()", null, validator.validate(null)); assertEquals("Instance match()", null, validator.match(null)); }
/** * Test instance methods with single regular expression. */ public void testSingle() { RegexValidator sensitive = new RegexValidator(REGEX); RegexValidator insensitive = new RegexValidator(REGEX, false); // isValid() assertEquals("Sensitive isValid() valid", true, sensitive.isValid("ac-DE-1")); assertEquals("Sensitive isValid() invalid", false, sensitive.isValid("AB-de-1")); assertEquals("Insensitive isValid() valid", true, insensitive.isValid("AB-de-1")); assertEquals("Insensitive isValid() invalid", false, insensitive.isValid("ABd-de-1")); // validate() assertEquals("Sensitive validate() valid", "acDE1", sensitive.validate("ac-DE-1")); assertEquals("Sensitive validate() invalid", null, sensitive.validate("AB-de-1")); assertEquals("Insensitive validate() valid", "ABde1", insensitive.validate("AB-de-1")); assertEquals("Insensitive validate() invalid", null, insensitive.validate("ABd-de-1")); // match() checkArray("Sensitive match() valid", new String[] {"ac", "DE", "1"}, sensitive.match("ac-DE-1")); checkArray("Sensitive match() invalid", null, sensitive.match("AB-de-1")); checkArray("Insensitive match() valid", new String[] {"AB", "de", "1"}, insensitive.match("AB-de-1")); checkArray("Insensitive match() invalid", null, insensitive.match("ABd-de-1")); assertEquals("validate one", "ABC", (new RegexValidator("^([A-Z]*)$")).validate("ABC")); checkArray("match one", new String[] {"ABC"}, (new RegexValidator("^([A-Z]*)$")).match("ABC")); }
public static boolean isValidMac(final String macAddr) { RegexValidator mv = new RegexValidator("^(?:[0-9a-f]{1,2}([-:\\.]))(?:[0-9a-f]{1,2}\\1){4}[0-9a-f]{1,2}$", false); return mv.isValid(macAddr); }
RegexValidator multiple = new RegexValidator(MULTIPLE_REGEX, false); RegexValidator single1 = new RegexValidator(REGEX_1, false); RegexValidator single2 = new RegexValidator(REGEX_2, false); RegexValidator single3 = new RegexValidator(REGEX_3, false);
RegexValidator multiple = new RegexValidator(MULTIPLE_REGEX); RegexValidator single1 = new RegexValidator(REGEX_1); RegexValidator single2 = new RegexValidator(REGEX_2); RegexValidator single3 = new RegexValidator(REGEX_3);
validator = new CodeValidator(new RegexValidator(regex), 6, (CheckDigit)null); assertEquals("Reformat 123-456", "123456", validator.validate("123-456")); assertEquals("Reformat 123 456", "123456", validator.validate("123 456")); validator = new CodeValidator(new RegexValidator(regex), 6, (CheckDigit)null); assertEquals("Reformat 2 Regex", "RegexValidator{" + regex + "}", validator.getRegexValidator().toString()); assertEquals("Reformat 2 123-456", "123456", validator.validate("123-456"));
public void testValidator248() { RegexValidator regex = new RegexValidator(new String[] {"localhost", ".*\\.my-testing"}); UrlValidator validator = new UrlValidator(regex, 0); assertTrue("localhost URL should validate", validator.isValid("http://localhost/test/index.html")); assertTrue("first.my-testing should validate", validator.isValid("http://first.my-testing/test/index.html")); assertTrue("sup3r.my-testing should validate", validator.isValid("http://sup3r.my-testing/test/index.html")); assertFalse("broke.my-test should not validate", validator.isValid("http://broke.my-test/test/index.html")); assertTrue("www.apache.org should still validate", validator.isValid("http://www.apache.org/test/index.html")); // Now check using options validator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS); assertTrue("localhost URL should validate", validator.isValid("http://localhost/test/index.html")); assertTrue("machinename URL should validate", validator.isValid("http://machinename/test/index.html")); assertTrue("www.apache.org should still validate", validator.isValid("http://www.apache.org/test/index.html")); }
UrlValidator urlValidator = new UrlValidator(schemes, new RegexValidator("^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$"), 0L);
RegexValidator regex = new RegexValidator("^[0-9]*$");
private static UrlValidator createUrlValidatorWithRegex(final boolean allowLocalUrls, final String authorityValidationRegEx, final boolean authorityValidationRegExCaseSensitive) { if (StringUtils.isEmpty(authorityValidationRegEx)) { return null; } val authorityValidator = new RegexValidator(authorityValidationRegEx, authorityValidationRegExCaseSensitive); val options = allowLocalUrls ? UrlValidator.ALLOW_LOCAL_URLS : 0; return new UrlValidator(authorityValidator, options); }
/** * Checks if the value matches the regular expression. */ public boolean matchRegex(String pattern, String input) { return new RegexValidator(pattern).isValid(input); }
public WidgetValidator() { super(); RegexValidator regex = new RegexValidator(new String[] {"http", "https","((localhost)(:[0-9]+))"}); urlValidator = new UrlValidator(regex, 0); }
public static boolean isValidMac(final String macAddr) { final RegexValidator mv = new RegexValidator("^(?:[0-9a-f]{1,2}([-:\\.]))(?:[0-9a-f]{1,2}\\1){4}[0-9a-f]{1,2}$", false); return mv.isValid(macAddr); }
// Copied from org.apache.commons.validator.routines.DomainValidator private static final String DOMAIN_LABEL_REGEX = "\\p{Alnum}(?>[\\p{Alnum}-]*\\p{Alnum})*"; // Changed to include new gTLD - http://data.iana.org/TLD/tlds-alpha-by-domain.txt private static final String TOP_LABEL_REGEX = "\\p{Alpha}[\\p{Alnum}-]*\\p{Alpha}"; // Copied from org.apache.commons.validator.routines.DomainValidator private static final String DOMAIN_NAME_REGEX = "^(?:" + DOMAIN_LABEL_REGEX + "\\.)+" + "(" + TOP_LABEL_REGEX + ")$"; private static final RegexValidator domainRegex = new RegexValidator(DOMAIN_NAME_REGEX); private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator(); public static boolean isValidDomain(String domain) { String[] groups = domainRegex.match(domain); return groups != null && groups.length > 0; }
@Override public ValidationResult validate(MuleEvent event) { RegexValidator validator = new RegexValidator(new String[] {regex}, caseSensitive); return validator.isValid(value) ? ok() : fail(); }