32e9e90d02684a0229a7ce837dbb9560217a42b4
[phpeclipse.git] /
1 package net.sourceforge.phpeclipse.xdebug.core.xdebug;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5
6 import javax.xml.parsers.DocumentBuilder;
7 import javax.xml.parsers.DocumentBuilderFactory;
8 import javax.xml.parsers.ParserConfigurationException;
9
10 import net.sourceforge.phpeclipse.xdebug.core.IPHPDebugEvent;
11 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
12 //import net.sourceforge.phpeclipse.xdebug.core.PathMapItem;
13 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
14 //import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
15 //import net.sourceforge.phpeclipse.xdebug.php.model.XDebugLineBreakpoint;
16 //import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
17
18 //import org.eclipse.core.runtime.IPath;
19 //import org.eclipse.core.runtime.Path;
20
21 //import org.eclipse.core.resources.IMarker;
22 //import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.core.runtime.jobs.Job;
27 import org.eclipse.debug.core.DebugEvent;
28 //import org.eclipse.debug.core.DebugException;
29 import org.eclipse.debug.core.DebugPlugin;
30 //import org.eclipse.debug.core.model.IBreakpoint;
31 //import org.eclipse.debug.core.model.ILineBreakpoint;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.NamedNodeMap;
34 import org.w3c.dom.Node;
35 import org.xml.sax.SAXException;
36 import org.w3c.dom.CDATASection;
37
38 /**
39  * Listens to events from the XDebug and fires corresponding 
40  * debug events.
41  */
42
43 public class ResponseListener extends Job {
44         public class DebugResponse {
45                 private Node parentNode;
46                 private int fTransactionID=-1;
47                 private String fCommand="";
48                 private String fStatus;
49                 private String fReason;
50                 private String fName;
51                 private boolean  fError;
52
53                 private String fValue;
54                 private String fType;
55                 private String fAddress;
56                 
57                 public synchronized void setParentNode (String xmlInput){
58                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
59                         DocumentBuilder builder=null;
60                         Document doc=null;
61                         try {
62                                 builder = factory.newDocumentBuilder();
63                         } catch (ParserConfigurationException e) {
64                                 // TODO Auto-generated catch block
65                                 e.printStackTrace();
66                         }
67                         ByteArrayInputStream InputXMLStream = new ByteArrayInputStream(xmlInput.getBytes());
68                         
69                         try {
70                                 doc = builder.parse(InputXMLStream);
71                         } catch (SAXException e) {
72                                 // TODO Auto-generated catch block
73                                 e.printStackTrace();
74                         } catch (IOException e) {
75                                 // TODO Auto-generated catch block
76                                 e.printStackTrace();
77                         }
78                         
79
80                         parentNode=doc.getFirstChild();
81                         
82                         fName=parentNode.getNodeName();
83                         String idStr = getAttributeValue("transaction_id");
84                         if (!"".equals(idStr))
85                                 fTransactionID = Integer.parseInt(idStr);
86                         fCommand = getAttributeValue("command");
87                         fStatus = getAttributeValue("status");
88                         fReason = getAttributeValue("reason");
89
90                         if( fCommand.compareTo("eval") == 0 ) {
91                                 try {
92                                         Node property = parentNode.getFirstChild();
93
94                                         NamedNodeMap listAttribute = property.getAttributes();
95                                         Node attribute = listAttribute.getNamedItem("type");
96                                         if (attribute !=null) {
97                                                 fType = attribute.getNodeValue();
98                                         }
99
100                                         Node attribute1 = listAttribute.getNamedItem("address");
101                                         if (attribute1 !=null) {
102                                                 fAddress = attribute1.getNodeValue();
103                                         }
104                                         
105                                         Node firstChild1 = (Node) property.getFirstChild();
106                                         
107                                         if( firstChild1 != null ) {
108                                                 fValue = firstChild1.getNodeValue();
109                                         }
110                                 } catch (Exception e) {
111                                         // TODO: handle exception
112                                 }
113                         } else {
114                                 try {
115                                         CDATASection firstChild = (CDATASection) parentNode.getFirstChild();
116         
117                                         if( firstChild != null ) {
118                                                 fValue = parentNode.getFirstChild().getNodeValue();
119                                         }
120                                 } catch (Exception e) {
121                                         // TODO: handle exception
122                                 }
123                         }
124                 }
125                 
126                 public String getAttributeValue (String AttributeName) {
127                         String strValue = "";
128                         if (parentNode.hasAttributes()) {
129                                 NamedNodeMap listAttribute = parentNode.getAttributes();
130                                 Node attribute = listAttribute.getNamedItem(AttributeName);
131                                 if (attribute !=null)
132                                         strValue = attribute.getNodeValue();
133                         }
134                         return strValue;
135                 }
136                 
137                 public synchronized Node getParentNode(){
138                         return parentNode;
139                 }
140                 
141                 public synchronized String getCommand() {
142                         return fCommand;
143                 }
144                 public synchronized String getName() {
145                         return fName;
146                 }
147                 
148                 public synchronized String getValue() {
149                         return fValue;
150                 }
151
152                 public synchronized String getType() {
153                         return fType;
154                 }
155
156                 public synchronized String getAddress() {
157                         return fAddress;
158                 }
159
160                 DebugResponse() {
161                         fTransactionID = -1;
162                         fCommand = "";
163                         fStatus = "";
164                         fReason = "";                   
165                         fName= "";
166                 }
167                 
168                 DebugResponse(String XMLInput) {
169                         setParentNode(XMLInput);
170                 }
171
172                 public synchronized String getReason() {
173                         return fReason;
174                 }
175
176                 public synchronized String getStatus() {
177                         return fStatus;
178                 }
179
180                 public synchronized int getTransactionID() {
181                         return fTransactionID;
182                 }
183                 
184                 public boolean  isError() {
185                         return fError;
186                 }
187
188                 public void setError(boolean error) {
189                         fError = error;
190                 }       
191         }
192
193         private XDebugConnection fConnection;
194         private DebugResponse lastResponse; 
195
196         public ResponseListener(XDebugConnection connection) {
197                 super("XDebug Event Dispatch");
198                 setSystem(true);
199                 fConnection=connection;
200                 lastResponse= new DebugResponse();
201         }
202                 
203         private void checkResponse(DebugResponse response) {
204                 Node node=response.getParentNode();
205                 if (node.hasChildNodes()) {
206                         Node child=node.getFirstChild();
207                         if (child.getNodeName().equals("error")) {
208                                 int code = Integer.parseInt(PHPDebugUtils.getAttributeValue(child, "code"));
209                                 String text=(child.getFirstChild()).getNodeValue();
210                                 XDebugCorePlugin.log(IStatus.ERROR," ERROR "+code+": "+text);
211                                 lastResponse.setError(true);
212                                 return;
213                         }
214                 }
215                 lastResponse.setError(false);
216                 if (response.getStatus().equals("stopping")) {
217                         //this.cancel();
218                         fireEvent(IPHPDebugEvent.STOPPED);
219                 } else if (response.getStatus().equals("break") && response.getReason().equals("ok")){ 
220                         if (response.getCommand().equals("run")) {
221                                 fireEvent(IPHPDebugEvent.BREAKPOINT_HIT, null);
222                         } else if (response.getCommand().equals("step_into")) {
223                                 fireEvent(IPHPDebugEvent.STEP_END);
224                         } else if (response.getCommand().equals("step_over")) {
225                                 fireEvent(IPHPDebugEvent.STEP_END);
226                         } else if (response.getCommand().equals("step_out")) {
227                                 fireEvent(IPHPDebugEvent.STEP_END);
228                         } 
229                 } 
230         }
231         
232         protected void fireEvent(int detail) {
233                 DebugEvent event = new DebugEvent(this, DebugEvent.MODEL_SPECIFIC, detail);
234                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
235         }
236         
237         protected void fireEvent(int detail, Object data) {
238                 DebugEvent event = new DebugEvent(this, DebugEvent.MODEL_SPECIFIC, detail);
239                 event.setData(data);
240                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
241         }
242         
243         /* (non-Javadoc)
244          * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
245          */
246         protected IStatus run(IProgressMonitor monitor) {
247                 String InputXML = "";
248                 while (!fConnection.isClosed() && (InputXML != null)) {
249                         InputXML = fConnection.readData();
250                         if (InputXML != null) {
251                                 XDebugCorePlugin.log(IStatus.INFO, InputXML);
252                                 lastResponse.setParentNode(InputXML);
253                                 if (lastResponse.getName() == "response") {
254                                         fConnection.addResponse(lastResponse,lastResponse.getTransactionID());
255                                         checkResponse(lastResponse);
256                                 }
257                         }
258                 }
259                 return Status.OK_STATUS;
260         }
261 }