Importing the XDebugProxy code in the HEAD. The repo was tagged with T_BEFORE_XDEBUGP...
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / model / XDebugAbstractValue.java
1 /*
2  * Created on 23.11.2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package net.sourceforge.phpeclipse.xdebug.php.model;
8
9 import net.sourceforge.phpeclipse.xdebug.core.Base64;
10 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
11
12 import org.eclipse.debug.core.DebugException;
13 import org.eclipse.debug.core.model.IValue;
14 import org.eclipse.debug.core.model.IVariable;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17
18 /**
19  * @author Axel
20  *
21  * TODO To change the template for this generated type comment go to
22  * Window - Preferences - Java - Code Style - Code Templates
23  */
24 public abstract class XDebugAbstractValue  extends XDebugElement implements IValue {
25         
26         public static final int VALUETYPE_UNKNOWN = -1;
27         public static final int VALUETYPE_UNINITIALIZED = 0;
28         public static final int VALUETYPE_STRING = 1;
29         public static final int VALUETYPE_INT = 4;
30         public static final int VALUETYPE_FLOAT = 5;
31         public static final int VALUETYPE_BOOLEAN = 6;
32         public static final int VALUETYPE_ARRAY = 8;
33         public static final int VALUETYPE_HASH = 9;
34         public static final int VALUETYPE_OBJECT = 10;
35         public static final int VALUETYPE_RESOURCE = 11;
36
37         
38         protected XDebugVariable fVariable;
39         private IVariable[] fVariables;
40         protected String fValueString;
41         protected int fType;
42         protected String fTypeName;
43         
44         private boolean fhasChanged;
45
46         public XDebugAbstractValue(XDebugVariable  variable, String typeName) {
47                 super((XDebugTarget) variable.getDebugTarget());
48                 fVariable = variable;
49                 setType(typeName);
50                 fVariables= new IVariable[0];
51         }
52
53         
54         public XDebugAbstractValue(XDebugVariable  variable,Node varNode,String typeName) {
55                 super((XDebugTarget) variable.getDebugTarget());
56                 fVariable = variable;
57                 if (varNode==null){
58                         try {
59                                 System.out.println(variable.getName()+"=null");
60                         } catch (DebugException e) {
61                                 // TODO Auto-generated catch block
62                                 e.printStackTrace();
63                         }
64                         return; 
65                 }
66                 setType(typeName);
67                 NodeList property = varNode.getChildNodes();
68                 if (variable.hasChildren()) {
69                         renderValueString(""+property.getLength());
70                         fVariables = new IVariable[property.getLength()];
71                         for (int i = 0; i<property.getLength(); i++) {
72                                 Node propertyNode = property.item(i);
73                                 fVariables[i] = new XDebugVariable(variable.getStackFrame(), propertyNode);
74                         }
75                 }else {
76 //                      fDataString="";
77                         fVariables = new IVariable[0];
78 //                      if (variable.getType()== XDebugVariable.VARTYPE_UNINITIALIZED)
79 //                              fValueString="uninitialized";
80 //                      else {
81                         String str="";
82                         try {
83                                 str=varNode.getFirstChild().getNodeValue();
84                         } catch (NullPointerException e) {
85                                 str="";
86                         }
87                         if (variable.getEncoding().equals("base64")) {
88                                 if (str.length()!=0)
89                                         str=new String(Base64.decode(str));
90                                 else
91                                         str="";
92                         }
93                         renderValueString(str);
94 //                      }
95                 }
96                 String className=PHPDebugUtils.getAttributeValue(varNode,"classname");
97                 if(!"".equals(className))
98                         renderValueString(className);
99
100         }
101         
102         public boolean hasChanged() {
103                 return fhasChanged;
104         }
105         
106         public void sethasChanged(boolean hasChanged) {
107                 fhasChanged = hasChanged;
108         }
109         
110         public void setChildVariables(IVariable[] newChildVariables) {
111                 fVariables = newChildVariables;
112         }
113         
114         /* (non-Javadoc)
115          * @see org.eclipse.debug.core.model.IValue#getReferenceTypeName()
116          */
117         public String getReferenceTypeName() throws DebugException {
118                 return fTypeName;
119         }
120         
121         /* (non-Javadoc)
122          * @see org.eclipse.debug.core.model.IValue#getValueString()
123          */
124         public String getValueString() throws DebugException {
125                 return fValueString;
126         }
127         
128         /* (non-Javadoc)
129          * @see org.eclipse.debug.core.model.IValue#isAllocated()
130          */
131         public boolean isAllocated() throws DebugException {
132                 return true;
133         }
134         
135         /* (non-Javadoc)
136          * @see org.eclipse.debug.core.model.IValue#getVariables()
137          */
138         public IVariable[] getVariables() throws DebugException {
139                 return fVariables;
140         }
141         
142         /* (non-Javadoc)
143          * @see org.eclipse.debug.core.model.IValue#hasVariables()
144          */
145         public boolean hasVariables() throws DebugException {
146                 return (fVariables.length > 0);
147         }
148         
149         public boolean isArray() {
150                 return ((fType & VALUETYPE_ARRAY) > 0);
151         }
152         
153         public abstract void setType(String typeName);
154         public abstract void renderValueString(String data);
155
156         public abstract boolean verifyValue(String expression);
157         
158         public boolean setValue(String expression) {
159                 if (!verifyValue(expression))
160                         return false;
161         if( fTarget == null ) {
162             renderValueString(expression);
163             } else {
164                     if(fTarget.setVarValue(fVariable.getFullName(),expression)) {
165                             renderValueString(expression);
166                             return true;
167                     }
168             }
169                 return false;
170         }
171         
172         public boolean setValueA(String expression) {
173         if(! fValueString.toString().equals(expression)) {
174                 fVariables= new IVariable[0];
175                 fValueString = expression;
176             fhasChanged = true;                 
177         } else {
178             fhasChanged = false;                        
179         }
180
181         return true;
182         }
183
184         public boolean setValueB(IValue value) {
185                 try {
186                         fTypeName = value.getReferenceTypeName();
187                 } catch (DebugException e) {
188                 }
189                 
190                 fhasChanged = false;
191                 
192                 switch (((XDebugAbstractValue) value). getType()) {
193                         case VALUETYPE_UNINITIALIZED:
194                         case VALUETYPE_STRING:
195                         case VALUETYPE_INT:
196                         case VALUETYPE_FLOAT:
197                         case VALUETYPE_BOOLEAN:
198                                 try {
199                                         if (! fValueString.equals(value.getValueString())) {
200                                                 fValueString = value.getValueString();
201                                                 fhasChanged = true;
202                                         }
203                                 } catch (DebugException e) {
204                                         int a = 10;
205                                         a++;
206                                 }
207                                 break;
208
209                         case VALUETYPE_RESOURCE:
210                                 try {
211                                         if (! fValueString.equals(value.getValueString())) {
212                                                 fValueString = value.getValueString();
213                                                 fhasChanged = true;
214                                         }
215                                 } catch (DebugException e) {
216                                         int a = 10;
217                                         a++;
218                                 }
219                                 break;
220
221                         case VALUETYPE_HASH:
222                                 int a = 20;
223                                 a = a +2;
224                                 break;
225                                 
226                         case VALUETYPE_OBJECT:
227                         case VALUETYPE_ARRAY:
228                                 try {
229                                         IVariable[] newVariable = value.getVariables();
230                                         
231                                         if (fVariables.length == 0) {
232                                                 if (newVariable.length > 0) {
233                                                         fValueString = value.getValueString();
234                                                         fVariables = newVariable;
235                                                         fhasChanged = true;
236                                                 }
237                                         } else {
238                                                 for(int i = 0; i < fVariables.length; i++) {
239                                                         // da capire quando e perche'
240                                                         try {
241                                                                 if (! ((XDebugVariable)fVariables[i]).equals(((XDebugVariable)newVariable[i]))) {
242                                                                         //fVariables[i].setValue(newVariable[i].getValue());
243                                                                         fhasChanged = true;
244                                                                 }
245                                                         } catch (Exception e) {
246                                                                 int b = 1;
247                                                         }
248                                                 }
249                                                 if (fhasChanged) {
250                                                         fValueString = value.getValueString();
251                                                         for(int i = 0; i < fVariables.length; i++) {
252                                                                 try {
253                                                                         fVariables[i].setValue(newVariable[i].getValue());
254                                                                 } catch (Exception e) {
255                                                                         int b = 1;
256                                                                 }
257                                                         }
258                                                 }
259                                         }
260                                 } catch (DebugException e) {
261                                         int b = 10;
262                                         b++;
263                                 }
264                                 
265                                 break;
266                 }
267
268         return true;
269         }
270         
271         public boolean setValueBOld(IValue value) {
272                 fhasChanged = false;
273
274                 switch (((XDebugAbstractValue) value). getType()) {
275                         case VALUETYPE_UNINITIALIZED:
276                         case VALUETYPE_STRING:
277                         case VALUETYPE_INT:
278                         case VALUETYPE_FLOAT:
279                         case VALUETYPE_BOOLEAN:
280                                 try {
281                                         if (! fValueString.equals(value.getValueString())) {
282                                                 fValueString = value.getValueString();
283                                                 fhasChanged = true;
284                                         }
285                                 } catch (DebugException e) {
286                                 }
287                                 break;
288
289                         case VALUETYPE_HASH:
290                                 int a = 20;
291                                 a = a +2;
292                                 break;
293                                 
294                         case VALUETYPE_OBJECT:
295                         case VALUETYPE_ARRAY:
296                                 try {
297                                         IVariable[] newVariable = value.getVariables();
298                                         
299                                         if (fVariables.length == 0) {
300                                                 if (newVariable.length > 0) {
301                                                         fValueString = value.getValueString();
302                                                         fVariables = newVariable;
303                                                         fhasChanged = true;
304                                                 }
305                                         } else {
306                                                 for(int i = 0; i < fVariables.length; i++) {
307                                                         if (! ((XDebugVariable)fVariables[i]).equals(((XDebugVariable)newVariable[i]))) {
308                                                                 //fVariables[i].setValue(newVariable[i].getValue());
309                                                                 fhasChanged = true;
310                                                         }
311                                                 }
312                                                 if (fhasChanged) {
313                                                         fValueString = value.getValueString();
314                                                         for(int i = 0; i < fVariables.length; i++) {
315                                                                 fVariables[i].setValue(newVariable[i].getValue());
316                                                         }
317                                                 }
318                                         }
319                                 } catch (DebugException e) {
320                                 }
321                                 
322                                 break;
323                 }
324
325         return true;
326         }
327
328         public int getType() {
329                 return fType;
330         }
331
332         public boolean supportsValueModification() {
333                 return false;
334         }
335 }