sysperfana/memspyext/com.nokia.s60tools.memspy/src/com/nokia/s60tools/memspy/model/AnalyserXMLParser.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.BufferedReader;
       
    23 import java.util.ArrayList;
       
    24 
       
    25 
       
    26 /**
       
    27  * class AnalyserXMLParser.
       
    28  * class that parses symbol and thread name information from debug configuration file.
       
    29  *
       
    30  */
       
    31 
       
    32 public class AnalyserXMLParser {
       
    33 	
       
    34 	static final String DEBUG_META_DATA_START_TAG 	= "<category name=\"debug_meta_data\">";
       
    35 	static final String DEBUG_META_DATA_END_TAG 	= "</category>";
       
    36 	static final String THREAD_TAG 					= "<command name=\"thread\">";
       
    37 	static final String FILE_NAME 					= "file name";
       
    38 	static final String DIRECTORY_NAME				= "directory name";
       
    39 
       
    40 	
       
    41 	/**
       
    42 	 * Add wanted lines from given input to XML generator and returns the generator.
       
    43 	 * @param reader
       
    44 	 * @return XML generator
       
    45 	 */
       
    46 	public static AnalyserXMLGenerator parseXML( BufferedReader reader ){
       
    47 		AnalyserXMLGenerator retVal = new AnalyserXMLGenerator();
       
    48 		
       
    49 		boolean readingDebugMetaData = false;
       
    50 		ArrayList<String> debugMetaDataFiles = new ArrayList<String>();
       
    51 		String debugMetaDataDirectory = null;
       
    52 		String threadName = "";
       
    53 		
       
    54 		try{
       
    55 			String line = "";
       
    56 			
       
    57 			// Go thru file in loop and search for lines that contain debugMetaData -text. 
       
    58 			// If text is found, save meta data file information into AnalyserXMLGenerator-object.
       
    59 			
       
    60 			while( reader.ready() ){
       
    61 
       
    62 				line = reader.readLine();
       
    63 				
       
    64 				if( line == null){
       
    65 					break;
       
    66 				}
       
    67 				
       
    68 				// Search for debug meta data start and end tags.
       
    69 				if( line.contains(DEBUG_META_DATA_START_TAG) ){
       
    70 					readingDebugMetaData = true;
       
    71 				}
       
    72 				else if( line.contains(DEBUG_META_DATA_END_TAG) ){
       
    73 					readingDebugMetaData = false;
       
    74 				}
       
    75 				
       
    76 				// if between debug meta data start and end tags, search for debug meta data info.
       
    77 				if( readingDebugMetaData ){
       
    78 					if(line.contains(FILE_NAME)){
       
    79 						debugMetaDataFiles.add( getMetaDataInfo(line) );
       
    80 					}
       
    81 					if(line.contains(DIRECTORY_NAME)){
       
    82 						debugMetaDataDirectory = getMetaDataInfo(line);
       
    83 					}
       
    84 				}
       
    85 				
       
    86 				// search for thread name tag.
       
    87 				if( line.contains(THREAD_TAG) ){
       
    88 					threadName = getThreadName(line);
       
    89 				}
       
    90 			}
       
    91 			
       
    92 		}
       
    93 		catch (Exception e){
       
    94 			e.printStackTrace();
       
    95 			return null; 		
       
    96 		}
       
    97 		
       
    98 		if(threadName.equals("") ){
       
    99 			return null;
       
   100 		}
       
   101 		
       
   102 		// check that thread name and at least one symbol file was found.
       
   103 		if( debugMetaDataFiles.size() != 0 ){
       
   104 			// set found data into AnalyserXMLGenerator-object.
       
   105 			retVal.setXMLDebugMetaDataFile(debugMetaDataFiles.toArray(new String [debugMetaDataFiles.size()]));			
       
   106 		}
       
   107 		
       
   108 		if( !threadName.equals("")){
       
   109 			retVal.setXMLDebugMetaDataDirectory(debugMetaDataDirectory);
       
   110 		}
       
   111 		retVal.setXMLThreadName(threadName);
       
   112 		
       
   113 		return retVal;
       
   114 		
       
   115 	}
       
   116 	
       
   117 	/**
       
   118 	 * getMetaDataInfo.
       
   119 	 * @param line String that is parsed
       
   120 	 * @return string between quotation marks
       
   121 	 */
       
   122 	private static String getMetaDataInfo( String line ) {
       
   123 		int firstIndex = line.indexOf("\"") + 1 ;
       
   124 		int secondIndex = line.indexOf(("\""), firstIndex );
       
   125 		return line.substring(firstIndex, secondIndex);
       
   126 	}
       
   127 
       
   128 	
       
   129 	/**
       
   130 	 * getThreadName.
       
   131 	 * @param line String that is parsed
       
   132 	 * @return string between threas tags
       
   133 	 */
       
   134 	private static String getThreadName(String line){
       
   135 		int firstIndex = line.indexOf("=\"thread\">") + 10;
       
   136 		int secondIndex = line.indexOf("</command>"); 
       
   137 		return line.substring(firstIndex, secondIndex);
       
   138 	}
       
   139 	
       
   140 }