sysperfana/memspyext/com.nokia.s60tools.memspy/src/com/nokia/s60tools/memspy/model/AnalyserXMLGenerator.java
changeset 7 8e12a575a9b5
equal deleted inserted replaced
6:f65f740e69f9 7:8e12a575a9b5
       
     1 /*
       
     2 * Copyright (c) 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 package com.nokia.s60tools.memspy.model;
       
    21 
       
    22 import java.io.BufferedWriter;
       
    23 import java.io.IOException;
       
    24 
       
    25 /**
       
    26  * AnalyserXMLGenerator.
       
    27  * XML file generator that writes configuration XML-files for Heap Analyser and 
       
    28  * Crash Analyser based on parameters it has received.
       
    29  * 
       
    30  * AnalyserXMLGenerator can be used for example like this:
       
    31  * <PRE>
       
    32  * AnalyserXMLGenerator generator = new AnalyserXMLGenerator(XMLGeneratorAction.ANALYSE_HEAP, "c:\\temp\\source.txt", "c:\\directory\\", null, null, "thread", "c:\\temp\\output.xls", null, null);
       
    33  * File filename = new File("c:\\temp\\testxml.xml");
       
    34  * BufferedWriter writer = null;
       
    35  * 	try {
       
    36  * 		writer = new BufferedWriter(new FileWriter(filename)); 
       
    37  * 		generator.GenerateXML(writer);
       
    38  * 	} catch (IOException e) {
       
    39  * 		e.printStackTrace();
       
    40  * 	} 
       
    41  * 	finally {
       
    42  *  	try {
       
    43  *   		if (writer != null) {
       
    44  *   			writer.flush();
       
    45  *   			writer.close();
       
    46  *   		}
       
    47  *  	} 
       
    48  *  	catch (IOException ex) {
       
    49  *  		ex.printStackTrace();
       
    50  *  		return false;
       
    51  *  	}
       
    52  *  }
       
    53  *  </PRE>
       
    54  */
       
    55 
       
    56 public class AnalyserXMLGenerator {
       
    57 
       
    58 	/**
       
    59 	 * Enumeration for Heap Analyser actions
       
    60 	 */
       
    61 	public enum XMLGeneratorAction {
       
    62 		ANALYSE_HEAP, COMPARE_HEAPS, ANALYSE_CRASH_SUMMARY, ANALYSE_CRASH_FULL 
       
    63 	}
       
    64 	
       
    65 	private XMLGeneratorAction 	XMLAction;
       
    66 	private String[] 			XMLSourceFile;
       
    67 	private String 				XMLSourceDirectory;
       
    68 	private String[] 			XMLDebugMetaDataFile = null;
       
    69 	private String 				XMLDebugMetaDataDirectory;
       
    70 	private String 				XMLThreadName;
       
    71 	private String 				XMLExtension;
       
    72 	private String 				XMLAnalyseFileOutput;
       
    73 	private String 				XMLAnalyseDirectoryOutput;	
       
    74 
       
    75 
       
    76 	//Commands for XML
       
    77 	
       
    78 	private final static String COMMAND_VIEWER 			= "Viewer";
       
    79 	private final static String COMMAND_COMPARE_HEAPS 	= "CompareTwoHeaps";
       
    80 	private final static String COMMAND_SUMMARY 		= "Summary";
       
    81 	private final static String COMMAND_FULL_ANALYSIS	= "Full";
       
    82 	
       
    83 	// XML-tags
       
    84 
       
    85 	private final static String CRASH_ANALYSER_START 	= "<crash_analysis>";
       
    86 	private final static String CRASH_ANALYSER_END 		= "</crash_analysis>";
       
    87 
       
    88 	private final static String HEAP_ANALYSER_START 	= "<heap_analysis>";
       
    89 	private final static String HEAP_ANALYSER_END 		= "</heap_analysis>";
       
    90 
       
    91 	private final static String SOURCE_START 			= "  <category name=\"source\">";
       
    92 	private final static String SOURCE_END 				= "  </category>";
       
    93 
       
    94 	private final static String DEBUG_META_DATA_START 	= "  <category name=\"debug_meta_data\">";
       
    95 	private final static String DEBUG_META_DATA_END 	= "  </category>";
       
    96 
       
    97 	private final static String FILE_START 				= "    <file name=\"";
       
    98 	private final static String FILE_END 				= "\"/>";
       
    99 
       
   100 	private final static String DIRECTORY_START 		= "    <directory name=\"";
       
   101 	private final static String DIRECTORY_END 			= "\"/>";
       
   102 	
       
   103 	private final static String PARAMETERS_START 		= "  <category name=\"parameters\">";
       
   104 	private final static String PARAMETERS_END 			= "  </category>";
       
   105 
       
   106 	private final static String THREAD_START 			= "    <command name=\"thread\">";
       
   107 	private final static String THREAD_END 				= "</command>";
       
   108 
       
   109 	private final static String COMMAND_START 			= "    <command name=\"analysis_type\">";
       
   110 	private final static String COMMAND_END 			= "</command>";
       
   111 
       
   112 	private final static String OUTPUT_START 			= "  <category name=\"output\">";
       
   113 	private final static String OUTPUT_END 				= "  </category>";
       
   114 	
       
   115 	private final static String EXTENSION_START 		= "    <extension name=\"";
       
   116 	private final static String EXTENSION_END 			= "\"/>";
       
   117 	
       
   118 	
       
   119 	
       
   120 	/**
       
   121 	 * XMLGenerator()
       
   122 	 * Constructor
       
   123 	 */
       
   124 	public AnalyserXMLGenerator() {
       
   125 		
       
   126 		XMLAction					= null;
       
   127 		XMLSourceFile 				= null;
       
   128 		XMLSourceDirectory			= null;
       
   129 		XMLDebugMetaDataFile 		= null;
       
   130 		XMLDebugMetaDataDirectory 	= null;
       
   131 		XMLThreadName 				= null;
       
   132 		XMLAnalyseFileOutput 		= null;
       
   133 		XMLExtension				= null;
       
   134 	
       
   135 	}
       
   136 
       
   137 	/**
       
   138 	 * AnalyserXMLGenerator()
       
   139 	 * Constructor for XMLGenerator
       
   140 	 *
       
   141 	 * @param action 
       
   142 	 * @param sourceFile file that is added into <source> -section under <file> -tag
       
   143 	 * @param sourceDirectory directory that is added into <source> -section under <Directory> -tag
       
   144 	 * @param debugMetaDataFile file that is added into <debug_meta_data> -section under <file> -tag
       
   145 	 * @param debugMetaDataDirectory directory that is added into <debug_meta_data> -section under <Directory> -tag
       
   146 	 * @param threadName thread name that is added into <parameters> -section under <thread> -tag
       
   147 	 * @param extension extension text that is is added into <parameters> -section under <extension> -tag
       
   148 	 * @param analyseFileOutput file name that is added into <output> -section under <file> -tag
       
   149  	 * @param analyseDirectoryOutput file name that is added into <output> -section under <directory> -tag
       
   150 	 */
       
   151 	
       
   152 	public AnalyserXMLGenerator( XMLGeneratorAction action,
       
   153 								 String[] sourceFile, 
       
   154 								 String sourceDirectory,
       
   155 								 String[] debugMetaDataFile, 
       
   156 								 String debugMetaDataDirectory, 
       
   157 								 String threadName, 
       
   158 								 String analyseFileOutput,
       
   159 								 String analyseDirectoryOutput,
       
   160 								 String extension ) { 
       
   161 		
       
   162 		XMLAction					= action;
       
   163 		XMLSourceFile 				= sourceFile;
       
   164 		XMLSourceDirectory			= sourceDirectory;
       
   165 		XMLDebugMetaDataFile 		= debugMetaDataFile;
       
   166 		XMLDebugMetaDataDirectory	= debugMetaDataDirectory;
       
   167 		XMLThreadName 				= threadName;
       
   168 		XMLAnalyseFileOutput 		= analyseFileOutput;
       
   169 		XMLAnalyseDirectoryOutput	= analyseDirectoryOutput;
       
   170 		XMLExtension				= extension;
       
   171 	}
       
   172 	
       
   173 	/**
       
   174 	 * GenerateXML()
       
   175 	 * Writes XML file.
       
   176 	 * @param writer BufferedWriter stream where XML is written. 
       
   177 	 * @throws IOException if something goes when writing to stream 
       
   178 	 */
       
   179 	
       
   180 	public void GenerateXML( BufferedWriter writer ) throws IOException{
       
   181 	
       
   182 		
       
   183 		if( XMLAction != null )	{
       
   184 			// Begin XML
       
   185 			switch( XMLAction ){
       
   186 				case ANALYSE_CRASH_FULL:
       
   187 				case ANALYSE_CRASH_SUMMARY:
       
   188 					writer.write(CRASH_ANALYSER_START);
       
   189 					break;
       
   190 				case ANALYSE_HEAP:
       
   191 				case COMPARE_HEAPS:
       
   192 					writer.write(HEAP_ANALYSER_START);
       
   193 					break;
       
   194 				default:
       
   195 					break;
       
   196 			}
       
   197 					
       
   198 			writer.newLine();
       
   199 			writer.newLine();
       
   200 		}	
       
   201 		// Write Sources
       
   202 		this.writeSource(writer);
       
   203 		
       
   204 		
       
   205 		// Write debug meta data
       
   206 		this.writeDebugMetaData(writer);
       
   207 		
       
   208 		// Write parameters
       
   209 		this.writeParameters(writer);
       
   210 		
       
   211 		// Write output location
       
   212 		this.writeOutput(writer);
       
   213 		
       
   214 		// End XML
       
   215 		if( XMLAction != null )	{
       
   216 			switch(XMLAction){
       
   217 				case ANALYSE_CRASH_SUMMARY:
       
   218 				case ANALYSE_CRASH_FULL:
       
   219 					writer.write(CRASH_ANALYSER_END);
       
   220 					break;
       
   221 				case ANALYSE_HEAP:
       
   222 				case COMPARE_HEAPS:
       
   223 					writer.write(HEAP_ANALYSER_END);
       
   224 					break;
       
   225 				default:
       
   226 					break;
       
   227 			}
       
   228 			writer.newLine();
       
   229 		}
       
   230 
       
   231 		
       
   232 	}
       
   233 
       
   234 	/**
       
   235 	 * writeSource()
       
   236 	 * Writes source - parameters
       
   237 	 * @param writer BufferedWriter stream where XML is written. 
       
   238 	 */
       
   239 	private void writeSource( BufferedWriter writer ) throws IOException{
       
   240 		
       
   241 		
       
   242 		// Start Source definition
       
   243 		writer.write(SOURCE_START);
       
   244 		writer.newLine();
       
   245 	
       
   246 		if( XMLSourceFile != null ){
       
   247 			// File Definition
       
   248 			for(int i = 0; i < XMLSourceFile.length; i++ ){
       
   249 				writer.write( FILE_START );
       
   250 				writer.write( XMLSourceFile[i] );
       
   251 				writer.write( FILE_END );
       
   252 				writer.newLine();
       
   253 			}
       
   254 		}
       
   255 		if( XMLSourceDirectory != null ) {
       
   256 			writer.write( DIRECTORY_START );
       
   257 			writer.write( XMLSourceDirectory );
       
   258 			writer.write( DIRECTORY_END );
       
   259 			writer.newLine();
       
   260 		}
       
   261 		
       
   262 		// End Source definition
       
   263 		writer.write(SOURCE_END);
       
   264 		writer.newLine();
       
   265 		writer.newLine();
       
   266 	}
       
   267 	
       
   268 	/**
       
   269 	 * writeDebugMetaData()
       
   270 	 * Writes debug_meta_data - parameters
       
   271 	 * @param writer BufferedWriter stream where XML is written. 
       
   272 	 * @throws IOException
       
   273 	 */
       
   274 	private void writeDebugMetaData( BufferedWriter writer ) throws IOException{
       
   275 		// Start Debug meta data definition
       
   276 	
       
   277 		if( XMLDebugMetaDataFile != null || 
       
   278 			XMLDebugMetaDataDirectory != null ){
       
   279 		
       
   280 			writer.write(DEBUG_META_DATA_START);
       
   281 			writer.newLine();
       
   282 			
       
   283 			// File Definition
       
   284 			if( XMLDebugMetaDataFile != null ){
       
   285 				for(int i = 0; i < XMLDebugMetaDataFile.length; i++ ){
       
   286 					writer.write( FILE_START );
       
   287 					writer.write( XMLDebugMetaDataFile[i] );
       
   288 					writer.write( FILE_END );
       
   289 					writer.newLine();
       
   290 				}
       
   291 			}			
       
   292 			if( XMLDebugMetaDataDirectory != null ) {
       
   293 				writer.write( DIRECTORY_START );
       
   294 				writer.write( XMLDebugMetaDataDirectory );
       
   295 				writer.write( DIRECTORY_END );
       
   296 				writer.newLine();
       
   297 			}
       
   298 			
       
   299 			// End Debug meta data definition
       
   300 			writer.write(DEBUG_META_DATA_END);
       
   301 			writer.newLine();
       
   302 			writer.newLine();
       
   303 		}
       
   304 	}
       
   305 		
       
   306 	
       
   307 	
       
   308 	/**
       
   309 	 * writeParameters(
       
   310 	 * Writes parameters - parameters
       
   311 	 * @param writer BufferedWriter stream where XML is written. 
       
   312 	 * @throws IOException
       
   313 	 */
       
   314 		
       
   315 	private void writeParameters( BufferedWriter writer ) throws IOException {
       
   316 	
       
   317 		if( XMLThreadName != null || XMLAction != null || XMLExtension != null){
       
   318 			
       
   319 		
       
   320 			// Start parameter definition
       
   321 			writer.write(PARAMETERS_START);
       
   322 			writer.newLine();
       
   323 			
       
   324 			
       
   325 			if( XMLThreadName != null ){
       
   326 				// Add thread info 
       
   327 				writer.write( THREAD_START );
       
   328 				writer.write( XMLThreadName );
       
   329 				writer.write( THREAD_END );
       
   330 				writer.newLine();				
       
   331 				
       
   332 			}	
       
   333 		
       
   334 			if( XMLAction != null ){
       
   335 				// Command
       
   336 				
       
   337 				writer.write(COMMAND_START);
       
   338 		
       
   339 				// Begin XML
       
   340 				switch(XMLAction){
       
   341 					case ANALYSE_CRASH_SUMMARY:
       
   342 						writer.write(COMMAND_SUMMARY);
       
   343 						break;
       
   344 					case ANALYSE_CRASH_FULL:
       
   345 						writer.write(COMMAND_FULL_ANALYSIS);
       
   346 						break;
       
   347 					case ANALYSE_HEAP:
       
   348 						writer.write(COMMAND_VIEWER);
       
   349 						break;
       
   350 					case COMPARE_HEAPS:
       
   351 						writer.write(COMMAND_COMPARE_HEAPS);
       
   352 						break;
       
   353 					default:
       
   354 						break;
       
   355 				}		
       
   356 				writer.write(COMMAND_END);
       
   357 				writer.newLine();
       
   358 			}			
       
   359 			
       
   360 			if( XMLExtension != null ){
       
   361 				// extension
       
   362 				writer.write( EXTENSION_START );
       
   363 				writer.write( XMLExtension );
       
   364 				writer.write( EXTENSION_END );
       
   365 				writer.newLine();
       
   366 			}
       
   367 			
       
   368 			
       
   369 					
       
   370 			// End parameter definition
       
   371 			writer.write(PARAMETERS_END);
       
   372 			writer.newLine();
       
   373 			writer.newLine();
       
   374 		}		
       
   375 	
       
   376 	
       
   377 	}
       
   378 	
       
   379 	
       
   380 	/**
       
   381 	 * Writes output - parameters
       
   382 	 * @param writer BufferedWriter stream where XML is written. 
       
   383 	 * @throws IOException
       
   384 	 */
       
   385 	private void writeOutput( BufferedWriter writer ) throws IOException{
       
   386 	
       
   387 		if( XMLAnalyseFileOutput != null || XMLAnalyseDirectoryOutput != null ){
       
   388 			writer.write(OUTPUT_START);
       
   389 			writer.newLine();
       
   390 			
       
   391 			// Output file
       
   392 			if( XMLAnalyseFileOutput != null ){
       
   393 				writer.write(FILE_START);
       
   394 				writer.write(XMLAnalyseFileOutput);
       
   395 				writer.write(FILE_END);
       
   396 				writer.newLine();
       
   397 			}
       
   398 			// Output directory
       
   399 			if( XMLAnalyseDirectoryOutput != null ) {
       
   400 				writer.write(DIRECTORY_START);
       
   401 				writer.write(XMLAnalyseDirectoryOutput);
       
   402 				writer.write(DIRECTORY_END);
       
   403 				writer.newLine();
       
   404 			}
       
   405 		
       
   406 			writer.write(OUTPUT_END);
       
   407 			writer.newLine();
       
   408 			writer.newLine();
       
   409 		
       
   410 			
       
   411 		}
       
   412 	}
       
   413 	
       
   414 	
       
   415 	
       
   416 		
       
   417 	
       
   418 	/**
       
   419 	 * Get file name parameters
       
   420 	 * @return XMLSourceFile
       
   421 	 */
       
   422 	public String[] getXMLSourceFile() {
       
   423 		return XMLSourceFile;
       
   424 	}
       
   425 
       
   426 	/**
       
   427 	 * Set file name parameters
       
   428 	 * @param sourceFile the XMLSourceFile to set
       
   429 	 */
       
   430 	public void setXMLSourceFile(String[] sourceFile) {
       
   431 		XMLSourceFile = sourceFile;
       
   432 	}
       
   433 
       
   434 	/**
       
   435 	 * Get symbol or map files
       
   436 	 * @return XMLDebugMetaDataFile
       
   437 	 */
       
   438 	public String[] getXMLDebugMetaDataFile() {
       
   439 		return XMLDebugMetaDataFile;
       
   440 	}
       
   441 
       
   442 	/**
       
   443 	 * Set symbol or map files
       
   444 	 * @param XMLDebugMetaDataFile to set
       
   445 	 */
       
   446 	public void setXMLDebugMetaDataFile(String[] debugMetaDataFile) {
       
   447 		XMLDebugMetaDataFile = debugMetaDataFile;
       
   448 	}
       
   449 	
       
   450 	/**
       
   451 	 * Check if XMLDebugMetaDataFile has been set
       
   452 	 * @return XMLDebugMetaDataFile
       
   453 	 */
       
   454 	public boolean hasXMLDebugMetaDataFile() {
       
   455 		boolean isXMLSet = XMLDebugMetaDataFile!=null;
       
   456 		if(isXMLSet){
       
   457 			isXMLSet =  XMLDebugMetaDataFile.length > 0;
       
   458 		}
       
   459 		return isXMLSet;
       
   460 	}	
       
   461 
       
   462 	/**
       
   463 	 * Get thread parameter
       
   464 	 * @return XMLThreadName
       
   465 	 */
       
   466 	public String getXMLThreadName() {
       
   467 		return XMLThreadName;
       
   468 	}
       
   469 
       
   470 	/**
       
   471 	 * Set thread parameter
       
   472 	 * @param XMLThreadName to set
       
   473 	 */
       
   474 	public void setXMLThreadName(String threadName) {
       
   475 		XMLThreadName = threadName;
       
   476 	}
       
   477 
       
   478 	/**
       
   479 	 * Get analyse output file
       
   480 	 * @return XMLAnalyseFileOutput
       
   481 	 */
       
   482 	public String getXMLAnalyseFileOutput() {
       
   483 		return XMLAnalyseFileOutput;
       
   484 	}
       
   485 
       
   486 	/**
       
   487 	 * Set analyse output file
       
   488 	 * @param XMLAnalyseFileOutput to set
       
   489 	 */
       
   490 	public void setXMLAnalyseFileOutput(String analysefileOutput) {
       
   491 		XMLAnalyseFileOutput = analysefileOutput;
       
   492 	}
       
   493 
       
   494 	/**
       
   495 	 * Get current action
       
   496 	 * @return XMLAction
       
   497 	 */
       
   498 	public XMLGeneratorAction getXMLAction() {
       
   499 		return XMLAction;
       
   500 	}
       
   501 	
       
   502 	/**
       
   503 	 * Set action
       
   504 	 * @param action the XMLGeneratorAction to set
       
   505 	 */
       
   506 	public void setXMLAction(XMLGeneratorAction action) {
       
   507 		XMLAction = action;
       
   508 	}
       
   509 
       
   510 	/**
       
   511 	 * Get directory parameter
       
   512 	 * @return XMLSourceDirectory
       
   513 	 */
       
   514 	public String getXMLSourceDirectory() {
       
   515 		return XMLSourceDirectory;
       
   516 	}
       
   517 
       
   518 	/**
       
   519 	 * Set directory parameter
       
   520 	 * @param XMLSourceDirectory to set
       
   521 	 */
       
   522 	public void setXMLSourceDirectory(String sourceDirectory) {
       
   523 		XMLSourceDirectory = sourceDirectory;
       
   524 	}
       
   525 
       
   526 	/**
       
   527 	 * Get debug metadata directory. In UI it's shown as Map file folder.
       
   528 	 * @return XMLDebugMetaDataDirectory
       
   529 	 */
       
   530 	public String getXMLDebugMetaDataDirectory() {
       
   531 		return XMLDebugMetaDataDirectory;
       
   532 	}
       
   533 
       
   534 	/**
       
   535 	 * Set debug metadata directory. In UI it's shown as Map file folder.
       
   536 	 * @param XMLDebugMetaDataDirectory to set
       
   537 	 */
       
   538 	public void setXMLDebugMetaDataDirectory(String debugMetaDataDirectory) {
       
   539 		XMLDebugMetaDataDirectory = debugMetaDataDirectory;
       
   540 	}
       
   541 
       
   542 	/**
       
   543 	 * Get extension parameter
       
   544 	 * @return XMLExtension
       
   545 	 */
       
   546 	public String getXMLExtension() {
       
   547 		return XMLExtension;
       
   548 	}
       
   549 
       
   550 	/**
       
   551 	 * Set extension parameter
       
   552 	 * @param extension the XMLExtension to set
       
   553 	 */
       
   554 	public void setXMLExtension(String extension) {
       
   555 		XMLExtension = extension;
       
   556 	}
       
   557 
       
   558 	/**
       
   559 	 * Get output directory
       
   560 	 * @return XMLAnalyseDirectoryOutput
       
   561 	 */
       
   562 	public String getXMLAnalyseDirectoryOutput() {
       
   563 		return XMLAnalyseDirectoryOutput;
       
   564 	}
       
   565 
       
   566 	/**
       
   567 	 * Set output directory
       
   568 	 * @param analyseDirectoryOutput the XMLAnalyseDirectoryOutput to set
       
   569 	 */
       
   570 	public void setXMLAnalyseDirectoryOutput(String analyseDirectoryOutput) {
       
   571 		XMLAnalyseDirectoryOutput = analyseDirectoryOutput;
       
   572 	}
       
   573 
       
   574 }