1 package net.sourceforge.phpeclipse.xdebug.core.xdebug;
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
6 import javax.xml.parsers.DocumentBuilder;
7 import javax.xml.parsers.DocumentBuilderFactory;
8 import javax.xml.parsers.ParserConfigurationException;
10 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
11 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
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;
20 public class XDebugResponse {
21 final public static String TYPE_INIT = "init";
23 final public static String TYPE_RESPONSE = "response";
25 final public static String TYPE_STREAM = "stream";
27 private Node parentNode;
28 private int fTransactionID = -1;
29 private String fCommand = "";
30 private String fStatus;
31 private String fReason;
33 private boolean fError;
35 private String fValue;
37 private String fAddress;
38 private String fIdeKey;
40 public XDebugResponse(String XMLInput) {
46 setParentNode(XMLInput);
49 private synchronized void setParentNode(String xmlInput) {
50 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
51 DocumentBuilder builder = null;
55 builder = factory.newDocumentBuilder();
56 } catch (ParserConfigurationException e) {
59 ByteArrayInputStream InputXMLStream = new ByteArrayInputStream(xmlInput.getBytes());
61 doc = builder.parse(InputXMLStream);
62 } catch (SAXException e) {
64 } catch (IOException e) {
68 parentNode = doc.getFirstChild();
70 String responseType = parentNode.getNodeName();
71 if (responseType == TYPE_INIT) {
73 parseInit(parentNode);
74 } else if (responseType == TYPE_RESPONSE) {
75 fName = TYPE_RESPONSE;
76 parseResponse(parentNode);
77 } else if (responseType == TYPE_STREAM) {
85 private void parseInit(Node parentNode) {
86 fIdeKey = getAttributeValue("idekey");
88 /*int startIdx = initString.indexOf("idekey=\"");
92 int endIdx=initString.indexOf('"',startIdx);
95 fSessionID = initString.substring(startIdx,endIdx);*/
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);
115 fStatus = getAttributeValue("status");
116 fReason = getAttributeValue("reason");
118 if( fCommand.compareTo("eval") == 0 ) {
120 Node property = parentNode.getFirstChild();
122 NamedNodeMap listAttribute = property.getAttributes();
123 Node attribute = listAttribute.getNamedItem("type");
124 if (attribute !=null) {
125 fType = attribute.getNodeValue();
128 Node attribute1 = listAttribute.getNamedItem("address");
129 if (attribute1 !=null) {
130 fAddress = attribute1.getNodeValue();
133 Node firstChild1 = (Node) property.getFirstChild();
135 if( firstChild1 != null ) {
136 fValue = firstChild1.getNodeValue();
140 } catch (Exception e) {
141 // TODO: handle exception
145 CDATASection firstChild = (CDATASection) parentNode.getFirstChild();
147 if( firstChild != null ) {
148 fValue = parentNode.getFirstChild().getNodeValue();
150 } catch (Exception e) {
156 private void parseStream() {
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();
172 public synchronized Node getParentNode(){
176 public /*synchronized*/ String getCommand() {
180 /*private*/public /*synchronized*/ String getName() {
184 public synchronized String getValue() {
188 public synchronized String getReason() {
192 public synchronized String getStatus() {
196 public synchronized int getTransactionID() {
197 return fTransactionID;
200 public boolean isError() {