KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > wizards > datatransfer > ZipFileExportOperation


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

11 package org.eclipse.ui.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.ui.internal.ide.IDEWorkbenchPlugin;
31
32 /**
33  * Operation for exporting a resource and its children to a new .zip file
34  */

35 /*package*/
36 class ZipFileExportOperation implements IRunnableWithProgress {
37     private ZipFileExporter exporter;
38     private String JavaDoc destinationFilename;
39     private IProgressMonitor monitor;
40
41     private List JavaDoc resourcesToExport;
42     private IResource resource;
43     private List JavaDoc errorTable = new ArrayList JavaDoc(1); //IStatus
44

45     private boolean useCompression = true;
46     private boolean createLeadupStructure = true;
47     private boolean generateManifestFile = false;
48     /**
49      * Create an instance of this class. Use this constructor if you wish to
50      * export specific resources without a common parent resource
51      *
52      * @param resources java.util.Vector
53      * @param filename java.lang.String
54      */

55     public ZipFileExportOperation(List JavaDoc resources, String JavaDoc filename) {
56         super();
57
58         // Eliminate redundancies in list of resources being exported
59
Iterator JavaDoc elementsEnum = resources.iterator();
60         while (elementsEnum.hasNext()) {
61             IResource currentResource = (IResource) elementsEnum.next();
62             if (isDescendent(resources, currentResource))
63                 elementsEnum.remove(); //Removes currentResource;
64
}
65
66         resourcesToExport = resources;
67         destinationFilename = filename;
68     }
69     /**
70      * Create an instance of this class. Use this constructor if you wish
71      * to recursively export a single resource.
72      *
73      * @param res org.eclipse.core.resources.IResource;
74      * @param filename java.lang.String
75      */

76     public ZipFileExportOperation(IResource res, String JavaDoc filename) {
77         super();
78         resource = res;
79         destinationFilename = filename;
80     }
81     /**
82      * Create an instance of this class. Use this constructor if you wish to
83      * export specific resources with a common parent resource (affects container
84      * directory creation)
85      *
86      * @param res org.eclipse.core.resources.IResource
87      * @param resources java.util.Vector
88      * @param filename java.lang.String
89      */

90     public ZipFileExportOperation(
91         IResource res,
92         List JavaDoc resources,
93         String JavaDoc filename) {
94         this(res, filename);
95         resourcesToExport = resources;
96     }
97     /**
98      * Add a new entry to the error table with the passed information
99      */

100     protected void addError(String JavaDoc message, Throwable JavaDoc e) {
101         errorTable.add(
102             new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, 0, message, e));
103     }
104     /**
105      * Answer the total number of file resources that exist at or below self
106      * in the resources hierarchy.
107      *
108      * @return int
109      * @param checkResource org.eclipse.core.resources.IResource
110      */

111     protected int countChildrenOf(IResource checkResource) throws CoreException {
112         if (checkResource.getType() == IResource.FILE)
113             return 1;
114
115         int count = 0;
116         if (checkResource.isAccessible()) {
117             IResource[] children = ((IContainer) checkResource).members();
118             for (int i = 0; i < children.length; i++)
119                 count += countChildrenOf(children[i]);
120         }
121
122         return count;
123     }
124     /**
125      * Answer a boolean indicating the number of file resources that were
126      * specified for export
127      *
128      * @return int
129      */

130     protected int countSelectedResources() throws CoreException {
131         int result = 0;
132         Iterator JavaDoc resources = resourcesToExport.iterator();
133         while (resources.hasNext())
134             result += countChildrenOf((IResource) resources.next());
135
136         return result;
137     }
138
139     /**
140      * Export the passed resource to the destination .zip. Export with
141      * no path leadup
142      *
143      * @param exportResource org.eclipse.core.resources.IResource
144      */

145     protected void exportResource(IResource exportResource)
146         throws InterruptedException JavaDoc {
147         exportResource(exportResource, 1);
148     }
149
150     /**
151      * Export the passed resource to the destination .zip
152      *
153      * @param exportResource org.eclipse.core.resources.IResource
154      * @param depth - the number of resource levels to be included in
155      * the path including the resourse itself.
156      */

157     protected void exportResource(IResource exportResource, int leadupDepth)
158         throws InterruptedException JavaDoc {
159         if (!exportResource.isAccessible())
160             return;
161
162         if (exportResource.getType() == IResource.FILE) {
163             String JavaDoc destinationName;
164             IPath fullPath = exportResource.getFullPath();
165             if (createLeadupStructure)
166                 destinationName = fullPath.makeRelative().toString();
167             else
168                 destinationName =
169                     fullPath
170                         .removeFirstSegments(
171                             fullPath.segmentCount() - leadupDepth)
172                         .toString();
173             monitor.subTask(destinationName);
174
175             try {
176                 exporter.write((IFile) exportResource, destinationName);
177             } catch (IOException JavaDoc e) {
178                 addError(DataTransferMessages.format("DataTransfer.errorExporting", //$NON-NLS-1$
179
new Object JavaDoc[] {
180                     exportResource.getFullPath().makeRelative(),
181                     e.getMessage()}),
182                     e);
183             } catch (CoreException e) {
184                 addError(DataTransferMessages.format("DataTransfer.errorExporting", //$NON-NLS-1$
185
new Object JavaDoc[] {
186                     exportResource.getFullPath().makeRelative(),
187                     e.getMessage()}),
188                     e);
189             }
190
191             monitor.worked(1);
192             ModalContext.checkCanceled(monitor);
193         } else {
194             IResource[] children = null;
195
196             try {
197                 children = ((IContainer) exportResource).members();
198             } catch (CoreException e) {
199                 // this should never happen because an #isAccessible check is done before #members is invoked
200
addError(DataTransferMessages.format("DataTransfer.errorExporting", new Object JavaDoc[] { exportResource.getFullPath()}), e); //$NON-NLS-1$
201
}
202
203             for (int i = 0; i < children.length; i++)
204                 exportResource(children[i], leadupDepth + 1);
205
206         }
207     }
208     /**
209      * Export the resources contained in the previously-defined
210      * resourcesToExport collection
211      */

212     protected void exportSpecifiedResources() throws InterruptedException JavaDoc {
213         Iterator JavaDoc resources = resourcesToExport.iterator();
214
215         while (resources.hasNext()) {
216             IResource currentResource = (IResource) resources.next();
217             exportResource(currentResource);
218         }
219     }
220     /**
221      * Answer the error table
222      *
223      * @return Vector of IStatus
224      */

225     public List JavaDoc getResult() {
226         return errorTable;
227     }
228     /**
229      * Returns the status of the operation.
230      * If there were any errors, the result is a status object containing
231      * individual status objects for each error.
232      * If there were no errors, the result is a status object with error code <code>OK</code>.
233      *
234      * @return the status
235      */

236     public IStatus getStatus() {
237         IStatus[] errors = new IStatus[errorTable.size()];
238         errorTable.toArray(errors);
239         return new MultiStatus(IDEWorkbenchPlugin.IDE_WORKBENCH, IStatus.OK, errors, DataTransferMessages.getString("FileSystemExportOperation.problemsExporting"), //$NON-NLS-1$
240
null);
241     }
242     /**
243      * Initialize this operation
244      *
245      * @exception java.io.IOException
246      */

247     protected void initialize() throws IOException JavaDoc {
248         exporter =
249             new ZipFileExporter(
250                 destinationFilename,
251                 useCompression,
252                 generateManifestFile);
253
254     }
255     /**
256      * Answer a boolean indicating whether the passed child is a descendent
257      * of one or more members of the passed resources collection
258      *
259      * @return boolean
260      * @param resources java.util.Vector
261      * @param child org.eclipse.core.resources.IResource
262      */

263     protected boolean isDescendent(List JavaDoc resources, IResource child) {
264         if (child.getType() == IResource.PROJECT)
265             return false;
266
267         IResource parent = child.getParent();
268         if (resources.contains(parent))
269             return true;
270
271         return isDescendent(resources, parent);
272     }
273     /**
274      * Export the resources that were previously specified for export
275      * (or if a single resource was specified then export it recursively)
276      */

277     public void run(IProgressMonitor progressMonitor)
278         throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
279         this.monitor = progressMonitor;
280
281         try {
282             initialize();
283         } catch (IOException JavaDoc e) {
284             throw new InvocationTargetException JavaDoc(e, DataTransferMessages.format("ZipExport.cannotOpen", new Object JavaDoc[] { e.getMessage()})); //$NON-NLS-1$
285
}
286
287         try {
288             // ie.- a single resource for recursive export was specified
289
int totalWork = IProgressMonitor.UNKNOWN;
290             try {
291                 if (resourcesToExport == null)
292                     totalWork = countChildrenOf(resource);
293                 else
294                     totalWork = countSelectedResources();
295             } catch (CoreException e) {
296                 // Should not happen
297
}
298             monitor.beginTask(DataTransferMessages.getString("DataTransfer.exportingTitle"), totalWork); //$NON-NLS-1$
299
if (resourcesToExport == null) {
300                 exportResource(resource);
301             } else {
302                 // ie.- a list of specific resources to export was specified
303
exportSpecifiedResources();
304             }
305
306             try {
307                 exporter.finished();
308             } catch (IOException JavaDoc e) {
309                 throw new InvocationTargetException JavaDoc(e, DataTransferMessages.format("ZipExport.cannotClose", new Object JavaDoc[] { e.getMessage()})); //$NON-NLS-1$
310
}
311         } finally {
312             monitor.done();
313         }
314     }
315     /**
316      * Set this boolean indicating whether each exported resource's path should
317      * include containment hierarchies as dictated by its parents
318      *
319      * @param value boolean
320      */

321     public void setCreateLeadupStructure(boolean value) {
322         createLeadupStructure = value;
323     }
324     /**
325      * Set this boolean indicating whether a manifest.mf file based upon
326      * the exported contents should be created and included in the final
327      * archive
328      *
329      * @param value boolean
330      */

331     public void setGenerateManifestFile(boolean value) {
332         generateManifestFile = value;
333     }
334     /**
335      * Set this boolean indicating whether exported resources should
336      * be compressed (as opposed to simply being stored)
337      *
338      * @param value boolean
339      */

340     public void setUseCompression(boolean value) {
341         useCompression = value;
342     }
343 }
344
Popular Tags