Search in sources :

Example 1 with Default

use of app.hongs.util.verify.Default in project HongsCORE by ihongs.

the class VerifyHelper method addRulesByForm.

public VerifyHelper addRulesByForm(String conf, String form, Map map) throws HongsException {
    FormSet def = FormSet.getInstance("default");
    Map tps = def.getEnum("__types__");
    Map pts = def.getEnum("__patts__");
    Iterator it = map.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry et = (Map.Entry) it.next();
        String code = (String) et.getKey();
        Map optz = (Map) et.getValue();
        Map opts = new HashMap(optz);
        Object o;
        opts.put("__conf__", conf);
        opts.put("__form__", form);
        opts.put("__name__", code);
        o = opts.get("defiant");
        if (o != null) {
            Rule rule = new Defiant();
            rule.setParams(opts);
            this.addRule(code, rule);
        }
        o = opts.get("default");
        if (o != null) {
            Rule rule = new Default();
            rule.setParams(opts);
            this.addRule(code, rule);
        }
        o = opts.remove("__required__");
        if (!"".equals(o)) {
            if (Synt.declare(o, false)) {
                Rule rule = new Required();
                rule.setParams(opts);
                this.addRule(code, rule);
            } else {
                Rule rule = new Optional();
                rule.setParams(opts);
                this.addRule(code, rule);
            }
        }
        o = opts.remove("__repeated__");
        if (!"".equals(o)) {
            if (Synt.declare(o, false)) {
                Rule rule = new Repeated();
                rule.setParams(opts);
                this.addRule(code, rule);
            } else {
                Rule rule = new NoRepeat();
                rule.setParams(opts);
                this.addRule(code, rule);
            }
        }
        // 可设多个规则, 缺省情况按字符串处理
        List<String> list = Synt.toList(opts.get("__rule__"));
        if (list == null || list.isEmpty()) {
            String type = (String) opts.get("__type__");
            String item;
            if (tps.containsKey(type)) {
                // 类名转换
                item = (String) tps.get(type);
                String c = item.substring(0, 1);
                String n = item.substring(1);
                item = "Is" + c.toUpperCase() + n;
            } else if (pts.containsKey(type)) {
                // 类型正则
                opts.put("pattern", type);
                item = "IsString";
            } else if (!"@".equals(code)) {
                item = "IsString";
            } else {
                item = "Ignore";
            }
            list = Synt.listOf(item);
        }
        // 添加规则实例
        for (String item : list) {
            if (!item.contains(".")) {
                item = Rule.class.getPackage().getName() + "." + item;
            }
            Rule rule;
            try {
                rule = (Rule) (Class.forName(item).newInstance());
            } catch (ClassNotFoundException ex) {
                throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (InstantiationException ex) {
                throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (IllegalAccessException ex) {
                throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (ClassCastException ex) {
                throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            }
            rule.setParams(opts);
            addRule(code, rule);
        }
    }
    return this;
}
Also used : Optional(app.hongs.util.verify.Optional) HashMap(java.util.HashMap) Repeated(app.hongs.util.verify.Repeated) Defiant(app.hongs.util.verify.Defiant) Default(app.hongs.util.verify.Default) Required(app.hongs.util.verify.Required) HongsException(app.hongs.HongsException) Iterator(java.util.Iterator) Rule(app.hongs.util.verify.Rule) NoRepeat(app.hongs.util.verify.NoRepeat) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HongsException (app.hongs.HongsException)1 Default (app.hongs.util.verify.Default)1 Defiant (app.hongs.util.verify.Defiant)1 NoRepeat (app.hongs.util.verify.NoRepeat)1 Optional (app.hongs.util.verify.Optional)1 Repeated (app.hongs.util.verify.Repeated)1 Required (app.hongs.util.verify.Required)1 Rule (app.hongs.util.verify.Rule)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1