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

org.apache.commons.validator.routines.CodeValidator Maven / Gradle / Ivy

Go to download

Apache Commons Validator provides the building blocks for both client side validation and server side data validation. It may be used standalone or with a framework like Struts.

There is a newer version: 1.8.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.validator.routines;

import java.io.Serializable;

import org.apache.commons.validator.routines.checkdigit.CheckDigit;

/**
 * Generic Code Validation providing format, minimum/maximum
 * length and {@link CheckDigit} validations.
 * 

* Performs the following validations on a code: *

    *
  • if the code is null, return null/false as appropriate
  • *
  • trim the input. If the resulting code is empty, return null/false as appropriate
  • *
  • Check the format of the code using a regular expression. (if specified)
  • *
  • Check the minimum and maximum length (if specified) of the parsed code * (i.e. parsed by the regular expression).
  • *
  • Performs {@link CheckDigit} validation on the parsed code (if specified).
  • *
  • The {@link #validate(String)} method returns the trimmed, parsed input (or null if validation failed)
  • *
*

* Note * The {@link #isValid(String)} method will return true if the input passes validation. * Since this includes trimming as well as potentially dropping parts of the input, * it is possible for a String to pass validation * but fail the checkdigit test if passed directly to it (the check digit routines generally don't trim input * nor do they generally check the format/length). * To be sure that you are passing valid input to a method use {@link #validate(String)} as follows: *

 * Object valid = validator.validate(input); 
 * if (valid != null) {
 *    some_method(valid.toString());
 * }
 * 
*

* Configure the validator with the appropriate regular expression, minimum/maximum length * and {@link CheckDigit} validator and then call one of the two validation * methods provided:

*
    *
  • boolean isValid(code)
  • *
  • String validate(code)
  • *
*

* Codes often include format characters - such as hyphens - to make them * more easily human readable. These can be removed prior to length and check digit * validation by specifying them as a non-capturing group in the regular * expression (i.e. use the (?: ) notation). *
* Or just avoid using parentheses except for the parts you want to capture * * @version $Revision$ * @since Validator 1.4 */ public final class CodeValidator implements Serializable { private static final long serialVersionUID = 446960910870938233L; private final RegexValidator regexValidator; private final int minLength; private final int maxLength; private final CheckDigit checkdigit; /** * Construct a code validator with a specified regular * expression and {@link CheckDigit}. * The RegexValidator validator is created to be case-sensitive * * @param regex The format regular expression * @param checkdigit The check digit validation routine */ public CodeValidator(String regex, CheckDigit checkdigit) { this(regex, -1, -1, checkdigit); } /** * Construct a code validator with a specified regular * expression, length and {@link CheckDigit}. * The RegexValidator validator is created to be case-sensitive * * @param regex The format regular expression. * @param length The length of the code * (sets the mimimum/maximum to the same) * @param checkdigit The check digit validation routine */ public CodeValidator(String regex, int length, CheckDigit checkdigit) { this(regex, length, length, checkdigit); } /** * 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; } /** * Construct a code validator with a specified regular expression, * validator and {@link CheckDigit} validation. * * @param regexValidator The format regular expression validator * @param checkdigit The check digit validation routine. */ public CodeValidator(RegexValidator regexValidator, CheckDigit checkdigit) { this(regexValidator, -1, -1, checkdigit); } /** * Construct a code validator with a specified regular expression, * validator, length and {@link CheckDigit} validation. * * @param regexValidator The format regular expression validator * @param length The length of the code * (sets the mimimum/maximum to the same value) * @param checkdigit The check digit validation routine */ public CodeValidator(RegexValidator regexValidator, int length, CheckDigit checkdigit) { this(regexValidator, length, length, checkdigit); } /** * Construct a code validator with a specified regular expression * validator, minimum/maximum length and {@link CheckDigit} validation. * * @param regexValidator The format regular expression validator * @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(RegexValidator regexValidator, int minLength, int maxLength, CheckDigit checkdigit) { this.regexValidator = regexValidator; this.minLength = minLength; this.maxLength = maxLength; this.checkdigit = checkdigit; } /** * Return the check digit validation routine. *

* N.B. Optional, if not set no Check Digit * validation will be performed on the code. * * @return The check digit validation routine */ public CheckDigit getCheckDigit() { return checkdigit; } /** * Return the minimum length of the code. *

* N.B. Optional, if less than zero the * minimum length will not be checked. * * @return The minimum length of the code or * -1 if the code has no minimum length */ public int getMinLength() { return minLength; } /** * Return the maximum length of the code. *

* N.B. Optional, if less than zero the * maximum length will not be checked. * * @return The maximum length of the code or * -1 if the code has no maximum length */ public int getMaxLength() { return maxLength; } /** * Return the regular expression validator. *

* N.B. Optional, if not set no regular * expression validation will be performed on the code. * * @return The regular expression validator */ public RegexValidator getRegexValidator() { return regexValidator; } /** * Validate the code returning either true * or false. *

* This calls {@link #validate(String)} and returns false * if the return value is null, true otherwise. *

* Note that {@link #validate(String)} trims the input * and if there is a {@link RegexValidator} it may also * change the input as part of the validation. * * @param input The code to validate * @return true if valid, otherwise * false */ public boolean isValid(String input) { return (validate(input) != null); } /** * Validate the code returning either the valid code or * null if invalid. *

* Note that this method trims the input * and if there is a {@link RegexValidator} it may also * change the input as part of the validation. * * @param input The code to validate * @return The code if valid, otherwise null * if invalid */ public Object validate(String input) { if (input == null) { return null; } String code = input.trim(); if (code.length() == 0) { return null; } // validate/reformat using regular expression if (regexValidator != null) { code = regexValidator.validate(code); if (code == null) { return null; } } // check the length (must be done after validate as that can change the code) if ((minLength >= 0 && code.length() < minLength) || (maxLength >= 0 && code.length() > maxLength)) { return null; } // validate the check digit if (checkdigit != null && !checkdigit.isValid(code)) { return null; } return code; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy