package org.theonefx.wcframework.aop;
import org.theonefx.wcframework.aop.annotation.InterceptType;
import org.theonefx.wcframework.core.BeanDefinitionRegistry;
import org.theonefx.wcframework.core.XmlParseContext;
import org.theonefx.wcframework.core.XmlParser;
import org.theonefx.wcframework.core.XmlParserDelegate;
import org.theonefx.wcframework.core.exception.XmlParseException;
import org.theonefx.wcframework.utils.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class AopXmlParser implements XmlParser {
private final AopCore core;
public static final String TAG_ASPECT = "aspect";
public static final String TAG_POINTCUT = "pointcut";
public static final String TAG_BEFORE = "before";
public static final String TAG_AFTER = "after";
public static final String TAG_THROWING = "throwing";
public static final String ATTR_EXPRESSION = "expression";
public static final String ATTR_REF = "ref";
public static final String ATTR_POINTCUT_REF = "pointcut-ref";
public static final String ATTR_IGNORE_EXCEPTION = "ignore-exception";
public static final String ATTR_METHOD = "method";
public static final String ATTR_ID = "id";
public AopXmlParser(AopCore core) {
this.core = core;
}
@Override
public void parse(XmlParserDelegate delegate, Element element, BeanDefinitionRegistry registry, XmlParseContext context) {
if (TAG_ASPECT.equals(element.getNodeName())) {
AdvisorInfo info = new AdvisorInfo();
String ref = element.getAttribute(ATTR_REF);
if (StringUtils.isBlank(ref)) {
throw new XmlParseException("<aspect>的ref属性不能为空");
}
info.setRef(ref);
NodeList childs = element.getChildNodes();
for (int i = 0; i < childs.getLength(); i++) {
Node node = childs.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element ele = (Element) node;
if (TAG_POINTCUT.equalsIgnoreCase(ele.getNodeName())) {
processPointcut(ele, info);
} else if (TAG_BEFORE.equalsIgnoreCase(ele.getNodeName())) {
processAdvisor(ele, info, InterceptType.BEFORE);
} else if (TAG_AFTER.equalsIgnoreCase(ele.getNodeName())) {
processAdvisor(ele, info, InterceptType.AFTER);
} else if (TAG_THROWING.equalsIgnoreCase(ele.getNodeName())) {
processAdvisor(ele, info, InterceptType.THROWS);
}
}
}
core.registAdvisor(info);
}
}
private void processAdvisor(Element ele, AdvisorInfo info, InterceptType type) {
String method = ele.getAttribute(ATTR_METHOD);
String pointcut = ele.getAttribute(ATTR_POINTCUT_REF);
String ignoreException = ele.getAttribute(ATTR_IGNORE_EXCEPTION);
String[] ignoreExceptions = StringUtils.isNotBlank(ignoreException) ? ignoreException.split(",") : null;
info.addAdviceHolder(method, type, pointcut, ignoreExceptions);
}
private void processPointcut(Element ele, AdvisorInfo info) {
String id = ele.getAttribute(ATTR_ID);
String expression = ele.getAttribute(ATTR_EXPRESSION);
info.addPointcut(id, new ExpressionPointcut(expression));
}
}