1 11 package org.eclipse.pde.internal.ui.wizards.imports; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.lang.reflect.InvocationTargetException ; 17 import java.util.ArrayList ; 18 import java.util.List ; 19 import java.util.zip.ZipFile ; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.IWorkspaceRunnable; 25 import org.eclipse.core.runtime.CoreException; 26 import org.eclipse.core.runtime.IPath; 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.OperationCanceledException; 30 import org.eclipse.core.runtime.Status; 31 import org.eclipse.pde.internal.core.util.CoreUtility; 32 import org.eclipse.pde.internal.ui.PDEPlugin; 33 import org.eclipse.ui.dialogs.IOverwriteQuery; 34 import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider; 35 import org.eclipse.ui.wizards.datatransfer.ImportOperation; 36 import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider; 37 38 public abstract class JarImportOperation implements IWorkspaceRunnable { 39 40 protected void extractZipFile(File file, IPath destPath, IProgressMonitor monitor) 41 throws CoreException { 42 ZipFile zipFile = null; 43 try { 44 zipFile = new ZipFile (file); 45 ZipFileStructureProvider provider = new ZipFileStructureProvider(zipFile); 46 importContent(provider.getRoot(), destPath, provider, null, monitor); 47 } catch (IOException e) { 48 IStatus status = new Status(IStatus.ERROR, PDEPlugin.getPluginId(), 49 IStatus.ERROR, e.getMessage(), e); 50 throw new CoreException(status); 51 } finally { 52 if (zipFile != null) { 53 try { 54 zipFile.close(); 55 } catch (IOException e) { 56 } 57 } 58 } 59 } 60 61 protected void importContent(Object source, IPath destPath, 62 IImportStructureProvider provider, List filesToImport, 63 IProgressMonitor monitor) throws CoreException { 64 IOverwriteQuery query = new IOverwriteQuery() { 65 public String queryOverwrite(String file) { 66 return ALL; 67 } 68 }; 69 try { 70 ImportOperation op = new ImportOperation(destPath, source, provider, query); 71 op.setCreateContainerStructure(false); 72 if (filesToImport != null) { 73 op.setFilesToImport(filesToImport); 74 } 75 op.run(monitor); 76 } catch (InvocationTargetException e) { 77 IStatus status = new Status(IStatus.ERROR, PDEPlugin.getPluginId(), 78 IStatus.ERROR, e.getMessage(), e); 79 throw new CoreException(status); 80 } catch (InterruptedException e) { 81 throw new OperationCanceledException(e.getMessage()); 82 } 83 } 84 85 protected void extractResources(File file, IResource dest, IProgressMonitor monitor) throws CoreException { 86 ZipFile zipFile = null; 87 try { 88 zipFile = new ZipFile (file); 89 ZipFileStructureProvider provider = new ZipFileStructureProvider(zipFile); 90 ArrayList collected = new ArrayList (); 91 collectResources(provider, provider.getRoot(), true, collected); 92 importContent(provider.getRoot(), dest.getFullPath(), provider, collected, 93 monitor); 94 } catch (IOException e) { 95 IStatus status = new Status(IStatus.ERROR, PDEPlugin.getPluginId(), 96 IStatus.ERROR, e.getMessage(), e); 97 throw new CoreException(status); 98 } finally { 99 if (zipFile != null) { 100 try { 101 zipFile.close(); 102 } catch (IOException e) { 103 } 104 } 105 } 106 } 107 108 protected void extractJavaResources(File file, IResource dest, IProgressMonitor monitor) throws CoreException { 109 ZipFile zipFile = null; 110 try { 111 zipFile = new ZipFile (file); 112 ZipFileStructureProvider provider = new ZipFileStructureProvider(zipFile); 113 ArrayList collected = new ArrayList (); 114 collectJavaResources(provider, provider.getRoot(), collected); 115 importContent(provider.getRoot(), dest.getFullPath(), provider, collected, 116 monitor); 117 } catch (IOException e) { 118 IStatus status = new Status(IStatus.ERROR, PDEPlugin.getPluginId(), 119 IStatus.ERROR, e.getMessage(), e); 120 throw new CoreException(status); 121 } finally { 122 if (zipFile != null) { 123 try { 124 zipFile.close(); 125 } catch (IOException e) { 126 } 127 } 128 } 129 } 130 131 protected void importArchive(IProject project, File archive, IPath destPath) 132 throws CoreException { 133 try { 134 if (destPath.segmentCount() > 2) 135 CoreUtility.createFolder(project.getFolder(destPath.removeLastSegments(1))); 136 IFile file = project.getFile(destPath); 137 FileInputStream fstream = new FileInputStream (archive); 138 if (file.exists()) 139 file.setContents(fstream, true, false, null); 140 else 141 file.create(fstream, true, null); 142 fstream.close(); 143 } catch (IOException e) { 144 IStatus status = new Status(IStatus.ERROR, PDEPlugin.getPluginId(), 145 IStatus.OK, e.getMessage(), e); 146 throw new CoreException(status); 147 } 148 } 149 150 private void collectResources(ZipFileStructureProvider provider, Object element, boolean excludeMeta, ArrayList collected) { 151 List children = provider.getChildren(element); 152 if (children != null && !children.isEmpty()) { 153 for (int i = 0; i < children.size(); i++) { 154 Object curr = children.get(i); 155 if (provider.isFolder(curr)) { 156 if (!excludeMeta || !provider.getLabel(curr).equals("META-INF")) { collectResources(provider, curr, excludeMeta, collected); 158 } 159 } else if (!provider.getLabel(curr).endsWith(".class")) { collected.add(curr); 161 } 162 } 163 } 164 } 165 166 protected void collectNonJavaResources(ZipFileStructureProvider provider, Object element, ArrayList collected) { 167 List children = provider.getChildren(element); 168 if (children != null && !children.isEmpty()) { 169 for (int i = 0; i < children.size(); i++) { 170 Object curr = children.get(i); 171 if (provider.isFolder(curr)) { 172 if (!provider.getLabel(curr).equals("src") && !isClassFolder(provider, curr)) { ArrayList list = new ArrayList (); 174 collectResources(provider, curr, false, list); 175 collected.addAll(list); 176 } 177 } else if (!provider.getLabel(curr).endsWith(".class")) { collected.add(curr); 179 } 180 } 181 } 182 } 183 184 protected void collectJavaFiles(ZipFileStructureProvider provider, Object element, ArrayList collected) { 185 List children = provider.getChildren(element); 186 if (children != null && !children.isEmpty()) { 187 for (int i = 0; i < children.size(); i++) { 188 Object curr = children.get(i); 189 if (provider.isFolder(curr)) { 190 if (provider.getLabel(curr).equals("src")) { ArrayList list = new ArrayList (); 192 collectResources(provider, curr, false, list); 193 collected.addAll(list); 194 } 195 } 196 } 197 } 198 } 199 200 protected void collectJavaResources(ZipFileStructureProvider provider, Object element, ArrayList collected) { 201 List children = provider.getChildren(element); 202 if (children != null && !children.isEmpty()) { 203 for (int i = 0; i < children.size(); i++) { 204 Object curr = children.get(i); 205 if (provider.isFolder(curr)) { 206 if (isClassFolder(provider, curr)) { 207 ArrayList list = new ArrayList (); 208 collectResources(provider, curr, false, list); 209 collected.addAll(list); 210 } 211 } 212 } 213 } 214 } 215 216 private boolean isClassFolder(ZipFileStructureProvider provider, Object element) { 217 List children = provider.getChildren(element); 218 if (children != null && !children.isEmpty()) { 219 for (int i = 0; i < children.size(); i++) { 220 Object curr = children.get(i); 221 if (provider.isFolder(curr)) { 222 if (isClassFolder(provider, curr)) { 223 return true; 224 } 225 } else if (provider.getLabel(curr).endsWith(".class")) { return true; 227 } 228 } 229 } 230 return false; 231 } 232 233 protected boolean hasEmbeddedSource(ZipFileStructureProvider provider) { 234 List children = provider.getChildren(provider.getRoot()); 235 if (children != null && !children.isEmpty()) { 236 for (int i = 0; i < children.size(); i++) { 237 Object curr = children.get(i); 238 if (provider.isFolder(curr) && provider.getLabel(curr).equals("src")) { return true; 240 } 241 } 242 } 243 return false; 244 } 245 246 protected boolean containsCode(ZipFileStructureProvider provider) { 247 List children = provider.getChildren(provider.getRoot()); 248 if (children != null && !children.isEmpty()) { 249 for (int i = 0; i < children.size(); i++) { 250 Object curr = children.get(i); 251 if (provider.isFolder(curr) && isClassFolder(provider, curr)) { 252 return true; 253 } 254 } 255 } 256 return false; 257 } 258 259 protected boolean containsCode(File file) { 260 ZipFile zipFile = null; 261 try { 262 zipFile = new ZipFile (file); 263 return containsCode(new ZipFileStructureProvider(zipFile)); 264 } catch (IOException e) { 265 } finally { 266 if (zipFile != null) { 267 try { 268 zipFile.close(); 269 } catch (IOException e) { 270 } 271 } 272 } 273 return true; 274 } 275 276 protected String [] getTopLevelResources(File file) { 277 ArrayList result = new ArrayList (); 278 ZipFile zipFile = null; 279 try { 280 zipFile = new ZipFile (file); 281 ZipFileStructureProvider provider = new ZipFileStructureProvider(zipFile); 282 List children = provider.getChildren(provider.getRoot()); 283 if (children != null && !children.isEmpty()) { 284 for (int i = 0; i < children.size(); i++) { 285 Object curr = children.get(i); 286 if (provider.isFolder(curr)) { 287 if (!isClassFolder(provider, curr)) 288 result.add(provider.getLabel(curr) + "/"); else { 290 if (!result.contains(".")) result.add("."); } 293 } else { 294 result.add(provider.getLabel(curr)); 295 } 296 } 297 } 298 } catch (IOException e) { 299 } finally { 300 if (zipFile != null) { 301 try { 302 zipFile.close(); 303 } catch (IOException e) { 304 } 305 } 306 } 307 return (String [])result.toArray(new String [result.size()]); 308 } 309 310 } 311 | Popular Tags |