根据XML配置规则导入Excel数据(四)添加数据有效验证

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(); 
  } 

 
根据xml 中property节点定义formular定义验证规则,验证对应属性cell值是否合法。
同时,还提供了错误信息生成功能,则根据property节点的warringFormat属性 与Value值合成。



本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/756537,如需转载请自行联系原作者
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值