Separate XDebugResponse from ResponseListner.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / core / xdebug / XDebugResponse.java
1 package net.sourceforge.phpeclipse.xdebug.core.xdebug;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5
6 import javax.xml.parsers.DocumentBuilder;
7 import javax.xml.parsers.DocumentBuilderFactory;
8 import javax.xml.parsers.ParserConfigurationException;
9
10 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
11 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.w3c.dom.CDATASection;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.NamedNodeMap;
17 import org.w3c.dom.Node;
18 import org.xml.sax.SAXException;
19
20 public class XDebugResponse {
21         final public static String TYPE_INIT = "init";
22         
23         final public static String TYPE_RESPONSE = "response";
24         
25         final public static String TYPE_STREAM = "stream";
26
27         private Node parentNode;
28         private int fTransactionID = -1;
29         private String fCommand = "";
30         private String fStatus;
31         private String fReason;
32         private String fName;
33         private boolean  fError;
34
35         private String fValue;
36         private String fType;
37         private String fAddress;
38         private String fIdeKey;
39                 
40         public XDebugResponse(String XMLInput) {
41                 fTransactionID = -1;
42                 fCommand = "";
43                 fStatus = "";
44                 fReason = "";                   
45                 fName= "";
46                 setParentNode(XMLInput);
47         }
48
49         private synchronized void setParentNode(String xmlInput) {
50                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
51                 DocumentBuilder builder = null;
52                 Document doc = null;
53                 
54                 try {
55                         builder = factory.newDocumentBuilder();
56                 } catch (ParserConfigurationException e) {
57                         e.printStackTrace();
58                 }
59                 ByteArrayInputStream InputXMLStream = new ByteArrayInputStream(xmlInput.getBytes());
60                 try {
61                         doc = builder.parse(InputXMLStream);
62                 } catch (SAXException e) {
63                         e.printStackTrace();
64                 } catch (IOException e) {
65                         e.printStackTrace();
66                 }
67
68                 parentNode = doc.getFirstChild();
69                 
70                 String responseType = parentNode.getNodeName();
71                 if (responseType == TYPE_INIT) {
72                         fName = TYPE_INIT;
73                         parseInit(parentNode);
74                 } else if (responseType == TYPE_RESPONSE) {
75                         fName = TYPE_RESPONSE;
76                         parseResponse(parentNode);
77                 } else if (responseType == TYPE_STREAM) {
78                         fName = TYPE_STREAM;
79                         parseStream();
80                 } else {
81                         fName = null;
82                 }
83         }
84         
85         private void parseInit(Node parentNode) {
86                 fIdeKey = getAttributeValue("idekey");
87                 
88                 /*int startIdx = initString.indexOf("idekey=\"");
89                 if (startIdx == -1)
90                         return;
91                 startIdx += 8;
92                 int endIdx=initString.indexOf('"',startIdx);
93                 if (endIdx==-1)
94                         return;
95                 fSessionID = initString.substring(startIdx,endIdx);*/
96         }
97         
98         private void parseResponse(Node parentNode) {
99                 String idStr = getAttributeValue("transaction_id");
100                 if (!"".equals(idStr))
101                         fTransactionID = Integer.parseInt(idStr);
102                 fCommand = getAttributeValue("command");
103                 if (parentNode.hasChildNodes()) {
104                         Node child = parentNode.getFirstChild();
105                         if (child.getNodeName().equals("error")) {
106                                 int code = Integer.parseInt(PHPDebugUtils.getAttributeValue(child, "code"));
107                                 String text = (child.getFirstChild()).getNodeValue();
108                                 XDebugCorePlugin.log(IStatus.ERROR," ERROR "+code+": "+text);
109                                 fError = true;
110                                 return;
111                         }
112                 }
113                 fError = false;
114                 
115                 fStatus = getAttributeValue("status");
116                 fReason = getAttributeValue("reason");
117
118                 if( fCommand.compareTo("eval") == 0 ) {
119                         try {
120                                 Node property = parentNode.getFirstChild();
121
122                                 NamedNodeMap listAttribute = property.getAttributes();
123                                 Node attribute = listAttribute.getNamedItem("type");
124                                 if (attribute !=null) {
125                                         fType = attribute.getNodeValue();
126                                 }
127
128                                 Node attribute1 = listAttribute.getNamedItem("address");
129                                 if (attribute1 !=null) {
130                                         fAddress = attribute1.getNodeValue();
131                                 }
132                                 
133                                 Node firstChild1 = (Node) property.getFirstChild();
134                                 
135                                 if( firstChild1 != null ) {
136                                         fValue = firstChild1.getNodeValue();
137                                 } else {
138                                         fValue = "";
139                                 }
140                         } catch (Exception e) {
141                                 // TODO: handle exception
142                         }
143                 } else {
144                         try {
145                                 CDATASection firstChild = (CDATASection) parentNode.getFirstChild();
146
147                                 if( firstChild != null ) {
148                                         fValue = parentNode.getFirstChild().getNodeValue();
149                                 }
150                         } catch (Exception e) {
151                         }
152                 }
153                 
154         }
155         
156         private void parseStream() {
157                 
158         }
159         
160         
161         public String getAttributeValue (String AttributeName) {
162                 String strValue = "";
163                 if (parentNode.hasAttributes()) {
164                         NamedNodeMap listAttribute = parentNode.getAttributes();
165                         Node attribute = listAttribute.getNamedItem(AttributeName);
166                         if (attribute !=null)
167                                 strValue = attribute.getNodeValue();
168                 }
169                 return strValue;
170         }
171         
172         public synchronized Node getParentNode(){
173                 return parentNode;
174         }
175         
176         public /*synchronized*/ String getCommand() {
177                 return fCommand;
178         }
179         
180         /*private*/public /*synchronized*/ String getName() {
181                 return fName;
182         }
183         
184         public synchronized String getValue() {
185                 return fValue;
186         }
187
188         public synchronized String getReason() {
189                 return fReason;
190         }
191
192         public synchronized String getStatus() {
193                 return fStatus;
194         }
195
196         public synchronized int getTransactionID() {
197                 return fTransactionID;
198         }
199         
200         public boolean isError() {
201                 return fError;
202         }
203 }