1 package net.sourceforge.phpeclipse.xdebug.core.xdebug;
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
6 import javax.xml.parsers.DocumentBuilder;
7 import javax.xml.parsers.DocumentBuilderFactory;
8 import javax.xml.parsers.ParserConfigurationException;
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;
18 //import org.eclipse.core.runtime.IPath;
19 //import org.eclipse.core.runtime.Path;
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;
39 * Listens to events from the XDebug and fires corresponding
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;
51 private boolean fError;
53 private String fValue;
55 private String fAddress;
57 public synchronized void setParentNode (String xmlInput){
58 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
59 DocumentBuilder builder=null;
62 builder = factory.newDocumentBuilder();
63 } catch (ParserConfigurationException e) {
64 // TODO Auto-generated catch block
67 ByteArrayInputStream InputXMLStream = new ByteArrayInputStream(xmlInput.getBytes());
70 doc = builder.parse(InputXMLStream);
71 } catch (SAXException e) {
72 // TODO Auto-generated catch block
74 } catch (IOException e) {
75 // TODO Auto-generated catch block
80 parentNode=doc.getFirstChild();
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");
90 if( fCommand.compareTo("eval") == 0 ) {
92 Node property = parentNode.getFirstChild();
94 NamedNodeMap listAttribute = property.getAttributes();
95 Node attribute = listAttribute.getNamedItem("type");
96 if (attribute !=null) {
97 fType = attribute.getNodeValue();
100 Node attribute1 = listAttribute.getNamedItem("address");
101 if (attribute1 !=null) {
102 fAddress = attribute1.getNodeValue();
105 Node firstChild1 = (Node) property.getFirstChild();
107 if( firstChild1 != null ) {
108 fValue = firstChild1.getNodeValue();
110 } catch (Exception e) {
111 // TODO: handle exception
115 CDATASection firstChild = (CDATASection) parentNode.getFirstChild();
117 if( firstChild != null ) {
118 fValue = parentNode.getFirstChild().getNodeValue();
120 } catch (Exception e) {
121 // TODO: handle exception
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();
137 public synchronized Node getParentNode(){
141 public synchronized String getCommand() {
144 public synchronized String getName() {
148 public synchronized String getValue() {
152 public synchronized String getType() {
156 public synchronized String getAddress() {
168 DebugResponse(String XMLInput) {
169 setParentNode(XMLInput);
172 public synchronized String getReason() {
176 public synchronized String getStatus() {
180 public synchronized int getTransactionID() {
181 return fTransactionID;
184 public boolean isError() {
188 public void setError(boolean error) {
193 private XDebugConnection fConnection;
194 private DebugResponse lastResponse;
196 public ResponseListener(XDebugConnection connection) {
197 super("XDebug Event Dispatch");
199 fConnection=connection;
200 lastResponse= new DebugResponse();
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);
215 lastResponse.setError(false);
216 if (response.getStatus().equals("stopping")) {
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);
232 protected void fireEvent(int detail) {
233 DebugEvent event = new DebugEvent(this, DebugEvent.MODEL_SPECIFIC, detail);
234 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
237 protected void fireEvent(int detail, Object data) {
238 DebugEvent event = new DebugEvent(this, DebugEvent.MODEL_SPECIFIC, detail);
240 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
244 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
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);
259 return Status.OK_STATUS;