01: package net.sourceforge.pmd.jsp.rules;
02:
03: import java.util.Set;
04:
05: import net.sourceforge.pmd.jsp.ast.ASTAttribute;
06: import net.sourceforge.pmd.jsp.ast.ASTElement;
07: import net.sourceforge.pmd.util.CollectionUtil;
08:
09: /**
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
23: .asSet(new String[] { "B", "I", "FONT", "BASEFONT", "U",
24: "CENTER" });
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
30: .asSet(new String[] { "P", "TABLE", "THEAD", "TBODY",
31: "TFOOT", "TR", "TD", "COL", "COLGROUP" });
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
38: .asSet(new String[] { "STYLE", "FONT", "SIZE", "COLOR",
39: "FACE", "ALIGN", "VALIGN", "BGCOLOR" });
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()
65: .toUpperCase());
66: }
67:
68: /**
69: * Checks whether the attributeNode argument is a style attribute of a HTML element
70: * that can have style attributes.
71: *
72: * @param elementNode
73: * @return boolean
74: */
75: private boolean isStyleAttribute(ASTAttribute attributeNode) {
76: if (STYLE_ATTRIBUTES.contains(attributeNode.getName()
77: .toUpperCase())) {
78: if (attributeNode.jjtGetParent() instanceof ASTElement) {
79: ASTElement parent = (ASTElement) attributeNode
80: .jjtGetParent();
81: if (ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES
82: .contains(parent.getName().toUpperCase())) {
83: return true;
84: }
85: }
86: }
87:
88: return false;
89: }
90: }
|