View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.jsp.rule;
5   
6   import java.util.List;
7   
8   import net.sourceforge.pmd.Rule;
9   import net.sourceforge.pmd.RuleContext;
10  import net.sourceforge.pmd.lang.ast.Node;
11  import net.sourceforge.pmd.lang.jsp.ast.ASTCompilationUnit;
12  import net.sourceforge.pmd.lang.jsp.ast.JspNode;
13  import net.sourceforge.pmd.lang.jsp.ast.JspParserVisitor;
14  import net.sourceforge.pmd.lang.jsp.ast.JspParserVisitorAdapter;
15  import net.sourceforge.pmd.lang.rule.AbstractRuleChainVisitor;
16  import net.sourceforge.pmd.lang.rule.XPathRule;
17  
18  public class JspRuleChainVisitor extends AbstractRuleChainVisitor {
19  
20      protected void indexNodes(List<Node> nodes, RuleContext ctx) {
21          JspParserVisitor jspParserVisitor = new JspParserVisitorAdapter() {
22              // Perform a visitation of the AST to index nodes which need
23              // visiting by type
24              public Object visit(JspNode node, Object data) {
25                  indexNode(node);
26                  return super.visit(node, data);
27              }
28          };
29  
30          for (int i = 0; i < nodes.size(); i++) {
31              jspParserVisitor.visit((ASTCompilationUnit)nodes.get(i), ctx);
32          }
33      }
34  
35      protected void visit(Rule rule, Node node, RuleContext ctx) {
36          // Rule better either be a JspParserVisitor, or a XPathRule
37          if (rule instanceof JspParserVisitor) {
38              ((JspNode) node).jjtAccept((JspParserVisitor) rule, ctx);
39          } else {
40              ((XPathRule) rule).evaluate(node, ctx);
41          }
42      }
43  }