KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > datatransfer > ArchiveFileExportOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.internal.wizards.datatransfer;
12
13 import java.io.IOException JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.MultiStatus;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.jface.operation.IRunnableWithProgress;
29 import org.eclipse.jface.operation.ModalContext;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
32
33 /**
34  * Operation for exporting a resource and its children to a new .zip or
35  * .tar.gz file.
36  *
37  * @since 3.1
38  */

39 public class ArchiveFileExportOperation implements IRunnableWithProgress {
40     private IFileExporter exporter;
41
42     private String JavaDoc destinationFilename;
43
44     private IProgressMonitor monitor;
45
46     private List JavaDoc resourcesToExport;
47
48     private IResource resource;
49
50     private List JavaDoc errorTable = new ArrayList JavaDoc(1); //IStatus
51

52     private boolean useCompression = true;
53     
54     private boolean useTarFormat = false;
55
56     private boolean createLeadupStructure = true;
57
58     /**
59      * Create an instance of this class. Use this constructor if you wish to
60      * export specific resources without a common parent resource
61      *
62      * @param resources java.util.Vector
63      * @param filename java.lang.String
64      */

65     public ArchiveFileExportOperation(List JavaDoc resources, String JavaDoc filename) {
66         super();
67
68         // Eliminate redundancies in list of resources being exported
69
Iterator JavaDoc elementsEnum = resources.iterator();
70         while (elementsEnum.hasNext()) {
71             IResource currentResource = (IResource) elementsEnum.next();
72             if (isDescendent(resources, currentResource)) {
73                 elementsEnum.remove(); //Removes currentResource;
74
}
75         }
76
77         resourcesToExport = resources;
78         destinationFilename = filename;
79     }
80
81     /**
82      * Create an instance of this class. Use this constructor if you wish
83      * to recursively export a single resource.
84      *
85      * @param res org.eclipse.core.resources.IResource;
86      * @param filename java.lang.String
87      */

88     public ArchiveFileExportOperation(IResource res, String JavaDoc filename) {
89         super();
90         resource = res;
91         destinationFilename = filename;
92     }
93
94     /**
95      * Create an instance of this class. Use this constructor if you wish to
96      * export specific resources with a common parent resource (affects container
97      * directory creation)
98      *
99      * @param res org.eclipse.core.resources.IResource
100      * @param resources java.util.Vector
101      * @param filename java.lang.String
102      */

103     public ArchiveFileExportOperation(IResource res, List JavaDoc resources, String JavaDoc filename) {
104         this(res, filename);
105         resourcesToExport = resources;
106     }
107
108     /**
109      * Add a new entry to the error table with the passed information
110      */

111     protected void addError(String JavaDoc message, Throwable JavaDoc e) {
112         errorTable.add(new Status(IStatus.ERROR,
113                 IDEWorkbenchPlugin.IDE_WORKBENCH, 0, message, e));
114     }
115
116     /**
117      * Answer the total number of file resources that exist at or below self
118      * in the resources hierarchy.
119      *
120      * @return int
121      * @param checkResource org.eclipse.core.resources.IResource
122      */

123     protected int countChildrenOf(IResource checkResource) throws CoreException {
124         if (checkResource.getType() == IResource.FILE) {
125             return 1;
126         }
127
128         int count = 0;
129         if (checkResource.isAccessible()) {
130             IResource[] children = ((IContainer) checkResource).members();
131             for (int i = 0; i < children.length; i++) {
132                 count += countChildrenOf(children[i]);
133             }
134         }
135
136         return count;
137     }
138
139     /**
140      * Answer a boolean indicating the number of file resources that were
141      * specified for export
142      *
143      * @return int
144      */

145     protected int countSelectedResources() throws CoreException {
146         int result = 0;
147         Iterator JavaDoc resources = resourcesToExport.iterator();
148         while (resources.hasNext()) {
149             result += countChildrenOf((IResource) resources.next());
150         }
151
152         return result;
153     }
154
155     /**
156      * Export the passed resource to the destination .zip. Export with
157      * no path leadup
158      *
159      * @param exportResource org.eclipse.core.resources.IResource
160      */

161     protected void exportResource(IResource exportResource)
162             throws InterruptedException JavaDoc {
163         exportResource(exportResource, 1);
164     }
165
166     /**
167      * Export the passed resource to the destination .zip
168      *
169      * @param exportResource org.eclipse.core.resources.IResource
170      * @param leadupDepth the number of resource levels to be included in
171      * the path including the resourse itself.
172      */

173     protected void exportResource(IResource exportResource, int leadupDepth)
174             throws InterruptedException JavaDoc {
175         if (!exportResource.isAccessible()) {
176             return;
177         }
178
179         if (exportResource.getType() == IResource.FILE) {
180             String JavaDoc destinationName;
181             IPath fullPath = exportResource.getFullPath();
182             if (createLeadupStructure) {
183                 destinationName = fullPath.makeRelative().toString();
184             } else {
185                 destinationName = fullPath.removeFirstSegments(
186                         fullPath.segmentCount() - leadupDepth).toString();
187             }
188             monitor.subTask(destinationName);
189
190             try {
191                 exporter.write((IFile) exportResource, destinationName);
192             } catch (IOException JavaDoc e) {
193                 addError(NLS.bind(DataTransferMessages.DataTransfer_errorExporting, exportResource.getFullPath().makeRelative(), e.getMessage()), e);
194             } catch (CoreException e) {
195                 addError(NLS.bind(DataTransferMessages.DataTransfer_errorExporting, exportResource.getFullPath().makeRelative(), e.getMessage()), e);
196             }
197
198             monitor.worked(1);
199             ModalContext.checkCanceled(monitor);
200         } else {
201             IResource[] children = null;
202
203             try {
204                 children = ((IContainer) exportResource).members();
205             } catch (CoreException e) {
206                 // this should never happen because an #isAccessible check is done before #members is invoked
207
addError(NLS.bind(DataTransferMessages.DataTransfer_errorExporting, exportResource.getFullPath()), e);
208             }
209
210             for (int i = 0; i < children.length; i++) {
211                 exportResource(children[i], leadupDepth + 1);
212             }
213
214         }
215     }
216
217     /**
218      * Export the resources contained in the previously-defined
219      * resourcesToExport collection
220      */

221     protected void exportSpecifiedResources() throws InterruptedException JavaDoc {
222         Iterator JavaDoc resources = resourcesToExport.iterator();
223
224         while (resources.hasNext()) {
225             IResource currentResource = (IResource) resources.next();
226             exportResource(currentResource);
227         }
228     }
229
230     /**
231      * Returns the status of the operation.
232      * If there were any errors, the result is a status object containing
233      * individual status objects for each error.
234      * If there were no errors, the result is a status object with error code <code>OK</code>.
235      *
236      * @return the status
237      */

238     public IStatus getStatus() {
239         IStatus[] errors = new IStatus[errorTable.size()];
240         errorTable.toArray(errors);
241         return new MultiStatus(
242                 IDEWorkbenchPlugin.IDE_WORKBENCH,
243                 IStatus.OK,
244                 errors,
245                 DataTransferMessages.FileSystemExportOperation_problemsExporting,
246                 null);
247     }
248
249     /**
250      * Initialize this operation
251      *
252      * @exception java.io.IOException
253      */

254     protected void initialize() throws IOException JavaDoc {
255         if(useTarFormat) {
256             exporter = new TarFileExporter(destinationFilename, useCompression);
257         } else {
258             exporter = new ZipFileExporter(destinationFilename, useCompression);
259         }
260     }
261
262     /**
263      * Answer a boolean indicating whether the passed child is a descendent
264      * of one or more members of the passed resources collection
265      *
266      * @return boolean
267      * @param resources java.util.Vector
268      * @param child org.eclipse.core.resources.IResource
269      */

270     protected boolean isDescendent(List JavaDoc resources, IResource child) {
271         if (child.getType() == IResource.PROJECT) {
272             return false;
273         }
274
275         IResource parent = child.getParent();
276         if (resources.contains(parent)) {
277             return true;
278         }
279
280         return isDescendent(resources, parent);
281     }
282
283     /**
284      * Export the resources that were previously specified for export
285      * (or if a single resource was specified then export it recursively)
286      */

287     public void run(IProgressMonitor progressMonitor)
288             throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
289         this.monitor = progressMonitor;
290
291         try {
292             initialize();
293         } catch (IOException JavaDoc e) {
294             throw new InvocationTargetException JavaDoc(e, NLS.bind(DataTransferMessages.ZipExport_cannotOpen, e.getMessage()));
295         }
296
297         try {
298             // ie.- a single resource for recursive export was specified
299
int totalWork = IProgressMonitor.UNKNOWN;
300             try {
301                 if (resourcesToExport == null) {
302                     totalWork = countChildrenOf(resource);
303                 } else {
304                     totalWork = countSelectedResources();
305                 }
306             } catch (CoreException e) {
307                 // Should not happen
308
}
309             monitor.beginTask(DataTransferMessages.DataTransfer_exportingTitle, totalWork);
310             if (resourcesToExport == null) {
311                 exportResource(resource);
312             } else {
313                 // ie.- a list of specific resources to export was specified
314
exportSpecifiedResources();
315             }
316
317             try {
318                 exporter.finished();
319             } catch (IOException JavaDoc e) {
320                 throw new InvocationTargetException JavaDoc(
321                         e,
322                         NLS.bind(DataTransferMessages.ZipExport_cannotClose, e.getMessage()));
323             }
324         } finally {
325             monitor.done();
326         }
327     }
328
329     /**
330      * Set this boolean indicating whether each exported resource's path should
331      * include containment hierarchies as dictated by its parents
332      *
333      * @param value boolean
334      */

335     public void setCreateLeadupStructure(boolean value) {
336         createLeadupStructure = value;
337     }
338
339     /**
340      * Set this boolean indicating whether exported resources should
341      * be compressed (as opposed to simply being stored)
342      *
343      * @param value boolean
344      */

345     public void setUseCompression(boolean value) {
346         useCompression = value;
347     }
348     
349     /**
350      * Set this boolean indicating whether the file should be output
351      * in tar.gz format rather than .zip format.
352      *
353      * @param value boolean
354      */

355     public void setUseTarFormat(boolean value) {
356         useTarFormat = value;
357     }
358 }
359
Popular Tags