com.feilong.lib.validator.RegexValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feilong Show documentation
Show all versions of feilong Show documentation
feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.
/*
* 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 com.feilong.lib.validator;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Regular Expression validation (using JDK 1.4+ regex support).
*
* Construct the validator either for a single regular expression or a set (array) of
* regular expressions. By default validation is case sensitive but constructors
* are provided to allow case in-sensitive validation. For example to create
* a validator which does case in-sensitive validation for a set of regular
* expressions:
*
*
*
*
* String[] regexs = new String[] {...};
* RegexValidator validator = new RegexValidator(regexs, false);
*
*
*
*
* - Validate
true
or false
:
* -
*
* boolean valid = validator.isValid(value);
*
*
* - Validate returning an aggregated String of the matched groups:
* -
*
* String result = validator.validate(value);
*
*
* - Validate returning the matched groups:
* -
*
* String[] result = validator.match(value);
*
*
*
*
* Note that patterns are matched against the entire input.
*
*
* Cached instances pre-compile and re-use {@link Pattern}(s) - which according
* to the {@link Pattern} API are safe to use in a multi-threaded environment.
*
*
* @version $Revision: 1739356 $
* @since Validator 1.4
*/
public class RegexValidator implements Serializable{
private static final long serialVersionUID = -8832409930574867162L;
private final Pattern[] patterns;
/**
* Construct a case sensitive validator for a single
* regular expression.
*
* @param regex
* The regular expression this validator will
* validate against
*/
public RegexValidator(String regex){
this(new String[] { regex }, true);
}
/**
* Construct a validator that matches any one of the set of regular
* expressions with the specified case sensitivity.
*
* @param regexs
* The set of regular expressions this validator will
* validate against
* @param caseSensitive
* when true
matching is case
* sensitive, otherwise matching is case in-sensitive
*/
public RegexValidator(String[] regexs, boolean caseSensitive){
if (regexs == null || regexs.length == 0){
throw new IllegalArgumentException("Regular expressions are missing");
}
patterns = new Pattern[regexs.length];
int flags = (caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
for (int i = 0; i < regexs.length; i++){
if (regexs[i] == null || regexs[i].length() == 0){
throw new IllegalArgumentException("Regular expression[" + i + "] is missing");
}
patterns[i] = Pattern.compile(regexs[i], flags);
}
}
/**
* Validate a value against the set of regular expressions.
*
* @param value
* The value to validate.
* @return true
if the value is valid
* otherwise false
.
*/
public boolean isValid(String value){
if (value == null){
return false;
}
for (Pattern pattern : patterns){
if (pattern.matcher(value).matches()){
return true;
}
}
return false;
}
/**
* Validate a value against the set of regular expressions
* returning the array of matched groups.
*
* @param value
* The value to validate.
* @return String array of the groups matched if
* valid or null
if invalid
*/
public String[] match(String value){
if (value == null){
return null;
}
for (Pattern pattern : patterns){
Matcher matcher = pattern.matcher(value);
if (matcher.matches()){
int count = matcher.groupCount();
String[] groups = new String[count];
for (int j = 0; j < count; j++){
groups[j] = matcher.group(j + 1);
}
return groups;
}
}
return null;
}
/**
* Provide a String representation of this validator.
*
* @return A String representation of this validator
*/
@Override
public String toString(){
StringBuilder buffer = new StringBuilder();
buffer.append("RegexValidator{");
for (int i = 0; i < patterns.length; i++){
if (i > 0){
buffer.append(",");
}
buffer.append(patterns[i].pattern());
}
buffer.append("}");
return buffer.toString();
}
}