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