1 19 20 package org.netbeans.modules.scripting.php.dbginterface; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import org.netbeans.modules.scripting.php.dbginterface.models.Variable; 25 26 30 public class DbgEvalValues { 31 private List <Variable> vars; 32 private char[]buffer; 33 private int startIdx; 34 35 36 public DbgEvalValues(String str, String name) { 37 if (str == null || str.length() == 0) { 38 return; 39 } 40 41 buffer = str.toCharArray(); 42 startIdx = 0; 43 vars = new ArrayList <Variable>(); 44 List <Variable> varList = new ArrayList <Variable>(); 45 46 parseFull(name, null, vars, varList, true); 47 } 48 49 public List <Variable> getVars() { 50 return vars; 51 } 52 53 private boolean parseFull(String name, Variable parent, List <Variable> list, 54 List <Variable> varList, boolean makePhpString) { 55 if (startIdx >= buffer.length) { 56 return false; 57 } 58 59 char ch = buffer[startIdx++]; 60 boolean ret_value = false; 61 62 switch (ch) { 63 case 'N': 64 ret_value = ParseEvalNULL(parent, name, list, varList); 65 break; 66 case 'i': 67 ret_value = ParseEvalInt(parent, name, list, varList); 68 break; 69 case 'd': 70 ret_value = ParseEvalDouble(parent, name, list, varList); 71 break; 72 case 's': 73 ret_value = ParseEvalString(parent, name, list, varList, makePhpString); 74 break; 75 case 'a': 76 ret_value = ParseEvalArray(parent, name, list, varList, "", Variable.Type.ARRAY); 77 break; 78 case 'O': 79 ret_value = ParseEvalObject(parent, name, list, varList); 80 break; 81 case 'b': 82 ret_value = ParseEvalBool(parent, name, list, varList); 83 break; 84 case 'z': 85 ret_value = ParseEvalResource(parent, name, list, varList); 86 break; 87 case 'R': 88 ret_value = ParseEvalRef(parent, name, list, varList, false); 89 break; 90 case 'r': 91 ret_value = ParseEvalRef(parent, name, list, varList, true); 92 break; 93 } 94 95 if (!ret_value) { 96 int i = startIdx; 98 99 while (i < buffer.length && buffer[i] != '{' && buffer[i]!= ';') { 100 i++; 101 } 102 103 if (i < buffer.length && buffer[i] == '{') { 104 int cnt = 1; 105 106 i++; 107 while (i < buffer.length && cnt != 0) { 108 if (buffer[i] == '{') { 109 cnt++; 110 } 111 else if (buffer[i] == '}') { 112 cnt--; 113 } 114 115 i++; 116 } 117 } 118 119 startIdx = i; 120 } 121 122 return ret_value; 123 } 124 125 private boolean ParseEvalNULL(Variable parent, String name, List <Variable> list, 126 List <Variable> varList) { 127 int idx = startIdx; 128 129 if (idx >= buffer.length || buffer[idx] != ';') { 130 return false; 131 } 132 133 startIdx++; 134 135 Variable item = new Variable(parent, name, Variable.Type.UNDEFINED, "", "NULL"); 136 137 list.add(item); 138 139 if (varList != null) { 140 varList.add(item); 141 } 142 143 return true; 144 } 145 146 private boolean ParseEvalInt(Variable parent, String name, List <Variable> list, 147 List <Variable> varList) { 148 String subs; 149 150 if ((subs = ExtractSubStr(':', ';')) == null) { 151 return false; 152 } 153 154 Variable item = new Variable(parent, name, Variable.Type.LONG, "", subs); 155 156 list.add(item); 157 158 if (varList != null) { 159 varList.add(item); 160 } 161 162 return true; 163 } 164 165 private boolean ParseEvalDouble(Variable parent, String name, List <Variable> list, 166 List <Variable> varList) { 167 String subs; 168 169 if ((subs = ExtractSubStr(':', ';')) == null) { 170 return false; 171 } 172 173 Variable item = new Variable(parent, name, Variable.Type.DOUBLE, "", subs); 174 175 list.add(item); 176 177 if (varList != null) { 178 varList.add(item); 179 } 180 181 return true; 182 } 183 184 private boolean ParseEvalString(Variable parent, String name, List <Variable> list, 185 List <Variable> varList, boolean makePhpStr) { 186 Integer slenI = ExtractInt(':', ':'); 187 188 if (slenI == null) { 189 return false; 190 } 191 192 int slen = slenI.intValue(); 193 194 if (startIdx >= (buffer.length - 1) || buffer[startIdx] != '"') { 195 return false; 196 } 197 198 startIdx++; 199 200 String subs = new String (buffer, startIdx, slen); 201 202 startIdx += slen; 203 204 if (startIdx > (buffer.length - 2) || buffer[startIdx] != '"' || 205 buffer[startIdx + 1] != ';' || subs.length() != slen) { 206 return false; 207 } 208 209 startIdx += 2; 210 211 if (makePhpStr) { 212 subs = ConvertToPhpString(subs); 213 } 214 215 Variable item = new Variable(parent, name, Variable.Type.STRING, "", subs); 216 217 list.add(item); 218 219 if (varList != null) { 220 varList.add(item); 221 } 222 223 return true; 224 } 225 226 private boolean ParseEvalArray(Variable parent, String name, List <Variable> list, 227 List <Variable> varList, String classname, Variable.Type atype) { 228 Integer arritemsI = ExtractInt(':', ':'); 229 230 if (arritemsI == null) { 231 return false; 232 } 233 234 int arritems = arritemsI.intValue(); 235 236 if (startIdx >= (buffer.length - 1) || buffer[startIdx] != '{') { 237 return false; 238 } 239 240 startIdx++; 241 242 Variable item = new Variable(parent, name, atype, classname); 243 List <Variable> children = null; 244 245 list.add(item); 246 247 if (varList != null) { 248 varList.add(item); 249 } 250 251 if (arritems > 0) { 252 children = new ArrayList <Variable>(); 253 item.setChildren(children); 254 } 255 else if (buffer[startIdx] != '}') { 256 return false; 257 } 258 259 while (startIdx < buffer.length && buffer[startIdx] != '}') { 260 List <Variable> tmplst = new ArrayList <Variable>(); 261 if (!parseFull("", null, tmplst, null, false) || tmplst.size() != 1) { 263 return false; 264 } 265 266 if (!parseFull(tmplst.get(0).getValue(), item, children, varList, true)) { 268 return false; 269 } 270 } 271 272 startIdx++; 273 274 return true; 275 } 276 277 private boolean ParseEvalObject(Variable parent, String name, List <Variable> list, 278 List <Variable> varList) { 279 Integer slenI = ExtractInt(':', ':'); 280 281 if (slenI == null) { 282 return false; 283 } 284 285 int slen = slenI.intValue(); 286 287 if (startIdx >= (buffer.length - 1)) { 288 return false; 289 } 290 291 String classname; 292 293 if ((classname = ExtractQuotedSubStr(slen)) == null) { 294 return false; 295 } 296 297 if (classname.length() != slen) { 298 return false; 299 } 300 301 return ParseEvalArray(parent, name, list, varList, classname, Variable.Type.OBJECT); 302 } 303 304 private boolean ParseEvalBool(Variable parent, String name, List <Variable> list, 305 List <Variable> varList) { 306 Integer bI = ExtractInt(':', ';'); 307 308 if (bI == null) { 309 return false; 310 } 311 312 Variable item = new Variable(parent, name, Variable.Type.BOOLEAN, "", 313 (bI.intValue() == 0 ? "FALSE" : "TRUE")); 314 315 list.add(item); 316 317 if (varList != null) { 318 varList.add(item); 319 } 320 321 return true; 322 } 323 324 private boolean ParseEvalResource(Variable parent, String name, List <Variable> list, 325 List <Variable> varList) { 326 Integer slenI = ExtractInt(':', ':'); 327 328 if (slenI == null) { 329 return false; 330 } 331 332 int slen = slenI.intValue(); 333 String restype; 334 335 if ((restype = ExtractQuotedSubStr(slen)) == null) { 336 return false; 337 } 338 339 if (restype.length() != slen) { 340 return false; 341 } 342 343 Integer valI = ExtractInt(':', ';'); 344 345 if (valI == null) { 346 return false; 347 } 348 349 Variable item = new Variable(parent, name, Variable.Type.RESOURCE, restype, valI.toString()); 350 351 list.add(item); 352 353 if (varList != null) { 354 varList.add(item); 355 } 356 357 return true; 358 } 359 360 private boolean ParseEvalRef(Variable parent, String name, List <Variable> list, 361 List <Variable> varList, boolean isSoftRef) { 362 Integer valI = ExtractInt(':', ';'); 363 364 if (valI == null) { 365 return false; 366 } 367 368 int val = valI.intValue(); 369 370 Variable item = new Variable(parent, name, 371 (isSoftRef ? Variable.Type.SOFT_REFERENCE : Variable.Type.REFERENCE), ""); 372 373 list.add(item); 374 375 val--; 377 if (varList == null || val < 0 || val >= varList.size()) { 378 } 381 else { 382 item.setRef(varList.get(val)); 385 } 386 387 return true; 388 389 } 390 391 private String ExtractSubStr(char chstart, char chend) { 392 int idx = startIdx; 393 394 if (idx >= (buffer.length - 1) || buffer[idx] != chstart) { 395 return null; 396 } 397 398 int i = ++idx; 399 400 while (i < buffer.length && buffer[i] != chend) { 401 i++; 402 } 403 404 if (i == buffer.length) { 405 return null; 406 } 407 408 String ret = new String (buffer, idx, i - idx); 409 410 startIdx = i + 1; 411 return ret; 412 } 413 414 String ExtractQuotedSubStr(int slen) { 415 int idx = startIdx; 416 417 if (idx + slen + 1 >= buffer.length || buffer[idx] != '"' || 418 buffer[idx + slen + 1] != '"') { 419 return null; 420 } 421 422 String rslt = new String (buffer, idx + 1, slen); 423 424 startIdx += slen + 2; 425 426 return rslt; 427 } 428 429 Integer ExtractInt(char chstart, char chend) { 430 String subs; 431 432 if ((subs = ExtractSubStr(chstart, chend)) == null) { 433 return null; 434 } 435 436 Integer ret = null; 437 438 try { 439 ret = Integer.decode(subs); 440 } 441 catch (NumberFormatException nfe) { 442 } 444 445 return ret; 446 } 447 448 private String ConvertToPhpString(String s) { 449 char[] sIn = s.toCharArray(); 450 String rslt = "\""; 451 452 for (int i = 0; i < sIn.length; i++) { 453 char ch =sIn[i]; 454 455 switch (ch) { 456 case '"': 457 case '\\': 458 case '$': 459 rslt += '\\' + ch; 460 break; 461 case '\r': 462 rslt += '\\' + 'r'; 463 break; 464 case '\n': 465 rslt += '\\' + 'n'; 466 break; 467 case '\t': 468 rslt += '\\' + 't'; 469 break; 470 default: 471 if (ch >= 0 && ch < 32) { 472 rslt += "\\x" + Integer.toHexString(ch); 473 } 474 else { 475 rslt += ch; 476 } 477 } 478 } 479 480 rslt += "\""; 481 482 return rslt; 483 } 484 } | Popular Tags |