View Javadoc

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