1
2
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
15
16
17
18
19 public class NoInlineStyleInformationRule extends AbstractJspRule {
20
21
22
23
24
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
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
39
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
63
64
65
66
67 private boolean isStyleElement(ASTElement elementNode) {
68 return STYLE_ELEMENT_NAMES.contains(elementNode.getName().toUpperCase());
69 }
70
71
72
73
74
75
76
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 }