f79af8f92981b08b53d2d864adc9f20dc44a0b35
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / core / xdebug / ResponseListener.java
1 package net.sourceforge.phpeclipse.xdebug.core.xdebug;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5
6 //import javax.print.attribute.standard.Fidelity;
7 import javax.xml.parsers.DocumentBuilder;
8 import javax.xml.parsers.DocumentBuilderFactory;
9 import javax.xml.parsers.ParserConfigurationException;
10
11 import net.sourceforge.phpeclipse.xdebug.core.IPHPDebugEvent;
12 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
13 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
14 import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugResponse;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.debug.core.DebugEvent;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.NamedNodeMap;
23 import org.w3c.dom.Node;
24 import org.xml.sax.SAXException;
25 import org.w3c.dom.CDATASection;
26
27 /**
28  * Listens to events from the XDebug and fires corresponding 
29  * debug events.
30  */
31
32 public class ResponseListener extends Job {
33         private XDebugConnection fConnection;
34         private ResponseList fResponseList;
35         
36         public ResponseListener(XDebugConnection connection) {
37                 super("XDebug Event Dispatch");
38                 setSystem(true);
39                 fConnection = connection;
40                 fResponseList = new ResponseList();
41         }
42                 
43         private void checkResponse(XDebugResponse response) {
44                 if (response.getStatus().equals("stopping") || response.getStatus().equals("stopped")) {
45                         this.cancel();
46                         fireEvent(IPHPDebugEvent.STOPPED, null);
47                 } else if (response.getStatus().equals("break") && response.getReason().equals("ok")){ 
48                         if (response.getCommand().equals("run")) {
49                                 fireEvent(IPHPDebugEvent.BREAKPOINT_HIT, null);
50                         } else if (response.getCommand().equals("step_into")) {
51                                 fireEvent(IPHPDebugEvent.STEP_END, null);
52                         } else if (response.getCommand().equals("step_over")) {
53                                 fireEvent(IPHPDebugEvent.STEP_END, null);
54                         } else if (response.getCommand().equals("step_out")) {
55                                 fireEvent(IPHPDebugEvent.STEP_END, null);
56                         } 
57                 } 
58         }
59         
60         private void fireEvent(int detail, Object data) {
61                 DebugEvent event = new DebugEvent(this, DebugEvent.MODEL_SPECIFIC, detail);
62                 event.setData(data);
63                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
64         }
65         
66         /* (non-Javadoc)
67          * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
68          */
69         protected IStatus run(IProgressMonitor monitor) {
70                 String InputXML = "";
71                 while (!fConnection.isClosed()) {
72                         if (!monitor.isCanceled()) {
73                                 try {
74                                         InputXML = fConnection.readData();
75                                 } catch (Exception e) {
76                                         ; //
77                                 }
78                                 if (InputXML != null) {
79                                         XDebugCorePlugin.log(IStatus.INFO, InputXML);
80                                         XDebugResponse response = new XDebugResponse(InputXML);
81                                         if (response.getName() == "response") {
82                                                 fResponseList.add(response);
83                                                 checkResponse(response);
84                                         }
85                                 }
86                         }
87                 }
88                 return Status.OK_STATUS;
89         }
90
91         public XDebugResponse getResponse(int id) {
92                 return fResponseList.get(id);
93         }
94 }