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 / XDebugVariable.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.PHPDebugUtils;
10 import org.eclipse.debug.core.DebugException;
11 import org.eclipse.debug.core.DebugEvent;
12 import org.eclipse.debug.core.model.IValue;
13 import org.eclipse.debug.core.model.IVariable;
14 import org.w3c.dom.Node;
15
16 /**
17  * @author Axel
18  *
19  * TODO To change the template for this generated type comment go to
20  * Window - Preferences - Java - Code Style - Code Templates
21  */
22 public class XDebugVariable  extends XDebugElement implements IVariable {
23         public static final int VARTYPE_UNKNOWN = -1;
24         public static final int VARTYPE_UNINITIALIZED = 0;
25         public static final int VARTYPE_STRING = 1;
26         public static final int VARTYPE_INT = 2;
27         public static final int VARTYPE_FLOAT = 3;
28         public static final int VARTYPE_ARRAY = 8;
29         public static final int VARTYPE_HASH = 9;
30         public static final int VARTYPE_OBJECT = 10;
31         public static final int VARTYPE_RESOURCE = 11;
32         
33         // name & stack frmae
34         private String fName;
35         private XDebugStackFrame fFrame;
36         private String fFullName;
37         private XDebugAbstractValue fValue;
38         private String fEncoding;
39         private int fNumChildren;
40         
41         /**
42          * Constructs a variable contained in the given stack frame
43          * with the given name.
44          * 
45          * @param frame owning stack frame
46          * @param name variable name
47          */
48         public XDebugVariable( String typeName, String fullname ) {
49                 fFullName = fullname;
50                 fName = fullname;
51                 if (typeName.equals("int") ) 
52                         fValue= new XDebugIntValue(this,typeName);
53                 else if (typeName.equals("float") ) 
54                         fValue= new XDebugFloatValue(this,typeName);
55                 else if (typeName.equals("bool") ) 
56                         fValue= new XDebugBooleanValue(this,typeName);
57                 else if (typeName.equals("string") )
58                         fValue= new XDebugStringValue(this,typeName);
59                 else if (typeName.equals("array") )
60                         fValue= new XDebugArrayValue(this,typeName);
61                 else if (typeName.equals("hash") )
62                         fValue= new XDebugArrayValue(this,typeName);
63                 else if (typeName.equals("object") )
64                         fValue= new XDebugArrayValue(this,typeName);
65                 else if (typeName.equals("resource") )
66                         fValue= new XDebugResourceValue(this,typeName);
67                 else
68                         fValue= new XDebugValue(this,typeName);
69         }
70         
71         public XDebugVariable(XDebugStackFrame frame, Node property) {
72                 super((XDebugTarget) frame.getDebugTarget());
73                 fFrame = frame;
74                 init(property);
75         }
76         
77         public XDebugVariable(XDebugStackFrame frame, String fullname, String name, String typeName) {
78                 super((XDebugTarget) frame.getDebugTarget());
79                 fFrame = frame;
80                 fFullName = fullname;
81                 fName = name;
82                 if (typeName.equals("int") ) 
83                         fValue= new XDebugIntValue(this,typeName);
84                 else if (typeName.equals("float") ) 
85                         fValue= new XDebugFloatValue(this,typeName);
86                 else if (typeName.equals("bool") ) 
87                         fValue= new XDebugBooleanValue(this,typeName);
88                 else if (typeName.equals("string") )
89                         fValue= new XDebugStringValue(this,typeName);
90                 else if (typeName.equals("array") )
91                         fValue= new XDebugArrayValue(this,typeName);
92                 else if (typeName.equals("hash") )
93                         fValue= new XDebugArrayValue(this,typeName);
94                 else if (typeName.equals("object") )
95                         fValue= new XDebugArrayValue(this,typeName);
96                 else if (typeName.equals("resource") )
97                         fValue= new XDebugResourceValue(this,typeName);
98                 else
99                         fValue= new XDebugValue(this,typeName);
100         }
101         
102         private void  init(Node property) {
103                 fFullName=PHPDebugUtils.getAttributeValue(property,"fullname");
104                 fName=PHPDebugUtils.getAttributeValue(property,"name");
105                 fEncoding=PHPDebugUtils.getAttributeValue(property,"encoding");
106                 if (PHPDebugUtils.getAttributeValue(property,"numchildren").equals(""))
107                         fNumChildren = 0;
108                 else
109                         fNumChildren=Integer.parseInt(PHPDebugUtils.getAttributeValue(property,"numchildren"));
110
111                 String typeName=PHPDebugUtils.getAttributeValue(property,"type");
112
113 //              if (typeName.equals("uninitialized") )
114 //                      fValue= new XDebugValue(this,property,typeName);
115                 if (typeName.equals("int") ) 
116                         fValue= new XDebugIntValue(this,property,typeName);
117                 else if (typeName.equals("float") ) 
118                         fValue= new XDebugFloatValue(this,property,typeName);
119                 else if (typeName.equals("bool") ) 
120                         fValue= new XDebugBooleanValue(this,property,typeName);
121                 else if (typeName.equals("string") )
122                         fValue= new XDebugStringValue(this,property,typeName);
123                 else if (typeName.equals("array") )
124                         fValue= new XDebugArrayValue(this,property,typeName);
125                 else if (typeName.equals("hash") )
126                         fValue= new XDebugArrayValue(this,property,typeName);
127                 else if (typeName.equals("object") )
128                         fValue= new XDebugArrayValue(this,property,typeName);
129                 else
130                         fValue= new XDebugValue(this,property,typeName);
131         }
132         
133         /* (non-Javadoc)
134          * @see org.eclipse.debug.core.model.IVariable#getValue()
135          */
136         public IValue getValue() throws DebugException {
137                 return fValue;
138 //              return ((XDebugTarget)getDebugTarget()).getVariableValue(this);
139         }
140         
141         /* (non-Javadoc)
142          * @see org.eclipse.debug.core.model.IVariable#getName()
143          */
144         public String getName() throws DebugException {
145                 if (fFullName.endsWith("]"))
146                         return fFullName.substring(fFullName.lastIndexOf('['));
147                 else
148                         return fName;
149         }
150         
151         /* (non-Javadoc)
152          * @see org.eclipse.debug.core.model.IVariable#getReferenceTypeName()
153          */
154         public String getReferenceTypeName() throws DebugException {
155                 return fValue.getReferenceTypeName();
156         }
157         
158         /* (non-Javadoc)
159          * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
160          */
161         public boolean hasValueChanged() throws DebugException {
162                 return fValue.hasChanged();
163         }
164         
165         /* (non-Javadoc)
166          * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
167          */
168         public void setValue(String expression) throws DebugException {
169                 if(fValue.setValue(expression))
170                         fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CONTENT));
171                 //fValue.setValueA(expression);
172         }
173         
174         /* (non-Javadoc)
175          * @see org.eclipse.debug.core.model.IValueModification#setValue(org.eclipse.debug.core.model.IValue)
176          */
177         public void setValue(IValue value) throws DebugException {
178                 fValue.setValueB(value);
179         }
180
181         /* (non-Javadoc)
182          * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
183          */
184         public boolean supportsValueModification() {
185                 return fValue.supportsValueModification();
186         }
187
188         /* (non-Javadoc)
189          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
190          */
191         public boolean verifyValue(String expression) throws DebugException {
192                 return fValue.verifyValue(expression);
193         }
194
195         /* (non-Javadoc)
196          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
197          */
198         public boolean verifyValue(IValue value) throws DebugException {
199                 return false;
200         }
201         
202         /**
203          * Returns the stack frame owning this variable.
204          * 
205          * @return the stack frame owning this variable
206          */
207         protected XDebugStackFrame getStackFrame() {
208                 return fFrame;
209         }
210         
211         public void setStackFrame(XDebugStackFrame frame) {
212                 fFrame=frame;
213                 super.setDebugTarget((XDebugTarget)frame.getDebugTarget());
214         }
215
216 //      public int getType() {
217 //              return fType;
218 //      }
219
220         public String getValueString() throws DebugException {
221                 return fValue.getValueString();
222         }
223         
224         /*public setValueString(String newValueStrig) throws DebugException {
225                 fValue.setValueString getValueString();
226         }*/
227         
228         public boolean hasChildren() {
229                 return (fNumChildren > 0);
230         }
231
232         public boolean isArray() {
233                 return (fValue.isArray());
234         }
235
236         public String getEncoding() {
237                 return fEncoding;
238         }
239
240         public void setEncoding(String encoding) {
241                 fEncoding = encoding;
242         }
243         
244         public String toString() {
245                 return fValue.toString();
246         }
247
248         public String getFullName() {
249                 return fFullName;
250         }
251
252         public int getNumChildren() {
253                 return fNumChildren;
254         }
255
256         public void setNumChildren(int numChildren) {
257                 fNumChildren = numChildren;
258         }
259         
260         public void setChange(IVariable oldVariable)  throws DebugException {
261                 XDebugAbstractValue oldValue = null;
262                 IVariable[] newVariable = null;
263                 IVariable[] oldVariable1 = null;
264
265                 try {
266                         oldValue = (XDebugAbstractValue) oldVariable.getValue();
267                 } catch (DebugException e) {
268                 }
269
270                 try {
271                         oldVariable1 = ((XDebugVariable)oldVariable).getValue().getVariables();
272                         newVariable = fValue.getVariables();
273                 } catch (DebugException e) {
274                 }
275                 
276                 
277                 if(((XDebugVariable) oldVariable).getNumChildren() > 0) {
278                         if ( this.getNumChildren() == 0) {
279                                 try {
280                                         if (!fValue.getValueString().equals(((XDebugAbstractValue)oldValue).getValueString())) {
281                                                 fValue.sethasChanged(true);
282                                         } else {
283                                                 fValue.sethasChanged(false);                                            
284                                         }
285                                 } catch (DebugException e) {
286                                 }
287                         } else {
288                                 for(int i = 0; i < newVariable.length; i++) {
289                                         ((XDebugVariable)newVariable[i]).setChange(((XDebugVariable)oldVariable1[i]));
290                                 }
291                         }                       
292                 } else {
293                         if ( this.getNumChildren() == 0) {
294                                 // controllare anche il tipo (unknow)
295                                 //if (!fValue.getValueString().equals("uninitialized")) {
296                                 if (!fValue.getValueString().equals(oldValue.getValueString())) {
297                                         fValue.sethasChanged(true);
298                                 } else {
299                                         fValue.sethasChanged(false);                                    
300                                 }
301                         } else {
302                                 IVariable dummy = new XDebugVariable("UNKNOWN", "");
303                                 dummy.setValue("uninitialized");
304                                 for(int i = 0; i < newVariable.length; i++) {
305                                         ((XDebugVariable)newVariable[i]).setChange(dummy);
306                                 }
307                                 fValue.sethasChanged(true);                             
308                         }
309                 }
310         }
311         
312 }