package br.com.metricminer2.parser.html; import java.lang.reflect.Method; import org.jsoup.nodes.Node; import org.jsoup.select.NodeVisitor; public class HtmlNodeVisitor implements NodeVisitor { @Override public void head(Node node, int depth) { visitNodeByClass(node); } @Override public void tail(Node node, int depth) { } private void visitNodeByClass(Node node) { Method visitorMethod = findVisitorMethodForNodeClass(node); if (visitorMethod != null && visitorMethod.getReturnType().equals(void.class)) { try { visitorMethod.invoke(this, node); } catch (Exception e) { throw new RuntimeException(e); } } } private Method findVisitorMethodForNodeClass(Node node) { try { return this.getClass().getMethod("visit", node.getClass()); } catch (NoSuchMethodException e) { return null; } } }