⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paramchecker.java~2~

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA~2~
📖 第 1 页 / 共 4 页
字号:
package cn.js.fan.util;

import java.util.Vector;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import com.redmoon.kit.util.FileUpload;
import java.util.HashMap;
import cn.js.fan.security.SecurityUtil;
import java.text.SimpleDateFormat;
import cn.js.fan.web.SkinUtil;
import java.util.Date;
import org.apache.log4j.Logger;
import com.cloudwebsoft.framework.base.ISequence;

/**
 * <p>Title: </p>
 *
 * <p>Description:
 * 单独验证并取值置于HashMap中:类型(String int Date String[] int[]),域名,描述, null=(not|empty|allow|current|ip),format=(yyyy-mm-dd), email=(true|false), min(>|>=)20, max(<|<=)20,isNotCN=true|false 如果出错则退出验证,sql=SQLSERVER|MYSQL|ORACLE(用于SQL语句)
 * 联合验证: 表单域名称1 >= | <= | = 表单域名称2
 * 例:String, code, 编码, not, email=true, min<=20 max=30, isNotCN=true, exclude=#|;|", sql=sqlserver 表示类型为string型的变量code,名称为编码,不允许为空......
 * format=yyyy-MM-dd HH:mm:ss
 * 固定格式:第一个必须是类型,第一个必须是域名,第三个是域的描述,第四个必须是能否为null,字符串中一律使用小写
 * null=not|empty not表示不能为空empty置其为空字符串
 * 字符串类型时min表示最小长度,max表示最大长度
 * int类型时min表示最小值,max表示最大值
 * Date类型时 current表示当前时间 min表示日期的最小值, max表示日期的最大值
 * 说明:对于大小写不敏感,如RegName,将会被转换为regname
 * 域的描述格式: #资源名称
 *               非#开头,表示直接使用
 * 资源文件中的key使用小写
 * </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ParamChecker {
    int sourceType;

    HttpServletRequest request;
    FileUpload fu;

    String formResource = "";

    String res = "res.checker";

    Logger logger = Logger.getLogger(ParamConfig.class.getName());

    public static String TYPE_DATE = "Date";
    public static String TYPE_INT = "int";
    public static String TYPE_STRING = "String";
    public static String TYPE_STRING_ARRAY = "String[]";
    public static String TYPE_INT_ARRAY = "int[]";
    public static String TYPE_BOOLEAN = "boolean";
    public static String TYPE_LONG = "long";

    public static int SOURCE_TYPE_REQUEST = 0;
    public static int SOURCE_TYPE_FILEUPLOAD = 1;

    HashMap fields = new HashMap(); // 用来存取域中所有的元素及其值

    boolean onErrorExit = false;

    public Vector msgs = new Vector(); // 存储出错信息

    public ParamChecker(HttpServletRequest request) {
        this.request = request;
        this.sourceType = SOURCE_TYPE_REQUEST;
    }

    public ParamChecker(HttpServletRequest request, Object sourceObj) {
        this.request = request;
        if (sourceObj instanceof HttpServletRequest) {
            this.sourceType = SOURCE_TYPE_REQUEST;
        } else {
            this.sourceType = SOURCE_TYPE_FILEUPLOAD;
            this.fu = (FileUpload) sourceObj;
        }
    }

    public String getFieldValue(String field) {
        if (sourceType == SOURCE_TYPE_REQUEST) {
            return ParamUtil.get(request, field, false);
        } else {
            return fu.getFieldValue(field);
        }
    }

    public String[] getFieldValues(String field) {
        if (sourceType == SOURCE_TYPE_REQUEST) {
            return ParamUtil.getParameters(request, "field");
        } else {
            return fu.getFieldValues(field);
        }
    }

    public static String[] split(String str) {
        str = str.replaceAll(",", ","); // 替换全角
        String[] r = StrUtil.split(str, ",");
        if (r!=null) {
            int len = r.length;
            for (int i=0; i<len;  i++)
                r[i] = r[i].trim();
        }
        return r;
    }

    public Object getValue(String fieldName) throws ErrMsgException {
        if (fields.containsKey(fieldName)) {
            // logger.info("getValue:" + ((Field) fields.get(fieldName)).value);
            return ((Field) fields.get(fieldName)).value;
        }
        else
            throw new ErrMsgException(fieldName + " is not found in the form fields."); // 调试用,所以不调用资源文件
    }

    public String getString(String fieldName) throws ErrMsgException {
        return (String)getValue(fieldName);
    }

    public int getInt(String fieldName) throws ErrMsgException {
        Integer it = (Integer)getValue(fieldName);
        if (it==null)
            return -1;
        else
            return it.intValue();
    }

    public Date getDate(String fieldName) throws ErrMsgException {
        Date d = (Date)getValue(fieldName);
        return d;
    }

    public String[] getStringValues(String fieldName) throws ErrMsgException {
        return (String[])getValue(fieldName);
    }

    public int[] getIntValues(String fieldName) throws ErrMsgException {
        return (int[])getValue(fieldName);
    }

    public boolean getBoolean(String fieldName) throws ErrMsgException {
        Boolean b = (Boolean)getValue(fieldName);
        if (b==null)
            return false;
        else
            return b.booleanValue();
    }

    public long getLong(String fieldName) throws ErrMsgException {
        Long v = (Long)getValue(fieldName);
        if (v==null)
            return -1;
        else
            return v.longValue();
    }

    /**
     * 获取UnionCond中的成结的Field的值
     * @param rule String
     * @param token String
     * @param b Boolean 传递参数,表示获值是否成功
     * @return String[]
     */
    public boolean checkUnionCond(String rulePairStr, String token) {
        boolean isValid = true;
        String[] pairs = rulePairStr.split(token);
        if (pairs.length < 2) {
            addMsg("err_format", new String[] {rulePairStr});
            isValid = false;
        }
        String leftFieldName = pairs[0].trim();
        String rightFieldName = pairs[1].trim();

        // logger.info("checkUnionCond leftField name=" + leftFieldName + " rightFieldName=" + rightFieldName);

        Field leftField = (Field) fields.get(leftFieldName);
        Field rightField = (Field) fields.get(rightFieldName);
        if (leftField == null) {
            addMsg("err_not_in_result", new String[] {leftFieldName});
            isValid = false;
            return false;
        }
        if (rightField == null) {
            addMsg("err_not_in_result", new String[] {rightFieldName});
            isValid = false;
            return false;
        }

        String leftFieldDesc = leftField.desc;
        String rightFieldDesc = rightField.desc;

        // logger.info("checkUnionCond leftField name=" + leftField.name + " value=" + leftField.value + " type=" + leftField.type);
        // logger.info("checkUnionCond rightField name=" + rightField.name + " value=" + rightField.value + " type=" + rightField.type);
        if (leftField.value==null || rightField.value==null)
            return false;

        if (!leftField.type.equals(rightField.type)) {
            isValid = false;
            addMsg("err_type_not_match", new String[] {leftFieldDesc, "" +rightFieldDesc});
        }

        if (leftField.type.equals(this.TYPE_STRING) || leftField.type.equals(this.TYPE_INT) || leftField.type.equals(this.TYPE_DATE) || leftField.type.equals(this.TYPE_LONG))
            ;
        else {
            addMsg("err_can_not_compare", new String[] {leftFieldDesc, "" +rightFieldDesc});
            isValid = false;
            return isValid;
        }
        if (token.equals(">=")) {
            if (leftField.type.equals(this.TYPE_INT)) {
                int a = ((Integer) leftField.value).intValue();
                int b = ((Integer) rightField.value).intValue();
                if (a >= b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_more_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            }
            else if (leftField.type.equals(this.TYPE_DATE)) {
                Date a = (Date)leftField.value;
                Date b = (Date)rightField.value;
                if (DateUtil.compare(a, b)==1 || DateUtil.compare(a, b)==0)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_more_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            } else if (leftField.type.equals(this.TYPE_LONG)) {
                long a = ((Long) leftField.value).longValue();
                long b = ((Long) rightField.value).longValue();
                if (a >= b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_more_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            }
        } else if (token.equals(">")) {
            if (leftField.type.equals(this.TYPE_INT)) {
                int a = ((Integer) leftField.value).intValue();
                int b = ((Integer) rightField.value).intValue();
                if (a > b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_more", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            }
            else if (leftField.type.equals(this.TYPE_DATE)) {
                Date a = (Date)leftField.value;
                Date b = (Date)rightField.value;
                if (DateUtil.compare(a, b)==1)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_more", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            } else if (leftField.type.equals(this.TYPE_LONG)) {
                long a = ((Long) leftField.value).longValue();
                long b = ((Long) rightField.value).longValue();
                if (a > b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_more", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            }
        } else if (token.equals("<=")) {
            if (leftField.type.equals(this.TYPE_INT)) {
                int a = ((Integer) leftField.value).intValue();
                int b = ((Integer) rightField.value).intValue();
                if (a <= b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_less_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            }
            else if (leftField.type.equals(this.TYPE_DATE)) {
                Date a = (Date)leftField.value;
                Date b = (Date)rightField.value;
                if (DateUtil.compare(a, b)==2 || DateUtil.compare(a, b)==0)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_less_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            } else if (leftField.type.equals(this.TYPE_LONG)) {
                long a = ((Long) leftField.value).longValue();
                long b = ((Long) rightField.value).longValue();
                if (a <= b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_less_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            }
        } else if (token.equals("<")) {
            if (leftField.type.equals(this.TYPE_INT)) {
                int a = ((Integer) leftField.value).intValue();
                int b = ((Integer) rightField.value).intValue();
                if (a < b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_less", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            }
            else if (leftField.type.equals(this.TYPE_DATE)) {
                Date a = (Date)leftField.value;
                Date b = (Date)rightField.value;
                if (DateUtil.compare(a, b)==2)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_less", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            } else if (leftField.type.equals(this.TYPE_LONG)) {
                long a = ((Long) leftField.value).longValue();
                long b = ((Long) rightField.value).longValue();
                if (a < b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_less", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            }
        } else if (token.equals("=")) {
            if (leftField.type.equals(this.TYPE_INT)) {
                int a = ((Integer) leftField.value).intValue();
                int b = ((Integer) rightField.value).intValue();
                if (a == b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            } else if (leftField.type.equals(this.TYPE_DATE)) {
                Date a = (Date) leftField.value;
                Date b = (Date) rightField.value;
                if (DateUtil.compare(a, b) == 0)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            } else if (leftField.type.equals(this.TYPE_LONG)) {
                long a = ((Long) leftField.value).longValue();
                long b = ((Long) rightField.value).longValue();
                if (a == b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            } else if (leftField.type.equals(this.TYPE_BOOLEAN)) {
                boolean a = ((Boolean) leftField.value).booleanValue();
                boolean b = ((Boolean) rightField.value).booleanValue();
                if (a == b)
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            } else if (leftField.type.equals(this.TYPE_STRING)) {
                String a = (String) leftField.value;
                String b = (String) rightField.value;
                if (a.equals(b))
                    ;
                else {
                    isValid = false;
                    addMsg("err_need_equal", new String[] {leftFieldDesc, "" +rightFieldDesc});
                }
            }
        } else {
            isValid = false;
            addMsg("err_format", new String[] {rulePairStr});
        }
        return isValid;
    }

    /**
     * 检查联合条件,如两个表单域之间的相等、大于、小于
     * 如:onerror=exit|resume,pwd=pwd1,date1>date2,num1<=num2
     */
    public void checkUnionCond(Vector rules) throws CheckErrException {
        int len = rules.size();
        // 先找到onerror
        boolean isValid = true;

        for (int i = 0; i < len; i++) {
            isValid = true;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -