64dbc326581755eebec6cc3e287b857e49df84c7
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / model / XDebugWatchExpressionDelegate.java
1 package net.sourceforge.phpeclipse.xdebug.php.model;
2
3
4 import net.sourceforge.phpeclipse.xdebug.core.Base64;
5 import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugConnection;
6 import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.DebugResponse;
7 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugVariable;
8 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
9 import org.eclipse.debug.core.model.IDebugElement;
10 import org.eclipse.debug.core.model.IWatchExpressionDelegate;
11 import org.eclipse.debug.core.model.IWatchExpressionListener;
12 import org.eclipse.debug.core.model.IWatchExpressionResult;
13 import org.w3c.dom.Node;
14
15 public class XDebugWatchExpressionDelegate implements IWatchExpressionDelegate {
16         public void evaluateExpression(String expression, IDebugElement context, IWatchExpressionListener listener) {
17                 IWatchExpressionResult x;
18                 XDebugConnection connection;
19                 XDebugTarget s;
20
21                 x = new XDebugWatchExpressionResult(expression, null, null);
22                 
23                 s = (XDebugTarget) context.getDebugTarget();
24                 connection = (XDebugConnection) s.getDebugConnection();
25
26                 if( connection != null ) {
27                         try {
28                                 if( ! connection.isClosed() ) {
29                                         String encoded = Base64.encodeBytes(expression.getBytes());
30                                         DebugResponse evalCommand = connection.sendRequestA( "eval", "-- "+encoded );
31                                         
32                                         Node evalNode = evalCommand.getParentNode();
33                                         XDebugVariable var= connection.getVariableFromNodeA( null, evalNode.getFirstChild());
34                                         XDebugVariable result[] = {var};
35                                         
36                                         if (result.length == 0) {
37                                                 x = new XDebugWatchExpressionResult(expression, null, null);
38                                         } else {
39                                                 x = new XDebugWatchExpressionResult(expression, result[0].getValue(), null);
40                                         }
41                                 }
42                         } catch (Exception e) {
43                                 String[] s1;
44         
45                                 s1 = new String[1];
46                                 s1[0] = e.toString();
47                                 x = new XDebugWatchExpressionResult(expression, null, s1);
48                         }
49                 }
50
51                 listener.watchEvaluationFinished(x);
52         }
53 }