KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > jsp > rules > DuplicateJspImports


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 JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Set JavaDoc;
10 import java.util.StringTokenizer JavaDoc;
11
12 public class DuplicateJspImports extends AbstractJspRule {
13
14     private Set JavaDoc imports = new HashSet JavaDoc();
15
16     public void apply(List JavaDoc 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 JavaDoc visit(ASTJspDirectiveAttribute node, Object JavaDoc data) {
27
28         if (!"import".equals(node.getName())) {
29             return super.visit(node, data);
30         }
31         String JavaDoc values = node.getValue();
32         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(values, ",");
33         int count = st.countTokens();
34         for (int ix = 0; ix < count; ix++) {
35             String JavaDoc 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 }
47
Popular Tags