View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.lang.xml.rule;
    5
    6   import java.util.Collections;
    7   import java.util.List;
    8   import java.util.Stack;
    9
    10  import net.sourceforge.pmd.Rule;
    11  import net.sourceforge.pmd.RuleContext;
    12  import net.sourceforge.pmd.lang.ast.Node;
    13  import net.sourceforge.pmd.lang.rule.AbstractRuleChainVisitor;
    14  import net.sourceforge.pmd.lang.rule.XPathRule;
    15
    16  public class XmlRuleChainVisitor extends AbstractRuleChainVisitor {
    17
    18      protected void indexNodes(List<Node> nodes, RuleContext ctx) {
    19  	// Visit Nodes in DFS order
    20  	Stack<Node> stack = new Stack<Node>();
    21  	stack.addAll(nodes);
    22  	Collections.reverse(stack);
    23  	while (!stack.isEmpty()) {
    24  	    Node node = stack.pop();
    25  	    indexNode(node);
    26  	    if (node.jjtGetNumChildren() > 0) {
    27  		for (int i = node.jjtGetNumChildren() - 1; i >= 0; i--) {
    28  		    stack.push(node.jjtGetChild(i));
    29  		}
    30  	    }
    31  	}
    32      }
    33
    34      protected void visit(Rule rule, Node node, RuleContext ctx) {
    35  	// Rule better be a XPathRule
    36  	((XPathRule) rule).evaluate(node, ctx);
    37      }
    38  }