sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi/src/com/nokia/carbide/cpp/pi/importer/SampleImporter.java
changeset 2 b9ab3b238396
child 5 844b047e260d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi/src/com/nokia/carbide/cpp/pi/importer/SampleImporter.java	Thu Feb 11 15:32:31 2010 +0200
@@ -0,0 +1,477 @@
+/*
+ * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
+ * All rights reserved.
+ * This component and the accompanying materials are made available
+ * under the terms of the License "Eclipse Public License v1.0"
+ * which accompanies this distribution, and is available
+ * at the URL "http://www.eclipse.org/legal/epl-v10.html".
+ *
+ * Initial Contributors:
+ * Nokia Corporation - initial contribution.
+ *
+ * Contributors:
+ *
+ * Description: 
+ *
+ */
+
+package com.nokia.carbide.cpp.pi.importer;
+
+import java.io.ByteArrayInputStream;
+import java.util.ArrayList;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
+
+import com.nokia.carbide.cpp.internal.pi.analyser.AnalyserDataProcessor;
+import com.nokia.carbide.cpp.pi.util.GeneralMessages;
+
+
+public class SampleImporter {
+	private static SampleImporter instance;
+	
+	// location of input .dat file
+	private String datFileName;
+	// location of output project
+	private String outputContainerName;
+	// location of output .pi file
+	private String piFileName;
+	// workspace resource .npi file
+	private IFile piFile;
+	// PKG file and their SDK_ID or EPOCROOT
+	public static class PkgObyFile {
+		public String fileAbsolutePath;
+		public String epocRoot;
+		PkgObyFile(String epocRoot, String fileAbsolutePath) {
+			this.fileAbsolutePath = fileAbsolutePath;
+			this.epocRoot = epocRoot;
+		}
+	}
+	private ArrayList<PkgObyFile> pkgObyFileList = new ArrayList<PkgObyFile>();
+	
+	private boolean importStripTimeStamp = false;	// importer is just doing dummy import for stripping time stamp for diffing
+	
+	// ROM related stuff
+	private String romEpocroot;
+	private String romSymbolFile;
+	private String romObyFile;
+	private ArrayList<String> rofsSymbolFileList = new ArrayList<String>();
+	private ArrayList<String> rofsObyFileList = new ArrayList<String>();
+	
+	// Custom sample
+	private String custAbsolutePath;
+	private boolean custIsValueBased;
+	private String custDelimitor;
+	
+	// Bup sample
+	private String bupMapProfileId;
+	private String bupMapSymbianSDKId;
+	private boolean bupMapIsBuiltIn;
+	private boolean bupMapIsWorkspace;
+	
+	private SampleImporter() {
+		clear();
+		// singleton
+	}
+	
+	public static SampleImporter getInstance() {
+		if (instance == null) {
+			instance = new SampleImporter();
+		}
+		
+		return instance;
+	}
+	
+	public Object clone() throws CloneNotSupportedException {
+		throw new CloneNotSupportedException();
+	}
+
+	public String getDatFileName () {
+		return datFileName;
+	}
+
+	public void setDatFileName (String file) {
+		datFileName = file;
+	}
+
+	public void clearDatFileName () {
+		setDatFileName(""); //$NON-NLS-1$
+	}
+
+	public String getProjectName () {
+		return outputContainerName;
+	}
+
+	public void setProjectName (String file) {
+		outputContainerName = file;
+	}
+
+	public void clearProjectName () {
+		setProjectName(""); //$NON-NLS-1$
+	}
+	
+	public String getPiFileName () {
+		return piFileName;
+	}
+
+	public void setPiFileName (String file) {
+		piFileName = file;
+	}
+	
+	public void clearPiFileName () {
+		setPiFileName(""); //$NON-NLS-1$
+	}
+	
+	public IFile getPiFile() {
+		return this.piFile;
+	}
+	
+	public void setPiFile(IFile piFile) {
+		this.piFile = piFile;
+	}
+	
+	public void setStripTimeStampActive () {
+		this.importStripTimeStamp = true;
+	}
+	
+	public void setStripTimeStampDone() {
+		this.importStripTimeStamp = false;
+	}
+	
+	public boolean isStrippingTimeStamp() {
+		return this.importStripTimeStamp;
+	}
+
+	public PkgObyFile[] getPkgObyFilesList () {
+		ArrayList<PkgObyFile> result = new ArrayList<PkgObyFile>();
+		// round up all ROM OBY and app PKG
+		if (romObyFile != null && romObyFile.length() > 0) {
+			PkgObyFile romObyEntry = new PkgObyFile(romEpocroot, romObyFile);
+			result.add(romObyEntry);
+		}
+		for (String rofsObyFile : rofsObyFileList) {
+			PkgObyFile rofsObyEntry = new PkgObyFile(romEpocroot, rofsObyFile);
+			result.add(rofsObyEntry);
+		}
+		result.addAll(pkgObyFileList);
+		return result.toArray(new PkgObyFile[result.size()]);
+	}
+
+	public void addPkgObyFile (String epocroot, String filePath) {
+		pkgObyFileList.add(new PkgObyFile(epocroot, filePath));
+	}
+	
+	public void clearPkgObyFileList () {
+		pkgObyFileList.clear();
+	}
+
+	public String getRomEpocroot () {
+		return romEpocroot;
+	}
+	
+	public void setRomEpocroot (String epocroot) {
+		romEpocroot = epocroot;
+	}
+
+	public void clearRomEpocroot () {
+		setRomEpocroot(""); //$NON-NLS-1$
+	}
+
+	public String getRomSymbolFile () {
+		return romSymbolFile;
+	}
+	
+	public void setRomSymbolFile (String filename) {
+		romSymbolFile = filename;
+	}
+
+	public void clearRomSymbolFile () {
+		setRomSymbolFile(""); //$NON-NLS-1$
+	}
+	
+	public String getRomObyFile () {
+		return romObyFile;
+	}
+	
+	public void setRomObyFile (String filename) {
+		romObyFile = filename;
+	}
+	
+	public void clearRomObyFile () {
+		setRomObyFile(""); //$NON-NLS-1$
+	}
+
+	public String[] getRofsSymbolFileList () {
+		return rofsSymbolFileList.toArray(new String[rofsSymbolFileList.size()]);
+	}
+	
+	public void addRofsSymbolFile (String filename) {
+		rofsSymbolFileList.add(filename);
+	}
+
+	public void clearRofsSymbolFileList () {
+		rofsSymbolFileList.clear();
+	}
+
+	public String[] getRofsObyFileList () {
+		return rofsObyFileList.toArray(new String[rofsObyFileList.size()]);
+	}
+	
+	public void addRofsObyFile (String filename) {
+		rofsObyFileList.add(filename);
+	}
+
+	public void clearRofsObyFileList () {
+		rofsObyFileList.clear();
+	}
+
+	public String getCustAbsolutePath () {
+		return custAbsolutePath;
+	}
+	
+	public void setCustAbsolutePath (String pathName) {
+		custAbsolutePath = pathName;
+	}
+
+	public void clearCustAbsolutePath () {
+		setCustAbsolutePath(""); //$NON-NLS-1$
+	}
+
+	public boolean getCustIsValueBased () {
+		return custIsValueBased;
+	}
+	
+	public void setCustIsValueBased (boolean value) {
+		custIsValueBased = value;
+	}
+
+	public void clearCustIsValueBased () {
+		setCustIsValueBased(true);
+	}
+	
+	public String getCustDelimitor () {
+		return custDelimitor;
+	}
+	
+	public void setCustDelimitor (String delimitor) {
+		custDelimitor = delimitor;
+	}
+
+	public void clearCustDelimitor () {
+		setCustDelimitor(""); //$NON-NLS-1$
+	}
+	
+	public String getBupMapProfileId() {
+		return bupMapProfileId;
+	}
+
+	public void setBupMapProfileId(String bupMapProfileId) {
+		this.bupMapProfileId = bupMapProfileId;
+	}
+	
+	public void clearBupMapProfileId() {
+		setBupMapProfileId("");	//$NON-NLS-1$
+	}
+
+	public String getBupMapSymbianSDKId() {
+		return bupMapSymbianSDKId;
+	}
+
+	public void setBupMapSymbianSDKId(String bupMapSymbianSDKId) {
+		this.bupMapSymbianSDKId = bupMapSymbianSDKId != null ? bupMapSymbianSDKId : "";	//$NON-NLS-1$
+	}
+	
+	public void clearBupMapSymbianSDKId() {
+		setBupMapSymbianSDKId("");	//$NON-NLS-1$
+	}
+
+	public boolean isBupMapIsBuiltIn() {
+		return bupMapIsBuiltIn;
+	}
+
+	public void setBupMapIsBuiltIn(boolean bupMapIsBuiltIn) {
+		this.bupMapIsBuiltIn = bupMapIsBuiltIn;
+	}
+	
+	public void clearBupMapIsBuiltIn() {
+		setBupMapIsBuiltIn(false);
+	}
+
+	public boolean isBupMapIsWorkspace() {
+		return bupMapIsWorkspace;
+	}
+
+	public void setBupMapIsWorkspace(boolean bupMapIsWorkspace) {
+		this.bupMapIsWorkspace = bupMapIsWorkspace;
+	}
+
+	public void clearBupMapIsWorkspace() {
+		setBupMapIsWorkspace(false);
+	}
+
+	public void clear() {
+		clearDatFileName ();
+		clearProjectName ();
+		clearPiFileName ();
+		clearPkgObyFileList ();
+		clearRomEpocroot ();
+		clearRomSymbolFile ();
+		clearRomObyFile ();
+		clearRofsSymbolFileList ();
+		clearRofsObyFileList ();
+		clearCustAbsolutePath ();
+		clearCustIsValueBased ();
+		clearCustDelimitor ();
+		clearBupMapProfileId ();
+		clearBupMapSymbianSDKId ();
+		clearBupMapIsBuiltIn ();
+		clearBupMapIsWorkspace ();
+	}
+	
+	public boolean validate () {
+		if (getDatFileName().equals("")) { //$NON-NLS-1$
+			return false;
+		} else if (new java.io.File(getDatFileName()).exists() == false) {
+			return false;
+		}
+		
+		if (getProjectName().equals("")) {	//$NON-NLS-1$
+			return false;
+		}
+		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+		IResource resource = root.findMember(getProjectName());
+		if (resource.exists() == false) {
+			return false;
+		}
+		
+		if (getPiFileName().equals("")) {	//$NON-NLS-1$
+			return false;
+		}
+		
+		if (getRomEpocroot().equals("") != false) {	//$NON-NLS-1$
+			if (getRomSymbolFile().equals("") == false) { 	//$NON-NLS-1$
+				if (new java.io.File(getRomSymbolFile()).exists() == false) {
+					return false;
+				}
+			}
+			
+			if (getRomObyFile().equals("") == false) { 	//$NON-NLS-1$
+				if (new java.io.File(getRomObyFile()).exists() == false) {
+					return false;
+				}
+			}
+			
+			for (String fileName : getRofsObyFileList()) {
+				if (fileName.equals("") == false) {	//$NON-NLS-1$
+					if (new java.io.File(fileName).exists() == false) {
+						return false;
+					}
+				}	
+			}			
+		}
+		
+		if (pkgObyFileList.size() < 0) {
+			return false;
+		}
+		
+		return true;
+	}
+
+	public void importSamples(boolean pollTillNpiSaved) {
+		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+		IResource resource = root.findMember(getProjectName());
+		
+		IContainer piContainer;
+		if (resource instanceof IContainer) {
+			piContainer = (IContainer) resource;
+		} else {
+			return;
+		}
+		
+		// empty file if there is no symbol file specified, or analyser would crash
+		java.io.File dummySymbol = null;
+		if (getRomSymbolFile() == null || getRomSymbolFile().length() <= 0) {
+			try {
+				dummySymbol = java.io.File.createTempFile("dummySymbol", ".symbol");	//$NON-NLS-1$ //$NON-NLS-2$
+				setRomSymbolFile(dummySymbol.getAbsolutePath());
+			} catch (Exception e) {
+				e.printStackTrace();
+				return;
+			}
+		}
+
+		try {	
+			// import the new project according to wizard
+			piFile = piContainer.getFile(new Path(getPiFileName()));
+
+			if (piFile.exists())
+			{
+				piFile.delete(true, null);
+			}
+		} catch (CoreException e) {
+			GeneralMessages.showErrorMessage(com.nokia.carbide.cpp.pi.importer.Messages.getString("SampleImporter.importerInternalError"));  //$NON-NLS-1$
+			GeneralMessages.PiLog(com.nokia.carbide.cpp.pi.importer.Messages.getString("SampleImporter.importerCoreException"), GeneralMessages.ERROR);  //$NON-NLS-1$
+			return;
+		}
+		
+		// import and save the file as npi
+		AnalyserDataProcessor.getInstance().importSaveAndOpen(piFile, pollTillNpiSaved);
+		
+		if (dummySymbol != null) {
+			dummySymbol.delete();
+		}
+
+	}
+	
+	// support for removing timestamp to support test
+	public void stripeTimestamp(final String sourceFilePath, final String dstFilePath) {
+		IWorkspace workspace = ResourcesPlugin.getWorkspace();
+		final IProject piProjectHandle = ResourcesPlugin.getWorkspace().getRoot().getProject("pifilecompartor_temp"); //$NON-NLS-1$
+		final IProjectDescription description = workspace.newProjectDescription(piProjectHandle.getName());
+		SampleImporter sampleImporter = SampleImporter.getInstance();
+
+		// strip first file of timestamp by importer like trick
+		sampleImporter.setDatFileName(sourceFilePath);
+		sampleImporter.setPiFileName(dstFilePath);
+
+		setStripTimeStampActive();
+		
+		try {
+			if (!piProjectHandle.exists()) {
+				piProjectHandle.create(description, null);
+				piProjectHandle.open(null);
+			} else {
+				piProjectHandle.open(null);
+			}
+			
+			// import the new project according to wizard
+			IFile piFile = piProjectHandle.getFile("temp.npi"); //$NON-NLS-1$
+
+			if (piFile.exists())
+			{
+				piFile.delete(true, null);
+			}
+			// create the .pi on the project so IDE.openEditor works
+			piFile.create(new ByteArrayInputStream("".getBytes()), false, null); //$NON-NLS-1$
+
+			IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() , piFile, true);
+			// take out stalled temp.npi window
+			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAllEditors(true);
+
+		} catch (CoreException e) {
+			e.printStackTrace();
+		}
+		setStripTimeStampDone();
+	}
+		
+}