KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > imports > JarImportOperation


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.imports;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.zip.ZipFile JavaDoc;
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 JavaDoc file, IPath destPath, IProgressMonitor monitor)
41             throws CoreException {
42         ZipFile JavaDoc zipFile = null;
43         try {
44             zipFile = new ZipFile JavaDoc(file);
45             ZipFileStructureProvider provider = new ZipFileStructureProvider(zipFile);
46             importContent(provider.getRoot(), destPath, provider, null, monitor);
47         } catch (IOException JavaDoc 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 JavaDoc e) {
56                 }
57             }
58         }
59     }
60
61     protected void importContent(Object JavaDoc source, IPath destPath,
62             IImportStructureProvider provider, List JavaDoc filesToImport,
63             IProgressMonitor monitor) throws CoreException {
64         IOverwriteQuery query = new IOverwriteQuery() {
65             public String JavaDoc queryOverwrite(String JavaDoc 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 JavaDoc e) {
77             IStatus status = new Status(IStatus.ERROR, PDEPlugin.getPluginId(),
78                     IStatus.ERROR, e.getMessage(), e);
79             throw new CoreException(status);
80         } catch (InterruptedException JavaDoc e) {
81             throw new OperationCanceledException(e.getMessage());
82         }
83     }
84
85     protected void extractResources(File JavaDoc file, IResource dest, IProgressMonitor monitor) throws CoreException {
86         ZipFile JavaDoc zipFile = null;
87         try {
88             zipFile = new ZipFile JavaDoc(file);
89             ZipFileStructureProvider provider = new ZipFileStructureProvider(zipFile);
90             ArrayList JavaDoc collected = new ArrayList JavaDoc();
91             collectResources(provider, provider.getRoot(), true, collected);
92             importContent(provider.getRoot(), dest.getFullPath(), provider, collected,
93                     monitor);
94         } catch (IOException JavaDoc 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 JavaDoc e) {
103                 }
104             }
105         }
106     }
107     
108     protected void extractJavaResources(File JavaDoc file, IResource dest, IProgressMonitor monitor) throws CoreException {
109         ZipFile JavaDoc zipFile = null;
110         try {
111             zipFile = new ZipFile JavaDoc(file);
112             ZipFileStructureProvider provider = new ZipFileStructureProvider(zipFile);
113             ArrayList JavaDoc collected = new ArrayList JavaDoc();
114             collectJavaResources(provider, provider.getRoot(), collected);
115             importContent(provider.getRoot(), dest.getFullPath(), provider, collected,
116                     monitor);
117         } catch (IOException JavaDoc 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 JavaDoc e) {
126                 }
127             }
128         }
129     }
130
131     protected void importArchive(IProject project, File JavaDoc 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 JavaDoc fstream = new FileInputStream JavaDoc(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 JavaDoc 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 JavaDoc element, boolean excludeMeta, ArrayList JavaDoc collected) {
151         List JavaDoc children = provider.getChildren(element);
152         if (children != null && !children.isEmpty()) {
153             for (int i = 0; i < children.size(); i++) {
154                 Object JavaDoc curr = children.get(i);
155                 if (provider.isFolder(curr)) {
156                     if (!excludeMeta || !provider.getLabel(curr).equals("META-INF")) { //$NON-NLS-1$
157
collectResources(provider, curr, excludeMeta, collected);
158                     }
159                 } else if (!provider.getLabel(curr).endsWith(".class")) { //$NON-NLS-1$
160
collected.add(curr);
161                 }
162             }
163         }
164     }
165
166     protected void collectNonJavaResources(ZipFileStructureProvider provider, Object JavaDoc element, ArrayList JavaDoc collected) {
167         List JavaDoc children = provider.getChildren(element);
168         if (children != null && !children.isEmpty()) {
169             for (int i = 0; i < children.size(); i++) {
170                 Object JavaDoc curr = children.get(i);
171                 if (provider.isFolder(curr)) {
172                     if (!provider.getLabel(curr).equals("src") && !isClassFolder(provider, curr)) { //$NON-NLS-1$
173
ArrayList JavaDoc list = new ArrayList JavaDoc();
174                         collectResources(provider, curr, false, list);
175                         collected.addAll(list);
176                     }
177                 } else if (!provider.getLabel(curr).endsWith(".class")) { //$NON-NLS-1$
178
collected.add(curr);
179                 }
180             }
181         }
182     }
183     
184     protected void collectJavaFiles(ZipFileStructureProvider provider, Object JavaDoc element, ArrayList JavaDoc collected) {
185         List JavaDoc children = provider.getChildren(element);
186         if (children != null && !children.isEmpty()) {
187             for (int i = 0; i < children.size(); i++) {
188                 Object JavaDoc curr = children.get(i);
189                 if (provider.isFolder(curr)) {
190                     if (provider.getLabel(curr).equals("src")) { //$NON-NLS-1$
191
ArrayList JavaDoc list = new ArrayList JavaDoc();
192                         collectResources(provider, curr, false, list);
193                         collected.addAll(list);
194                     }
195                 }
196             }
197         }
198     }
199     
200     protected void collectJavaResources(ZipFileStructureProvider provider, Object JavaDoc element, ArrayList JavaDoc collected) {
201         List JavaDoc children = provider.getChildren(element);
202         if (children != null && !children.isEmpty()) {
203             for (int i = 0; i < children.size(); i++) {
204                 Object JavaDoc curr = children.get(i);
205                 if (provider.isFolder(curr)) {
206                     if (isClassFolder(provider, curr)) {
207                         ArrayList JavaDoc list = new ArrayList JavaDoc();
208                         collectResources(provider, curr, false, list);
209                         collected.addAll(list);
210                     }
211                 }
212             }
213         }
214     }
215     
216     private boolean isClassFolder(ZipFileStructureProvider provider, Object JavaDoc element) {
217         List JavaDoc children = provider.getChildren(element);
218         if (children != null && !children.isEmpty()) {
219             for (int i = 0; i < children.size(); i++) {
220                 Object JavaDoc 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")) { //$NON-NLS-1$
226
return true;
227                 }
228             }
229         }
230         return false;
231     }
232     
233     protected boolean hasEmbeddedSource(ZipFileStructureProvider provider) {
234         List JavaDoc children = provider.getChildren(provider.getRoot());
235         if (children != null && !children.isEmpty()) {
236             for (int i = 0; i < children.size(); i++) {
237                 Object JavaDoc curr = children.get(i);
238                 if (provider.isFolder(curr) && provider.getLabel(curr).equals("src")) { //$NON-NLS-1$
239
return true;
240                 }
241             }
242         }
243         return false;
244     }
245     
246     protected boolean containsCode(ZipFileStructureProvider provider) {
247         List JavaDoc children = provider.getChildren(provider.getRoot());
248         if (children != null && !children.isEmpty()) {
249             for (int i = 0; i < children.size(); i++) {
250                 Object JavaDoc 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 JavaDoc file) {
260         ZipFile JavaDoc zipFile = null;
261         try {
262             zipFile = new ZipFile JavaDoc(file);
263             return containsCode(new ZipFileStructureProvider(zipFile));
264         } catch (IOException JavaDoc e) {
265         } finally {
266             if (zipFile != null) {
267                 try {
268                     zipFile.close();
269                 } catch (IOException JavaDoc e) {
270                 }
271             }
272         }
273         return true;
274     }
275     
276     protected String JavaDoc[] getTopLevelResources(File JavaDoc file) {
277         ArrayList JavaDoc result = new ArrayList JavaDoc();
278         ZipFile JavaDoc zipFile = null;
279         try {
280             zipFile = new ZipFile JavaDoc(file);
281             ZipFileStructureProvider provider = new ZipFileStructureProvider(zipFile);
282             List JavaDoc children = provider.getChildren(provider.getRoot());
283             if (children != null && !children.isEmpty()) {
284                 for (int i = 0; i < children.size(); i++) {
285                     Object JavaDoc curr = children.get(i);
286                     if (provider.isFolder(curr)) {
287                         if (!isClassFolder(provider, curr))
288                             result.add(provider.getLabel(curr) + "/"); //$NON-NLS-1$
289
else {
290                             if (!result.contains(".")) //$NON-NLS-1$
291
result.add("."); //$NON-NLS-1$
292
}
293                     } else {
294                         result.add(provider.getLabel(curr));
295                     }
296                 }
297             }
298         } catch (IOException JavaDoc e) {
299         } finally {
300             if (zipFile != null) {
301                 try {
302                     zipFile.close();
303                 } catch (IOException JavaDoc e) {
304                 }
305             }
306         }
307         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
308     }
309     
310 }
311
Popular Tags