Initial implementation of the new Debug Plugin
[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.URI;
10 import java.net.URISyntaxException;
11
12 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
13 import net.sourceforge.phpeclipse.xdebug.core.DebugConnection.DebugResponse;
14
15 import org.eclipse.core.runtime.Path;
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.model.IRegisterGroup;
18 import org.eclipse.debug.core.model.IStackFrame;
19 import org.eclipse.debug.core.model.IThread;
20 import org.eclipse.debug.core.model.IVariable;
21 import org.w3c.dom.Node;
22 import org.w3c.dom.NodeList;
23
24 /**
25  * @author Axel
26  *
27  * TODO To change the template for this generated type comment go to
28  * Window - Preferences - Java - Code Style - Code Templates
29  */
30 public class XDebugStackFrame  extends XDebugElement implements IStackFrame {
31         
32         private XDebugThread fThread;
33 //      private String fName;
34         private int fLineNo;
35         private String fFileName;
36         private int fId;
37         private IVariable[] fVariables;
38         private String fLevel;
39         private String fType;
40         private String fWhere;
41         
42         /**
43          * Constructs a stack frame in the given thread with the given
44          * frame data.
45          * 
46          * @param thread
47          * @param data frame data
48          * @param id stack frame id (0 is the bottom of the stack)
49          */
50         public XDebugStackFrame(XDebugThread thread, Node stackNode, int id) {
51                 super((XDebugTarget) thread.getDebugTarget());
52                 fId = id;
53                 fThread = thread;
54                 init(stackNode);
55         }
56         
57         /**
58          * Initializes this frame based on its data
59          * 
60          * @param data
61          */
62         private void init(Node stackNode) {
63                 
64                 fLevel=PHPDebugUtils.getAttributeValue(stackNode,"level");
65                 fType=PHPDebugUtils.getAttributeValue(stackNode,"type");
66                 String fileName=PHPDebugUtils.unescapeString(PHPDebugUtils.getAttributeValue(stackNode,"filename"));
67                 String lineNo=PHPDebugUtils.getAttributeValue(stackNode,"lineno");
68
69                 if (lineNo!="")
70                         fLineNo= Integer.parseInt(lineNo);
71                 
72                 fWhere=PHPDebugUtils.getAttributeValue(stackNode,"where");
73                 
74                 fFileName = (new Path(fileName)).lastSegment();
75                 int id=-1;
76                 try {
77                         id=((XDebugTarget)getDebugTarget()).sendRequest("context_get","-d "+fLevel);
78                 } catch (DebugException e) {
79                         // TODO Auto-generated catch block
80                         e.printStackTrace();
81                 }
82                 DebugResponse response=((XDebugTarget)getDebugTarget()).getResponse(id);
83                 Node responseNode = response.getParentNode();
84                 NodeList property = responseNode.getChildNodes();
85                 fVariables = new IVariable[property.getLength()];
86                 for (int i = 0; i < property.getLength(); i++) {
87                         Node propertyNode = property.item(i);
88                         fVariables[i] = new XDebugVariable(this, propertyNode);
89                 }
90
91 //              int numVars = strings.length - 3;
92 //              fVariables = new IVariable[numVars];
93 //              for (int i = 0; i < numVars; i++) {
94 //                      fVariables[i] = new XDebugVariable(this, strings[i + 3]);
95 //              }
96         }
97         
98         /* (non-Javadoc)
99          * @see org.eclipse.debug.core.model.IStackFrame#getThread()
100          */
101         public IThread getThread() {
102                 return fThread;
103         }
104         /* (non-Javadoc)
105          * @see org.eclipse.debug.core.model.IStackFrame#getVariables()
106          */
107         public IVariable[] getVariables() throws DebugException {
108                 return fVariables;
109         }
110         /* (non-Javadoc)
111          * @see org.eclipse.debug.core.model.IStackFrame#hasVariables()
112          */
113         public boolean hasVariables() throws DebugException {
114                 return fVariables.length > 0;
115         }
116         /* (non-Javadoc)
117          * @see org.eclipse.debug.core.model.IStackFrame#getLineNumber()
118          */
119         public int getLineNumber() throws DebugException {
120                 return fLineNo;
121         }
122         /* (non-Javadoc)
123          * @see org.eclipse.debug.core.model.IStackFrame#getCharStart()
124          */
125         public int getCharStart() throws DebugException {
126                 return -1;
127         }
128         /* (non-Javadoc)
129          * @see org.eclipse.debug.core.model.IStackFrame#getCharEnd()
130          */
131         public int getCharEnd() throws DebugException {
132                 return -1;
133         }
134         /* (non-Javadoc)
135          * @see org.eclipse.debug.core.model.IStackFrame#getName()
136          */
137         public String getName() throws DebugException {
138                 return fFileName+"::"+fWhere+ " : lineno "+ fLineNo;
139         }
140         /* (non-Javadoc)
141          * @see org.eclipse.debug.core.model.IStackFrame#getRegisterGroups()
142          */
143         public IRegisterGroup[] getRegisterGroups() throws DebugException {
144                 return null;
145         }
146         /* (non-Javadoc)
147          * @see org.eclipse.debug.core.model.IStackFrame#hasRegisterGroups()
148          */
149         public boolean hasRegisterGroups() throws DebugException {
150                 return false;
151         }
152         /* (non-Javadoc)
153          * @see org.eclipse.debug.core.model.IStep#canStepInto()
154          */
155         public boolean canStepInto() {
156                 return getThread().canStepInto();
157         }
158         /* (non-Javadoc)
159          * @see org.eclipse.debug.core.model.IStep#canStepOver()
160          */
161         public boolean canStepOver() {
162                 return getThread().canStepOver();
163         }
164         /* (non-Javadoc)
165          * @see org.eclipse.debug.core.model.IStep#canStepReturn()
166          */
167         public boolean canStepReturn() {
168                 return getThread().canStepReturn();
169         }
170         /* (non-Javadoc)
171          * @see org.eclipse.debug.core.model.IStep#isStepping()
172          */
173         public boolean isStepping() {
174                 return getThread().isStepping();
175         }
176         /* (non-Javadoc)
177          * @see org.eclipse.debug.core.model.IStep#stepInto()
178          */
179         public void stepInto() throws DebugException {
180                 getThread().stepInto();
181         }
182         /* (non-Javadoc)
183          * @see org.eclipse.debug.core.model.IStep#stepOver()
184          */
185         public void stepOver() throws DebugException {
186                 getThread().stepOver();
187         }
188         /* (non-Javadoc)
189          * @see org.eclipse.debug.core.model.IStep#stepReturn()
190          */
191         public void stepReturn() throws DebugException {
192                 getThread().stepReturn();
193         }
194         /* (non-Javadoc)
195          * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
196          */
197         public boolean canResume() {
198                 return getThread().canResume();
199         }
200         /* (non-Javadoc)
201          * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
202          */
203         public boolean canSuspend() {
204                 return getThread().canSuspend();
205         }
206         /* (non-Javadoc)
207          * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
208          */
209         public boolean isSuspended() {
210                 return getThread().isSuspended();
211         }
212         /* (non-Javadoc)
213          * @see org.eclipse.debug.core.model.ISuspendResume#resume()
214          */
215         public void resume() throws DebugException {
216                 getThread().resume();
217         }
218         /* (non-Javadoc)
219          * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
220          */
221         public void suspend() throws DebugException {
222                 getThread().suspend();
223         }
224         /* (non-Javadoc)
225          * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
226          */
227         public boolean canTerminate() {
228                 return getThread().canTerminate();
229         }
230         /* (non-Javadoc)
231          * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
232          */
233         public boolean isTerminated() {
234                 return getThread().isTerminated();
235         }
236         /* (non-Javadoc)
237          * @see org.eclipse.debug.core.model.ITerminate#terminate()
238          */
239         public void terminate() throws DebugException {
240                 getThread().terminate();
241         }
242         
243         /**
244          * Returns the name of the source file this stack frame is associated
245          * with.
246          * 
247          * @return the name of the source file this stack frame is associated
248          * with
249          */
250         public String getSourceName() {
251                 return fFileName;
252         }
253         /* (non-Javadoc)
254          * @see java.lang.Object#equals(java.lang.Object)
255          */
256         public boolean equals(Object obj) {
257                 if (obj instanceof XDebugStackFrame) {
258                         XDebugStackFrame sf = (XDebugStackFrame)obj;
259                         try {
260                                 return sf.getSourceName().equals(getSourceName()) &&
261                                         sf.getLineNumber() == getLineNumber() &&
262                                         sf.fId == fId;
263                         } catch (DebugException e) {
264                         }
265                 }
266                 return false;
267         }
268         /* (non-Javadoc)
269          * @see java.lang.Object#hashCode()
270          */
271         public int hashCode() {
272                 return getSourceName().hashCode() + fId;
273         }
274         
275         /**
276          * Returns this stack frame's unique identifier within its thread
277          * 
278          * @return this stack frame's unique identifier within its thread
279          */
280         protected int getIdentifier() {
281                 return fId;
282         }
283 }