View Javadoc

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