package com.ivfly.xlsbean;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.lang.StringUtils;
/**
* xls 导入规则
* @author Administrator
*
*/
public class XlsImpRule {
private StringBuffer output = new StringBuffer();
/**
* 验证数据的合法性
* @param propertyName
* @param value
* @return TRUE:表示通过
*/
public Boolean validateProperty(BeanSpecification bs,String propertyName,Object value){
PropertySpecification ps = bs.getProperty().get(propertyName);
String formular = ps.getFormular();
if(StringUtils.isBlank(formular)){
return true;
}
String ismethodName = "is"+StringUtils.capitalize(formular);
String setmethodName = "set"+StringUtils.capitalize(formular);
Object result = null;
try {
result = MethodUtils.invokeStaticMethod(ValidateUtil. class,ismethodName,value.toString());
} catch (NoSuchMethodException e) {
e.printStackTrace();
// try {
// result = MethodUtils.invokeStaticMethod(ValidateUtil.class,setmethodName,value);
// } catch (Exception es) {
// es.printStackTrace();
// }
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
if(result!= null){
return (Boolean)result;
}
return Boolean.TRUE;
}
/**
* 添加某属性警告
* @return
*/
public void addPropertyWarrning( int rowNum,BeanSpecification bs,String propertyName){
PropertySpecification ps = bs.getProperty().get(propertyName);
String wf = ps.getWarringFormat();
String result = String.format(wf,ps.getValue());
output.append( "第"+rowNum+ "行数据:");
output.append(result).append( "\n");
}
/**
* 报告警告信息
* @return
*/
public String reportWarrning(){
return output.toString();
}
}
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.lang.StringUtils;
/**
* xls 导入规则
* @author Administrator
*
*/
public class XlsImpRule {
private StringBuffer output = new StringBuffer();
/**
* 验证数据的合法性
* @param propertyName
* @param value
* @return TRUE:表示通过
*/
public Boolean validateProperty(BeanSpecification bs,String propertyName,Object value){
PropertySpecification ps = bs.getProperty().get(propertyName);
String formular = ps.getFormular();
if(StringUtils.isBlank(formular)){
return true;
}
String ismethodName = "is"+StringUtils.capitalize(formular);
String setmethodName = "set"+StringUtils.capitalize(formular);
Object result = null;
try {
result = MethodUtils.invokeStaticMethod(ValidateUtil. class,ismethodName,value.toString());
} catch (NoSuchMethodException e) {
e.printStackTrace();
// try {
// result = MethodUtils.invokeStaticMethod(ValidateUtil.class,setmethodName,value);
// } catch (Exception es) {
// es.printStackTrace();
// }
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
if(result!= null){
return (Boolean)result;
}
return Boolean.TRUE;
}
/**
* 添加某属性警告
* @return
*/
public void addPropertyWarrning( int rowNum,BeanSpecification bs,String propertyName){
PropertySpecification ps = bs.getProperty().get(propertyName);
String wf = ps.getWarringFormat();
String result = String.format(wf,ps.getValue());
output.append( "第"+rowNum+ "行数据:");
output.append(result).append( "\n");
}
/**
* 报告警告信息
* @return
*/
public String reportWarrning(){
return output.toString();
}
}
根据xml 中property节点定义formular定义验证规则,验证对应属性cell值是否合法。
同时,还提供了错误信息生成功能,则根据property节点的warringFormat属性 与Value值合成。
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/756537,如需转载请自行联系原作者