use of io.github.ihongs.util.verify.Ordinary in project HongsCORE by ihongs.
the class VerifyHelper method addRulesByForm.
public VerifyHelper addRulesByForm(String conf, String form, Map fs) throws HongsException {
FormSet formSet;
formSet = FormSet.getInstance("default");
Map ts = formSet.getEnum("__types__");
Map ps = formSet.getEnum("__typos__");
Iterator it = fs.entrySet().iterator();
while (it.hasNext()) {
Map.Entry et = (Map.Entry) it.next();
String name = (String) et.getKey();
if ("@".equals(name)) {
continue;
}
Map optz = (Map) et.getValue();
Map opts = new HashMap(optz);
String ruls = (String) opts.remove("__rule__");
opts.put("__conf__", conf);
opts.put("__form__", form);
opts.put("__name__", name);
if (ruls == null || !ruls.startsWith("@")) {
Object o;
o = opts.get("defiant");
if (o != null) {
Rule rule = new Defiant();
rule.config(opts);
this.addRule(name, rule);
}
o = opts.get("default");
if (o != null) {
Rule rule = new Default();
rule.config(opts);
this.addRule(name, rule);
}
o = opts.remove("__required__");
if (Synt.declare(o, false)) {
Rule rule = new Required();
rule.config(opts);
this.addRule(name, rule);
} else {
Rule rule = new Optional();
rule.config(opts);
this.addRule(name, rule);
}
o = opts.remove("__repeated__");
if (Synt.declare(o, false)) {
Rule rule = new Repeated();
rule.config(opts);
this.addRule(name, rule);
} else {
Rule rule = new Ordinary();
rule.config(opts);
this.addRule(name, rule);
}
}
String[] list;
if (ruls == null || (ruls.length() == 0)) {
String type = (String) opts.get("__type__");
String item;
if (ts.containsKey(type)) {
// 类名转换
item = (String) ts.get(type);
String c = item.substring(0, 1);
String n = item.substring(1);
item = "Is" + c.toUpperCase() + n;
} else if (ps.containsKey(type)) {
// 类型正则
opts.put("pattern", type);
item = "IsString";
} else {
item = "IsString";
}
list = new String[] { item };
} else {
if (ruls.startsWith("@")) {
ruls = ruls.substring(1);
}
list = ruls.split("[,;]");
}
// 添加规则实例
for (String item : list) {
item = item.trim();
if (item.length() == 0) {
continue;
}
if (!item.contains(".")) {
item = Rule.class.getPackage().getName() + "." + item;
}
Rule rule;
try {
rule = (Rule) (Class.forName(item).getDeclaredConstructor().newInstance());
} catch (ClassNotFoundException ex) {
throw new HongsException(1125, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
} catch (NoSuchMethodException ex) {
throw new HongsException(1125, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
} catch (InstantiationException ex) {
throw new HongsException(1126, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
} catch (IllegalAccessException ex) {
throw new HongsException(1126, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
} catch (IllegalArgumentException ex) {
throw new HongsException(1126, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
} catch (InvocationTargetException ex) {
throw new HongsException(1126, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
} catch (ClassCastException ex) {
throw new HongsException(1126, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
}
rule.config(opts);
this.addRule(name, rule);
}
}
/**
* 唯一键约束
*/
Map fp = (Map) fs.get("@");
if (fp != null) {
String fn;
Object uk;
Object ut;
ut = fp.get("data-ut");
if (ut == null) {
ut = fp.get("data-at");
if (ut == null) {
ut = conf + "/" + form + "/search";
}
}
for (Object o : fp.entrySet()) {
Map.Entry e = (Map.Entry) o;
fn = e.getKey().toString();
if (fn.startsWith("data-uk-")) {
fn = "UK-" + fn.substring(8);
} else if (fn.equals(/**/
"data-uk")) {
fn = "UK";
} else {
continue;
}
uk = e.getValue();
Rule fr = new IsUnique(true).config(Synt.mapOf("__name__", fn, "data-ut", ut, "data-uk", uk));
this.addRule(fn, fr);
}
}
return this;
}
Aggregations