1 11 12 package org.eclipse.jdt.core.dom; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 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 26 public class StringLiteral extends Expression { 27 28 32 public static final SimplePropertyDescriptor ESCAPED_VALUE_PROPERTY = 33 new SimplePropertyDescriptor(StringLiteral.class, "escapedValue", String .class, MANDATORY); 35 40 private static final List PROPERTY_DESCRIPTORS; 41 42 static { 43 List propertyList = new ArrayList (2); 44 createPropertyList(StringLiteral.class, propertyList); 45 addProperty(ESCAPED_VALUE_PROPERTY, propertyList); 46 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); 47 } 48 49 60 public static List propertyDescriptors(int apiLevel) { 61 return PROPERTY_DESCRIPTORS; 62 } 63 64 68 private String escapedValue = "\"\""; 70 79 StringLiteral(AST ast) { 80 super(ast); 81 } 82 83 86 final List internalStructuralPropertiesForType(int apiLevel) { 87 return propertyDescriptors(apiLevel); 88 } 89 90 93 final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) { 94 if (property == ESCAPED_VALUE_PROPERTY) { 95 if (get) { 96 return getEscapedValue(); 97 } else { 98 setEscapedValue((String ) value); 99 return null; 100 } 101 } 102 return super.internalGetSetObjectProperty(property, get, value); 104 } 105 106 109 final int getNodeType0() { 110 return STRING_LITERAL; 111 } 112 113 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 126 final boolean subtreeMatch0(ASTMatcher matcher, Object other) { 127 return matcher.match(this, other); 129 } 130 131 134 void accept0(ASTVisitor visitor) { 135 visitor.visit(this); 136 visitor.endVisit(this); 137 } 138 139 148 public String getEscapedValue() { 149 return this.escapedValue; 150 } 151 152 167 public void setEscapedValue(String token) { 168 if (token == null) { 170 throw new IllegalArgumentException ("Token cannot be null"); } 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 ("Invalid string literal : >" + token + "<"); } 184 } catch(InvalidInputException e) { 185 throw new IllegalArgumentException ("Invalid string literal : >" + token + "<"); } 187 preValueChange(ESCAPED_VALUE_PROPERTY); 188 this.escapedValue = token; 189 postValueChange(ESCAPED_VALUE_PROPERTY); 190 } 191 192 195 void internalSetEscapedValue(String token) { 196 preValueChange(ESCAPED_VALUE_PROPERTY); 197 this.escapedValue = token; 198 postValueChange(ESCAPED_VALUE_PROPERTY); 199 } 200 201 220 public String getLiteralValue() { 221 String s = getEscapedValue(); 222 int len = s.length(); 223 if (len < 2 || s.charAt(0) != '\"' || s.charAt(len-1) != '\"' ) { 224 throw new IllegalArgumentException (); 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 (); 238 } 239 } catch(InvalidInputException e) { 240 throw new IllegalArgumentException (); 241 } 242 } 243 244 264 public void setLiteralValue(String value) { 265 if (value == null) { 266 throw new IllegalArgumentException (); 267 } 268 int len = value.length(); 269 StringBuffer b = new StringBuffer (len + 2); 270 271 b.append("\""); for (int i = 0; i < len; i++) { 273 char c = value.charAt(i); 274 switch(c) { 275 case '\b' : 276 b.append("\\b"); break; 278 case '\t' : 279 b.append("\\t"); break; 281 case '\n' : 282 b.append("\\n"); break; 284 case '\f' : 285 b.append("\\f"); break; 287 case '\r' : 288 b.append("\\r"); break; 290 case '\"': 291 b.append("\\\""); break; 293 case '\'': 294 b.append("\\\'"); break; 296 case '\\': 297 b.append("\\\\"); break; 299 case '\0' : 300 b.append("\\0"); break; 302 case '\1' : 303 b.append("\\1"); break; 305 case '\2' : 306 b.append("\\2"); break; 308 case '\3' : 309 b.append("\\3"); break; 311 case '\4' : 312 b.append("\\4"); break; 314 case '\5' : 315 b.append("\\5"); break; 317 case '\6' : 318 b.append("\\6"); break; 320 case '\7' : 321 b.append("\\7"); break; 323 default: 324 b.append(c); 325 } 326 } 327 b.append("\""); setEscapedValue(b.toString()); 329 } 330 333 int memSize() { 334 int size = BASE_NODE_SIZE + 1 * 4 + stringSize(escapedValue); 335 return size; 336 } 337 338 341 int treeSize() { 342 return memSize(); 343 } 344 } 345 | Popular Tags |