|
1 /* |
|
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Definitions for the class ParseXMLFileSAX |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.s60tools.analyzetool.engine; |
|
19 |
|
20 import java.io.File; |
|
21 import java.io.IOException; |
|
22 import java.util.AbstractList; |
|
23 import java.util.ArrayList; |
|
24 |
|
25 import javax.xml.parsers.ParserConfigurationException; |
|
26 import javax.xml.parsers.SAXParser; |
|
27 import javax.xml.parsers.SAXParserFactory; |
|
28 |
|
29 import org.eclipse.core.resources.IProject; |
|
30 import org.xml.sax.Attributes; |
|
31 import org.xml.sax.InputSource; |
|
32 import org.xml.sax.Locator; |
|
33 import org.xml.sax.SAXException; |
|
34 import org.xml.sax.XMLReader; |
|
35 |
|
36 /** |
|
37 * Parses atool.exe generated xml file and creates memory analysis results. |
|
38 * Using the SAX parser to parse xml file. SAX parser fits better than DOM |
|
39 * parser because it is possible that AT must handle very big xml files. |
|
40 * |
|
41 * @author kihe |
|
42 * |
|
43 */ |
|
44 public class ParseXMLFileSAX implements org.xml.sax.ContentHandler { |
|
45 /** XML constants. */ |
|
46 public static final String XML_BUILD_TARGET = "build_target"; |
|
47 public static final String XML_CALC_MEMADDR = "calc_addr"; |
|
48 public static final String XML_CALLSTACK = "callstack"; |
|
49 public static final String XML_COUNT = "count"; |
|
50 public static final String XML_END_TIME = "end_time"; |
|
51 public static final String XML_FILE = "file"; |
|
52 public static final String XML_FUNCTION = "function"; |
|
53 public static final String XML_FUNCTION_LINE = "function_line"; |
|
54 public static final String XML_HANDLE_LEAKS = "handle_leaks"; |
|
55 public static final String XML_ID = "id"; |
|
56 public static final String XML_ITEM = "item"; |
|
57 public static final String XML_LEAK = "leak"; |
|
58 public static final String XML_LEAKS = "leaks"; |
|
59 public static final String XML_LINE = "line"; |
|
60 public static final String XML_MEM_LEAKS = "mem_leaks"; |
|
61 public static final String XML_MEMADDR = "memaddress"; |
|
62 public static final String XML_MODULE = "module"; |
|
63 public static final String XML_NAME = "name"; |
|
64 public static final String XML_PROCESS_NAME = "process_name"; |
|
65 public static final String XML_RUN = "run"; |
|
66 public static final String XML_SIZE = "size"; |
|
67 public static final String XML_START_TIME = "start_time"; |
|
68 public static final String XML_SUBTEST = "subtest"; |
|
69 public static final String XML_TIME = "time"; |
|
70 public static final String XML_UNKNOWN = "???"; |
|
71 |
|
72 /**Active call stack item id*/ |
|
73 private int activeCallstackID = 1; |
|
74 |
|
75 /** Active item(memory leak) information */ |
|
76 private AnalysisItem activeItem; |
|
77 |
|
78 /**Active item(memory leak) id*/ |
|
79 private int activeItemID = 1; |
|
80 |
|
81 /** Summary field active module memory leak count*/ |
|
82 private String activeModuleCount; |
|
83 |
|
84 /** Summary field active module name*/ |
|
85 private String activeModuleName; |
|
86 |
|
87 /** Active run information */ |
|
88 protected RunResults activeRunResults; |
|
89 |
|
90 /** Contains all the runs parsed results */ |
|
91 private final AbstractList<RunResults> activeRuns; |
|
92 |
|
93 /** Active subtest item information */ |
|
94 private Subtest activeSubtest; |
|
95 |
|
96 /**Active subtest id*/ |
|
97 private int activeSubtestID = 1; |
|
98 |
|
99 /** Active callstack item information */ |
|
100 private CallstackItem callstackItem; |
|
101 |
|
102 /** xml file path. */ |
|
103 private final String filePath; |
|
104 |
|
105 /** Do we parsing module leak summary field */ |
|
106 private boolean moduleLeakActive = true; |
|
107 |
|
108 /** List of modules */ |
|
109 private final AbstractList<String> moduleList; |
|
110 |
|
111 /** Do we parsing subtest info just now */ |
|
112 private boolean parsingSubtest = false; |
|
113 |
|
114 /** Project reference */ |
|
115 private final IProject project; |
|
116 |
|
117 /** Project result reference where to save results */ |
|
118 private final ProjectResults results; |
|
119 |
|
120 /** Index for active items */ |
|
121 private int runID = 1; |
|
122 |
|
123 |
|
124 /** |
|
125 * Constructor. |
|
126 * |
|
127 * @param projectRef |
|
128 * Project reference |
|
129 * @param newFilePath |
|
130 * Used file path |
|
131 * @param projResults |
|
132 * Project results reference |
|
133 */ |
|
134 public ParseXMLFileSAX( |
|
135 final org.eclipse.core.resources.IProject projectRef, |
|
136 final String newFilePath, final ProjectResults projResults) { |
|
137 results = projResults; |
|
138 filePath = newFilePath; |
|
139 project = projectRef; |
|
140 activeRuns = new ArrayList<RunResults>(); |
|
141 moduleList = new ArrayList<String>(); |
|
142 } |
|
143 |
|
144 /** |
|
145 * Adds module name to list |
|
146 * |
|
147 * @param moduleName |
|
148 * Module to add to the list |
|
149 */ |
|
150 private void addModuleNameToList(final String moduleName) { |
|
151 if (moduleName == null || ("").equals(moduleName)) { |
|
152 return; |
|
153 } |
|
154 if (!moduleList.contains(moduleName)) { |
|
155 moduleList.add(moduleName); |
|
156 } |
|
157 } |
|
158 |
|
159 public void characters(char[] ch, int start, int length) { |
|
160 // Do nothing be design, because AT is not interested of this |
|
161 // information |
|
162 // also this method is overloaded |
|
163 } |
|
164 |
|
165 /** |
|
166 * When the SAX parser reach the end of the XML file this function is |
|
167 * called. |
|
168 * |
|
169 * Updates results. |
|
170 */ |
|
171 public void endDocument() throws SAXException { |
|
172 results.updateRunResults(project, activeRuns, filePath); |
|
173 } |
|
174 |
|
175 /** |
|
176 * When the SAX parser reach the end of the xml tag this function is called. |
|
177 * |
|
178 * Checks what tag read is finished and stores corresponding results |
|
179 */ |
|
180 public void endElement(String uri, String localName, String name) |
|
181 throws SAXException { |
|
182 // "results" tag => store results |
|
183 if (name.equals(XML_RUN)) { |
|
184 activeRuns.add(activeRunResults); |
|
185 } else if (name.equals(XML_LEAK)) { |
|
186 |
|
187 //not enough information so clear the active item |
|
188 if( activeItem == null || !activeItem.checkData() ) { |
|
189 activeItem = null; |
|
190 } |
|
191 else if (parsingSubtest) { |
|
192 activeSubtest.addAnalysisItem(activeItem); |
|
193 } else { |
|
194 activeRunResults.addAnalysisItem(activeItem); |
|
195 } |
|
196 } else if (name.equals(XML_ITEM)) { |
|
197 if( callstackItem.isEmpty() ) { |
|
198 callstackItem = null; |
|
199 } |
|
200 else { |
|
201 activeItem.addCallstackItem(callstackItem); |
|
202 } |
|
203 } else if (name.equals(XML_SUBTEST)) { |
|
204 activeRunResults.addSubtest(activeSubtest); |
|
205 parsingSubtest = false; |
|
206 } else if (name.equals(XML_MODULE)) { |
|
207 |
|
208 try { |
|
209 // check that module count value is valid |
|
210 if (activeModuleCount == null || ("").equals(activeModuleCount)) { |
|
211 moduleLeakActive = true; |
|
212 return; |
|
213 } |
|
214 |
|
215 // convert module count information to string object |
|
216 int count = Integer.parseInt(activeModuleCount,16); |
|
217 |
|
218 // if module leak summary is active |
|
219 if (moduleLeakActive) { |
|
220 activeRunResults.addModuleLeak(activeModuleName, count); |
|
221 } |
|
222 // if handle leak summary is active |
|
223 else { |
|
224 activeRunResults.addHandleLeak(activeModuleName, count); |
|
225 } |
|
226 |
|
227 }catch( NumberFormatException npe ) { |
|
228 npe.printStackTrace(); |
|
229 } |
|
230 } |
|
231 } |
|
232 |
|
233 public void endPrefixMapping(String prefix) throws SAXException { |
|
234 // Do nothing be design, because AT is not interested of this |
|
235 // information |
|
236 // also this method is overloaded |
|
237 } |
|
238 |
|
239 /** |
|
240 * Returns list of modules |
|
241 * |
|
242 * @return List of modules |
|
243 */ |
|
244 public AbstractList<String> getModules() { |
|
245 return moduleList; |
|
246 } |
|
247 |
|
248 public void ignorableWhitespace(char[] ch, int start, int length) |
|
249 throws SAXException { |
|
250 // Do nothing be design, because AT is not interested of this |
|
251 // information |
|
252 // also this method is overloaded |
|
253 } |
|
254 |
|
255 /** |
|
256 * Parses xml file content. |
|
257 * |
|
258 * @return True if there are no errors while parsing XML file otherwise |
|
259 * False |
|
260 */ |
|
261 public final boolean parse() { |
|
262 |
|
263 boolean ret = false; |
|
264 try { |
|
265 |
|
266 // check that file exists |
|
267 File file = new File(filePath); |
|
268 if (file.exists()) { |
|
269 |
|
270 SAXParserFactory factor = null; |
|
271 SAXParser parser = null; |
|
272 XMLReader xmlReader = null; |
|
273 |
|
274 // get parser factory |
|
275 factor = SAXParserFactory.newInstance(); |
|
276 |
|
277 if (factor != null) { |
|
278 // get xml parser |
|
279 parser = factor.newSAXParser(); |
|
280 |
|
281 if (parser != null) { |
|
282 // get xml reader |
|
283 xmlReader = parser.getXMLReader(); |
|
284 |
|
285 if (xmlReader != null) { |
|
286 // set content handle to this class |
|
287 xmlReader.setContentHandler(this); |
|
288 |
|
289 // parse xml file |
|
290 xmlReader.parse(new InputSource(file.getPath())); |
|
291 ret = true; |
|
292 } |
|
293 } |
|
294 } |
|
295 } |
|
296 |
|
297 // file does not exists |
|
298 else { |
|
299 ret = false; |
|
300 } |
|
301 |
|
302 //check that XML file contains data |
|
303 //otherwise return false |
|
304 if( activeRunResults == null ) { |
|
305 ret = false; |
|
306 } |
|
307 } catch (SAXException sae) { |
|
308 sae.printStackTrace(); |
|
309 ret = false; |
|
310 } catch (ParserConfigurationException pce) { |
|
311 pce.printStackTrace(); |
|
312 ret = false; |
|
313 } catch (IOException ioe) { |
|
314 ioe.printStackTrace(); |
|
315 ret = false; |
|
316 } |
|
317 |
|
318 // no errors => return positive value |
|
319 return ret; |
|
320 } |
|
321 |
|
322 public void processingInstruction(String target, String data) |
|
323 throws SAXException { |
|
324 // Do nothing be design, because AT is not interested of this |
|
325 // information |
|
326 // also this method is overloaded |
|
327 } |
|
328 |
|
329 public void setDocumentLocator(Locator locator) { |
|
330 // Do nothing be design, because AT is not interested of this |
|
331 // information |
|
332 // also this method is overloaded |
|
333 } |
|
334 |
|
335 public void skippedEntity(String name) throws SAXException { |
|
336 // Do nothing be design, because AT is not interested of this |
|
337 // information |
|
338 // also this method is overloaded |
|
339 } |
|
340 |
|
341 public void startDocument() throws SAXException { |
|
342 // Do nothing be design, because AT is not interested of this |
|
343 // information |
|
344 // also this method is overloaded |
|
345 } |
|
346 |
|
347 /** |
|
348 * When the SAX parser start to read xml tag this function is called. |
|
349 * |
|
350 * Checks what tag read is started and initializes corresponding objects |
|
351 */ |
|
352 public void startElement(String uri, String localName, String name, |
|
353 Attributes atts) throws SAXException { |
|
354 |
|
355 //parse one run information |
|
356 if (name.equals(XML_RUN)) { |
|
357 activeRunResults = new RunResults(runID); |
|
358 activeRunResults.setEndTime(atts.getValue(XML_END_TIME)); |
|
359 activeRunResults.setBuildTarget(atts.getValue(XML_BUILD_TARGET)); |
|
360 activeRunResults.setProcessName(atts.getValue(XML_PROCESS_NAME)); |
|
361 activeRunResults.setStartTime(atts.getValue(XML_START_TIME)); |
|
362 runID++; |
|
363 activeItemID = 1; |
|
364 activeSubtestID = 1; |
|
365 } |
|
366 //parse one memory leak information |
|
367 else if (name.equals(XML_LEAK)) { |
|
368 try { |
|
369 activeItem = new AnalysisItem(); |
|
370 activeItem.setID(activeItemID); |
|
371 activeItem.setMemoryAddress(atts.getValue(XML_MEMADDR)); |
|
372 activeItem.setMemoryLeakTime(atts.getValue(XML_TIME)); |
|
373 String moduleName = atts.getValue(XML_MODULE); |
|
374 addModuleNameToList(moduleName); |
|
375 activeItem.setModuleName(moduleName); |
|
376 String size = atts.getValue(XML_SIZE); |
|
377 if (size != null && !("").equals(size)) { |
|
378 activeItem.setLeakSize(Integer |
|
379 .parseInt(size)); |
|
380 } |
|
381 |
|
382 }catch(NumberFormatException npe) { |
|
383 npe.printStackTrace(); |
|
384 } |
|
385 activeItemID++; |
|
386 activeCallstackID = 1; |
|
387 } |
|
388 //parse call stack item information |
|
389 else if (name.equals(XML_ITEM)) { |
|
390 try{ |
|
391 //create new item |
|
392 callstackItem = new CallstackItem(); |
|
393 callstackItem.setID(activeCallstackID); |
|
394 callstackItem.setFileName(atts.getValue(XML_FILE)); |
|
395 callstackItem.setFunctionName(atts.getValue(XML_FUNCTION)); |
|
396 callstackItem.setMemoryAddress(atts.getValue(XML_MEMADDR)); |
|
397 |
|
398 //get module name and add it to list |
|
399 //module name list is used later. |
|
400 String moduleName = atts.getValue(XML_MODULE); |
|
401 addModuleNameToList(moduleName); |
|
402 callstackItem.setModuleName(moduleName); |
|
403 |
|
404 //if line number is added to the XML file |
|
405 //this means that the results are generated to UREL |
|
406 String lineNumber = atts.getValue(XML_LINE); |
|
407 if (lineNumber != null && !("").equals(lineNumber) && !(XML_UNKNOWN).equals(lineNumber)) { |
|
408 callstackItem.setLeakLineNumber(Integer.parseInt(lineNumber)); |
|
409 } |
|
410 |
|
411 //if function line is added to the XML file |
|
412 //this means that the results are generated to UREL |
|
413 String functionLine = atts.getValue(XML_FUNCTION_LINE); |
|
414 if (functionLine != null && !("").equals(functionLine) && !(XML_UNKNOWN).equals(functionLine)) { |
|
415 callstackItem.setLeakLineNumber(Integer.parseInt(functionLine)); |
|
416 callstackItem.setUrelBuild(true); |
|
417 } |
|
418 }catch(NumberFormatException npe) { |
|
419 npe.printStackTrace(); |
|
420 } |
|
421 activeCallstackID++; |
|
422 |
|
423 } |
|
424 //parse subtest information |
|
425 else if (name.equals(XML_SUBTEST)) { |
|
426 activeSubtest = new Subtest(activeSubtestID); |
|
427 activeItemID = 1; |
|
428 activeCallstackID = 1; |
|
429 activeSubtest.setEndTime(atts.getValue(XML_END_TIME)); |
|
430 activeSubtest.setName(atts.getValue(XML_NAME)); |
|
431 activeSubtest.setStartTime(atts.getValue(XML_START_TIME)); |
|
432 parsingSubtest = true; |
|
433 activeSubtestID++; |
|
434 } else if (name.equals(XML_HANDLE_LEAKS)) { |
|
435 moduleLeakActive = false; |
|
436 } else if (name.equals(XML_MEM_LEAKS)) { |
|
437 moduleLeakActive = true; |
|
438 } else if (name.equals(XML_MODULE)) { |
|
439 activeModuleName = atts.getValue(XML_NAME); |
|
440 activeModuleCount = atts.getValue(XML_LEAKS); |
|
441 } |
|
442 |
|
443 } |
|
444 |
|
445 public void startPrefixMapping(String prefix, String uri) |
|
446 throws SAXException { |
|
447 // Do nothing be design, because AT is not interested of this |
|
448 // information |
|
449 // also this method is overloaded |
|
450 } |
|
451 } |