1
2
3 package net.sourceforge.pmd.ast;
4
5 import java.util.regex.Pattern;
6
7 public class ASTLiteral extends SimpleJavaTypeNode {
8
9 private boolean isInt;
10 private boolean isFloat;
11 private boolean isChar;
12 private boolean isString;
13
14 public ASTLiteral(int id) {
15 super(id);
16 }
17
18 public ASTLiteral(JavaParser p, int id) {
19 super(p, id);
20 }
21
22 /**
23 * Accept the visitor. *
24 */
25 public Object jjtAccept(JavaParserVisitor visitor, Object data) {
26 return visitor.visit(this, data);
27 }
28
29 public void setIntLiteral() {
30 this.isInt = true;
31 }
32 public boolean isIntLiteral() {
33 return isInt;
34 }
35
36 public void setFloatLiteral() {
37 this.isFloat = true;
38 }
39 public boolean isFloatLiteral() {
40 return isFloat;
41 }
42
43 public void setCharLiteral() {
44 this.isChar = true;
45 }
46 public boolean isCharLiteral() {
47 return isChar;
48 }
49
50 public void setStringLiteral() {
51 this.isString = true;
52 }
53 public boolean isStringLiteral() {
54 return isString;
55 }
56
57 /**
58 * Returns true if this is a String literal with only one character.
59 * Handles octal and escape characters.
60 *
61 * @return true is this is a String literal with only one character
62 */
63 public boolean isSingleCharacterStringLiteral() {
64 if (isString) {
65 String image = getImage();
66 int length = image.length();
67 if (length == 3) {
68 return true;
69 } else if (image.charAt(1) == '\\') {
70 return SINGLE_CHAR_ESCAPE_PATTERN.matcher(image).matches();
71 }
72 }
73 return false;
74 }
75
76 /**
77 * Pattern used to detect a single escaped character or octal character in a String.
78 */
79 private static final Pattern SINGLE_CHAR_ESCAPE_PATTERN = Pattern
80 .compile("^\"\\\\(([ntbrf\\\\'\\\"])|([0-7][0-7]?)|([0-3][0-7][0-7]))\"");
81
82 }