Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 87 |  | Methods: | 4
| NCLOC: | 43 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
NoInlineStyleInformation.java | 80% | 100% | 100% | 92.6% |

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 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 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 STYLE_ATTRIBUTES = CollectionUtil.asSet(
38 |  | new String[]{"STYLE", "FONT", "SIZE", "COLOR", "FACE", "ALIGN", "VALIGN", "BGCOLOR"}
39 |  | );
40 |  |
41 | 3 | public Object visit(ASTAttribute node, Object data) {
42 | 3 | if (isStyleAttribute(node)) {
43 | 2 | addViolation(data, node);
44 |  | }
45 |  |
46 | 3 | return super.visit(node, data);
47 |  | }
48 |  |
49 | 9 | public Object visit(ASTElement node, Object data) {
50 | 9 | if (isStyleElement(node)) {
51 | 1 | addViolation(data, node);
52 |  | }
53 |  |
54 | 9 | 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 | 9 | private boolean isStyleElement(ASTElement elementNode) {
64 | 9 | 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 | 3 | private boolean isStyleAttribute(ASTAttribute attributeNode) {
75 | 3 | if (STYLE_ATTRIBUTES.contains(attributeNode.getName().toUpperCase())) {
76 | 2 | if (attributeNode.jjtGetParent() instanceof ASTElement) {
77 | 2 | ASTElement parent = (ASTElement) attributeNode.jjtGetParent();
78 | 2 | if (ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES.contains(parent
79 |  | .getName().toUpperCase())) {
80 | 2 | return true;
81 |  | }
82 |  | }
83 |  | }
84 |  |
85 | 1 | return false;
86 |  | }
87 |  | }