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 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| public class NoInlineStyleInformation extends AbstractJspRule { |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| private static final List STYLE_ELEMENT_NAMES = Arrays.asList(new String[]{"B", |
23 |
| "I", "FONT", "BASEFONT", "U", "CENTER"}); |
24 |
| |
25 |
| |
26 |
| |
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 |
| |
34 |
| |
35 |
| |
36 |
| private static final List STYLE_ATTRIBUTES = Arrays.asList(new String[]{"STYLE", |
37 |
| "FONT", "SIZE", "COLOR", "FACE", "ALIGN", "VALIGN", "BGCOLOR"}); |
38 |
| |
39 |
2
| public Object visit(ASTAttribute node, Object data) {
|
40 |
2
| if (isStyleAttribute(node)) {
|
41 |
2
| addViolation(data, node);
|
42 |
| } |
43 |
| |
44 |
2
| return super.visit(node, data);
|
45 |
| } |
46 |
| |
47 |
9
| public Object visit(ASTElement node, Object data) {
|
48 |
9
| if (isStyleElement(node)) {
|
49 |
1
| addViolation(data, node);
|
50 |
| } |
51 |
| |
52 |
9
| return super.visit(node, data);
|
53 |
| } |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
9
| private boolean isStyleElement(ASTElement elementNode) {
|
62 |
9
| return STYLE_ELEMENT_NAMES.contains(elementNode.getName().toUpperCase());
|
63 |
| } |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
2
| private boolean isStyleAttribute(ASTAttribute attributeNode) {
|
73 |
2
| if (STYLE_ATTRIBUTES.contains(attributeNode.getName().toUpperCase())) {
|
74 |
2
| if (attributeNode.jjtGetParent() instanceof ASTElement) {
|
75 |
2
| ASTElement parent = (ASTElement) attributeNode.jjtGetParent();
|
76 |
2
| if (ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES.contains(parent
|
77 |
| .getName().toUpperCase())) { |
78 |
2
| return true;
|
79 |
| } |
80 |
| } |
81 |
| } |
82 |
| |
83 |
0
| return false;
|
84 |
| } |
85 |
| } |