View Javadoc

    1   package net.sourceforge.pmd.jsp.rules;
    2
    3   import java.util.Set;
    4
    5   import net.sourceforge.pmd.jsp.ast.ASTAttribute;
    6   import net.sourceforge.pmd.jsp.ast.ASTElement;
    7   import net.sourceforge.pmd.util.CollectionUtil;
    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 Set<String> STYLE_ELEMENT_NAMES = CollectionUtil.asSet(
    23      		new String[]{"B", "I", "FONT", "BASEFONT", "U", "CENTER"}
    24      		);
    25
    26      /**
    27       * List of HTML element-names that can have attributes defining style.
    28       */
    29      private static final Set<String> ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES = CollectionUtil.asSet(
    30      		new String[]{"P", "TABLE", "THEAD", "TBODY", "TFOOT", "TR", "TD", "COL", "COLGROUP"}
    31      		);
    32
    33      /**
    34       * List of attributes that define style when they are attributes of HTML elements with
    35       * names in ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES.
    36       */
    37      private static final Set<String> STYLE_ATTRIBUTES = CollectionUtil.asSet(
    38      		new String[]{"STYLE", "FONT", "SIZE", "COLOR", "FACE", "ALIGN", "VALIGN", "BGCOLOR"}
    39      		);
    40
    41      public Object visit(ASTAttribute node, Object data) {
    42          if (isStyleAttribute(node)) {
    43              addViolation(data, node);
    44          }
    45
    46          return super.visit(node, data);
    47      }
    48
    49      public Object visit(ASTElement node, Object data) {
    50          if (isStyleElement(node)) {
    51              addViolation(data, node);
    52          }
    53
    54          return super.visit(node, data);
    55      }
    56
    57      /**
    58       * Checks whether the name of the elementNode argument is one of STYLE_ELEMENT_NAMES.
    59       *
    60       * @param elementNode
    61       * @return boolean
    62       */
    63      private boolean isStyleElement(ASTElement elementNode) {
    64          return STYLE_ELEMENT_NAMES.contains(elementNode.getName().toUpperCase());
    65      }
    66
    67      /**
    68       * Checks whether the attributeNode argument is a style attribute of a HTML element
    69       * that can have style attributes.
    70       *
    71       * @param elementNode
    72       * @return boolean
    73       */
    74      private boolean isStyleAttribute(ASTAttribute attributeNode) {
    75          if (STYLE_ATTRIBUTES.contains(attributeNode.getName().toUpperCase())) {
    76              if (attributeNode.jjtGetParent() instanceof ASTElement) {
    77                  ASTElement parent = (ASTElement) attributeNode.jjtGetParent();
    78                  if (ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES.contains(parent
    79                          .getName().toUpperCase())) {
    80                      return true;
    81                  }
    82              }
    83          }
    84
    85          return false;
    86      }
    87  }