Node srcClassNode = node.getAttributes().getNamedItem("srcClass");
Node targetClassNode = node.getAttributes().getNamedItem("targetClass");
if (clazzNode != null) {
Class clazz = ReflectionHelper.forName(clazzNode.getNodeValue());
if (!Convertor.class.isAssignableFrom(clazz)) { // 检查下必须为Convertor的子类
throw new BeanMappingException(clazz.toString() + " is not implements Convertor");
}
Convertor convertor = (Convertor) ReflectionHelper.newInstance(clazz);
if (aliasNode != null) {
// 注册为别名
String alias = aliasNode.getNodeValue();
ConvertorHelper.getInstance().registerConvertor(alias, convertor);
if (logger.isDebugEnabled()) {
logger.debug("register Convertor[" + clazz.toString() + "] to alias[" + alias + "]");
}
} else {
String srcClass = srcClassNode.getNodeValue();
String targetClass = targetClassNode.getNodeValue();
if (StringUtils.isEmpty(srcClass) || StringUtils.isEmpty(targetClass)) {
throw new BeanMappingException(clazz.toString() + " should fix srcClass and targetClass!");
}
Class srcClazz = ReflectionHelper.forName(srcClassNode.getNodeValue());
Class targetClazz = ReflectionHelper.forName(targetClassNode.getNodeValue());
// 根据srcClass/targetClass进行自动匹配
ConvertorHelper.getInstance().registerConvertor(srcClazz, targetClazz, convertor);