Refactory.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / model / XDebugStackFrame.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 java.net.MalformedURLException;
10 import java.net.URL;
11
12 import org.eclipse.core.runtime.IPath;
13 import org.eclipse.core.runtime.Path;
14 import org.eclipse.debug.core.DebugException;
15 import org.eclipse.debug.core.model.IRegisterGroup;
16 import org.eclipse.debug.core.model.IStackFrame;
17 import org.eclipse.debug.core.model.IThread;
18 import org.eclipse.debug.core.model.IVariable;
19 import org.w3c.dom.Node;
20 import org.w3c.dom.NodeList;
21
22 /**
23  * @author PHPeclipse team
24  * @author Axel
25  *
26  */
27 public class XDebugStackFrame  extends XDebugElement implements IStackFrame {
28         private XDebugThread fThread;
29
30         private int fId;
31         
32         private URL fName;
33         
34         private int fLineNumber;
35         private int fLevel;
36         private String fType;
37         private String fWhere;
38
39         private IVariable[] fVariables;
40
41         private int fStepCount = 0;
42         
43         /**
44          * Constructs a stack frame in the given thread with the given
45          * frame data.
46          * 
47          * @param thread
48          * @param data frame data
49          * @param id stack frame id (0 is the bottom of the stack)
50          */
51         public XDebugStackFrame(XDebugThread thread, int id, String type, int lineNumber, String where, /*URL*/String filename) {
52                 super(thread == null ? null : (XDebugTarget) thread.getDebugTarget());
53                 
54                 fId = id;
55                 fLevel = id;
56                 fThread = thread;
57                 fType = type;
58                 fLineNumber = lineNumber;
59                 fWhere = where;
60                 
61                 try {
62                 fName = new URL(filename);
63                 } catch (MalformedURLException e) {
64                         e.printStackTrace();
65                 }
66         }
67         
68         public void incrementStepCounter() {
69                 fStepCount++;
70         }
71         
72         /* (non-Javadoc)
73          * @see org.eclipse.debug.core.model.IStackFrame#getThread()
74          */
75         public IThread getThread() {
76                 return fThread;
77         }
78         
79         public IVariable[] getVariables() throws DebugException {
80                 if (fVariables == null) {
81                         Node dfl = ((XDebugTarget) getDebugTarget()).getLocalVariables(fLevel);
82                         Node dfg = ((XDebugTarget) getDebugTarget()).getGlobalVariables(fLevel);
83                         parseVariable(dfl, dfg);
84                 }
85
86                 return fVariables;
87         }
88         
89         private void parseVariable(Node localVariables, Node globalVariables) {
90                 NodeList property = localVariables.getChildNodes();
91                 
92                 NodeList propertyGlobal = globalVariables.getChildNodes();
93                 
94                 fVariables = new IVariable[property.getLength() + propertyGlobal.getLength()];
95                 
96                 int length = property.getLength();
97                 for (int i = 0; i < length; i++) {
98                         XDebugVariable var = new XDebugVariable(this, property.item(i));
99                         fVariables[i] = var;
100                 }
101
102                 int globalLength = propertyGlobal.getLength();
103                 for (int k = 0; k < globalLength; k++) {
104                         XDebugVariable var = new XDebugVariable(this, propertyGlobal.item(k));
105                         fVariables[k + length] = var;
106                 }
107         }
108         
109         /*public void evaluateChange(IStackFrame OldStackFrame) throws DebugException {
110                 IVariable[] OldVariable = ((XDebugStackFrame) OldStackFrame).getVariables();
111                 for (int i = 0; i < fVariables.length; i++) {
112                         ((XDebugVariable) fVariables[i]).setChange(OldVariable[i]);
113                 }
114         }*/
115         
116         /* (non-Javadoc)
117          * @see org.eclipse.debug.core.model.IStackFrame#hasVariables()
118          */
119         public boolean hasVariables() throws DebugException {
120                 /*return fVariables.length > 0;*/
121                 return true;
122         }
123         
124         /* (non-Javadoc)
125          * @see org.eclipse.debug.core.model.IStackFrame#getLineNumber()
126          */
127         public int getLineNumber() throws DebugException {
128                 return fLineNumber;
129         }
130         
131         /* (non-Javadoc)
132          * @see org.eclipse.debug.core.model.IStackFrame#getCharStart()
133          */
134         public int getCharStart() throws DebugException {
135                 return -1;
136         }
137         
138         /* (non-Javadoc)
139          * @see org.eclipse.debug.core.model.IStackFrame#getCharEnd()
140          */
141         public int getCharEnd() throws DebugException {
142                 return -1;
143         }
144         
145         /* (non-Javadoc)fName
146          * @see org.eclipse.debug.core.model.IStackFrame#getName()
147          */
148         public String getName() throws DebugException {
149                 return fName.toString()+"::"+fWhere+ " line: "+ fLineNumber;
150         }
151         
152         /* (non-Javadoc)
153          * @see org.eclipse.debug.core.model.IStackFrame#getRegisterGroups()
154          */
155         public IRegisterGroup[] getRegisterGroups() throws DebugException {
156                 return null;
157         }
158         
159         /* (non-Javadoc)
160          * @see org.eclipse.debug.core.model.IStackFrame#hasRegisterGroups()
161          */
162         public boolean hasRegisterGroups() throws DebugException {
163                 return false;
164         }
165         
166         /* (non-Javadoc)
167          * @see org.eclipse.debug.core.model.IStep#canStepInto()
168          */
169         public boolean canStepInto() {
170                 return fThread.canStepInto();
171         }
172         
173         /* (non-Javadoc)
174          * @see org.eclipse.debug.core.model.IStep#canStepOver()
175          */
176         public boolean canStepOver() {
177                 return fThread.canStepOver();
178         }
179         
180         /* (non-Javadoc)
181          * @see org.eclipse.debug.core.model.IStep#canStepReturn()
182          */
183         public boolean canStepReturn() {
184                 return fThread.canStepReturn();
185         }
186         
187         /* (non-Javadoc)
188          * @see org.eclipse.debug.core.model.IStep#isStepping()
189          */
190         public boolean isStepping() {
191                 return fThread.isStepping();
192         }
193         
194         /* (non-Javadoc)
195          * @see org.eclipse.debug.core.model.IStep#stepInto()
196          */
197         public void stepInto() throws DebugException {
198                 fThread.stepInto();
199         }
200         
201         /* (non-Javadoc)
202          * @see org.eclipse.debug.core.model.IStep#stepOver()
203          */
204         public void stepOver() throws DebugException {
205                 fThread.stepOver();
206         }
207         
208         /* (non-Javadoc)
209          * @see org.eclipse.debug.core.model.IStep#stepReturn()
210          */
211         public void stepReturn() throws DebugException {
212                 fThread.stepReturn();
213         }
214         
215         /* (non-Javadoc)
216          * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
217          */
218         public boolean canResume() {
219                 return fThread.canResume();
220         }
221         
222         /* (non-Javadoc)
223          * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
224          */
225         public boolean canSuspend() {
226                 return fThread.canSuspend();
227         }
228         
229         /* (non-Javadoc)
230          * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
231          */
232         public boolean isSuspended() {
233                 return fThread.isSuspended();
234         }
235         
236         /* (non-Javadoc)
237          * @see org.eclipse.debug.core.model.ISuspendResume#resume()
238          */
239         public void resume() throws DebugException {
240                 fThread.resume();
241         }
242         
243         /* (non-Javadoc)
244          * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
245          */
246         public void suspend() throws DebugException {
247                 fThread.suspend();
248         }
249         
250         /* (non-Javadoc)
251          * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
252          */
253         public boolean canTerminate() {
254                 return fThread.canTerminate();
255         }
256         
257         /* (non-Javadoc)
258          * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
259          */
260         public boolean isTerminated() {
261                 return fThread.isTerminated();
262         }
263         
264         /* (non-Javadoc)
265          * @see org.eclipse.debug.core.model.ITerminate#terminate()
266          */
267         public void terminate() throws DebugException {
268                 fThread.terminate();
269         }
270         
271         /**
272          * Returns the name of the source file this stack frame is associated
273          * with.
274          * 
275          * @return the name of the source file this stack frame is associated
276          * with. If the file associated with this frame does not exists, it returns null.
277          */
278         public String getSourceName() {
279                 if (fName == null) {
280                         return null;
281                 }
282                 IPath a = new Path(fName.getFile());
283                 return a.lastSegment();
284         }
285
286         public boolean isSameStackFrame(Object obj) {
287                 boolean isSameStackFrame = false;
288                 
289                 if (obj instanceof XDebugStackFrame) {
290                         XDebugStackFrame sf = (XDebugStackFrame)obj;
291                         //try {
292                                 isSameStackFrame = sf.getSourceName().equals(getSourceName()) &&
293                                         /*sf.getLineNumber() == getLineNumber() &&*/
294                                         /*sf.getLevel() == getLevel() &&*/
295                                         sf.getType().equals(getType()) &&
296                                         sf.getWhere().equals(getWhere()); //&&
297                                         /*sf.fId == fId;*/
298                         /*} catch (DebugException e) {
299                         }*/
300                 }
301
302                 return isSameStackFrame;
303         }
304         
305         /* (non-Javadoc)
306          * @see java.lang.Object#equals(java.lang.Object)
307          */
308         public boolean equals(Object obj) {
309                 if (obj instanceof XDebugStackFrame) {
310                         XDebugStackFrame sf = (XDebugStackFrame)obj;
311                         try {
312                                 return sf.getSourceName().equals(getSourceName()) &&
313                                         sf.getLineNumber() == getLineNumber() &&
314                                         sf.getLevel() == getLevel() &&
315                                         sf.getType().equals(getType()) &&
316                                         sf.getWhere().equals(getWhere());
317 /*                                      sf.getType() == getType() &&
318                                         sf.getWhere() == getWhere() &&*/
319                                         /*sf.fId == fId;*/
320                         } catch (DebugException e) {
321                         }
322                 }
323
324                 return false;
325         }
326         
327         /* (non-Javadoc)
328          * @see java.lang.Object#equals(java.lang.Object)
329          */
330         public boolean equalsOld(Object obj) {
331                 if (obj instanceof XDebugStackFrame) {
332                         XDebugStackFrame sf = (XDebugStackFrame)obj;
333                         try {
334                                 return sf.getSourceName().equals(getSourceName()) &&
335                                         sf.getLineNumber() == getLineNumber() &&
336                                         sf.fId == fId;
337                         } catch (DebugException e) {
338                         }
339                 }
340                 return false;
341         }
342         
343         /* (non-Javadoc)
344          * @see java.lang.Object#hashCode()
345          */
346         public int hashCode() {
347                 return getSourceName().hashCode() + fLevel;
348         }
349         
350         /**
351          * 
352          * @return this stack frame's unique identifier within its thread
353          */
354         /*protected int getIdentifier() {
355                 return fId;
356         }*/
357
358         public URL getFullName() {
359                 return fName;
360         }
361         
362
363         public int getLevel() {
364                 return fLevel;
365         }
366
367         public String getType() {
368                 return fType;
369         }
370
371         public String getWhere() {
372                 return fWhere;
373         }
374
375         public boolean setVariableValue(XDebugVariable variable, String expression)  throws DebugException {
376                 return ((XDebugTarget) getDebugTarget()).setVarValue("$" + variable.getName(), expression);
377         }
378 }