1 11 package org.eclipse.update.internal.ui.wizards; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.Enumeration ; 18 import java.util.zip.ZipEntry ; 19 import java.util.zip.ZipFile ; 20 21 import org.eclipse.jface.dialogs.MessageDialog; 22 import org.eclipse.swt.custom.BusyIndicator; 23 import org.eclipse.swt.widgets.DirectoryDialog; 24 import org.eclipse.swt.widgets.FileDialog; 25 import org.eclipse.swt.widgets.Shell; 26 import org.eclipse.update.internal.ui.UpdateUI; 27 import org.eclipse.update.internal.ui.UpdateUIMessages; 28 import org.eclipse.update.internal.ui.model.SiteBookmark; 29 30 32 public class LocalSiteSelector { 33 34 37 static String lastLocation = null; 38 public LocalSiteSelector() { 39 super(); 40 } 41 public static SiteBookmark getLocaLSite(Shell parent, SiteBookmark[] siteBookmarks) { 42 DirectoryDialog dialog = new DirectoryDialog(parent); 43 dialog.setMessage( 44 UpdateUIMessages.LocalSiteSelector_dialogMessage); 45 dialog.setFilterPath(lastLocation); 46 String dir = dialog.open(); 47 48 SiteBookmark siteBookmark = null; 49 while (dir != null && siteBookmark == null) { 50 File dirFile = new File (dir); 51 if (isDirSite(dirFile)) { 52 if (!isDuplicate( dirFile, siteBookmarks)) { 53 siteBookmark = createDirSite(dirFile); 54 lastLocation = dir; 55 } else { 56 MessageDialog.openInformation(parent, 57 UpdateUIMessages.LocalSiteSelector_dirInfoTitle, 58 UpdateUIMessages.LocalSiteSelector_dirDuplicateDefinition); 59 dialog.setFilterPath(dir); 60 dir = dialog.open(); 61 } 62 } else { 63 MessageDialog.openInformation( 64 parent, 65 UpdateUIMessages.LocalSiteSelector_dirInfoTitle, 66 UpdateUIMessages.LocalSiteSelector_dirInfoMessage); 67 dialog.setFilterPath(dir); 68 dir = dialog.open(); 69 } 70 } 71 return siteBookmark; 72 } 73 public static SiteBookmark getLocaLZippedSite(Shell parent, SiteBookmark[] siteBookmarks) { 74 FileDialog dialog = new FileDialog(parent); 75 dialog.setText( 76 UpdateUIMessages.LocalSiteSelector_dialogMessagezip); 77 dialog.setFilterExtensions(new String [] { "*.jar;*.zip" }); 81 SiteBookmark siteBookmark = null; 82 83 String zip = dialog.open(); 84 while (zip != null && siteBookmark == null) { 85 File zipF = new File (zip); 86 if (isZipSite(zipF)) { 87 siteBookmark = createZipSite(zipF); 88 if (isDuplicate( siteBookmark.getURL(), siteBookmarks)) { 89 MessageDialog.openInformation(parent, 90 UpdateUIMessages.LocalSiteSelector_zipInfoTitle, 91 UpdateUIMessages.LocalSiteSelector_zipDuplicateDefinition); 92 siteBookmark = null; 93 zip = dialog.open(); 94 } 95 } else { 96 MessageDialog.openInformation( 97 parent, 98 UpdateUIMessages.LocalSiteSelector_zipInfoTitle, 99 UpdateUIMessages.LocalSiteSelector_zipInfoMessage); 100 zip = dialog.open(); 101 } 102 } 103 return siteBookmark; 104 } 105 111 static boolean isZipSite(File file) { 112 if (!file.getName().toLowerCase().endsWith(".zip") && !file.getName().toLowerCase().endsWith(".jar")) { return false; 115 } 116 117 ZippedSiteValidator validator = new ZippedSiteValidator(file); 118 BusyIndicator.showWhile( 119 UpdateUI.getActiveWorkbenchShell().getDisplay(), 120 validator); 121 return validator.isValid(); 122 } 123 124 130 static boolean isDirSite(File dir) { 131 File siteXML = new File (dir, "site.xml"); File featuresDir = new File (dir, "features"); File pluginsDir = new File (dir, "plugins"); return siteXML.exists() 135 || featuresDir.exists() 136 && featuresDir.isDirectory() 137 && pluginsDir.exists() 138 && pluginsDir.isDirectory(); 139 } 140 141 147 static SiteBookmark createZipSite(File file) { 148 try { 149 URL fileURL = new URL ("file", null, file.getAbsolutePath()); URL url = 151 new URL ( 152 "jar:" + fileURL.toExternalForm().replace('\\', '/') 154 + "!/"); SiteBookmark site = new SiteBookmark(file.getName(), url, false); 156 site.setLocal(true); 157 return site; 158 } catch (Exception e) { 159 return null; 160 } 161 } 162 163 169 static SiteBookmark createDirSite(File file) { 170 try { 171 URL url = file.toURL(); 172 String parent = file.getParent(); 173 if(parent == null) 174 parent = ""; else 176 parent = new File (parent).getName(); 177 String siteName = parent + "/" + file.getName(); SiteBookmark site = new SiteBookmark(siteName, url, false); 179 site.setLocal(true); 180 return site; 181 } catch (Exception e) { 182 return null; 183 } 184 } 185 static class ZippedSiteValidator implements Runnable { 186 File file; 187 boolean valid = false; 188 public ZippedSiteValidator(File file) { 189 this.file = file; 190 } 191 public void run() { 192 ZipFile siteZip = null; 193 try { 194 siteZip = new ZipFile (file); 196 if (siteZip.getEntry("site.xml") != null) { valid = true; 198 return; 199 } 200 201 boolean hasFeatures = false; 202 boolean hasPlugins = false; 203 for (Enumeration iterator = siteZip.entries(); 204 iterator.hasMoreElements(); 205 ) { 206 ZipEntry zEntry = (ZipEntry ) iterator.nextElement(); 207 if (!hasFeatures 208 && zEntry.getName().startsWith("features")) { hasFeatures = true; 210 } 211 if (!hasPlugins 212 && zEntry.getName().startsWith("plugins")) { hasPlugins = true; 214 } 215 if (hasFeatures && hasPlugins) { 216 valid = true; 217 return; 218 } 219 } 220 } catch (Exception e) { 221 } finally { 222 try { 223 if (siteZip != null) { 224 siteZip.close(); 225 } 226 } catch (IOException ioe) { 227 } 228 } 229 230 } 231 233 public boolean isValid() { 234 return valid; 235 } 236 } 237 238 private static boolean isDuplicate( File file, SiteBookmark[] siteBookmarks) { 239 240 try { 241 return isDuplicate(file.toURL(), siteBookmarks); 242 } catch (MalformedURLException mue) { 243 UpdateUI.logException(mue); 245 } 246 247 return false; 248 } 249 250 private static boolean isDuplicate( URL url, SiteBookmark[] siteBookmarks) { 251 252 if ( siteBookmarks == null) 253 return false; 254 255 for( int i = 0; i < siteBookmarks.length; i++) { 256 if (siteBookmarks[i].getURL().equals(url)) 257 return true; 258 259 } 260 return false; 261 262 } 263 264 } 265 | Popular Tags |