Separate XDebugResponse from ResponseListner.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / core / xdebug / ResponseListener.java
index d700859..f79af8f 100644 (file)
@@ -3,32 +3,21 @@ package net.sourceforge.phpeclipse.xdebug.core.xdebug;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 
+//import javax.print.attribute.standard.Fidelity;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
 import net.sourceforge.phpeclipse.xdebug.core.IPHPDebugEvent;
 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
-import net.sourceforge.phpeclipse.xdebug.core.PathMapItem;
 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
-import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
-import net.sourceforge.phpeclipse.xdebug.php.model.XDebugLineBreakpoint;
-import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
-
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.Path;
-
-import org.eclipse.core.resources.IMarker;
-import org.eclipse.core.runtime.CoreException;
+import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugResponse;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.debug.core.DebugEvent;
-import org.eclipse.debug.core.DebugException;
 import org.eclipse.debug.core.DebugPlugin;
-import org.eclipse.debug.core.model.IBreakpoint;
-import org.eclipse.debug.core.model.ILineBreakpoint;
 import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
@@ -41,307 +30,65 @@ import org.w3c.dom.CDATASection;
  */
 
 public class ResponseListener extends Job {
-       
-
-       public class DebugResponse {
-
-                       private Node parentNode;
-                       private int fTransactionID=-1;
-                       private String fCommand="";
-                       private String fStatus;
-                       private String fReason;
-                       private String fName;
-                       private boolean  fError;
-
-                       private String fValue;
-                       private String fType;
-                       private String fAddress;
-                       
-                       public synchronized void setParentNode (String xmlInput){
-                               DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-                               DocumentBuilder builder=null;
-                               Document doc=null;
-                               try {
-                                       builder = factory.newDocumentBuilder();
-                               } catch (ParserConfigurationException e) {
-                                       // TODO Auto-generated catch block
-                                       e.printStackTrace();
-                               }
-                               ByteArrayInputStream InputXMLStream = new ByteArrayInputStream(xmlInput.getBytes());
-                               
-                               try {
-                                       doc = builder.parse(InputXMLStream);
-                               } catch (SAXException e) {
-                                       // TODO Auto-generated catch block
-                                       e.printStackTrace();
-                               } catch (IOException e) {
-                                       // TODO Auto-generated catch block
-                                       e.printStackTrace();
-                               }
-                               
-
-                               parentNode=doc.getFirstChild();
-                               
-                               fName=parentNode.getNodeName();
-                               String idStr = getAttributeValue("transaction_id");
-                               if (!"".equals(idStr))
-                                       fTransactionID = Integer.parseInt(idStr);
-                               fCommand = getAttributeValue("command");
-                               fStatus = getAttributeValue("status");
-                               fReason = getAttributeValue("reason");
-
-                               if( fCommand.compareTo("eval") == 0 ) {
-                                       try {
-                                               Node property = parentNode.getFirstChild();
-
-                                               NamedNodeMap listAttribute = property.getAttributes();
-                                               Node attribute = listAttribute.getNamedItem("type");
-                                               if (attribute !=null) {
-                                                       fType = attribute.getNodeValue();
-                                               }
-
-                                               Node attribute1 = listAttribute.getNamedItem("address");
-                                               if (attribute1 !=null) {
-                                                       fAddress = attribute1.getNodeValue();
-                                               }
-                                               
-                                               Node firstChild1 = (Node) property.getFirstChild();
-                                               
-                                               if( firstChild1 != null ) {
-                                                       fValue = firstChild1.getNodeValue();
-                                               }
-                                       } catch (Exception e) {
-                                               // TODO: handle exception
-                                       }
-                               } else {
-                                       try {
-                                               CDATASection firstChild = (CDATASection) parentNode.getFirstChild();
-               
-                                               if( firstChild != null ) {
-                                                       fValue = parentNode.getFirstChild().getNodeValue();
-                                               }
-                                       } catch (Exception e) {
-                                               // TODO: handle exception
-                                       }
-                               }
-                       }
-                       
-                       public String getAttributeValue (String AttributeName) {
-                               String strValue = "";
-                               if (parentNode.hasAttributes()) {
-                                       NamedNodeMap listAttribute = parentNode.getAttributes();
-                                       Node attribute = listAttribute.getNamedItem(AttributeName);
-                                       if (attribute !=null)
-                                               strValue = attribute.getNodeValue();
-                               }
-                               return strValue;
-                       }
-                       
-                       public synchronized Node getParentNode(){
-                               return parentNode;
-                       }
-                       
-                       public synchronized String getCommand() {
-                               return fCommand;
-                       }
-                       public synchronized String getName() {
-                               return fName;
-                       }
-                       
-                       public synchronized String getValue() {
-                               return fValue;
-                       }
-
-                       public synchronized String getType() {
-                               return fType;
-                       }
-
-                       public synchronized String getAddress() {
-                               return fAddress;
-                       }
-
-                       DebugResponse () {
-                               fTransactionID = -1;
-                               fCommand = "";
-                               fStatus = "";
-                               fReason = "";                   
-                               fName= "";
-                       }
-                       
-                       DebugResponse (String XMLInput) {
-                               setParentNode(XMLInput);
-                       }
-
-                       public synchronized String getReason() {
-                               return fReason;
-                       }
-
-                       public synchronized String getStatus() {
-                               return fStatus;
-                       }
-
-                       public synchronized int getTransactionID() {
-                               return fTransactionID;
-                       }
-                       
-                       public boolean  isError() {
-                               return fError;
-                       }
-
-                       public void setError(boolean error) {
-                               fError = error;
-                       }
-                       
-               }
-
-       
        private XDebugConnection fConnection;
-       //private XDebugTarget fDebugTarget;
-       private DebugResponse lastResponse; 
-
+       private ResponseList fResponseList;
+       
        public ResponseListener(XDebugConnection connection) {
                super("XDebug Event Dispatch");
                setSystem(true);
-               fConnection=connection;
-               lastResponse= new DebugResponse();
+               fConnection = connection;
+               fResponseList = new ResponseList();
        }
-       
-       
-       
-       
-       private void checkResponse(DebugResponse response) {
-               Node node=response.getParentNode();
-               if (node.hasChildNodes()) {
-                       Node child=node.getFirstChild();
-                       if (child.getNodeName().equals("error")) {
-                               int code = Integer.parseInt(PHPDebugUtils.getAttributeValue(child, "code"));
-                               String text=(child.getFirstChild()).getNodeValue();
-                               XDebugCorePlugin.log(IStatus.ERROR," ERROR "+code+": "+text);
-                               lastResponse.setError(true);
-                               return;
-                       }
-               }
-               lastResponse.setError(false);
-               if (response.getStatus().equals("stopping")) {
-               //if (response.getStatus().equals("stopped")) {
-                       fConnection.close();
-                       fireEvent(IPHPDebugEvent.STOPPED);
+               
+       private void checkResponse(XDebugResponse response) {
+               if (response.getStatus().equals("stopping") || response.getStatus().equals("stopped")) {
+                       this.cancel();
+                       fireEvent(IPHPDebugEvent.STOPPED, null);
                } else if (response.getStatus().equals("break") && response.getReason().equals("ok")){ 
-                       if (response.getCommand().equals("run")) {  // breakpoint hit
-                               int id=-1;
-                               try {
-                                       id=fConnection.sendRequest("stack_get");
-                               } catch (DebugException e) {
-                                       // TODO Auto-generated catch block
-                                       e.printStackTrace();
-                               }
-                               String InputXML = fConnection.readData();
-                               if (InputXML != null) {
-                                       XDebugCorePlugin.log(IStatus.INFO, InputXML);
-                                       lastResponse.setParentNode(InputXML);
-                                       breakpointHit(lastResponse.getParentNode());
-                               }
-                               
-                       } else if (response.getCommand().equals("step_into")) { // step_into
-                               fireEvent(IPHPDebugEvent.STEP_END);
-//                             XDebugCorePlugin.log(IStatus.INFO,response.getCommand()+" STEP_END sent");
-                       } else if (response.getCommand().equals("step_over")) { // step_over
-                               fireEvent(IPHPDebugEvent.STEP_END);
-//                             XDebugCorePlugin.log(IStatus.INFO,response.getCommand()+" STEP_END sent");
-                       } else if (response.getCommand().equals("step_out")) { // step_over
-                               fireEvent(IPHPDebugEvent.STEP_END);
-//                             XDebugCorePlugin.log(IStatus.INFO,response.getCommand()+" STEP_END sent");
+                       if (response.getCommand().equals("run")) {
+                               fireEvent(IPHPDebugEvent.BREAKPOINT_HIT, null);
+                       } else if (response.getCommand().equals("step_into")) {
+                               fireEvent(IPHPDebugEvent.STEP_END, null);
+                       } else if (response.getCommand().equals("step_over")) {
+                               fireEvent(IPHPDebugEvent.STEP_END, null);
+                       } else if (response.getCommand().equals("step_out")) {
+                               fireEvent(IPHPDebugEvent.STEP_END, null);
                        } 
                } 
-
        }
        
-       protected void fireEvent(int detail) {
-               DebugEvent event = new DebugEvent(this, DebugEvent.MODEL_SPECIFIC, detail);
-               DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
-       }
-       
-       protected void fireEvent(int detail, Object data) {
+       private void fireEvent(int detail, Object data) {
                DebugEvent event = new DebugEvent(this, DebugEvent.MODEL_SPECIFIC, detail);
                event.setData(data);
                DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
        }
-
-       
-       protected void breakpointHit(Node node) {
-               Node child=node.getFirstChild();
-               if (child.getNodeName().equals("stack")) {
-                       int lineNumber = Integer.parseInt(PHPDebugUtils.getAttributeValue(child, "lineno"));
-                       String filename=PHPDebugUtils.getAttributeValue(child, "filename");  
-                       IBreakpoint[] breakpoints = XDebugCorePlugin.getBreakpoints();
-                       for (int i = 0; i < breakpoints.length; i++) {
-                               IBreakpoint breakpoint = breakpoints[i];
-                               if (supportsBreakpoint(breakpoint)) {
-                                       if (breakpoint instanceof ILineBreakpoint) {
-                                               ILineBreakpoint lineBreakpoint = (ILineBreakpoint) breakpoint;
-                                               try {                                           
-                                                       if (breakpoint.isEnabled()) {
-                                                               IMarker marker = breakpoint.getMarker();
-                                                               if (marker != null) {
-                                                                       String endfilename;
-                                                                       
-                                                                       if( XDebugCorePlugin.getDefault().getXDebugProxy().getTarget().getProcess() == null ) {
-                                                                               endfilename = marker.getResource().getLocation().lastSegment(); 
-                                                                       } else {
-                                                                               endfilename = marker.getResource().getLocation().toOSString();
-                                                                       }
-
-                                                                       if(PHPDebugUtils.unescapeString(filename).endsWith(endfilename) && (lineBreakpoint.getLineNumber() == lineNumber) ) {
-                                                                               fireEvent(IPHPDebugEvent.BREAKPOINT_HIT,breakpoint);
-                                                                               break;
-                                                                       }
-                                                               }
-                                                       }
-                                               } catch (CoreException e) {
-                                               }
-                                       }
-                               }
-                       }
-               }
-
-//             DebugEvent event = new DebugEvent(this, DebugEvent.MODEL_SPECIFIC, IPHPDebugEvent.STEP_END);
-//             DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
-       }
-       
-       private boolean supportsBreakpoint(IBreakpoint breakpoint) {
-               if (breakpoint.getModelIdentifier().equals(IXDebugConstants.ID_PHP_DEBUG_MODEL)) {
-                       return true;
-               }
-               return false;
-       }
-
-
        
        /* (non-Javadoc)
         * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
         */
        protected IStatus run(IProgressMonitor monitor) {
                String InputXML = "";
-               while (!fConnection.isClosed() && (InputXML != null)) {
-                       InputXML = fConnection.readData();
-                       if (InputXML != null) {
-                               XDebugCorePlugin.log(IStatus.INFO, InputXML);
-                               lastResponse.setParentNode(InputXML);
-//                             if (lastResponse.getName() == "init") {
-//                                     Node myNode=lastResponse.getParentNode();
-//                                     appID = PHPDebugUtils.getAttributeValue(myNode, "appid");
-//                                     fileuri = PHPDebugUtils.getAttributeValue(myNode, "fileuri");
-//                                     fDebugTarget.started();
-//                                     fDebugTarget.fireCreationEvent();
-                               if (lastResponse.getName() == "response") {
-                                       fConnection.addResponse(lastResponse,lastResponse.getTransactionID());
-                                       checkResponse(lastResponse);
+               while (!fConnection.isClosed()) {
+                       if (!monitor.isCanceled()) {
+                               try {
+                                       InputXML = fConnection.readData();
+                               } catch (Exception e) {
+                                       ; //
+                               }
+                               if (InputXML != null) {
+                                       XDebugCorePlugin.log(IStatus.INFO, InputXML);
+                                       XDebugResponse response = new XDebugResponse(InputXML);
+                                       if (response.getName() == "response") {
+                                               fResponseList.add(response);
+                                               checkResponse(response);
+                                       }
                                }
-//                             fConnection.addResponse(lastResponse,lastResponse.getTransactionID());
-//                             lastResponse.notifyWait();
                        }
                }
                return Status.OK_STATUS;
        }
-       
-}
+
+       public XDebugResponse getResponse(int id) {
+               return fResponseList.get(id);
+       }
+}
\ No newline at end of file