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