KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > dbginterface > DbgEvalValues


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.scripting.php.dbginterface;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.modules.scripting.php.dbginterface.models.Variable;
25
26 /**
27  *
28  * @author marcow
29  */

30 public class DbgEvalValues {
31     private List JavaDoc<Variable> vars;
32     private char[]buffer;
33     private int startIdx;
34     
35     /** Creates a new instance of DbgEvalValues */
36     public DbgEvalValues(String JavaDoc str, String JavaDoc name) {
37         if (str == null || str.length() == 0) {
38             return;
39         }
40         
41         buffer = str.toCharArray();
42         startIdx = 0;
43         vars = new ArrayList JavaDoc<Variable>();
44         List JavaDoc<Variable> varList = new ArrayList JavaDoc<Variable>();
45         
46         parseFull(name, null, vars, varList, true);
47     }
48     
49     public List JavaDoc<Variable> getVars() {
50         return vars;
51     }
52     
53     private boolean parseFull(String JavaDoc name, Variable parent, List JavaDoc<Variable> list,
54             List JavaDoc<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             // try to recover
97
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 JavaDoc name, List JavaDoc<Variable> list,
126             List JavaDoc<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 JavaDoc name, List JavaDoc<Variable> list,
147             List JavaDoc<Variable> varList) {
148         String JavaDoc 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 JavaDoc name, List JavaDoc<Variable> list,
166             List JavaDoc<Variable> varList) {
167         String JavaDoc 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 JavaDoc name, List JavaDoc<Variable> list,
185             List JavaDoc<Variable> varList, boolean makePhpStr) {
186         Integer JavaDoc 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 JavaDoc subs = new String JavaDoc(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 JavaDoc name, List JavaDoc<Variable> list,
227             List JavaDoc<Variable> varList, String JavaDoc classname, Variable.Type atype) {
228         Integer JavaDoc 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 JavaDoc<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 JavaDoc<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 JavaDoc<Variable> tmplst = new ArrayList JavaDoc<Variable>();
261                 // name
262
if (!parseFull("", null, tmplst, null, false) || tmplst.size() != 1) {
263                 return false;
264             }
265             
266                 // value
267
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 JavaDoc name, List JavaDoc<Variable> list,
278             List JavaDoc<Variable> varList) {
279         Integer JavaDoc 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 JavaDoc 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 JavaDoc name, List JavaDoc<Variable> list,
305             List JavaDoc<Variable> varList) {
306         Integer JavaDoc 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 JavaDoc name, List JavaDoc<Variable> list,
325             List JavaDoc<Variable> varList) {
326         Integer JavaDoc slenI = ExtractInt(':', ':');
327         
328         if (slenI == null) {
329             return false;
330         }
331         
332         int slen = slenI.intValue();
333         String JavaDoc 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 JavaDoc 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 JavaDoc name, List JavaDoc<Variable> list,
361             List JavaDoc<Variable> varList, boolean isSoftRef) {
362         Integer JavaDoc 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--; // ref ID is 1-based, EvalList is 0-based
376

377         if (varList == null || val < 0 || val >= varList.size()) {
378             // Keep the ref to null
379
// item.setRef(item); // self-resolving
380
}
381         else {
382             // XXX I don't know what that's supposed to do right now.
383
// So I store the info until I know more.
384
item.setRef(varList.get(val));
385         }
386         
387         return true;
388         
389     }
390     
391     private String JavaDoc 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 JavaDoc ret = new String JavaDoc(buffer, idx, i - idx);
409
410         startIdx = i + 1;
411         return ret;
412     }
413
414     String JavaDoc 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 JavaDoc rslt = new String JavaDoc(buffer, idx + 1, slen);
423         
424         startIdx += slen + 2;
425         
426         return rslt;
427     }
428
429     Integer JavaDoc ExtractInt(char chstart, char chend) {
430         String JavaDoc subs;
431         
432         if ((subs = ExtractSubStr(chstart, chend)) == null) {
433             return null;
434         }
435         
436         Integer JavaDoc ret = null;
437         
438         try {
439             ret = Integer.decode(subs);
440         }
441         catch (NumberFormatException JavaDoc nfe) {
442             // the ret == null is handled outside
443
}
444
445         return ret;
446 }
447
448     private String JavaDoc ConvertToPhpString(String JavaDoc s) {
449         char[] sIn = s.toCharArray();
450         String JavaDoc 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