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

📄 paramchecker.java~2~

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA~2~
📖 第 1 页 / 共 4 页
字号:
            String rule = (String) rules.get(i);
            rule = rule.trim();
            if (rule.indexOf(">=")!=-1) {
                isValid = this.checkUnionCond(rule, ">=");
            } else if (rule.indexOf("<=")!=-1) {
                isValid = this.checkUnionCond(rule, "<=");
            } else if (rule.indexOf(">")!=-1) {
                isValid = this.checkUnionCond(rule, ">");
            } else if (rule.indexOf("<")!=-1) {
                isValid = this.checkUnionCond(rule, "<");
            } else if (rule.indexOf("=")!=-1) {
                isValid = this.checkUnionCond(rule, "=");
            } else {
                addMsg("err_format", new String[] {rule});
                if (onErrorExit)
                    break;
            }
            if (!isValid) {
                if (onErrorExit)
                    break;
            }
        }
    }

    public void doCheck(FormRule fr) throws CheckErrException, ErrMsgException {
        if (fr==null)
            throw new ErrMsgException(LoadString("err_formrule_none", new String[] {}));
        onErrorExit = fr.isOnErrorExit();
        formResource = fr.getRes();
        check(fr.getRules());
        checkUnionCond(fr.getUnionRules());

        if (msgs.size()!=0)
            throw new ErrMsgException(getMessage(false));
    }

    public void check(Vector rules) throws CheckErrException, ErrMsgException {
        Iterator ir = rules.iterator();
        while (ir.hasNext()) {
            String rule = (String) ir.next();
            checkField(rule);
        }
    }

    public void checkField(String rule) throws CheckErrException, ErrMsgException {
        rule = rule.trim();

        if (rule.startsWith(this.TYPE_STRING)) {
            checkFieldString(rule);
        } else if (rule.startsWith(this.TYPE_INT)) {
            checkFieldInt(rule);
        } else if (rule.startsWith(this.TYPE_DATE)) {
            checkFieldDate(rule);
        } else if (rule.startsWith(this.TYPE_STRING_ARRAY)) {
            checkFieldStringArray(rule);
        } else if (rule.startsWith(this.TYPE_INT_ARRAY)) {
            checkFieldIntArray(rule);
        } else if (rule.startsWith(this.TYPE_LONG)) {
            checkFieldLong(rule);
        } else if (rule.startsWith(this.TYPE_BOOLEAN)) {
            checkFieldBoolean(rule);
        }
        else
            throw new ErrMsgException(LoadString("err_type", new String[] {rule}));
    }

    public String parseFieldDesc(String desc) {
        // logger.info("parseFieldDesc desc=" + desc + " formResource=" + formResource);
        if (formResource==null || formResource.equals(""))
            return desc;
        if (!desc.startsWith("#"))
            return desc;
        else {
            String key = desc.substring(1, desc.length()).trim();
            return SkinUtil.LoadString(request, formResource, key);
        }
    }

    public void checkFieldIntArray(String ruleStr) throws CheckErrException  {
        String fieldName = "";

        String[] rule = split(ruleStr);
        if (rule == null) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }

        int len = rule.length;
        if (len < 4) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }

        fieldName = rule[1];
        String fieldDesc = parseFieldDesc(rule[2]);

        String[] values = getFieldValues(fieldName);
        int[] intValues = null;
        len = 0;
        if (values!=null) {
            len = values.length;
            intValues = new int[len];
        }
        for (int i=0; i<len; i++) {
            try {
                intValues[i] = Integer.parseInt(values[i]);
            }
            catch (Exception e) {
                addMsg("err_not_num", new String[] {fieldDesc});
                break;
            }
        }

        Field f = new Field(fieldName, fieldDesc, intValues, this.TYPE_INT_ARRAY);
        fields.put(fieldName, f);

        String NULL = rule[3];
        if (values == null) {
            if (NULL.equalsIgnoreCase("not")) {
                addMsg("err_want", new String[] {fieldDesc});
                return;
            }
        }
    }

    public void checkFieldStringArray(String ruleStr) throws CheckErrException  {
        String fieldName = "";

        String[] rule = split(ruleStr);
        if (rule == null) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }

        int len = rule.length;
        if (len < 4) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }
        fieldName = rule[1];
        String fieldDesc = parseFieldDesc(rule[2]);

        String[] values = getFieldValues(fieldName);
        Field f = new Field(fieldName, fieldDesc, values, this.TYPE_STRING_ARRAY);
        fields.put(fieldName, f);

        String NULL = rule[3];

        if (values == null) {
            if (NULL.equalsIgnoreCase("not")) {
                addMsg("err_want", new String[] {fieldDesc});
            }
            else if (NULL.equals("allow"))
                ;
            else {
                addMsg("err_format", new String[] {NULL});
            }
        }
    }

    public void checkFieldBoolean(String ruleStr) throws CheckErrException  {
        String fieldName = "";

        String[] rule = split(ruleStr);
        if (rule == null) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }

        int len = rule.length;
        if (len < 4) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }
        fieldName = rule[1];

        String value = getFieldValue(fieldName);

        checkFieldBoolean(ruleStr, value);
    }

    public void checkFieldBoolean(String ruleStr, String value) throws CheckErrException {
        String fieldName = "";
        String type = "";
        String fieldDesc = "";

        String[] rule = split(ruleStr);
        if (rule == null) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }

        int len = rule.length;
        if (len < 4) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }
        type = rule[0];
        fieldName = rule[1];
        fieldDesc = parseFieldDesc(rule[2]);

        String NULL = rule[3];

        if (value == null) {
            if (NULL.equalsIgnoreCase("not")) {
                addMsg("err_want", new String[] {fieldDesc});
                return;
            }
            else if (NULL.equals("allow"))
                ;
            else {
                value = NULL;
            }
        }

        Boolean bu;

        if (value!=null && value.equals("true") || value.equals("1"))
            bu = new Boolean(true);
        else
            bu = new Boolean(false);

        boolean isValid = true;

        // 存储field值
        Field f = new Field(fieldName, fieldDesc, bu, type);
        fields.put(fieldName, f);

        // 验证

        // ...

        if (!isValid) {
            if (onErrorExit) {
                throw new CheckErrException(msgs);
            }
        }
    }

    public void checkFieldDate(String ruleStr) throws CheckErrException  {
        String fieldName = "";

        String[] rule = split(ruleStr);
        if (rule == null) {
                addMsg("err_format", new String[] {ruleStr});
            return;
        }

        int len = rule.length;
        if (len < 4) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }
        fieldName = rule[1];

        String value = getFieldValue(fieldName);

        checkFieldDate(ruleStr, value);
    }

    public void checkFieldDate(String ruleStr, String value) throws CheckErrException {
        String fieldName = "";
        String type = "";
        String fieldDesc = "";

        String[] rule = split(ruleStr);
        if (rule == null) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }

        int len = rule.length;
        if (len < 4) {
            addMsg("err_format", new String[] {ruleStr});
            return;
        }
        type = rule[0];
        fieldName = rule[1];
        fieldDesc = parseFieldDesc(rule[2]);

        String NULL = rule[3];

        if (value == null) {
            if (NULL.equalsIgnoreCase("not")) {
                addMsg("err_want", new String[] {fieldDesc});
                return;
            }
            else if (NULL.equals("allow"))
                ;
            else if (NULL.equals("current")) {
                // 如果默认为当前时间
                Field f = new Field(fieldName, fieldDesc, new Date(), type);
                fields.put(fieldName, f);
                return;
            } else {
                addMsg("err_format", new String[] {NULL});
                return;
            }
        }

        boolean isValid = true;

        String format = "";
        java.util.Date d = null;

        // 规则部分,以类似email=true的方式
        for (int i = 4; i < len; i++) {
            String cond = rule[i].trim();
            if (cond.startsWith("format")) {
                format = getCondValue(cond);
                SimpleDateFormat sdf = new SimpleDateFormat(
                        format, SkinUtil.getLocale(request));
                try {
                    d = sdf.parse(value);
                } catch (Exception e) {
                    isValid = false;
                    addMsg("err_format", new String[] {fieldDesc});
                }
            }
        }

        // 存储field值
        Field f = new Field(fieldName, fieldDesc, d, type);
        fields.put(fieldName, f);

        // 验证
        for (int i = 4; i < len; i++) {
            String cond = rule[i].trim();
            if (cond.startsWith("min")) {
                if (d != null) {
                    // 取出符号
                    char token = cond.charAt(3);
                    if (token == '>') {
                        if (cond.charAt(4) == '=') {
                            String strMin = cond.substring(5, cond.length()).
                                            trim();
                            try {
                                Date min = null;
                                try {
                                    min = DateUtil.parse(strMin, format,
                                            SkinUtil.getLocale(request));
                                }
                                catch (Exception e) {
                                    addMsg("err_format", new String[] {strMin});
                                }
                                if (min!=null) {
                                    if (DateUtil.compare(d, min) == 1 ||
                                        DateUtil.compare(d, min) == 0)
                                        ;
                                    else {
                                        isValid = false;
                                        addMsg("err_need_more_equal", new String[] {fieldDesc, "" + min});
                                    }
                                }
                            } catch (Exception e) {
                                isValid = false;
                                addMsg("err_format", new String[] {cond});
                            }
                        } else {
                            String strMin = cond.substring(5, cond.length()).
                                             trim();
                            try {
                                 Date min = null;
                                 try {
                                     min = DateUtil.parse(strMin, format,
                                             SkinUtil.getLocale(request));
                                 }
                                 catch (Exception e) {
                                    addMsg("err_format", new String[] {strMin});
                                 }
                                 if (min!=null) {
                                     if (DateUtil.compare(d, min) == 1)
                                         ;
                                     else {
                                         isValid = false;
                                         addMsg("err_need_more", new String[] {fieldDesc, "" + min});
                                     }
                                 }
                             } catch (Exception e) {
                                 isValid = false;
                                 addMsg("err_format", new String[] {cond});
                            }
                        }
                    } else {
                        isValid = false;
                        addMsg("err_format", new String[] {cond});
                    }
                }
            } else if (cond.startsWith("max")) {
                if (d != null) {
                    // 取出符号
                    char token = cond.charAt(3);
                    if (token == '>') {
                        if (cond.charAt(4) == '=') {
                            String strMax = cond.substring(5, cond.length()).
                                            trim();
                            try {
                                Date max = null;

⌨️ 快捷键说明

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