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.basic;
5   
6   import java.util.Set;
7   
8   import net.sourceforge.pmd.lang.jsp.ast.ASTAttribute;
9   import net.sourceforge.pmd.lang.jsp.ast.ASTElement;
10  import net.sourceforge.pmd.lang.jsp.rule.AbstractJspRule;
11  import net.sourceforge.pmd.util.CollectionUtil;
12  
13  /**
14   * This rule checks that no "style" elements (like <B>, <FONT>, ...) are used, and that no
15   * "style" attributes (like "font", "size", "align") are used.
16   *
17   * @author pieter_van_raemdonck
18   */
19  public class NoInlineStyleInformationRule extends AbstractJspRule {
20  
21      // These lists should probably be extended
22  	
23      /**
24       * List of HTML element-names that define style.
25       */
26      private static final Set<String> STYLE_ELEMENT_NAMES = CollectionUtil.asSet(
27      		new String[]{"B", "I", "FONT", "BASEFONT", "U", "CENTER"}
28      		);
29  
30      /**
31       * List of HTML element-names that can have attributes defining style.
32       */
33      private static final Set<String> ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES = CollectionUtil.asSet(
34      		new String[]{"P", "TABLE", "THEAD", "TBODY", "TFOOT", "TR", "TD", "COL", "COLGROUP"}
35      		);
36  
37      /**
38       * List of attributes that define style when they are attributes of HTML elements with
39       * names in ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES.
40       */
41      private static final Set<String> STYLE_ATTRIBUTES = CollectionUtil.asSet(
42      		new String[]{"STYLE", "FONT", "SIZE", "COLOR", "FACE", "ALIGN", "VALIGN", "BGCOLOR"}
43      		);
44      
45      public Object visit(ASTAttribute node, Object data) {
46          if (isStyleAttribute(node)) {
47              addViolation(data, node);
48          }
49  
50          return super.visit(node, data);
51      }
52  
53      public Object visit(ASTElement node, Object data) {
54          if (isStyleElement(node)) {
55              addViolation(data, node);
56          }
57  
58          return super.visit(node, data);
59      }
60  
61      /**
62       * Checks whether the name of the elementNode argument is one of STYLE_ELEMENT_NAMES.
63       *
64       * @param elementNode
65       * @return boolean
66       */
67      private boolean isStyleElement(ASTElement elementNode) {
68          return STYLE_ELEMENT_NAMES.contains(elementNode.getName().toUpperCase());
69      }
70  
71      /**
72       * Checks whether the attributeNode argument is a style attribute of a HTML element
73       * that can have style attributes.
74       *
75       * @param attributeNode The attribute node.
76       * @return <code>true</code> if a style attribute, <code>false</code> otherwise.
77       */
78      private boolean isStyleAttribute(ASTAttribute attributeNode) {
79          if (STYLE_ATTRIBUTES.contains(attributeNode.getName().toUpperCase())) {
80              if (attributeNode.jjtGetParent() instanceof ASTElement) {
81                  ASTElement parent = (ASTElement) attributeNode.jjtGetParent();
82                  if (ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES.contains(parent
83                          .getName().toUpperCase())) {
84                      return true;
85                  }
86              }
87          }
88  
89          return false;
90      }
91  }