KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * String literal nodes.
23  *
24  * @since 2.0
25  */

26 public class StringLiteral 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(StringLiteral.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 propertyList = new ArrayList JavaDoc(2);
44         createPropertyList(StringLiteral.class, propertyList);
45         addProperty(ESCAPED_VALUE_PROPERTY, propertyList);
46         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
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*</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 empty string.
67      */

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

70     /**
71      * Creates a new unparented string literal node owned by the given AST.
72      * By default, the string literal denotes the empty string.
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     StringLiteral(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 STRING_LITERAL;
111     }
112
113     /* (omit javadoc for this method)
114      * Method declared on ASTNode.
115      */

116     ASTNode clone0(AST target) {
117         StringLiteral result = new StringLiteral(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 to the given string
141      * literal token. The token is the sequence of characters that would appear
142      * in the source program, including enclosing double quotes and embedded
143      * escapes.
144      *
145      * @return the string literal token, including enclosing double
146      * quotes and embedded escapes
147      */

148     public String JavaDoc getEscapedValue() {
149         return this.escapedValue;
150     }
151         
152     /**
153      * Sets the string value of this literal node to the given string literal
154      * token. The token is the sequence of characters that would appear in the
155      * source program, including enclosing double quotes and embedded escapes.
156      * For example,
157      * <ul>
158      * <li><code>""</code> <code>setLiteral("\"\"")</code></li>
159      * <li><code>"hello world"</code> <code>setLiteral("\"hello world\"")</code></li>
160      * <li><code>"boo\nhoo"</code> <code>setLiteral("\"boo\\nhoo\"")</code></li>
161      * </ul>
162      *
163      * @param token the string literal token, including enclosing double
164      * quotes and embedded escapes
165      * @exception IllegalArgumentException if the argument is incorrect
166      */

167     public void setEscapedValue(String JavaDoc token) {
168         // update internalSetEscapedValue(String) if this is changed
169
if (token == null) {
170             throw new IllegalArgumentException JavaDoc("Token cannot be null"); //$NON-NLS-1$
171
}
172         Scanner scanner = this.ast.scanner;
173         char[] source = token.toCharArray();
174         scanner.setSource(source);
175         scanner.resetTo(0, source.length);
176         try {
177             int tokenType = scanner.getNextToken();
178             switch(tokenType) {
179                 case TerminalTokens.TokenNameStringLiteral:
180                     break;
181                 default:
182                     throw new IllegalArgumentException JavaDoc("Invalid string literal : >" + token + "<"); //$NON-NLS-1$//$NON-NLS-2$
183
}
184         } catch(InvalidInputException e) {
185             throw new IllegalArgumentException JavaDoc("Invalid string literal : >" + token + "<");//$NON-NLS-1$//$NON-NLS-2$
186
}
187         preValueChange(ESCAPED_VALUE_PROPERTY);
188         this.escapedValue = token;
189         postValueChange(ESCAPED_VALUE_PROPERTY);
190     }
191
192     /* (omit javadoc for this method)
193      * This method is a copy of setEscapedValue(String) that doesn't do any validation.
194      */

195     void internalSetEscapedValue(String JavaDoc token) {
196         preValueChange(ESCAPED_VALUE_PROPERTY);
197         this.escapedValue = token;
198         postValueChange(ESCAPED_VALUE_PROPERTY);
199     }
200
201     /**
202      * Returns the value of this literal node.
203      * <p>
204      * For example,
205      * <pre>
206      * StringLiteral s;
207      * s.setEscapedValue("\"hello\\nworld\"");
208      * assert s.getLiteralValue().equals("hello\nworld");
209      * </pre>
210      * </p>
211      * <p>
212      * Note that this is a convenience method that converts from the stored
213      * string literal token returned by <code>getEscapedLiteral</code>.
214      * </p>
215      *
216      * @return the string value without enclosing double quotes and embedded
217      * escapes
218      * @exception IllegalArgumentException if the literal value cannot be converted
219      */

220     public String JavaDoc getLiteralValue() {
221         String JavaDoc s = getEscapedValue();
222         int len = s.length();
223         if (len < 2 || s.charAt(0) != '\"' || s.charAt(len-1) != '\"' ) {
224             throw new IllegalArgumentException JavaDoc();
225         }
226         
227         Scanner scanner = this.ast.scanner;
228         char[] source = s.toCharArray();
229         scanner.setSource(source);
230         scanner.resetTo(0, source.length);
231         try {
232             int tokenType = scanner.getNextToken();
233             switch(tokenType) {
234                 case TerminalTokens.TokenNameStringLiteral:
235                     return scanner.getCurrentStringLiteral();
236                 default:
237                     throw new IllegalArgumentException JavaDoc();
238             }
239         } catch(InvalidInputException e) {
240             throw new IllegalArgumentException JavaDoc();
241         }
242     }
243
244     /**
245      * Sets the value of this literal node.
246      * <p>
247      * For example,
248      * <pre>
249      * StringLiteral s;
250      * s.setLiteralValue("hello\nworld");
251      * assert s.getEscapedValue("\"hello\\nworld\"");
252      * assert s.getLiteralValue().equals("hello\nworld");
253      * </pre>
254      * </p>
255      * <p>
256      * Note that this is a convenience method that converts to the stored
257      * string literal token acceptable to <code>setEscapedLiteral</code>.
258      * </p>
259      *
260      * @param value the string value without enclosing double quotes and
261      * embedded escapes
262      * @exception IllegalArgumentException if the argument is incorrect
263      */

264     public void setLiteralValue(String JavaDoc value) {
265         if (value == null) {
266             throw new IllegalArgumentException JavaDoc();
267         }
268         int len = value.length();
269         StringBuffer JavaDoc b = new StringBuffer JavaDoc(len + 2);
270         
271         b.append("\""); // opening delimiter //$NON-NLS-1$
272
for (int i = 0; i < len; i++) {
273             char c = value.charAt(i);
274             switch(c) {
275                 case '\b' :
276                     b.append("\\b"); //$NON-NLS-1$
277
break;
278                 case '\t' :
279                     b.append("\\t"); //$NON-NLS-1$
280
break;
281                 case '\n' :
282                     b.append("\\n"); //$NON-NLS-1$
283
break;
284                 case '\f' :
285                     b.append("\\f"); //$NON-NLS-1$
286
break;
287                 case '\r' :
288                     b.append("\\r"); //$NON-NLS-1$
289
break;
290                 case '\"':
291                     b.append("\\\""); //$NON-NLS-1$
292
break;
293                 case '\'':
294                     b.append("\\\'"); //$NON-NLS-1$
295
break;
296                 case '\\':
297                     b.append("\\\\"); //$NON-NLS-1$
298
break;
299                 case '\0' :
300                     b.append("\\0"); //$NON-NLS-1$
301
break;
302                 case '\1' :
303                     b.append("\\1"); //$NON-NLS-1$
304
break;
305                 case '\2' :
306                     b.append("\\2"); //$NON-NLS-1$
307
break;
308                 case '\3' :
309                     b.append("\\3"); //$NON-NLS-1$
310
break;
311                 case '\4' :
312                     b.append("\\4"); //$NON-NLS-1$
313
break;
314                 case '\5' :
315                     b.append("\\5"); //$NON-NLS-1$
316
break;
317                 case '\6' :
318                     b.append("\\6"); //$NON-NLS-1$
319
break;
320                 case '\7' :
321                     b.append("\\7"); //$NON-NLS-1$
322
break;
323                 default:
324                     b.append(c);
325             }
326         }
327         b.append("\""); // closing delimiter //$NON-NLS-1$
328
setEscapedValue(b.toString());
329     }
330     /* (omit javadoc for this method)
331      * Method declared on ASTNode.
332      */

333     int memSize() {
334         int size = BASE_NODE_SIZE + 1 * 4 + stringSize(escapedValue);
335         return size;
336     }
337     
338     /* (omit javadoc for this method)
339      * Method declared on ASTNode.
340      */

341     int treeSize() {
342         return memSize();
343     }
344 }
345
Popular Tags