Bug 2888 - IDE does not properly explain the reason for failure if the WGZ file is broken
authorEugene Ostroukhov <eugeneo@symbian.org>
Mon, 07 Jun 2010 13:48:20 -0700
changeset 351 e317b108976d
parent 350 9db385b0ca89
child 352 9e80d9bd54fe
Bug 2888 - IDE does not properly explain the reason for failure if the WGZ file is broken
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/importWizards/ImportWizard.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/importWizards/ImportWizardPage.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/ProjectUtils.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WgzImportWizard.java
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WgzImportWizardPage.java
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/importWizards/ImportWizard.java	Mon Jun 07 11:54:39 2010 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,74 +0,0 @@
-/**
- * Copyright (c) 2009 Symbian Foundation 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:
- * Symbian Foundation - initial contribution.
- * Contributors:
- * Description:
- * Overview:
- * Details:
- * Platforms/Drives/Compatibility:
- * Assumptions/Requirement/Pre-requisites:
- * Failures and causes:
- */
-package org.symbian.tools.wrttools.importWizards;
-
-/*******************************************************************************
- * Copyright (c) 2006 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *     IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.wizard.Wizard;
-import org.eclipse.ui.IImportWizard;
-import org.eclipse.ui.IWorkbench;
-
-public class ImportWizard extends Wizard implements IImportWizard {
-	
-	ImportWizardPage mainPage;
-
-	public ImportWizard() {
-		super();
-	}
-
-	/* (non-Javadoc)
-	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
-	 */
-	public boolean performFinish() {
-		IFile file = mainPage.createNewFile();
-        if (file == null)
-            return false;
-        return true;
-	}
-	 
-	/* (non-Javadoc)
-	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
-	 */
-	public void init(IWorkbench workbench, IStructuredSelection selection) {
-		setWindowTitle("File Import Wizard"); //NON-NLS-1
-		setNeedsProgressMonitor(true);
-		mainPage = new ImportWizardPage("Import File",selection); //NON-NLS-1
-	}
-	
-	/* (non-Javadoc)
-     * @see org.eclipse.jface.wizard.IWizard#addPages()
-     */
-    public void addPages() {
-        super.addPages(); 
-        addPage(mainPage);        
-    }
-
-}
-
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/importWizards/ImportWizardPage.java	Mon Jun 07 11:54:39 2010 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,119 +0,0 @@
-/**
- * Copyright (c) 2009 Symbian Foundation 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:
- * Symbian Foundation - initial contribution.
- * Contributors:
- * Description:
- * Overview:
- * Details:
- * Platforms/Drives/Compatibility:
- * Assumptions/Requirement/Pre-requisites:
- * Failures and causes:
- */
-package org.symbian.tools.wrttools.importWizards;
-/*******************************************************************************
- * Copyright (c) 2006 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *     IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.jface.preference.FileFieldEditor;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.ModifyEvent;
-import org.eclipse.swt.events.ModifyListener;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
-
-
-public class ImportWizardPage extends WizardNewFileCreationPage {
-	
-	protected FileFieldEditor editor;
-
-	public ImportWizardPage(String pageName, IStructuredSelection selection) {
-		super(pageName, selection);
-		setTitle(pageName); //NON-NLS-1
-		setDescription("Import a file from the local file system into the workspace"); //NON-NLS-1
-	}
-
-	/* (non-Javadoc)
-	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#createAdvancedControls(org.eclipse.swt.widgets.Composite)
-	 */	
-	protected void createAdvancedControls(Composite parent) {
-		Composite fileSelectionArea = new Composite(parent, SWT.NONE);
-		GridData fileSelectionData = new GridData(GridData.GRAB_HORIZONTAL
-				| GridData.FILL_HORIZONTAL);
-		fileSelectionArea.setLayoutData(fileSelectionData);
-
-		GridLayout fileSelectionLayout = new GridLayout();
-		fileSelectionLayout.numColumns = 3;
-		fileSelectionLayout.makeColumnsEqualWidth = false;
-		fileSelectionLayout.marginWidth = 0;
-		fileSelectionLayout.marginHeight = 0;
-		fileSelectionArea.setLayout(fileSelectionLayout);
-		
-		editor = new FileFieldEditor("fileSelect","Select File: ",fileSelectionArea); //NON-NLS-1 //NON-NLS-2
-		editor.getTextControl(fileSelectionArea).addModifyListener(new ModifyListener(){
-			public void modifyText(ModifyEvent e) {
-				IPath path = new Path(ImportWizardPage.this.editor.getStringValue());
-				setFileName(path.lastSegment());
-			}
-		});
-		String[] extensions = new String[] { "*.*" }; //NON-NLS-1
-		editor.setFileExtensions(extensions);
-		fileSelectionArea.moveAbove(null);
-
-	}
-	
-	 /* (non-Javadoc)
-	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#createLinkTarget()
-	 */
-	protected void createLinkTarget() {
-	}
-
-	/* (non-Javadoc)
-	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#getInitialContents()
-	 */
-	protected InputStream getInitialContents() {
-		try {
-			return new FileInputStream(new File(editor.getStringValue()));
-		} catch (FileNotFoundException e) {
-			return null;
-		}
-	}
-
-	/* (non-Javadoc)
-	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#getNewFileLabel()
-	 */
-	protected String getNewFileLabel() {
-		return "New File Name:"; //NON-NLS-1
-	}
-
-	/* (non-Javadoc)
-	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#validateLinkedResource()
-	 */
-	protected IStatus validateLinkedResource() {
-		return new Status(IStatus.OK, "org.symbian.tools.wrt", IStatus.OK, "", null); //NON-NLS-1 //NON-NLS-2
-	}
-}
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/ProjectUtils.java	Mon Jun 07 11:54:39 2010 -0700
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/ProjectUtils.java	Mon Jun 07 13:48:20 2010 -0700
@@ -380,10 +380,11 @@
             IProgressMonitor progressMonitor) throws IOException, CoreException {
         progressMonitor.beginTask(MessageFormat.format("Unpacking {0}", label), IProgressMonitor.UNKNOWN);
         ZipInputStream stream = new ZipInputStream(in);
-
         try {
             ZipEntry nextEntry;
+            int count = 0;
             while ((nextEntry = stream.getNextEntry()) != null) {
+                count++;
                 IPath p = new Path(nextEntry.getName()).removeFirstSegments(trimSegments);
                 if (!isIgnored(p) && !nextEntry.isDirectory()) {
                     IFile file = location.getFile(p);
@@ -396,6 +397,10 @@
                     }
                 }
             }
+            if (count == 0) {
+                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+                        "Selected archive file does not contain application files"));
+            }
         } finally {
             stream.close();
         }
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WgzImportWizard.java	Mon Jun 07 11:54:39 2010 -0700
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WgzImportWizard.java	Mon Jun 07 13:48:20 2010 -0700
@@ -16,6 +16,7 @@
 import org.eclipse.core.runtime.IExecutableExtension;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubProgressMonitor;
 import org.eclipse.jface.operation.IRunnableWithProgress;
@@ -56,12 +57,18 @@
 				new SubProgressMonitor(monitor, 10));
 
 		// 2. Unpack archive
+        boolean success = false;
 		try {
 			ProjectUtils.unzip(archiveName, project, 1, new SubProgressMonitor(
 					monitor, 40));
+            success = true;
 		} catch (IOException e) {
-			new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
 					"Archive unpacking failed", e));
+        } finally {
+            if (!success) {
+                project.delete(true, true, new NullProgressMonitor());
+            }
 		}
 		monitor.done();
 		return project;
--- a/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WgzImportWizardPage.java	Mon Jun 07 11:54:39 2010 -0700
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WgzImportWizardPage.java	Mon Jun 07 13:48:20 2010 -0700
@@ -1,8 +1,12 @@
 package org.symbian.tools.wrttools.wizards;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.net.URI;
 import java.text.MessageFormat;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
@@ -40,6 +44,7 @@
 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter;
 import org.symbian.tools.wrttools.Activator;
 import org.symbian.tools.wrttools.core.WrtIdeCorePreferences;
+import org.symbian.tools.wrttools.util.CoreUtil;
 
 @SuppressWarnings("restriction")
 public class WgzImportWizardPage extends WizardPage {
@@ -400,7 +405,7 @@
 		}
 		
 		File f = new File(archive);
-		if (!f.isFile()) {
+        if (!isValidArchive(f)) {
 			setErrorMessage(MessageFormat.format("{0} is not a valid WRT archive", archive));
 			return false;
 		}
@@ -441,4 +446,22 @@
 		return true;
 	}
 
+    private boolean isValidArchive(File f) {
+        if (f.isFile()) {
+            try {
+                final ZipInputStream stream = new ZipInputStream(new FileInputStream(f));
+                ZipEntry entry;
+                while ((entry = stream.getNextEntry()) != null) {
+                    final IPath path = new Path(entry.getName());
+                    if (path.segmentCount() == 2 && CoreUtil.METADATA_FILE.equals(path.segment(1))) {
+                        return true;
+                    }
+                }
+            } catch (IOException e) {
+                // Not a valid archive
+            }
+        }
+        return false;
+    }
+
 }