use of app.hongs.util.verify.Required 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;
}
Aggregations