模块间接口报文校验1

参数校验
模板直接同步报文需要对报文个字段的类型、长度进行校验
1、参数校验的文档
2、解析参数校验文档的类、读取报文信息与参数校验文档进行比较的方法
 web.xml配置信息
   <context-param>
    <param-name>checkConfigFilePath</param-name>
    <param-value>conf/para_check_config.xml</param-value>
  </context-param>
para_check_config.xml

<?xml version="1.0" encoding="UTF-8"?>
<ruleConfig>

    <!-- 模板同步
            Synchronize template -->
    <interface id="interRuler.SyncTemplateServiceImpl">
        <field id="TemplateID" regex="^[0-9]{1,13}$" maxLength="13" need="yes" />
        <field id="TemplateCode" regex="^[A-Za-z0-9_]$" maxLength="100" need="yes" />
        <field id="TemplateName" maxLength="300"  need="yes" />
        <field id="TemplateType" regex="^[0-9]{1,13}$" maxLength="2" need="yes" />
        <field id="IsDefault" regex="^[0,1]$" need="yes" />
        <field id="HomePage" maxLength="100" need="yes" />
        <field id="Remark" maxLength="300" need="no" />
        <field id="FilePath" maxLength="500" need="yes" />
        <field id="CityCode" regex="^[0-9]{1,6}$" maxLength="15" need="yes" />
    </interface>

    <!-- 栏目排序同步
         Synchronize the sequence of columns -->
    <interface id="interRuler.SyncColumnRankServiceImpl">
        <field id="RankModList" regex="^\." type="list" need="yes" />
        <field id="ColumnID" regex="^[0-9]{1,13}$" need="yes" />
        <field id="Rank" regex="^[1-9][0-9]{0,12}$" need="yes" />
    </interface>
    
    <!-- 内容上架排序同步
         Synchronize the sequence of loaded contents -->
    <interface id="interRuler.SyncUpShelfRankServiceImpl">
        <field id="RankModList" regex="^\." type="list" need="yes" />
        <field id="ColumnID" regex="^[0-9]{1,13}$" need="no" />
        <field id="Rank" regex="^[1-9][0-9]{0,12}$" need="yes" />
        <field id="ColumnRefResourceID" regex="^[0-9]{1,13}$" need="yes" />
    </interface>
    
    <!-- 栏目图片同步
         Synchronize column image -->
    <interface id="interRuler.SyncColumnImageServiceImpl">
        <field id="ComumnImageID" regex="^[0-9]{1,13}$" need="yes" />
        <field id="ColumnID" regex="^[0-9]{1,13}$" need="yes" />
        <field id="ImageName" regex="^.{1,100}$" need="no" />
        <field id="ImageUrl" regex="^.{1,512}$" need="no" />
        <field id="Rank" regex="^[1-9][0-9]{0,12}$" need="no" />
    </interface>
    <interface id="interRuler.DelColumnImageServiceImpl">
        <field id="ColumnImageList" regex="^\." type="list" need="yes" />
        <field id="ComumnImageID" regex="^[0-9]{1,13}$" need="yes" />
        <field id="ColumnID" regex="^[0-9]{1,13}$" need="yes" />
    </interface>
    
    <!-- 栏目关联产品信息同步
         Synchronize the information of products that are associated to columns -->
    <interface id="interRuler.SyncRelGoodsServiceImpl">
        <field id="ColumnID" regex="^[0-9]{1,13}$" need="yes" />
    </interface>
        
    <!-- 媒资删除
         Asset delete -->
    <interface id="interRuler.DelAssetPackageServiceImpl">
        <field id="resourceCode" regex="[\s\S]*" need="yes" maxLength="64"/>
        <field id="assetID" regex="^[a-zA-Z0-9]*$"  need="no" maxLength="21"/>
        <field id="CityCode" regex="[\s\S]*" need="no" maxLength="13"/>
    </interface>

    
</ruleConfig>


package com.comname.miss.util.linstener;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.comname.dhm.aaa.commons.util.Utility;
import com.comname.miss.util.config.CheckField;
import com.comname.miss.util.config.CheckRulerConfig;
import com.comname.miss.util.config.FieldRuler;
import com.comname.miss.util.config.InterRulerConfig;
import com.comname.miss.util.config.JoinFiledRuler;
import com.comname.miss.util.config.SerFactory;

/**
 * service层的初始化listener
 * SerListener.java
 * <p>Copyright: Copyright (c) 2009 <p>
 * <p>Company: comname</p>
 *  @author    
 *  @version   1.0
 */
public class InitSerListener implements ServletContextListener
{
    Logger logger = Logger.getLogger(InitSerListener.class);

    public void contextDestroyed(ServletContextEvent context)
    {
    }

    public void contextInitialized(ServletContextEvent context)
    {
        // 初始化参数校验配置
        this.initParaCheckConfig(context);
    }

    // 使用dom4j进行xml的解析
    @SuppressWarnings( { "unchecked", "unchecked" })
    private void initParaCheckConfig(ServletContextEvent context)
    {
        SAXReader saxReader = new SAXReader();
        //读取 web.xml的初始化值
        /**<context-param>                                      <context-param>
            <param-name>dhm-system-name</param-name>              <param-name>checkConfigFilePath</param-name>
            <param-value>portalMS</param-value>                   <param-value>conf/para_check_config.xml</param-value>
        </context-param>                                     </context-param> */
        String checkConfigPath = context.getServletContext().getInitParameter("checkConfigFilePath");
        String aaa_system_name = context.getServletContext().getInitParameter("dhm-system-name");
        //获取校验文件地址 D:\tomcat_portal\bin\portalMS\conf\para_check_config.xml
        String path = Utility.getConfigFullPath(checkConfigPath, aaa_system_name);
        try
        {   //读取para_check_config.xml文件内容
            //文档
            Document document = saxReader.read(new File(path));
            //文档根元素 (content = ArrayList<E>)
            Element root = document.getRootElement();
            //根元素内容
            Iterator iter = root.elementIterator();
            /**
             * public class CheckRulerConfig
            {
                public Map<String, InterRulerConfig> iRuleMap = new ConcurrentHashMap<String, InterRulerConfig>();
            
                public Map<String, InterRulerConfig> getIRuleMap()
                {
                    return iRuleMap;
                }
            }
            
            public class InterRulerConfig
            {
                public Map<String, FieldRuler> fRuleMap = new ConcurrentHashMap<String, FieldRuler>();
            
                public String ref = "";
                
             对于配置文件中的字段   
            public class FieldRuler
            {
                public String id;
                public String regex;
                public String need;
                public String type;
                public String maxLength;
                public JoinFiledRuler joinCheckFiled;
                
            public class JoinFiledRuler
            {
                public Map<String, CheckField> joinCheck;
                
            public class CheckField
            {
                private String refField;
                private String regex;
                private String maxLength;
                
            para_check_config.xml:    
             <?xml version="1.0" encoding="UTF-8"?>
                <ruleConfig>
                <!-- 栏目排序同步
                     Synchronize the sequence of columns -->
                    <interface id="interRuler.SyncColumnRankServiceImpl">
                        <field id="RankModList" regex="^\." type="list" need="yes" />
                        <field id="ColumnID" regex="^[0-9]{1,13}$" need="yes" />
                        <field id="Rank" regex="^[1-9][0-9]{0,12}$" need="yes" />
                    </interface>
                </ruleConfig>
           CheckRulerConfig -> InterRulerConfig -> FieldRuler ->JoinFiledRuler->CheckField   
             */
            CheckRulerConfig crc = new CheckRulerConfig();
            Map mc = crc.getIRuleMap();
            while (iter.hasNext())
            {   //获取一个配置项的值 <interface XXX> xxxx</interface> 之间的内容
                Element ie = (Element) iter.next();
                //用于遍历每个 <field  (interface 的子节点<field
                Iterator it = ie.elementIterator();
                InterRulerConfig irc = new InterRulerConfig();
               
                // 如果存在ref项就取出来
                if (ie.attributeValue("ref") != null)
                {
                    irc.setRef(ie.attributeValue("ref"));
                }
                //取出InterRulerConfig 的Map<String, FieldRuler> fRuleMap 给其赋值
                Map mi = irc.getFRuleMap();
                while (it.hasNext())
                {   //每个Field的值 如:<field id="ColumnID" regex="^[0-9]{1,13}$" need="yes" />
                    Element fe = (Element) it.next();
                    FieldRuler fr = new FieldRuler();
                    //给每个FieldRuler赋值
                    fr.setId(fe.attributeValue("id").toLowerCase());
                    fr.setRegex(fe.attributeValue("regex"));
                    fr.setNeed(fe.attributeValue("need"));
                    fr.setType(fe.attributeValue("type"));
                    fr.setMaxLength(fe.attributeValue("maxLength"));
                    
                    //遍历<field id="ColumnID" regex="^[0-9]{1,13}$" need="yes" /> 每个值给CheckField赋值
                    //<field 子节点 如果没有就不需要给CheckField赋值 直接跳过
                    Iterator it1 = fe.elementIterator();
                    while (it1.hasNext())
                    {
                        JoinFiledRuler joinRule = new JoinFiledRuler();
                        Map<String, CheckField> resultMap = new HashMap<String, CheckField>();
                        Element fe1 = (Element) it1.next();
                        buildJoinFiledFuler(fe1, resultMap);
                        joinRule.setJoinCheck(resultMap);
                        fr.setJoinCheckFiled(joinRule);
                    }
                    //FieldRuler 保存到 Map<String, FieldRuler> fRuleMap 中
                    mi.put(fr.getId().toLowerCase(), fr);
                    logger.debug(fr);

                }
                //给CheckRulerConfig的Map<String, InterRulerConfig> iRuleMap 赋值
                mc.put(ie.attributeValue("id"), irc);
            }
            //SerFactorychengy
            SerFactory.setCheckConfig(crc);
        }
        catch (Exception e)
        {
            logger.error("load check ruler file failed!", e);
        }

    }

    /**
     * 解析joinCheck元素下值
     * @param fe1
     * @return
     */
    @SuppressWarnings("unchecked")
    private static void buildJoinFiledFuler(Element fe1, Map<String, CheckField> resultMap)
    {

        Iterator it1 = fe1.elementIterator();
        while (it1.hasNext())
        {

            Element fe3 = (Element) it1.next();
            Iterator it2 = fe3.elementIterator();
            while (it2.hasNext())
            {
                CheckField checkField = new CheckField();
                Element fe4 = (Element) it2.next();
                checkField.setRefField(fe4.attributeValue("refField"));
                checkField.setRegex(fe4.attributeValue("regex"));
                checkField.setMaxLength(fe4.attributeValue("maxLength"));
                resultMap.put(fe3.attributeValue("value"), checkField);
            }
        }

    }

}


package com.comname.dhm.aaa.commons.util;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.log4j.Logger;
/**获取文件绝对路径的工具类*/
public class Utility
{

    public Utility()
    {
    }

    public static String getConfigFullPath(String configFileName)
    {
        String configFile = configFileName.replace('/', File.separator.charAt(0));
        if(!configFile.startsWith(File.separator))
            configFile = (new StringBuilder()).append(File.separator).append(configFile).toString();
        return (new StringBuilder()).append(System.getProperty("user.dir")).append(configFile).toString();
    }

    public static String getConfigFullPath(String configFileName, String systemName)
    {
        String configFile = configFileName.replace('/', File.separator.charAt(0));
        if(!configFile.startsWith(File.separator))
            configFile = (new StringBuilder()).append(File.separator).append(configFile).toString();
        if(!systemName.equals(""))
            systemName = systemName.startsWith(File.separator) ? systemName : (new StringBuilder()).append(File.separator).append(systemName).toString();
        return (new StringBuilder()).append(System.getProperty("user.dir")).append(systemName).append(configFile).toString();
    }

    public static Map transformHashMapToConcurrentHashMap(Map map)
    {
        Map cMap = new ConcurrentHashMap();
        String key;
        for(Iterator iter = map.keySet().iterator(); iter.hasNext(); cMap.put(key, map.get(key)))
            key = (String)iter.next();

        return cMap;
    }

    public static String getLocalIp()
    {
        String localIp = null;
        try
        {
            localIp = InetAddress.getLocalHost().getHostAddress();
        }
        catch(UnknownHostException e)
        {
            logger.error(e.getMessage(), e);
        }
        return localIp;
    }

    private static Logger logger = Logger.getLogger(com/comname/dhm/aaa/commons/util/Utility);
    public static final String OS_NAME = System.getProperty("os.name");
    public static boolean IS_WINDOWS = false;

    static
    {
        if(OS_NAME.toLowerCase().startsWith("windows"))
            IS_WINDOWS = true;
    }
}

package com.comname.miss.util.config;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CheckRulerConfig
{
    public Map<String, InterRulerConfig> iRuleMap = new ConcurrentHashMap<String, InterRulerConfig>();

    public Map<String, InterRulerConfig> getIRuleMap()
    {
        return iRuleMap;
    }
}


package com.coship.miss.util.config;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class InterRulerConfig
{
    public Map<String, FieldRuler> fRuleMap = new ConcurrentHashMap<String, FieldRuler>();

    public String ref = "";

    public String getRef()
    {
        return ref;
    }

    public void setRef(String ref)
    {
        this.ref = ref;
    }

    public Map<String, FieldRuler> getFRuleMap()
    {
        return fRuleMap;
    }
}


package com.comname.miss.util.config;

/**
 *it's javaBean ,用于参数校验框架
 * FieldRuler.java
 * <p>Copyright: Copyright (c) 2009 <p>
 * <p>Company: comname</p>
 *  @author   
 *  @version   1.0
 */

public class FieldRuler
{
    public String id;
    public String regex;
    public String need;
    public String type;
    public String maxLength;
    public JoinFiledRuler joinCheckFiled;

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getNeed()
    {
        return need;
    }

    public void setNeed(String need)
    {
        this.need = need;
    }

    public String getRegex()
    {
        return regex;
    }

    public void setRegex(String regex)
    {
        this.regex = regex;
    }

    public String getMaxLength()
    {
        return maxLength;
    }

    public void setMaxLength(String maxLength)
    {
        this.maxLength = maxLength;
    }

    public String toString()
    {
        return "id=" + id + ",regex=" + regex + ",constraint=" + need + ",type=" + type + ",maxLength=" + maxLength;
    }

    public JoinFiledRuler getJoinCheckFiled()
    {
        return joinCheckFiled;
    }

    public void setJoinCheckFiled(JoinFiledRuler joinCheckFiled)
    {
        this.joinCheckFiled = joinCheckFiled;
    }
}

package com.comname.miss.util.config;

import java.util.Map;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

/**
 * it's javaBean ,用于参数校验框架
 * JoinFiledRuler.java
 * <p>Copyright: Copyright (c) 2009 <p>
 * <p>Company: comname</p>
 *  @author    
 *  @version   1.0
 */
public class JoinFiledRuler
{
    public Map<String, CheckField> joinCheck;

    public Map<String, CheckField> getJoinCheck()
    {
        return joinCheck;
    }

    public void setJoinCheck(Map<String, CheckField> joinCheck)
    {
        this.joinCheck = joinCheck;
    }

    @Override
    public String toString()
    {

        return ReflectionToStringBuilder.toString(this);
    }

}

package com.comname.miss.util.config;

import org.springframework.context.ApplicationContext;

import com.comname.dhm.aaa.daf.DAFFactory;

public class SerFactory
{

    private static ApplicationContext Appcontext = DAFFactory.getCtx();

    private static SerMap serMapConfig;

    private static CheckRulerConfig checkConfig;

    public static CheckRulerConfig getCheckConfig()
    {

        return checkConfig;
    }

    public static void setCheckConfig(CheckRulerConfig config)
    {
        checkConfig = config;
    }

    public static Object getInstance(String key)
    {

        return Appcontext.getBean(key);
    }

    public static SerMap getSerMapConfig()
    {
        return serMapConfig;
    }

    public static void setSerMapConfig(SerMap config)
    {
        serMapConfig = config;
    }
}


package com.comname.miss.util.config;

import org.springframework.context.ApplicationContext;

import com.comname.dhm.aaa.daf.DAFFactory;

public class SerFactory
{

    private static ApplicationContext Appcontext = DAFFactory.getCtx();

    private static SerMap serMapConfig;

    private static CheckRulerConfig checkConfig;

    public static CheckRulerConfig getCheckConfig()
    {

        return checkConfig;
    }

    public static void setCheckConfig(CheckRulerConfig config)
    {
        checkConfig = config;
    }

    public static Object getInstance(String key)
    {

        return Appcontext.getBean(key);
    }

    public static SerMap getSerMapConfig()
    {
        return serMapConfig;
    }

    public static void setSerMapConfig(SerMap config)
    {
        serMapConfig = config;
    }
}

如何校验?
web.xml
  <servlet>
    <servlet-name>PublishTaskRetServlet</servlet-name>
    <servlet-class>
      com.comname.dhm.portalMS.base.web.servlet.PublishTaskRetServlet</servlet-class>
    <load-on-startup>3</load-on-startup>
  </servlet>
 
 
/*
 * 工 程 名:  nportalMS
 * 包       名:  com.comname.dhm.portalMS.base.web.servlet
 * 文 件 名:  CheckportalMSConfigServlet.java
 * 版       权:  Copyright (c) 2010 comname All Rights Reserved.
 * 描       述:  <描述>
 * 修 改 人:  
 * 修改时间:  2010-8-30
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */


package com.comname.dhm.portalMS.base.web.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;

import com.comname.dhm.common.config.impl.XMLFactory;
import com.comname.dhm.portalMS.sync.dataCheck.SyncDataCheck;

/**
 * <一句话功能简述>
 * <功能详细描述>
 *
 * @author
 * @version  [V200R001]
 * @date 2010-8-30
 * @see  [相关类/方法]
 * @since  [DHM.Core.portalMS-V200R002]
 */

public class CheckPortalMSConfigServlet extends HttpServlet
{

    /**
     * 注释内容
     */
    private static final long serialVersionUID = -7994025781217474094L;

    private static final Logger log = Logger.getLogger(CheckPortalMSConfigServlet.class);

    /**
     * 服务器启动成功,向NMS发送消息
     *
     * @param config
     *            FilterConfig对象
     * @throws ServletException
     */
    public void init(ServletConfig config) throws ServletException
    {
        String[] keys = { "nodeResource.maximumSize", "verificationCode.enable",
                "commonTemplateFileUpload.maxFileSize", "resourceRelation.number",
                "paginatedComponent.defaultPageSize", "paginatedComponent.pageSizeList" };
        String[] arrays = null;
        for (int i = 0; i < keys.length; i++)
        {
            arrays = XMLFactory.getValueArray(keys[i]);
            if (arrays == null || arrays.length == 0)
            {
                // 向网管告警
                // TrapSender.sendportalMSConfigError(keys[i]);
                // 记录系统错误日志
                log
                        .error("portalMSConfig.xml: Configure item  '"
                                + keys[i]
                                + "', is blank! ,Please strictly in accordance with the installation and configuration file configuration!");
                // 退出系统
                // System.exit(1);
                Runtime.getRuntime().exit(1);
                return;
            }
            else
            {
                StringBuffer sb = new StringBuffer();
                for (String value : arrays)
                {
                    sb = sb.append(value).append(",");
                }
                String tmp = sb.substring(0, sb.length() - 1).toString();
                if (!SyncDataCheck.checkItem("portalMSconfig." + keys[i], tmp))
                {
                    // 向网管告警
                    // TrapSender.sendportalMSConfigError(keys[i]);
                    // 记录系统错误日志
                    log
                            .error("portalMSConfig.xml: Configure item'"
                                    + keys[i]
                                    + "',check failure! value is"
                                    + tmp
                                    + ",Please strictly in accordance with the installation and configuration file configuration!");
                    // 退出系统
                    // System.exit(1);
                    Runtime.getRuntime().exit(1);
                    return;
                }
            }
        }
    }
}



/*
 * 工 程 名:  portalMS
 * 包       名:  com.comname.dhm.portalMS.sync.tool
 * 文 件 名:  SyncDataCheck.java
 * 版       权:  Copyright (c) 2009 comname All Rights Reserved.
 * 描       述:  同步数据的检查
 * 修 改 人:  
 * 修改时间:  2009-12-7
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.comname.dhm.portalMS.sync.dataCheck;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.comname.ChineseCharacter;
import com.comname.dhm.common.config.impl.PropertiesFactory;
import com.comname.dhm.common.uif.vo.cms.CMSConstants;
import com.comname.dhm.portalMS.common.DebugLogHelper;
import com.comname.dhm.portalMS.exception.PortalMSException;

/**
 * 同步数据的检查<br/>
 * 检查数据的长度,类型,是否为空,以及正则匹配
 *
 * @author  
 * @version  [V200R001, 2009-12-7]
 * @see  [相关类/方法]
 * @since  [DHM.Core.portalMS-V200R001]
 */
public class SyncDataCheck
{
    /**
     * 验证规则Map
     */
    private static Map<String, CheckObj> checkMap = null;

    /**
     * 日志对象
     */
    private static final DebugLogHelper logger = new DebugLogHelper(SyncDataCheck.class);

    /**
     * 检查XML的名字
     */
    private final static String CHECK_XML_NAME = "SyncCheckAction-UpdateData-validation.xml";

    /**
     * 检查项目的值是否合法,不合法则抛出异常
     *
     * @param itemName 待检查项目的名
     * @param value  待检查项目的值
     * @return [参数说明]
     *
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static void catchInvalidateData(String itemName, Object value) throws PortalMSException
    {
        if (!checkItem(itemName, value))
        {
            throw new PortalMSException( PropertiesFactory.getValueString(ChineseCharacter.EXCELDATACHECK_CONTROL_ITEM) + "[" + itemName + "]"
                    +  PropertiesFactory.getValueString(ChineseCharacter.EXCELDATACHECK_CHECK_FAIL )+ "。" +  PropertiesFactory.getValueString(ChineseCharacter.EXCELDATACHECK_CHECK_VALUE)
                    + "[" + value + "]");
        }
    }

    /**
     * 检查项目的值
     *
     * @param itemName 待检查项目的名
     * @param value  待检查项目的值
     * @return [参数说明]
     *
     * @return boolean [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static boolean checkItem(String itemName, Object value)
    {
        logger.enterFuncDebugLog();
        // 初始化Map
        initMap();

        logger.excepFuncDebugLog( PropertiesFactory.getValueString(ChineseCharacter.EXCELDATACHECK_CONTROL_ITEM )+ "[" + itemName + "]"
                +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_START )+  PropertiesFactory.getValueString(ChineseCharacter.EXCELDATACHECK_CHECK_VALUE) + "["
                + value + "]");

        CheckObj obj = checkMap.get(itemName);
        if (obj == null)
        {
            logger.exitFuncDebugLog( PropertiesFactory.getValueString(ChineseCharacter.EXCELDATACHECK_CONTROL_ITEM) + "[" + itemName + "]"
                    +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_END) +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_RETURN_TRUE) + "["
                    +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_OBJ_NULL )+ "]");
            return true;
        }
        // 检查项目
        String type = obj.getFieldType();
        String isNull = obj.getIsNull();
        int maxLength = obj.getMaxLength();
        String expression = obj.getExpression();
        logger.excepFuncDebugLog("type[" + type + "];isNull[" + isNull + "];maxLength[" + maxLength + "];expression[" + expression
                + "]");

        // 是否为空验证
        if (value == null || "".equals(value))
        {
            if (!"false".equalsIgnoreCase(isNull))
            {
                logger.exitFuncDebugLog( PropertiesFactory.getValueString(ChineseCharacter.EXCELDATACHECK_CONTROL_ITEM) + "[" + itemName + "]"
                        +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_END )+  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_RETURN_TRUE)
                        + "[" +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_OBJ_NULL) + "]");
                return true;
            }
            else
            {
                logger.exitFuncDebugLog( PropertiesFactory.getValueString(ChineseCharacter.EXCELDATACHECK_CONTROL_ITEM )+ "[" + itemName + "]"
                        +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_END) +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_RETURN_FALSE)
                        + "[" +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_OBJ_NULL )+ "]");
                return false;
            }
        }
        boolean isCheckPass = toCheck(value, type, maxLength, expression);
        logger.exitFuncDebugLog( PropertiesFactory.getValueString(ChineseCharacter.EXCELDATACHECK_CONTROL_ITEM )+ "[" + itemName + "]"
                +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_END) +  PropertiesFactory.getValueString(ChineseCharacter.SYNCDATACHECK_CHECK_RETURN)
                + String.valueOf(isCheckPass).toUpperCase());
        return isCheckPass;
    }

    private static boolean toCheck(Object value, String type, int maxLength, String expression)
    {
        logger.enterFuncDebugLog();
        boolean isCheckPass = true;
        // 类型验证
        if (type != null && !"".equals(type))
        {
            isCheckPass = checkType(value, type, isCheckPass);
        }
        // 最大长度验证
        if (maxLength != -1)
        {
            int java2oracleLength = getJava2OracleLength(String.valueOf(value));// value
            // 不可能为空了
            if (java2oracleLength > maxLength)
            {
                isCheckPass = false;
            }
        }
        // 正则表达式验证
        if (expression != null && !"".equals(expression))
        {
            if (!Pattern.matches(expression, String.valueOf(value)))
            {
                isCheckPass = false;
            }
        }
        logger.exitFuncDebugLog();
        return isCheckPass;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值