Change visibility to private for net.sourceforge.phpeclipse.xdebug.php.model.XDebugEl...
[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 abstract void setType(String typeName);
150         public abstract void renderValueString(String data);
151
152         public abstract boolean verifyValue(String expression);
153         
154         public boolean setValue(String expression) {
155                 if (!verifyValue(expression))
156                         return false;
157         if( getDebugTarget() == null ) {
158             renderValueString(expression);
159             } else {
160                     if(((XDebugTarget) getDebugTarget()).setVarValue(fVariable.getFullName(),expression)) {
161                             renderValueString(expression);
162                             return true;
163                     }
164             }
165                 return false;
166         }
167         
168         public boolean setValueA(String expression) {
169         if(! fValueString.toString().equals(expression)) {
170                 fVariables= new IVariable[0];
171                 fValueString = expression;
172             fhasChanged = true;                 
173         } else {
174             fhasChanged = false;                        
175         }
176
177         return true;
178         }
179
180         public boolean setValueB(IValue value) {
181                 try {
182                         fTypeName = value.getReferenceTypeName();
183                 } catch (DebugException e) {
184                 }
185                 
186                 fhasChanged = false;
187                 
188                 switch (((XDebugAbstractValue) value). getType()) {
189                         case VALUETYPE_UNINITIALIZED:
190                         case VALUETYPE_STRING:
191                         case VALUETYPE_INT:
192                         case VALUETYPE_FLOAT:
193                         case VALUETYPE_BOOLEAN:
194                                 try {
195                                         if (! fValueString.equals(value.getValueString())) {
196                                                 fValueString = value.getValueString();
197                                                 fhasChanged = true;
198                                         }
199                                 } catch (DebugException e) {
200                                         int a = 10;
201                                         a++;
202                                 }
203                                 break;
204
205                         case VALUETYPE_RESOURCE:
206                                 try {
207                                         if (! fValueString.equals(value.getValueString())) {
208                                                 fValueString = value.getValueString();
209                                                 fhasChanged = true;
210                                         }
211                                 } catch (DebugException e) {
212                                         int a = 10;
213                                         a++;
214                                 }
215                                 break;
216
217                         case VALUETYPE_HASH:
218                                 int a = 20;
219                                 a = a +2;
220                                 break;
221                                 
222                         case VALUETYPE_OBJECT:
223                         case VALUETYPE_ARRAY:
224                                 try {
225                                         IVariable[] newVariable = value.getVariables();
226                                         
227                                         if (fVariables.length == 0) {
228                                                 if (newVariable.length > 0) {
229                                                         fValueString = value.getValueString();
230                                                         fVariables = newVariable;
231                                                         fhasChanged = true;
232                                                 }
233                                         } else {
234                                                 for(int i = 0; i < fVariables.length; i++) {
235                                                         // da capire quando e perche'
236                                                         try {
237                                                                 if (! ((XDebugVariable)fVariables[i]).equals(((XDebugVariable)newVariable[i]))) {
238                                                                         //fVariables[i].setValue(newVariable[i].getValue());
239                                                                         fhasChanged = true;
240                                                                 }
241                                                         } catch (Exception e) {
242                                                                 //int b = 1;
243                                                         }
244                                                 }
245                                                 if (fhasChanged) {
246                                                         fValueString = value.getValueString();
247                                                         for(int i = 0; i < fVariables.length; i++) {
248                                                                 try {
249                                                                         fVariables[i].setValue(newVariable[i].getValue());
250                                                                 } catch (Exception e) {
251                                                                         //int b = 1;
252                                                                 }
253                                                         }
254                                                 }
255                                         }
256                                 } catch (DebugException e) {
257                                         int b = 10;
258                                         b++;
259                                 }
260                                 
261                                 break;
262                 }
263
264         return true;
265         }
266         
267         public boolean setValueBOld(IValue value) {
268                 fhasChanged = false;
269
270                 switch (((XDebugAbstractValue) value). getType()) {
271                         case VALUETYPE_UNINITIALIZED:
272                         case VALUETYPE_STRING:
273                         case VALUETYPE_INT:
274                         case VALUETYPE_FLOAT:
275                         case VALUETYPE_BOOLEAN:
276                                 try {
277                                         if (! fValueString.equals(value.getValueString())) {
278                                                 fValueString = value.getValueString();
279                                                 fhasChanged = true;
280                                         }
281                                 } catch (DebugException e) {
282                                 }
283                                 break;
284
285                         case VALUETYPE_HASH:
286                                 int a = 20;
287                                 a = a +2;
288                                 break;
289                                 
290                         case VALUETYPE_OBJECT:
291                         case VALUETYPE_ARRAY:
292                                 try {
293                                         IVariable[] newVariable = value.getVariables();
294                                         
295                                         if (fVariables.length == 0) {
296                                                 if (newVariable.length > 0) {
297                                                         fValueString = value.getValueString();
298                                                         fVariables = newVariable;
299                                                         fhasChanged = true;
300                                                 }
301                                         } else {
302                                                 for(int i = 0; i < fVariables.length; i++) {
303                                                         if (! ((XDebugVariable)fVariables[i]).equals(((XDebugVariable)newVariable[i]))) {
304                                                                 //fVariables[i].setValue(newVariable[i].getValue());
305                                                                 fhasChanged = true;
306                                                         }
307                                                 }
308                                                 if (fhasChanged) {
309                                                         fValueString = value.getValueString();
310                                                         for(int i = 0; i < fVariables.length; i++) {
311                                                                 fVariables[i].setValue(newVariable[i].getValue());
312                                                         }
313                                                 }
314                                         }
315                                 } catch (DebugException e) {
316                                 }
317                                 
318                                 break;
319                 }
320
321         return true;
322         }
323
324         public int getType() {
325                 return fType;
326         }
327
328         public boolean supportsValueModification() {
329                 return false;
330         }
331 }