01: package net.sourceforge.pmd.jsp.rules;
02:
03: import net.sourceforge.pmd.RuleContext;
04: import net.sourceforge.pmd.jsp.ast.ASTJspDirectiveAttribute;
05: import net.sourceforge.pmd.rules.ImportWrapper;
06:
07: import java.util.HashSet;
08: import java.util.List;
09: import java.util.Set;
10: import java.util.StringTokenizer;
11:
12: public class DuplicateJspImports extends AbstractJspRule {
13:
14: private Set<ImportWrapper> imports = new HashSet<ImportWrapper>();
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,
37: node);
38: if (imports.contains(wrapper)) {
39: addViolation(data, node, node.getImage());
40: } else {
41: imports.add(wrapper);
42: }
43: }
44: return super.visit(node, data);
45: }
46:
47: }
|