📄 validationconfiguration.java
字号:
/**
*
*/
package com.asterix.validation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.antlr.runtime.tree.CommonTree;
import com.asterix.validation.annotations.Rule;
import com.asterix.validation.annotations.RuleIterator;
/**
* @author Andrei A Adamian
*
*/
public class ValidationConfiguration {
private Map<String, Object> importedClasses = new HashMap<String, Object>();
private Map<String, CommonTree> groups = new HashMap<String, CommonTree>();
private Map<String, RuntimeMethod> rules = new HashMap<String, RuntimeMethod>();
private Map<String, IteratorMethod> iterators = new HashMap<String, IteratorMethod>();
public ValidationConfiguration(CommonTree tree) {
init(tree);
}
/**
* @param tree
*/
private void init(CommonTree tree) {
for (int i=0;i<tree.getChildCount();i++) {
CommonTree child = (CommonTree) tree.getChild(i);
String token = child.getText();
if ("IMPORT".equals(token)) {
loadClass(child);
} else if ("GROUP".equals(token)) {
groups.put(child.getChild(0).getText(), (CommonTree) child.getChild(1));
}
}
}
/**
* @param child
*/
private void loadClass(CommonTree child) {
String key = child.getChild(child.getChildCount()-1).getText();
String value = "";
for (int i=0;i<child.getChildCount();i++) {
value+=child.getChild(i).getText();
}
// TODO: classloader
try {
Class<IValidationRule> clazz = (Class<IValidationRule>) Class.forName(value);
initObject(clazz);
} catch (ClassNotFoundException e) {
}
}
/**
* @param clazz
*/
private void initObject(Class<IValidationRule> clazz) {
for (Method m:clazz.getMethods()) {
Rule rule = m.getAnnotation(Rule.class);
if (rule != null) {
rules.put(clazz.getSimpleName()+"."+m.getName(), new RuntimeMethod(m,clazz));
}
RuleIterator ruleIterator = m.getAnnotation(RuleIterator.class);
if (ruleIterator != null) {
iterators.put(clazz.getSimpleName()+"."+m.getName(), new IteratorMethod(m,clazz));
}
}
}
public CommonTree getGroup(String name) {
return groups.get(name);
}
public RuntimeMethod getRule(String name) {
RuntimeMethod method = rules.get(name);
if (method == null) {
throw new IllegalArgumentException("Unknown rule "+name);
}
try {
IValidationRule rule = method.getClazz().newInstance();
return new RuntimeMethod(method.getMethod(),rule);
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (ClassCastException e) {
throw new IllegalStateException("Rule class must implement IValidationRule interface.");
}
return null;
}
public IteratorMethod getIterator(String name) {
IteratorMethod method = iterators.get(name);
if (method == null) {
throw new IllegalArgumentException("Unknown iterator "+name);
}
try {
IValidationRule rule = method.getClazz().newInstance();
return new IteratorMethod(method.getMethod(),rule);
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (ClassCastException e) {
throw new IllegalStateException("Iterator class must implement IValidationRule interface.");
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -