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

📄 basevalidator.java

📁 这个是使用java开发的一个平台
💻 JAVA
字号:
package com.exp.web.util.validator;

import com.exp.fcl.xml.EXPXMLNode;
import com.exp.web.util.FormBean;

public abstract class BaseValidator {
    protected EXPXMLNode validator;

    private String message = "";

    private String target = "";

    protected String formName = "";

    private boolean genFrontCheck = false;

    private boolean emptyJump = false;

    /**
     * 设置验证子配置XML节点
     * 
     * @param validator
     */
    public final void setValidatorNode(EXPXMLNode validator) {
        this.validator = validator;
        this.message = this.validator.getAttributeValue("message");
        this.target = this.validator.getAttributeValue("target");
        String temp = this.validator.getAttributeValue("empty-jump");
        if (temp.equals("true")) {
            this.emptyJump = true;
        }
        temp = this.validator.getAttributeValue("front-check");
        if (temp.equals("true")) {
            this.genFrontCheck = true;
        }
        this.initConfig(validator);
    }

    /**
     * 根据验证子配置信息初始化验证子
     * 
     * @param validator
     */
    protected abstract void initConfig(EXPXMLNode validator);

    /**
     * 执行校验
     * 
     * @param formBean
     * @return
     */
    public abstract boolean checkValid(FormBean formBean);

    /**
     * 生成判断条件,重定义它
     * 
     * @return
     */
    protected abstract String genCheckCondition();

    /**
     * 得到待校验的目标
     * 
     * @return
     */
    public String getTarget() {
        return this.target;
    }

    /**
     * 得到校验错误消息
     * 
     * @return
     */
    public String getMessage() {
        return this.message;
    }

    /**
     * 是否当目标为空时不作校验
     * 
     * @return
     */
    public boolean isEmptyJump() {
        return this.emptyJump;
    }

    /**
     * 如果检验非法,系统进行的操作
     * 
     * @param buf
     */
    protected void checkHandle(StringBuffer buf) {
        buf.append("\n{\n\talert('");
        buf.append(this.getMessage());
        buf.append("');");
        buf.append("\n\t");
        buf.append(this.formName);
        buf.append(".");
        buf.append(this.getTarget());
        buf.append(".focus();");
        buf.append("\n\treturn false;\n}");
    }

    /**
     * 生成匹配正则表达式的脚本条件语句
     * 
     * @param reg
     * @return
     */
    protected String matchRegExp(String reg, boolean ignore) {
        StringBuffer buf = new StringBuffer();
        buf.append("if(");
        buf.append(" !matchRegExp(\"");
        buf.append(reg);
        buf.append("\",");
        buf.append(this.getTargetValueWithForm());
        buf.append(",");
        if (ignore) {
            buf.append("true");
        } else {
            buf.append("false");
        }
        buf.append(")");
        buf.append(this.getEmptyJumpScript());
        buf.append(" )");
        return buf.toString();
    }

    /**
     * 
     * @return
     */
    protected String getTargetValueWithForm() {
        return this.formName + "." + this.getTarget() + ".value";
    }

    /**
     * 针对如果目标为空,不作校验的前端脚本代码,需保护在genCheckCondition实现中
     * 
     * @return
     */
    protected String getEmptyJumpScript() {
        if (this.emptyJump) {
            StringBuffer buf = new StringBuffer();
            buf.append(" && (");
            buf.append(this.getTargetValueWithForm());
            buf.append("!='')");
            return buf.toString();
        } else {
            return "";
        }
    }

    /**
     * 得到针对该校验的前端脚本代码
     * 
     * @return
     */
    final public String getJavaScript() {
        String condition = this.genCheckCondition();
        if (this.genFrontCheck && !this.formName.equals("")
                && condition != null) {
            StringBuffer buf = new StringBuffer();
            buf.append(condition);
            this.checkHandle(buf);
            return buf.toString();
        } else {
            return null;
        }
    }

    /**
     * 设置前端校验需要的Form名称
     * 
     * @param formName
     */
    void setFormName(String formName) {
        this.formName = formName;
    }

}

⌨️ 快捷键说明

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