KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > rules > basic > AvoidUsingOctalValues


1 package net.sourceforge.pmd.rules.basic;
2
3 import java.util.regex.Pattern JavaDoc;
4
5 import net.sourceforge.pmd.AbstractRule;
6 import net.sourceforge.pmd.PropertyDescriptor;
7 import net.sourceforge.pmd.ast.ASTLiteral;
8 import net.sourceforge.pmd.properties.BooleanProperty;
9
10 public class AvoidUsingOctalValues extends AbstractRule {
11
12     public static final Pattern JavaDoc OCTAL_PATTERN = Pattern.compile("0[0-7]{2,}[lL]?");
13
14     public static final Pattern JavaDoc STRICT_OCTAL_PATTERN = Pattern.compile("0[0-7]+[lL]?");
15
16     private static final PropertyDescriptor strictMethodsDescriptor = new BooleanProperty(
17             "strict", "Detect violations for 00 to 07.", false, 1.0f
18             );
19
20     public Object JavaDoc visit(ASTLiteral node, Object JavaDoc data) {
21         boolean strict = getBooleanProperty(strictMethodsDescriptor);
22         Pattern JavaDoc p = strict?STRICT_OCTAL_PATTERN:OCTAL_PATTERN;
23
24         String JavaDoc img = node.getImage();
25         if (img != null && p.matcher(img).matches()) {
26             addViolation(data, node);
27         }
28
29         return data;
30     }
31
32 }
33
Popular Tags