sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.button/src/com/nokia/carbide/cpp/pi/button/BupUpdater.java
changeset 2 b9ab3b238396
child 12 ae255c9aa552
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     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 the License "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 package com.nokia.carbide.cpp.pi.button;
       
    19 
       
    20 import java.util.TreeMap;
       
    21 import java.util.Vector;
       
    22 
       
    23 import com.nokia.carbide.cpp.internal.pi.analyser.NpiInstanceRepository;
       
    24 import com.nokia.carbide.cpp.internal.pi.model.CusSample;
       
    25 import com.nokia.carbide.cpp.internal.pi.test.AnalysisInfoHandler;
       
    26 import com.nokia.carbide.cpp.internal.pi.test.PIAnalyser;
       
    27 
       
    28 
       
    29 public class BupUpdater {
       
    30 	
       
    31 	static private BupUpdater instance = null;
       
    32 	private static TreeMap<String, Integer> OneOneHexToStringMap = new TreeMap<String, Integer>();
       
    33 	
       
    34 	public static BupUpdater getInstance() {
       
    35 		if (instance == null) {
       
    36 			instance = new BupUpdater();
       
    37 		}
       
    38 		return instance;
       
    39 	}
       
    40 	
       
    41 	private BupUpdater() {
       
    42 		// singleton
       
    43 		
       
    44 		String nonPrintable[] = {
       
    45 				Messages.getString("BupUpdater.0"), Messages.getString("BupUpdater.1"), Messages.getString("BupUpdater.2"), Messages.getString("BupUpdater.3"), Messages.getString("BupUpdater.4"), Messages.getString("BupUpdater.5"), Messages.getString("BupUpdater.6"), Messages.getString("BupUpdater.7"), Messages.getString("BupUpdater.8"), Messages.getString("BupUpdater.9"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$
       
    46 				Messages.getString("BupUpdater.10"), Messages.getString("BupUpdater.11"), Messages.getString("BupUpdater.12"), Messages.getString("BupUpdater.13"), Messages.getString("BupUpdater.14"), Messages.getString("BupUpdater.15"), Messages.getString("BupUpdater.16"), Messages.getString("BupUpdater.17"), Messages.getString("BupUpdater.18"), Messages.getString("BupUpdater.19"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$
       
    47 				Messages.getString("BupUpdater.20"), Messages.getString("BupUpdater.21"), Messages.getString("BupUpdater.22"), Messages.getString("BupUpdater.23"), Messages.getString("BupUpdater.24"), Messages.getString("BupUpdater.25"), Messages.getString("BupUpdater.26"), Messages.getString("BupUpdater.27"), Messages.getString("BupUpdater.28"), Messages.getString("BupUpdater.29"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$
       
    48 				Messages.getString("BupUpdater.30"), Messages.getString("BupUpdater.31"), Messages.getString("BupUpdater.32") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       
    49 			};
       
    50 		for (int i = 0; i < 33; i++) {
       
    51 			OneOneHexToStringMap.put(nonPrintable[i], i);
       
    52 		}
       
    53 		for (int i = 33; i < 127; i++) {
       
    54 			OneOneHexToStringMap.put(Character.toString ((char) i), i);
       
    55 		}
       
    56 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.33"), 127); //$NON-NLS-1$
       
    57 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.34"), 63495);	 //$NON-NLS-1$
       
    58 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.35"), 63496);	 //$NON-NLS-1$
       
    59 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.36"), 63497);	 //$NON-NLS-1$
       
    60 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.37"), 63498); //$NON-NLS-1$
       
    61 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.38"), 63499);	 //$NON-NLS-1$
       
    62 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.39"), 63554); //$NON-NLS-1$
       
    63 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.40"), 63555); //$NON-NLS-1$
       
    64 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.41"), 63557);	 //$NON-NLS-1$
       
    65 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.42"), 63570); //$NON-NLS-1$
       
    66 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.43"), 63586);		 //$NON-NLS-1$
       
    67 		OneOneHexToStringMap.put(Messages.getString("BupUpdater.44"), 63587); //$NON-NLS-1$
       
    68 
       
    69 	}
       
    70 	
       
    71 	public void convertToLatest(Vector samples) {
       
    72 		String fileVersion = NpiInstanceRepository.getInstance().activeUidGetAnalysisInfoHandler().getFileVersion();
       
    73 		float version = 0;
       
    74 		try  {
       
    75 			version = Float.parseFloat(fileVersion);
       
    76 		} catch (NumberFormatException e) {
       
    77 			version = -1;
       
    78 		}
       
    79 
       
    80 		if (!fileVersion.equals(PIAnalyser.NPIFileFormat)) {		
       
    81 			
       
    82 			if (   fileVersion.startsWith(com.nokia.carbide.cpp.internal.pi.test.Messages.getString("AnalysisInfoHandler.unknownFileVersion")) //$NON-NLS-1$
       
    83 					|| fileVersion.startsWith(com.nokia.carbide.cpp.internal.pi.test.Messages.getString("AnalysisInfoHandler.unknown")) //$NON-NLS-1$
       
    84 					|| version <= Float.parseFloat("1.0")) //$NON-NLS-1$ // do not externalize this version number
       
    85 			{
       
    86 				NpiInstanceRepository.getInstance().activeUidGetAnalysisInfoHandler().setFileVersion(PIAnalyser.NPIFileFormat);
       
    87 				Vector <BupSample> bupSamples = new Vector <BupSample>();
       
    88 				// set additional info on file to proper profile
       
    89 				AnalysisInfoHandler handler = NpiInstanceRepository.getInstance().activeUidGetAnalysisInfoHandler();
       
    90 				ButtonPlugin.getDefault().setBupMapProfileToInfoHandler(handler, BupEventMapManager.getInstance().getLegacyProfile());
       
    91 				IBupEventMap map = BupEventMapManager.getInstance().captureMap(BupEventMapManager.getInstance().getLegacyProfile());
       
    92 				for (int i = 0; i < samples.size(); i++)
       
    93 				{
       
    94 					CusSample sample = (CusSample)samples.elementAt(i);
       
    95 					BupSample bupSample = convertFromOneZeroOrBefore(sample, map);
       
    96 					bupSamples.add(bupSample);
       
    97 				}
       
    98 				BupEventMapManager.getInstance().releaseMap(map);
       
    99 				samples.clear();
       
   100 				samples.addAll(bupSamples);
       
   101 			} else if (version <= Float.parseFloat("1.1")) {	//$NON-NLS-1$	// do not externalize this version number
       
   102 				// 1.2 introduce BupSample instead of using CusSample, do conversion for any file lower than this
       
   103 				NpiInstanceRepository.getInstance().activeUidGetAnalysisInfoHandler().setFileVersion(PIAnalyser.NPIFileFormat);
       
   104 				Vector <BupSample> bupSamples = new Vector <BupSample>();
       
   105 				// set additional info on file to proper profile
       
   106 				AnalysisInfoHandler handler = NpiInstanceRepository.getInstance().activeUidGetAnalysisInfoHandler();
       
   107 				ButtonPlugin.getDefault().setBupMapProfileToInfoHandler(handler, BupEventMapManager.getInstance().getLegacyProfile());
       
   108 				IBupEventMap map = BupEventMapManager.getInstance().captureMap(BupEventMapManager.getInstance().getLegacyProfile());
       
   109 				for (int i = 0; i < samples.size(); i++)
       
   110 				{
       
   111 					CusSample cusSample = (CusSample) samples.elementAt(i);
       
   112 					BupSample bupSample = convertFromOneOne(cusSample, map);
       
   113 					bupSamples.add(bupSample);
       
   114 				}
       
   115 				BupEventMapManager.getInstance().releaseMap(map);
       
   116 				samples.clear();
       
   117 				samples.addAll(bupSamples);
       
   118 			} else if (version <= Float.parseFloat("1.2")) {	//$NON-NLS-1$	// do not externalize this version number
       
   119 				// 1.3 introduce isLabelModified
       
   120 				Vector <BupSample> bupSamples = new Vector <BupSample>();
       
   121 				AnalysisInfoHandler handler = NpiInstanceRepository.getInstance().activeUidGetAnalysisInfoHandler();
       
   122 				// 2.0 M1 does not have current profile saved yet, reset to default
       
   123 				IBupEventMapProfile profile = ButtonPlugin.getDefault().getBupMapProfileFromInfoHandler(handler);
       
   124 				ButtonPlugin.getDefault().setBupMapProfileToInfoHandler(handler, profile);
       
   125 				IBupEventMap map = BupEventMapManager.getInstance().captureMap(profile);
       
   126 				for (int i = 0; i < samples.size(); i++)
       
   127 				{
       
   128 					BupSample bupSample = (BupSample) samples.elementAt(i);
       
   129 					bupSamples.add(convertFromOneTwo(bupSample, map));
       
   130 				}
       
   131 				BupEventMapManager.getInstance().releaseMap(map);
       
   132 				samples.clear();
       
   133 				samples.addAll(bupSamples);
       
   134 			}
       
   135 		}		
       
   136 	}
       
   137 	
       
   138 	
       
   139 	// 1.0 or before
       
   140 	// base on CustSample
       
   141 	// CustSample.name:
       
   142 	// unmodified name = hex string of corresponding key code e.g. 1 button is "49"
       
   143 	// modified name = label string typed
       
   144 	// CustSample.comment
       
   145 	
       
   146 
       
   147 	public BupSample convertFromOneZeroOrBefore(CusSample oldSample, IBupEventMap map)
       
   148 	{
       
   149 		int hexCode = 0;
       
   150 		BupSample bupSample = null;
       
   151 	    try {
       
   152 	    	// 1.0 or before save the hex string of corresponding key code e.g. 1 button is "49"
       
   153 	    	if (oldSample.name == null) {
       
   154 		    	bupSample = new BupSample(oldSample.sampleSynchTime, 0, map);
       
   155 		    	bupSample.setLabel(""); //$NON-NLS-1$
       
   156 	    	} else  {
       
   157 		    	hexCode = Integer.parseInt(oldSample.name);
       
   158 		    	bupSample = new BupSample(oldSample.sampleSynchTime, hexCode, map);	    		
       
   159 	    	}
       
   160 	    } catch (NumberFormatException exc) {
       
   161 	    	// a modified label with hexcode zero as we cannot recover that with 1.0
       
   162 	    	bupSample = new BupSample(oldSample.sampleSynchTime, 0, map);
       
   163 	    	bupSample.setLabel(oldSample.name);
       
   164 		}
       
   165 	    bupSample.setComment(oldSample.comment);
       
   166 	    return bupSample;
       
   167 	}	
       
   168 	
       
   169 	// 1.1
       
   170 	// base on CustSample
       
   171 	// CustSample.name:
       
   172 	// 0-33		ASCII name shorthand
       
   173 	// 33-126	printable ASCII char
       
   174 	// 127		DEL
       
   175 	// 128-		hex string for corresponding key
       
   176 	// CustSample.comment
       
   177 	public BupSample convertFromOneOne(CusSample oldSample, IBupEventMap map) {
       
   178 		Integer keyCode = 0;
       
   179 		keyCode = OneOneHexToStringMap.get(oldSample.name);
       
   180 		if (keyCode == null) {
       
   181 			try {
       
   182 				keyCode = Integer.parseInt(oldSample.name);
       
   183 			} catch (NumberFormatException e) {
       
   184 				// it may be just a label
       
   185 			}
       
   186 		}
       
   187 		BupSample bupSample = new BupSample(oldSample.sampleSynchTime, keyCode != null ? keyCode : 0, map);
       
   188 		if (keyCode == null) {
       
   189 			// this is a modified key label
       
   190 			bupSample.setLabel(oldSample.name);
       
   191 		}
       
   192 		bupSample.setComment(oldSample.comment);
       
   193 		return bupSample;
       
   194 	}
       
   195 
       
   196 	// 1.2
       
   197 	// base on BupSample
       
   198 	// BupSample.keyCode: hex key code of event
       
   199 	// BupSample.label: label to display
       
   200 	// BupSample.comment: comment for label
       
   201 	public BupSample convertFromOneTwo(BupSample oldSample, IBupEventMap map) {
       
   202 		BupSample bupSample = new BupSample(oldSample.sampleSynchTime, oldSample.getKeyCode(), map);
       
   203 		bupSample.setComment(oldSample.getComment());
       
   204 		// retain modified label
       
   205 		if (!oldSample.getLabel().equals(map.getLabel(oldSample.getKeyCode()))) {
       
   206 			bupSample.setLabel(oldSample.getLabel());
       
   207 		}
       
   208 		return bupSample;
       
   209 	}
       
   210 
       
   211 	// 1.3
       
   212 	// base on BupSample(same serial UID, additional field)
       
   213 	// BupSample.keyCode: hex key code of event
       
   214 	// BupSample.label: label to display
       
   215 	// BupSample.comment: comment for label	
       
   216 	// BupSample.labelModified: label is modified, not from mapping
       
   217 }