KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > CharacterLiteral


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jdt.core.compiler.InvalidInputException;
18 import org.eclipse.jdt.internal.compiler.parser.Scanner;
19 import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
20
21 /**
22  * Character literal nodes.
23  *
24  * @since 2.0
25  */

26 public class CharacterLiteral extends Expression {
27
28     /**
29      * The "escapedValue" structural property of this node type.
30      * @since 3.0
31      */

32     public static final SimplePropertyDescriptor ESCAPED_VALUE_PROPERTY =
33         new SimplePropertyDescriptor(CharacterLiteral.class, "escapedValue", String JavaDoc.class, MANDATORY); //$NON-NLS-1$
34

35     /**
36      * A list of property descriptors (element type:
37      * {@link StructuralPropertyDescriptor}),
38      * or null if uninitialized.
39      */

40     private static final List JavaDoc PROPERTY_DESCRIPTORS;
41     
42     static {
43         List JavaDoc properyList = new ArrayList JavaDoc(2);
44         createPropertyList(CharacterLiteral.class, properyList);
45         addProperty(ESCAPED_VALUE_PROPERTY, properyList);
46         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
47     }
48
49     /**
50      * Returns a list of structural property descriptors for this node type.
51      * Clients must not modify the result.
52      *
53      * @param apiLevel the API level; one of the
54      * <code>AST.JLS&ast;</code> constants
55
56      * @return a list of property descriptors (element type:
57      * {@link StructuralPropertyDescriptor})
58      * @since 3.0
59      */

60     public static List JavaDoc propertyDescriptors(int apiLevel) {
61         return PROPERTY_DESCRIPTORS;
62     }
63             
64     /**
65      * The literal string, including quotes and escapes; defaults to the
66      * literal for the character 'X'.
67      */

68     private String JavaDoc escapedValue = "\'X\'";//$NON-NLS-1$
69

70     /**
71      * Creates a new unparented character literal node owned by the given AST.
72      * By default, the character literal denotes an unspecified character.
73      * <p>
74      * N.B. This constructor is package-private.
75      * </p>
76      *
77      * @param ast the AST that is to own this node
78      */

79     CharacterLiteral(AST ast) {
80         super(ast);
81     }
82
83     /* (omit javadoc for this method)
84      * Method declared on ASTNode.
85      */

86     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
87         return propertyDescriptors(apiLevel);
88     }
89     
90     /* (omit javadoc for this method)
91      * Method declared on ASTNode.
92      */

93     final Object JavaDoc internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object JavaDoc value) {
94         if (property == ESCAPED_VALUE_PROPERTY) {
95             if (get) {
96                 return getEscapedValue();
97             } else {
98                 setEscapedValue((String JavaDoc) value);
99                 return null;
100             }
101         }
102         // allow default implementation to flag the error
103
return super.internalGetSetObjectProperty(property, get, value);
104     }
105     
106     /* (omit javadoc for this method)
107      * Method declared on ASTNode.
108      */

109     final int getNodeType0() {
110         return CHARACTER_LITERAL;
111     }
112
113     /* (omit javadoc for this method)
114      * Method declared on ASTNode.
115      */

116     ASTNode clone0(AST target) {
117         CharacterLiteral result = new CharacterLiteral(target);
118         result.setSourceRange(this.getStartPosition(), this.getLength());
119         result.setEscapedValue(getEscapedValue());
120         return result;
121     }
122
123     /* (omit javadoc for this method)
124      * Method declared on ASTNode.
125      */

126     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
127         // dispatch to correct overloaded match method
128
return matcher.match(this, other);
129     }
130
131     /* (omit javadoc for this method)
132      * Method declared on ASTNode.
133      */

134     void accept0(ASTVisitor visitor) {
135         visitor.visit(this);
136         visitor.endVisit(this);
137     }
138     
139     /**
140      * Returns the string value of this literal node. The value is the sequence
141      * of characters that would appear in the source program, including
142      * enclosing single quotes and embedded escapes.
143      *
144      * @return the escaped string value, including enclosing single quotes
145      * and embedded escapes
146      */

147     public String JavaDoc getEscapedValue() {
148         return this.escapedValue;
149     }
150         
151     /**
152      * Sets the string value of this literal node. The value is the sequence
153      * of characters that would appear in the source program, including
154      * enclosing single quotes and embedded escapes. For example,
155      * <ul>
156      * <li><code>'a'</code> <code>setEscapedValue("\'a\'")</code></li>
157      * <li><code>'\n'</code> <code>setEscapedValue("\'\\n\'")</code></li>
158      * </ul>
159      *
160      * @param value the string value, including enclosing single quotes
161      * and embedded escapes
162      * @exception IllegalArgumentException if the argument is incorrect
163      */

164     public void setEscapedValue(String JavaDoc value) {
165         // check setInternalEscapedValue(String) if this method is changed
166
if (value == null) {
167             throw new IllegalArgumentException JavaDoc();
168         }
169         Scanner scanner = this.ast.scanner;
170         char[] source = value.toCharArray();
171         scanner.setSource(source);
172         scanner.resetTo(0, source.length);
173         try {
174             int tokenType = scanner.getNextToken();
175             switch(tokenType) {
176                 case TerminalTokens.TokenNameCharacterLiteral:
177                     break;
178                 default:
179                     throw new IllegalArgumentException JavaDoc();
180             }
181         } catch(InvalidInputException e) {
182             throw new IllegalArgumentException JavaDoc();
183         }
184         preValueChange(ESCAPED_VALUE_PROPERTY);
185         this.escapedValue = value;
186         postValueChange(ESCAPED_VALUE_PROPERTY);
187     }
188
189
190     /* (omit javadoc for this method)
191      * This method is a copy of setEscapedValue(String) that doesn't do any validation.
192      */

193     void internalSetEscapedValue(String JavaDoc value) {
194         preValueChange(ESCAPED_VALUE_PROPERTY);
195         this.escapedValue = value;
196         postValueChange(ESCAPED_VALUE_PROPERTY);
197     }
198
199     /**
200      * Returns the value of this literal node.
201      * <p>
202      * For example,
203      * <pre>
204      * CharacterLiteral s;
205      * s.setEscapedValue("\'x\'");
206      * assert s.charValue() == 'x';
207      * </pre>
208      * </p>
209      *
210      * @return the character value without enclosing quotes and embedded
211      * escapes
212      * @exception IllegalArgumentException if the literal value cannot be converted
213      */

214     public char charValue() {
215         Scanner scanner = this.ast.scanner;
216         char[] source = escapedValue.toCharArray();
217         scanner.setSource(source);
218         scanner.resetTo(0, source.length);
219         int firstChar = scanner.getNextChar();
220         int secondChar = scanner.getNextChar();
221
222         if (firstChar == -1 || firstChar != '\'') {
223             throw new IllegalArgumentException JavaDoc("illegal character literal");//$NON-NLS-1$
224
}
225         char value = (char) secondChar;
226         char nextChar = (char) scanner.getNextChar();
227         if (secondChar == '\\') {
228             if (nextChar == -1) {
229                 throw new IllegalArgumentException JavaDoc("illegal character literal");//$NON-NLS-1$
230
}
231             switch(nextChar) {
232                 case 'b' :
233                     value = '\b';
234                     break;
235                 case 't' :
236                     value = '\t';
237                     break;
238                 case 'n' :
239                     value = '\n';
240                     break;
241                 case 'f' :
242                     value = '\f';
243                     break;
244                 case 'r' :
245                     value = '\r';
246                     break;
247                 case '\"':
248                     value = '\"';
249                     break;
250                 case '\'':
251                     value = '\'';
252                     break;
253                 case '\\':
254                     value = '\\';
255                     break;
256                 default : //octal (well-formed: ended by a ' )
257
if (Character.isDigit(nextChar)) {
258                         int number = Character.getNumericValue(nextChar);
259                         nextChar = (char) scanner.getNextChar();
260                         if (nextChar == -1) {
261                             throw new IllegalArgumentException JavaDoc("illegal character literal");//$NON-NLS-1$
262
}
263                         if (nextChar != '\'') {
264                             if (!Character.isDigit(nextChar)) {
265                                 throw new IllegalArgumentException JavaDoc("illegal character literal");//$NON-NLS-1$
266
}
267                             number = (number * 8) + Character.getNumericValue(nextChar);
268                         }
269                         nextChar = (char) scanner.getNextChar();
270                         if (nextChar == -1) {
271                             throw new IllegalArgumentException JavaDoc("illegal character literal");//$NON-NLS-1$
272
}
273                         if (nextChar != '\'') {
274                             if (!Character.isDigit(nextChar)) {
275                                 throw new IllegalArgumentException JavaDoc("illegal character literal");//$NON-NLS-1$
276
}
277                             number = (number * 8) + Character.getNumericValue(nextChar);
278                         }
279                         value = (char) number;
280                     } else {
281                         throw new IllegalArgumentException JavaDoc("illegal character literal");//$NON-NLS-1$
282
}
283                     break;
284             }
285             nextChar = (char) scanner.getNextChar();
286             if (nextChar == -1) {
287                 throw new IllegalArgumentException JavaDoc("illegal character literal");//$NON-NLS-1$
288
}
289         }
290         if (nextChar == -1 || nextChar != '\'') {
291             throw new IllegalArgumentException JavaDoc("illegal character literal");//$NON-NLS-1$
292
}
293         return value;
294     }
295     /**
296      * Sets the value of this character literal node to the given character.
297      * <p>
298      * For example,
299      * <pre>
300      * CharacterLiteral s;
301      * s.setCharValue('x');
302      * assert s.charValue() == 'x';
303      * assert s.getEscapedValue("\'x\'");
304      * </pre>
305      * </p>
306      *
307      * @param value the character value
308      */

309     public void setCharValue(char value) {
310         StringBuffer JavaDoc b = new StringBuffer JavaDoc(3);
311         
312         b.append('\''); // opening delimiter
313
switch(value) {
314             case '\b' :
315                 b.append("\\b"); //$NON-NLS-1$
316
break;
317             case '\t' :
318                 b.append("\\t"); //$NON-NLS-1$
319
break;
320             case '\n' :
321                 b.append("\\n"); //$NON-NLS-1$
322
break;
323             case '\f' :
324                 b.append("\\f"); //$NON-NLS-1$
325
break;
326             case '\r' :
327                 b.append("\\r"); //$NON-NLS-1$
328
break;
329             case '\"':
330                 b.append("\\\""); //$NON-NLS-1$
331
break;
332             case '\'':
333                 b.append("\\\'"); //$NON-NLS-1$
334
break;
335             case '\\':
336                 b.append("\\\\"); //$NON-NLS-1$
337
break;
338             case '\0' :
339                 b.append("\\0"); //$NON-NLS-1$
340
break;
341             case '\1' :
342                 b.append("\\1"); //$NON-NLS-1$
343
break;
344             case '\2' :
345                 b.append("\\2"); //$NON-NLS-1$
346
break;
347             case '\3' :
348                 b.append("\\3"); //$NON-NLS-1$
349
break;
350             case '\4' :
351                 b.append("\\4"); //$NON-NLS-1$
352
break;
353             case '\5' :
354                 b.append("\\5"); //$NON-NLS-1$
355
break;
356             case '\6' :
357                 b.append("\\6"); //$NON-NLS-1$
358
break;
359             case '\7' :
360                 b.append("\\7"); //$NON-NLS-1$
361
break;
362             default:
363                 b.append(value);
364         }
365         b.append('\''); // closing delimiter
366
setEscapedValue(b.toString());
367     }
368     
369     /* (omit javadoc for this method)
370      * Method declared on ASTNode.
371      */

372     int memSize() {
373         int size = BASE_NODE_SIZE + 1 * 4 + stringSize(escapedValue);
374         return size;
375     }
376     
377     /* (omit javadoc for this method)
378      * Method declared on ASTNode.
379      */

380     int treeSize() {
381         return memSize();
382     }
383 }
384
385
Popular Tags