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 15 public class NoInlineStyleInformation extends AbstractJspRule { 16 17 19 22 private static final Set STYLE_ELEMENT_NAMES = CollectionUtil.asSet( 23 new String []{"B", "I", "FONT", "BASEFONT", "U", "CENTER"} 24 ); 25 26 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 37 private static final Set STYLE_ATTRIBUTES = CollectionUtil.asSet( 38 new String []{"STYLE", "FONT", "SIZE", "COLOR", "FACE", "ALIGN", "VALIGN", "BGCOLOR"} 39 ); 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 63 private boolean isStyleElement(ASTElement elementNode) { 64 return STYLE_ELEMENT_NAMES.contains(elementNode.getName().toUpperCase()); 65 } 66 67 74 private boolean isStyleAttribute(ASTAttribute attributeNode) { 75 if (STYLE_ATTRIBUTES.contains(attributeNode.getName().toUpperCase())) { 76 if (attributeNode.jjtGetParent() instanceof ASTElement) { 77 ASTElement parent = (ASTElement) attributeNode.jjtGetParent(); 78 if (ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES.contains(parent 79 .getName().toUpperCase())) { 80 return true; 81 } 82 } 83 } 84 85 return false; 86 } 87 } 88 | Popular Tags |