Change to openFileInTextEditor to fix ticket issue #650.
[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.PHPDebugUtils;
6 import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugConnection;
7 import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.DebugResponse;
8 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugVariable;
9 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
10
11 import org.eclipse.debug.core.DebugException;
12 import org.eclipse.debug.core.model.IDebugElement;
13 import org.eclipse.debug.core.model.IVariable;
14 import org.eclipse.debug.core.model.IWatchExpressionDelegate;
15 import org.eclipse.debug.core.model.IWatchExpressionListener;
16 import org.eclipse.debug.core.model.IWatchExpressionResult;
17 import org.w3c.dom.Node;
18 import org.w3c.dom.NodeList;
19
20 public class XDebugWatchExpressionDelegate implements IWatchExpressionDelegate {
21         public void evaluateExpression(String expression, IDebugElement context, IWatchExpressionListener listener) {
22                 IWatchExpressionResult x;
23                 XDebugConnection connection;
24                 XDebugTarget s;
25
26                 x = new XDebugWatchExpressionResult(expression, null, null);
27                 
28                 s = (XDebugTarget) context.getDebugTarget();
29                 connection = (XDebugConnection) s.getDebugConnection();
30
31                 if( connection != null ) {
32                         try {
33                                 if( ! connection.isClosed() ) {
34                                         DebugResponse evalCommand = connection.eval(expression);
35                                         
36                                         Node evalNode = evalCommand.getParentNode();
37                                         XDebugVariable var= /*connection.*/getVariableFromNodeA( null, evalNode.getFirstChild());
38                                         XDebugVariable result[] = {var};
39                                         
40                                         if (result.length == 0) {
41                                                 x = new XDebugWatchExpressionResult(expression, null, null);
42                                         } else {
43                                                 x = new XDebugWatchExpressionResult(expression, result[0].getValue(), null);
44                                         }
45                                 }
46                         } catch (Exception e) {
47                                 String[] s1;
48         
49                                 s1 = new String[1];
50                                 s1[0] = e.toString();
51                                 x = new XDebugWatchExpressionResult(expression, null, s1);
52                         }
53                 }
54
55                 listener.watchEvaluationFinished(x);
56         }
57         
58         private XDebugVariable getVariableFromNodeA(XDebugStackFrame frame, Node property) {
59                 String address = PHPDebugUtils.getAttributeValue(property, "address");
60                 String varName;
61                 String Name = PHPDebugUtils.getAttributeValue(property,"name");
62                 if ("".equals(Name)) {
63                         varName = address;              
64                 } else {
65                         varName = Name;
66                 }
67
68                 String varEncoding=PHPDebugUtils.getAttributeValue(property,"encoding");
69                 int varNumChildren = 0;
70                 if (PHPDebugUtils.getAttributeValue(property,"numchildren").equals(""))
71                         varNumChildren = 0;
72                 else
73                         varNumChildren=Integer.parseInt(PHPDebugUtils.getAttributeValue(property,"numchildren"));
74
75                 String typeName=PHPDebugUtils.getAttributeValue(property,"type");
76
77                 XDebugVariable variable = new XDebugVariable(typeName, varName);
78                 variable.setEncoding(varEncoding);
79                 variable.setNumChildren(varNumChildren);
80                 XDebugAbstractValue val=null;
81                 try {
82                         val = (XDebugAbstractValue) variable.getValue();
83                 } catch (DebugException e1) {
84                         e1.printStackTrace();
85                 }
86                 if (val.getType()!= XDebugAbstractValue.VALUETYPE_UNINITIALIZED) {
87                         if (variable.hasChildren()) {
88                                 NodeList varNodes = property.getChildNodes();
89                                 val.renderValueString(""+varNodes.getLength());
90                                 IVariable[] variables = new IVariable[varNodes.getLength()];
91                                 for (int i = 0; i<varNodes.getLength(); i++) {
92                                         Node propertyNode = varNodes.item(i);
93                                         variables[i] = getVariableFromNodeA(frame, propertyNode);
94                                 }
95                                 val.setChildVariables(variables);
96                         }else {
97                                 String str="";
98                                 try {
99                                         str=property.getFirstChild().getNodeValue();
100                                 } catch (NullPointerException e) {
101                                         str="";
102                                 }
103                                 if (variable.getEncoding().equals("base64")) {
104                                         if (str.length()!=0)
105                                                 str=new String(Base64.decode(str));
106                                         else
107                                                 str="";
108                                 }
109                                 val.renderValueString(str);
110                         }
111                         
112                         String className=PHPDebugUtils.getAttributeValue(property,"classname");
113                         if(!"".equals(className))
114                                 val.renderValueString(className);
115                 }
116                 return variable;
117                 
118         }
119 }