Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 85 |  | Methods: | 4
| NCLOC: | 41 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
NoInlineStyleInformation.java | 60% | 92.3% | 100% | 81.5% |

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 | 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 |  | * Checks whether the name of the elementNode argument is one of STYLE_ELEMENT_NAMES.
57 |  | *
58 |  | * @param elementNode
59 |  | * @return boolean
60 |  | */
61 | 9 | private boolean isStyleElement(ASTElement elementNode) {
62 | 9 | 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 | 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 |  | }