View Javadoc

    1   package net.sourceforge.pmd.jsp.rules;
    2
    3   import net.sourceforge.pmd.RuleContext;
    4   import net.sourceforge.pmd.jsp.ast.ASTJspDirectiveAttribute;
    5   import net.sourceforge.pmd.rules.ImportWrapper;
    6
    7   import java.util.HashSet;
    8   import java.util.List;
    9   import java.util.Set;
    10  import java.util.StringTokenizer;
    11
    12  public class DuplicateJspImports extends AbstractJspRule {
    13
    14      private Set imports = new HashSet();
    15
    16      public void apply(List acus, RuleContext ctx) {
    17          /*
    18           * TODO: This method is a hack! It's overriding the parent's method
    19           * because the JSP parsing doesn't seem to hit ASTCompilationUnit
    20           * properly
    21           */
    22          imports.clear();
    23          super.apply(acus, ctx);
    24      }
    25
    26      public Object visit(ASTJspDirectiveAttribute node, Object data) {
    27
    28          if (!"import".equals(node.getName())) {
    29              return super.visit(node, data);
    30          }
    31          String values = node.getValue();
    32          StringTokenizer st = new StringTokenizer(values, ",");
    33          int count = st.countTokens();
    34          for (int ix = 0; ix < count; ix++) {
    35              String token = st.nextToken();
    36              ImportWrapper wrapper = new ImportWrapper(token, token, node);
    37              if (imports.contains(wrapper)) {
    38                  addViolation(data, node, node.getImage());
    39              } else {
    40                  imports.add(wrapper);
    41              }
    42          }
    43          return super.visit(node, data);
    44      }
    45
    46  }