1 package net.sourceforge.phpdt.internal.debug.core.model;
4 * Created on 17.04.2004
6 * To change the template for this generated file go to
7 * Window - Preferences - Java - Code Generation - Code and Comments
12 * To change the template for this generated type comment go to
13 * Window - Preferences - Java - Code Generation - Code and Comments
16 import java.util.Vector;
18 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.debug.core.DebugException;
26 public class PHPDBGEvalString {
29 private PHPStackFrame fStackFrame;
34 public PHPDBGEvalString(PHPStackFrame stack, String dataStr) {
46 String ExtractSubStr (char chstart, char chend, int startIdx) throws DebugException {
54 if (idx >= (workStr.length () - 1) ||
55 workStr.charAt (idx) != chstart) {
56 status = new Status (Status.ERROR, PHPDebugCorePlugin.getUniqueIdentifier (), Status.OK, "worng startIdx!", null);
58 throw new DebugException (status);
62 i = workStr.indexOf(chend, i);
65 status = new Status (Status.ERROR, PHPDebugCorePlugin.getUniqueIdentifier (), Status.OK, "endchar not found!", null);
67 throw new DebugException (status);
69 rslt = workStr.substring (idx, i);
70 workStr = workStr.substring (i + 1);
80 String ExtractQuotedSubStr (int slen, int startIdx) throws DebugException {
87 if ((idx + slen + 1) >= workStr.length () ||
88 workStr.charAt (idx)!= '"' ||
89 workStr.charAt (idx + slen + 1) != '"') {
90 status = new Status (Status.ERROR, PHPDebugCorePlugin.getUniqueIdentifier (), Status.OK, "no quoted substring found!", null);
92 throw new DebugException (status);
95 rslt = workStr.substring (idx + 1, idx + 1 + slen);
96 workStr = workStr.substring (idx + 2 + slen);
108 int ExtractInt (char chstart, char chend, int startIdx) throws DebugException {
111 subs = ExtractSubStr (chstart, chend, startIdx);
113 return (Integer.parseInt (subs));
119 * @param list The list of PHPVariables
122 * @param atype The type of the variable (Either PEVT_ARRAY or PEVT_OBJECT)
125 PHPVariable ParseEvalArray (String name, PHPVariable parent, Vector list, Vector var_list, String classname, int atype) throws DebugException {
126 long arritems; // The number of items (or fields or entries) for the array (or short, array size)
128 Vector subitems = null;
131 arritems = ExtractInt (':', ':', 0); // Get the number of items/fields for the array
132 // E.g. :12: means there are 12 entries in array
134 if ((workStr.length () > 0) && // Is there still something to parse?
135 (workStr.charAt (0) != '{')) { // And the next character is not a '{', then output an error
136 status = new Status (Status.ERROR, PHPDebugCorePlugin.getUniqueIdentifier (), Status.OK, "no array startcharacter!", null);
138 throw new DebugException (status);
141 workStr = workStr.substring (1); // Remove the '{'
142 item = new PHPVariable (fStackFrame, name, parent, classname, atype, null); // Create a new (empty) PHPVariable
144 list.add (item); // Add the newly created PHPVariable to list
146 if (var_list != null) { //
147 var_list.add (item); // Add the PHPVariable also to the var_list
150 if (arritems > 0) { // If the array is not empty
151 subitems = new Vector (); // Create a new child variable list for the array
152 } else if (workStr.charAt (0) != '}') { // If the array is empty the next character has to be '}'
153 status = new Status (Status.ERROR, PHPDebugCorePlugin.getUniqueIdentifier (), Status.OK, "no array endcharacter!", null);
155 throw new DebugException (status);
158 while ((workStr.length () > 0) && // Is there still something to parse?
159 (workStr.charAt (0) != '}')) { // And the next character is not '}'
160 Vector tmplst = new Vector (); // Create a temporary list
162 parse ("", null, tmplst, null, false, 0); // And parse the string for the array's name.
164 if (tmplst.size () != 1) { // Parsing should return exactly on entry (which is the name)
165 status = new Status (Status.ERROR, PHPDebugCorePlugin.getUniqueIdentifier (), Status.OK, "no name found!", null);
167 throw new DebugException (status);
169 // Go for the array values
170 parse (((PHPVariable) tmplst.elementAt (0)).getValue ().getValueString (), item, subitems, var_list, true, 0);
173 ((PHPValue) item.getValue ()).addVariable (subitems); // Append the list of all child variables to this PHPVariables PHPValue
174 workStr = workStr.substring (1); // Remove the '}'
176 return item; // And return the PHPVariable we just build
187 void ParseEvalNULL (String name, PHPVariable parent, Vector list, Vector var_list, int startIdx) throws DebugException {
194 if ((idx >= workStr.length ()) ||
195 (workStr.charAt (idx) != ';')) {
196 status = new Status (Status.ERROR, PHPDebugCorePlugin.getUniqueIdentifier (), Status.OK, "NULL not found!", null);
198 throw new DebugException(status);
201 workStr = workStr.substring (1);
202 item = new PHPVariable (fStackFrame, name, parent, "NULL", PHPValue.PEVT_UNKNOWN, null);
206 if (var_list != null) {
219 boolean ParseEvalInt (String name, PHPVariable parent, Vector list, Vector var_list, int startIdx) throws DebugException {
223 subs = ExtractSubStr (':', ';', startIdx);
224 item = new PHPVariable (fStackFrame, name, parent, subs, PHPValue.PEVT_LONG, null);
228 if (var_list != null) {
243 boolean ParseEvalDouble (String name, PHPVariable parent, Vector list, Vector var_list, int startIdx) throws DebugException {
247 subs = ExtractSubStr (':', ';', startIdx);
248 item = new PHPVariable (fStackFrame, name, parent, subs, PHPValue.PEVT_DOUBLE, null);
252 if (var_list != null) {
268 boolean ParseEvalString (String name, PHPVariable parent, Vector list, Vector var_list, boolean MakePhpStr, int startIdx)
269 throws DebugException {
275 slen = ExtractInt( ':', ':',startIdx);
277 if ((workStr.length () <= slen) ||
278 (workStr.charAt (0) != '"')) {
279 status = new Status (Status.ERROR, PHPDebugCorePlugin.getUniqueIdentifier (), Status.OK, "no String startcharecter!", null);
281 throw new DebugException (status);
284 workStr = workStr.substring (1);
285 subs = workStr.substring (0, slen);
288 subs = subs.replaceAll ("\\\\\\\\","\\\\");
290 if (workStr.charAt (slen) != '"') {
291 status = new Status (Status.ERROR, PHPDebugCorePlugin.getUniqueIdentifier (),Status.OK, "no String endcharecter!", null);
292 throw new DebugException (status);
295 workStr = workStr.substring (slen + 2);
298 ConvertToPhpString(subs, &subs);
301 item = new PHPVariable (fStackFrame, name, parent, subs, PHPValue.PEVT_STRING, null);
305 if (var_list != null) {
320 boolean ParseEvalBool (String name, PHPVariable parent, Vector list, Vector var_list, int startIdx) throws DebugException {
324 v = ExtractInt (':', ';', startIdx);
325 item = new PHPVariable (fStackFrame, name, parent, (v==0) ? ("FALSE") : ("TRUE"), PHPValue.PEVT_BOOLEAN, null);
329 if (var_list != null) {
344 boolean ParseEvalObject (String name, PHPVariable parent, Vector list, Vector var_list, int startIdx) throws DebugException {
348 slen = ExtractInt (':', ':', startIdx);
349 classname = ExtractQuotedSubStr (slen, startIdx);
351 if ((int) classname.length () != slen) {
355 ParseEvalArray (name,parent, list, var_list, classname,PHPValue.PEVT_OBJECT);
368 boolean ParseEvalResource (String name, PHPVariable parent, Vector list, Vector var_list, int startIdx) throws DebugException {
374 slen = ExtractInt (':', ':', startIdx);
375 restype = ExtractQuotedSubStr (slen, startIdx);
376 val = ExtractSubStr (':', ';', startIdx);
378 item = new PHPVariable (fStackFrame, name, parent, restype + ":" + val, PHPValue.PEVT_RESOURCE, null);
382 if (var_list != null) {
398 private boolean ParseEvalRef(String name, PHPVariable parent, Vector list,
399 Vector var_list, boolean isSoftRef, int startIdx)
400 throws DebugException {
403 PHPVariable var_item;
405 v = ExtractInt(':', ';', startIdx);
406 item = new PHPVariable(fStackFrame, name, parent, "",
407 isSoftRef ? PHPValue.PEVT_SOFTREF : PHPValue.PEVT_REF, null);
408 v--; // ref ID is 1-based, EvalList is 0-based
410 if ((var_list == null) || (v < 0) || (v >= var_list.size())) {
411 //item.ref = item; // self-resolving
414 var_item = (PHPVariable) var_list.get(v);
416 PHPValue new_val = (PHPValue) var_item.getValue();
418 // expand reduced structure to full tree
419 // each value must have its appropriate parent
421 new_val = copyItems(new_val);
422 } catch (CloneNotSupportedException e) {
428 //item.setValue(var_item.getValue());
429 //item.setReferenceType(var_item.getReferenceType());
430 //((PHPValue) item.getValue()).setParent(item);
431 item.setValue(new_val);
432 item.setReferenceType(var_item.getReferenceType());
433 new_val.setParent(item);
434 } catch (DebugException e) {
446 * @return The array of PHPVariables
448 public PHPVariable[] getVars() {
449 Vector list = new Vector();
450 Vector var_list = new Vector();
452 parse("", null, list, var_list, false, 0);
454 return (PHPVariable[]) list.toArray(new PHPVariable[list.size()]); // Convert the list to an array and return the array
459 * @return The PHPVariables as list
461 public Vector getVariables() {
462 Vector list = new Vector();
463 Vector var_list = new Vector();
465 parse("", null, list, var_list, false, 0);
467 //debugDump(list, "");
468 return list; // return the PHPVariable list
475 * @param name The name of the PHPVariable
476 * @param parent The PHPVariable to which this parsing belongs
482 boolean parse (String name, PHPVariable parent, Vector list, Vector var_list, boolean MakePhpStr, int startIdx) {
483 boolean ret_val = false;
486 if (startIdx >= workStr.length ()) { // Is there something to parse
487 return false; // No, then leave here
490 ch = workStr.charAt (startIdx); // The first character denotes the type of variable
491 workStr = workStr.substring (1); // Remove the 'variable type' character
494 switch (ch) { // Switch according the 'variable type'
495 case 'N': ParseEvalNULL (name, parent, list, var_list, startIdx); break;
496 case 'i': ParseEvalInt (name, parent, list, var_list, startIdx); break;
497 case 'd': ParseEvalDouble (name, parent, list, var_list, startIdx); break;
498 case 's': ParseEvalString (name, parent, list, var_list, MakePhpStr, startIdx); break;
499 case 'a': ParseEvalArray (name, parent, list, var_list, "", PHPValue.PEVT_ARRAY); break;
500 case 'O': ParseEvalObject (name, parent, list, var_list, startIdx); break;
501 case 'b': ParseEvalBool (name, parent, list, var_list, startIdx); break;
502 case 'z': ParseEvalResource (name, parent, list, var_list, startIdx); break;
503 case 'R': ParseEvalRef (name, parent, list, var_list, false, startIdx); break;
504 case 'r': ParseEvalRef (name, parent, list, var_list, true, startIdx); break;
505 case '?': ParseEvalUnknown(name, parent, list, var_list, startIdx); break;
507 } catch (DebugException e) {
508 PHPDebugCorePlugin.log(e);
511 /* if (!ret_val) { // try to recover
512 unsigned int i=*startIdx;
513 while (i<str.length() && str[i]!='{' && str[i]!=';') i++;
514 if (i<str.length() && str[i] == '{') {
517 while (i<str.length() && cnt!=0) {
520 else if (str[i] == '}')
528 return ret_val; // Always false
534 private void ParseEvalUnknown(String name, PHPVariable parent, Vector list,
535 Vector var_list, int startIdx) throws DebugException {
537 if ((startIdx >= workStr.length()) || (workStr.charAt(startIdx) != ';')) {
538 Status status = new Status(Status.ERROR, PHPDebugCorePlugin
539 .getUniqueIdentifier(), Status.OK, "unexpected response",
541 throw new DebugException(status);
544 workStr = workStr.substring(1);
545 PHPVariable item = new PHPVariable(fStackFrame, name, parent, "?",
546 PHPValue.PEVT_UNKNOWN, null);
548 if (var_list != null) {
554 * Copy referenced items tree
556 private PHPValue copyItems(PHPValue val) throws CloneNotSupportedException {
557 PHPValue newVal = (PHPValue) val.clone();
558 Vector vars = newVal.getChildVariables();
559 Vector newVars = new Vector();
560 for (int i = 0; i < vars.size(); i++) {
561 PHPVariable newVar = (PHPVariable) ((PHPVariable) vars.get(i)).clone();
563 newVar.setValue(copyItems((PHPValue) newVar.getValue()));
564 } catch (DebugException e) {
569 val.setVariables(newVars);
573 // private void debugDump(Vector list, String indent) {
574 // for (int i = 0; i < list.size(); i++) {
575 // PHPVariable var = (PHPVariable) list.get(i);
576 // System.out.print(indent + var.getName());
577 // PHPValue val = (PHPValue) var.getValue();
579 // if (val.hasVariables() && !var.getName().equals("['GLOBALS']")) {
580 // System.out.println();
581 // debugDump(val.getChildVariables(), indent + " ");
583 // PHPVariable parent = var.getParent();
584 // System.out.println(val.getValueString() + " \t>>" + (parent == null ? "null" : parent.getLongName()));
586 // } catch (DebugException e) {
587 // e.printStackTrace();