blob: 9a32e2ba12411b793340fd2f57c8e6b5fb182bde [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.update.internal.ui.wizards;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.*;
import org.eclipse.jface.dialogs.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.update.internal.ui.*;
import org.eclipse.update.internal.ui.model.*;
/**
*/
public class LocalSiteSelector {
/**
*
*/
static String lastLocation = null;
public LocalSiteSelector() {
super();
// TODO Auto-generated constructor stub
}
public static SiteBookmark getLocaLSite(Shell parent) {
DirectoryDialog dialog = new DirectoryDialog(parent);
dialog.setMessage(
UpdateUI.getString("LocalSiteSelector.dialogMessage"));
dialog.setFilterPath(lastLocation);
String dir = dialog.open();
SiteBookmark siteBookmark = null;
while (dir != null && siteBookmark == null) {
File dirFile = new File(dir);
if (isDirSite(dirFile)) {
siteBookmark = createDirSite(dirFile);
lastLocation = dir;
} else {
MessageDialog.openInformation(
parent,
UpdateUI.getString("LocalSiteSelector.dirInfoTitle"),
UpdateUI.getString("LocalSiteSelector.dirInfoMessage"));
dialog.setFilterPath(dir);
dir = dialog.open();
}
}
return siteBookmark;
}
public static SiteBookmark getLocaLZippedSite(Shell parent) {
FileDialog dialog = new FileDialog(parent);
dialog.setText(
UpdateUI.getString("LocalSiteSelector.dialogMessagezip"));
//dialog.setFilterExtensions(new String[] { "*.zip", "*.jar" });
// //$NON-NLS-1$
dialog.setFilterExtensions(new String[] { "*.jar;*.zip" }); //$NON-NLS-1$
SiteBookmark siteBookmark = null;
String zip = dialog.open();
while (zip != null && siteBookmark == null) {
File zipF = new File(zip);
if (isZipSite(zipF)) {
siteBookmark = createZipSite(zipF);
} else {
MessageDialog.openInformation(
parent,
UpdateUI.getString("LocalSiteSelector.zipInfoTitle"),
UpdateUI.getString("LocalSiteSelector.zipInfoMessage"));
zip = dialog.open();
}
}
return siteBookmark;
}
/**
* Returns true the zip file contains an update site
*
* @param file
* @return
*/
static boolean isZipSite(File file) {
if (!file.getName().toLowerCase().endsWith(".zip")
&& !file.getName().toLowerCase().endsWith(".jar")) {
return false;
}
ZippedSiteValidator validator = new ZippedSiteValidator(file);
BusyIndicator.showWhile(
UpdateUI.getActiveWorkbenchShell().getDisplay(),
validator);
return validator.isValid();
}
/**
* Returns true if the specified dir contains an update site
*
* @param dir
* @return
*/
static boolean isDirSite(File dir) {
File siteXML = new File(dir, "site.xml"); //$NON-NLS-1$
File featuresDir = new File(dir, "features"); //$NON-NLS-1$
File pluginsDir = new File(dir, "plugins"); //$NON-NLS-1$
return siteXML.exists()
|| featuresDir.exists()
&& featuresDir.isDirectory()
&& pluginsDir.exists()
&& pluginsDir.isDirectory();
}
/**
* Creates a bookmark to a zipped site
*
* @param file
* @return
*/
static SiteBookmark createZipSite(File file) {
try {
URL fileURL = new URL("file", null, file.getAbsolutePath());
URL url =
new URL(
"jar:"
+ fileURL.toExternalForm().replace('\\', '/')
+ "!/");
SiteBookmark site = new SiteBookmark(file.getName(), url, false);
site.setLocal(true);
return site;
} catch (Exception e) {
return null;
}
}
/**
* Creates a bookmark to a site on the file system
*
* @param file
* @return
*/
static SiteBookmark createDirSite(File file) {
try {
URL url = new URL("file:" + file.getAbsolutePath() + File.separator); //$NON-NLS-1$
String siteName = file.getAbsolutePath();
SiteBookmark site = new SiteBookmark(siteName, url, false);
site.setLocal(true);
return site;
} catch (Exception e) {
return null;
}
}
static class ZippedSiteValidator implements Runnable {
File file;
boolean valid = false;
public ZippedSiteValidator(File file) {
this.file = file;
}
public void run() {
ZipFile siteZip = null;
try {
// check if the zip file contains site.xml
siteZip = new ZipFile(file);
if (siteZip.getEntry("site.xml") != null) {
valid = true;
return;
}
boolean hasFeatures = false;
boolean hasPlugins = false;
for (Enumeration enum = siteZip.entries();
enum.hasMoreElements();
) {
ZipEntry zEntry = (ZipEntry) enum.nextElement();
if (!hasFeatures
&& zEntry.getName().startsWith("features")) {
hasFeatures = true;
}
if (!hasPlugins
&& zEntry.getName().startsWith("plugins")) {
hasPlugins = true;
}
if (hasFeatures && hasPlugins) {
valid = true;
return;
}
}
} catch (Exception e) {
} finally {
try {
if (siteZip != null) {
siteZip.close();
}
} catch (IOException ioe) {
}
}
}
/**
* @return Returns the valid. */
public boolean isValid() {
return valid;
}
}
}