Package: DbgpXmlParser

DbgpXmlParser

nameinstructionbranchcomplexitylinemethod
DbgpXmlParser()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
checkError(Element)
M: 40 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getIntAttribute(Element, String, int)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getStringAttribute(Element, String)
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
makeBoolean(String)
M: 7 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
parseBase64Content(Element)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
parseColumn(String)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
parseContent(Element)
M: 29 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
parseLine(String)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
parseSuccess(Element)
M: 11 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
parseXml(byte[])
M: 35 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.core.dbgp.internal.utils;
23:
24: import java.io.ByteArrayInputStream;
25: import java.io.IOException;
26:
27: import javax.xml.parsers.DocumentBuilder;
28: import javax.xml.parsers.DocumentBuilderFactory;
29: import javax.xml.parsers.ParserConfigurationException;
30:
31: import org.overture.ide.debug.core.dbgp.exceptions.DbgpProtocolException;
32: import org.w3c.dom.Document;
33: import org.w3c.dom.Element;
34: import org.w3c.dom.Node;
35: import org.w3c.dom.NodeList;
36: import org.xml.sax.InputSource;
37: import org.xml.sax.SAXException;
38: //import org.eclipse.dltk.compiler.util.Util;
39: import org.overture.ide.debug.core.dbgp.exceptions.DbgpDebuggingEngineException;
40:
41: public class DbgpXmlParser
42: {
43:         protected DbgpXmlParser()
44:         {
45:         }
46:
47:         protected static int parseLine(String s)
48:         {
49:                 final int index = s.indexOf(':');
50:•                if (index < 0)
51:                 {
52:                         return -1;
53:                 }
54:                 return Integer.parseInt(s.substring(0, index));
55:         }
56:
57:         protected static int parseColumn(String s)
58:         {
59:                 final int index = s.indexOf(':');
60:•                if (index < 0)
61:                 {
62:                         return -1;
63:                 }
64:                 return Integer.parseInt(s.substring(index + 1));
65:         }
66:
67:         protected static boolean makeBoolean(String s)
68:         {
69:•                return Integer.parseInt(s) == 0 ? false : true;
70:         }
71:
72:         public static Document parseXml(byte[] xml) throws DbgpProtocolException
73:         {
74:                 try
75:                 {
76:                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
77:
78:                         DocumentBuilder builder = factory.newDocumentBuilder();
79:
80:                         // builder.setErrorHandler(new ErrorHandler() {
81:                         // public void error(SAXParseException exception)
82:                         // throws SAXException {
83:                         // }
84:                         //
85:                         // public void fatalError(SAXParseException exception)
86:                         // throws SAXException {
87:                         // }
88:                         //
89:                         // public void warning(SAXParseException exception)
90:                         // throws SAXException {
91:                         // }
92:                         // });
93:
94:                         InputSource source = new InputSource(new ByteArrayInputStream(xml));
95:                         return builder.parse(source);
96:                 } catch (ParserConfigurationException e)
97:                 {
98:                         throw new DbgpProtocolException(e);
99:                 } catch (SAXException e)
100:                 {
101:                         throw new DbgpProtocolException(e);
102:                 } catch (IOException e)
103:                 {
104:                         throw new DbgpProtocolException(e);
105:                 }
106:         }
107:
108:         protected static String parseContent(Element element)
109:         {
110:                 NodeList list = element.getChildNodes();
111:•                for (int i = 0; i < list.getLength(); ++i)
112:                 {
113:                         Node e = list.item(i);
114:                         int type = e.getNodeType();
115:
116:•                        if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE)
117:                         {
118:                                 return e.getNodeValue();
119:                         }
120:                 }
121:
122:                 return Util.EMPTY_STRING;
123:         }
124:
125:         public static String parseBase64Content(Element element)
126:         {
127:                 return Base64Helper.decodeString(parseContent(element));
128:         }
129:
130:         public static DbgpDebuggingEngineException checkError(Element element)
131:         {
132:                 final String TAG_ERROR = "error"; //$NON-NLS-1$
133:                 final String TAG_MESSAGE = "message"; //$NON-NLS-1$
134:                 final String ATTR_CODE = "code"; //$NON-NLS-1$
135:
136:                 NodeList errors = element.getElementsByTagName(TAG_ERROR);
137:•                if (errors.getLength() > 0)
138:                 {
139:                         Element error = (Element) errors.item(0);
140:                         int errorCode = Integer.parseInt(error.getAttribute(ATTR_CODE));
141:
142:                         String errorText = "No message"; //$NON-NLS-1$
143:                         NodeList messages = error.getElementsByTagName(TAG_MESSAGE);
144:
145:•                        if (messages.getLength() > 0)
146:                         {
147:                                 errorText = parseContent((Element) messages.item(0));
148:                         }
149:
150:                         return new DbgpDebuggingEngineException(errorCode, errorText);
151:                 }
152:
153:                 return null;
154:         }
155:
156:         public static boolean parseSuccess(Element response)
157:         {
158:                 final String ATTR_SUCCESS = "success"; //$NON-NLS-1$
159:
160:                 // Strange assumption but it's required for compatibility
161:•                if (!response.hasAttribute(ATTR_SUCCESS))
162:                 {
163:                         return true;
164:                 }
165:
166:                 return makeBoolean(response.getAttribute(ATTR_SUCCESS));
167:         }
168:
169:         protected static String getStringAttribute(Element element, String name)
170:         {
171:
172:                 String value = ""; //$NON-NLS-1$
173:•                if (element.hasAttribute(name))
174:                 {
175:                         value = element.getAttribute(name);
176:                 }
177:
178:                 return value;
179:         }
180:
181:         protected static int getIntAttribute(Element element, String name,
182:                         int defaultValue)
183:         {
184:
185:                 int value = defaultValue;
186:
187:•                if (element.hasAttribute(name))
188:                 {
189:                         try
190:                         {
191:                                 value = Integer.parseInt(element.getAttribute(name));
192:                         } catch (NumberFormatException e)
193:                         {
194:                                 // TODO: this should probably be logged
195:                         }
196:                 }
197:
198:                 return value;
199:         }
200:
201: }