com.alogic.validator.impl.Regx Maven / Gradle / Ivy
package com.alogic.validator.impl;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import com.alogic.validator.Validator;
import com.anysoft.util.BaseException;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
/**
* 正则表达式匹配
* @author yyduan
* @since 1.6.12.11 [20181206 duanyy]
*/
public class Regx extends Validator.Abstract{
protected Pattern pattern = null;
protected String message = "";
protected String code = "clnt.e2000";
@Override
public void configure(Properties p) {
super.configure(p);
code = PropertiesConstants.getString(p,"code",code,true);
message = PropertiesConstants.getString(p,"message","",true);
String regex = PropertiesConstants.getString(p,"pattern","",true);
if (StringUtils.isNotEmpty(regex)){
try {
pattern = Pattern.compile(regex);
}catch (Exception ex){
LOG.error(ExceptionUtils.getStackTrace(ex));
}
}
}
@Override
public boolean check(String value, boolean throwException) {
boolean result = true;
if (pattern != null){
Matcher matcher = pattern.matcher(value);
result = matcher.matches();
}
if (!result && throwException){
throw new BaseException("clnt.e2000",message);
}
return result;
}
@Override
public String getMessage() {
return message;
}
@Override
public String getCode() {
return code;
}
}