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