package webapp.util.validation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 判断实体元素值长度是否大于数据表限制长度
* 及判断字段值是否符合限制输入的类型
* @author lzb
*
*/
public class CheckStringUtil {
public String errormsg="";//错误信息
public Map<String,String> map;
public CheckStringUtil(){
Map<String,String> map=new HashMap<String, String>();
map.put("*", "[\\w\\W]+");
map.put("n", "\\d+$");
map.put("s", "[\\u4E00-\\u9FA5\\uf900-\\ufa2d\\w\\.\\s]+$");
map.put("p", "[0-9]{6}$");
map.put("m", "13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$");
map.put("e", "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
map.put("url", "(\\w+:\\/\\/)?\\w+(\\.\\w+)+.*$");
map.put("IDCard", "[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{4}$");
map.put("d", "^(-?\\d+)(\\.\\d+)?$");///浮点型正则
map.put("y", "[A-Za-z0-9]+$");///不含特殊符号的非中文字符
map.put("zh", "[\u4e00-\u9fa5]+");///只能输入中文字符
this.map=map;
}
public String getErrormsg() {
return errormsg;
}
public void setErrormsg(String errormsg) {
this.errormsg = errormsg;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
/**
* 执行长度判断
*
* @param pojoClass
* 需要判断的实体类
* @param dataSet
* 存值后的实体对象
* @return 返回true 表示实体内所有值在限制长度之内,否则返回false
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public boolean checkLength(Class<?> pojoClass, Object obj)
throws SecurityException, NoSuchMethodException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
boolean chenkRS = true;// 检测结果
String missmsg="";
int error=0;
Field fileds[] = pojoClass.getDeclaredFields();
for (int i = 0; i < fileds.length; i++) {
Field field = fileds[i];
CheckString col = field.getAnnotation(CheckString.class);
if (col != null) {
String fieldname = field.getName();
StringBuffer getMethodName = new StringBuffer("get");
getMethodName.append(fieldname.substring(0, 1).toUpperCase());
getMethodName.append(fieldname.substring(1));
Method getMethod = pojoClass.getMethod(
getMethodName.toString(), new Class[] {});
Object str = getMethod.invoke(obj, new Object[] {});
if(isNotEmpty(str)){
str=str.toString().trim();
}
//判断必填字段输入的字是否为空
if(col.notNull()&&!isNotEmpty(str)){
missmsg+=col.errorMsg()+"\n";
error++;
continue;
}
//判断最大限制长度,不受限制输入的最小长度的作用
if (col.maxLength()>0&&col.mineLiegth()==-1) {
if(isNotEmpty(str)){
if(str.toString().length()>col.maxLength()){
missmsg+=col.errorMsg()+"\n";
error++;
continue;
}
}
}else if (col.maxLength()>0&&col.mineLiegth()>-1) {
if(isNotEmpty(str)){
if(str.toString().length()>col.maxLength()||str.toString().length()<col.mineLiegth()){
missmsg+=col.errorMsg()+"\n";
error++;
continue;
}
}else {
missmsg+=col.errorMsg()+"\n";
error++;
continue;
}
}else if(col.maxLength()==-1&&col.mineLiegth()>-1){
if(isNotEmpty(str)){
if(str.toString().length()<col.mineLiegth()){
missmsg+=col.errorMsg()+"\n";
error++;
continue;
}
}else {
missmsg+=col.errorMsg()+"\n";
error++;
continue;
}
}
if(isNotEmpty(col.dataType())){
//Map<String,String> map=this.getMap();
///String reg=map.get(col.dataType());
String reg=createRegex(col.dataType());
boolean rs=true;
if(isNotEmpty(col.dataType())&&col.ignore()){
if(isNotEmpty(str)){
if(isNotEmpty(reg)){
rs=isMatch(reg, str.toString());
}else{
rs=isMatch(col.dataType(), str.toString());
}
if (!rs){
missmsg+=col.errorMsg()+"\n";
error++;
continue;
}
}
}else if(isNotEmpty(col.dataType())&&!col.ignore()){
if(isNotEmpty(str)){
if(isNotEmpty(reg)){
rs=isMatch(reg, str.toString());
}else{
rs=isMatch(col.dataType(), str.toString());
}
}
if (!rs||!isNotEmpty(str)){
missmsg+=col.errorMsg()+"\n";
error++;
continue;
}
}
}
}
}
if(error>0){
chenkRS=false;
this.setErrormsg(missmsg);
}
return chenkRS;
}
/**
* 判断字段值是否为空
*
* @param str
* @return
*/
public static boolean isNotEmpty(Object str) {
boolean flag = true;
if ((str != null) && (!str.equals(""))) {
if (str.toString().length() > 0)
flag = true;
} else {
flag = false;
}
return flag;
}
/**
* 判断字符串是否满足正则
*
* @param regex
* 正则
* @param str
* 内容
* @return 符合则返回true
*/
public boolean isMatch(String regex, String str) {
boolean b = true;
Pattern p = Pattern.compile(regex);
java.util.regex.Matcher m = p.matcher(str);
b = m.matches();
return b;
}
/**
* 组装正则表达式
* @param dataType 正则校验类型
* @param value 需要校验的值
* @return
*/
public String createRegex(String dataType){
Map<String,String> map=this.getMap();///正则校验 map
String rs=map.get(dataType);
Pattern p = Pattern.compile("(zh(.*?)|y(.*?)|n(.*?)|s(.*?)|\\*(.*?))");///获取datatype 的校验类型,返还 zh,y,n,s,*
Matcher m = p.matcher(dataType);//正则校验
while(m.find()) {
String reg=map.get(m.group(0));///m.group(0) 返还的值为 zh,y,n,s,* 中的一个,通过返回值从map中得到相应的相应的正则
if(this.isNotEmpty(reg)){
String newReg=m.group(0);
if(newReg.indexOf("*")>-1){
newReg="\\"+newReg;
}
Pattern p1 = Pattern.compile(newReg+"(.*?);");///组装正则 获取 zh,y,n,s,* 以上类型后面的附带条件,如获取zh1-5(只能输入1-5位的中文字符)的附带信息为“1-5”
Matcher m1 = p1.matcher(dataType+";");
while(m1.find()){
String type=m1.group(1);//返回的为zh1-5 中1-5
///从新组装 从map 获取到的正则表达式 开始
if(this.isNotEmpty(type)&&type.indexOf("-")>-1){
type=type.replaceAll("-", ",");
type="{"+type+"}";
reg=reg.replaceAll("\\+", type);
}else if(this.isNotEmpty(type)&&type.indexOf("-")==-1){
type="{"+type+"}";
reg=reg.replaceAll("\\+", type);
}
/// 结束
rs=reg;
}
}
}
return rs;
}
public static void main(String a[]){
CheckStringUtil u=new CheckStringUtil();
// System.out.print(u.isMatch("[\u4e00-\u9fa5]+", "111"));
String filetext = "*1-10";
Map<String,String> regMap=new HashMap<String, String>();
// regMap.put("", value)
System.out.print(u.createRegex(filetext));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
checkUtil.zip (11个子文件)
checkUtil
checkutil工具类使用说明.doc 161KB
src
webapp
util
validation
CheckStringUtil.java 8KB
CheckString.java 1KB
Demo.java 2KB
bin
webapp
util
validation
Demo.class 2KB
CheckStringUtil.class 7KB
CheckString.class 633B
.classpath 307B
.settings
org.eclipse.core.resources.prefs 88B
org.eclipse.jdt.core.prefs 629B
.project 385B
共 11 条
- 1
资源评论
- 洋小葱2015-01-14借鉴一下。觉得不错。正好用在项目中。谢谢!
L-zbin
- 粉丝: 28
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功