fix #763.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / model / XDebugTarget.java
index 2257e84..06064eb 100644 (file)
@@ -3,8 +3,14 @@
  */
 package net.sourceforge.phpeclipse.xdebug.php.model;
 
+//import java.io.ByteArrayInputStream;
+//import java.io.IOException;
 import java.util.List;
 
+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.IProxyEventListener;
 import net.sourceforge.phpeclipse.xdebug.core.IXDebugPreferenceConstants;
@@ -30,12 +36,14 @@ import org.eclipse.debug.core.model.ILineBreakpoint;
 import org.eclipse.debug.core.model.IMemoryBlock;
 import org.eclipse.debug.core.model.IProcess;
 import org.eclipse.debug.core.model.IThread;
+import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
+//import org.xml.sax.SAXException;
 
 import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener;
 import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugConnection;
-import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.XDebugResponse;
+import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugResponse;
 
 /**
  * @author Christian
@@ -176,7 +184,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        fSuspended = false;
                        
                        XDebugCorePlugin.getBreakpointManager().removeBreakpointListener(this);
-                       fThread.fireTerminateEvent();
+                       fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));
                        DebugPlugin.getDefault().removeDebugEventListener(this);
                }
        }
@@ -359,7 +367,6 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
         */
        public boolean isDisconnected() {
                return (false);
-//             return (fDebugConnection==null);
        }
 
        /* (non-Javadoc)
@@ -479,12 +486,38 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                }
        }
        
-       public Node eval(String expression) {
-               int id = fDebugConnection.eval(expression);
-               XDebugResponse response = getResponse(id);
-
-               Node evalResponse = response.getParentNode();
-               Node evalProperty = evalResponse.getFirstChild();
+       public Node eval(String expression) throws DebugException {
+               Node evalProperty = null;
+               if (fDebugConnection != null) {
+                       int id = fDebugConnection.eval(expression);
+                       //Node evalProperty = new Node("");
+                       //if (id > 0) {
+                               XDebugResponse response = getResponse(id);
+               
+                               Node evalResponse = response.getParentNode();
+                               /*Node*/ evalProperty = evalResponse.getFirstChild();
+                       //} /*else {
+                               
+                       //}*/
+               } else {
+                       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+                       DocumentBuilder builder = null;
+                       Document doc = null;
+                       
+                       try {
+                               builder = factory.newDocumentBuilder();
+                       } catch (ParserConfigurationException e) {
+                               e.printStackTrace();
+                       }
+                       //try {
+                               doc =  builder.newDocument(); // .parse("");
+                               evalProperty = doc.createElement("value");
+                       /*} catch (SAXException e) {
+                               e.printStackTrace();
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }*/
+               }
                
                return evalProperty;
        }
@@ -566,25 +599,32 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                fThreads = new IThread[0];
        }
        
-       public void handleProxyEvent(/*String ideKey,*/ XDebugConnection connection) {
-               setDebugConnection(connection);
-               System.out.println("* New Connection - XDebug.Target: " + fDebugConnection.getSessionID());
+       public void handleProxyEvent(XDebugConnection connection) {
+               //System.out.println("* New Connection - XDebug.Target: " + fDebugConnection.getSessionID());
                
-               fThread = new XDebugThread(this);
-               fThreads = new IThread[] {fThread};
-               fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CHANGE));
-               try {
-                       started();
-               } catch( DebugException e ){
-                       e.printStackTrace();            
-               }               
+               if (setDebugConnection(connection)) {
+                       fThread = new XDebugThread(this);
+                       fThreads = new IThread[] {fThread};
+                       fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CHANGE));
+                       try {
+                               started();
+                       } catch( DebugException e ){
+                               e.printStackTrace();            
+                       }
+               }
        }
 
-       private void setDebugConnection(XDebugConnection connection) {
-               if (connection != null) {
+       private boolean setDebugConnection(XDebugConnection connection) {
+               if (connection != null && fDebugConnection == null) {
                        fDebugConnection = connection;
                        fResponseListener = new ResponseListener(connection);
                        startListener();
+                       
+                       return true;
+               } else {
+                       connection.close();
+                       
+                       return false;
                }
        }