tmf: Fix the progress monitor in package export and import
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / wizards / tracepkg / AbstractTracePackageOperation.java
CommitLineData
6e651d8b 1/*******************************************************************************
8d03681c 2 * Copyright (c) 2013, 2014 Ericsson
6e651d8b
MAL
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg;
14
15import java.io.IOException;
16import java.io.InputStream;
17import java.util.Enumeration;
18import java.util.Vector;
19import java.util.zip.ZipEntry;
20import java.util.zip.ZipException;
21import java.util.zip.ZipFile;
22
23import org.eclipse.core.runtime.IProgressMonitor;
24import org.eclipse.core.runtime.IStatus;
25import org.eclipse.ui.internal.wizards.datatransfer.TarEntry;
26import org.eclipse.ui.internal.wizards.datatransfer.TarException;
27import org.eclipse.ui.internal.wizards.datatransfer.TarFile;
28
29/**
30 * An abstract operation containing common code useful for other trace package
31 * operations
32 *
33 * @author Marc-Andre Laperle
34 */
35@SuppressWarnings("restriction")
36abstract public class AbstractTracePackageOperation {
37 private IStatus fStatus;
8d03681c
MAL
38 // Result of this operation, if any
39 private TracePackageElement[] fResultElements;
40
6e651d8b
MAL
41 private final String fFileName;
42
43 /**
44 * Constructs a new trace package operation
45 *
46 * @param fileName
47 * the output file name
48 */
49 public AbstractTracePackageOperation(String fileName) {
50 fFileName = fileName;
51 }
52
53 /**
54 * Run the operation. The status (result) of the operation can be obtained
55 * with {@link #getStatus}
56 *
57 * @param progressMonitor
58 * the progress monitor to use to display progress and receive
59 * requests for cancellation
60 */
61 public abstract void run(IProgressMonitor progressMonitor);
62
63 /**
64 * Returns the status of the operation (result)
65 *
66 * @return the status of the operation
67 */
68 public IStatus getStatus() {
69 return fStatus;
70 }
71
8d03681c
MAL
72 /**
73 * Get the resulting elements for this operation, if any
74 *
75 * @return the resulting elements or null if no result is produced by this
76 * operation
77 */
78 public TracePackageElement[] getResultElements() {
79 return fResultElements;
80 }
81
82 /**
83 * Set the resulting elements for this operation, if any
84 *
85 * @param elements
86 * the resulting elements produced by this operation, can be set
87 * to null
88 */
89 public void setResultElements(TracePackageElement[] elements) {
90 fResultElements = elements;
91 }
92
6e651d8b
MAL
93 /**
94 * Set the status for this operation
95 *
96 * @param status
97 * the status
98 */
99 protected void setStatus(IStatus status) {
100 fStatus = status;
101 }
102
103 /**
104 * Get the file name of the package
105 *
106 * @return the file name
107 */
108 protected String getFileName() {
109 return fFileName;
110 }
111
112 /**
113 * Answer a handle to the archive file currently specified as being the
114 * source. Return null if this file does not exist or is not of valid
115 * format.
116 *
117 * @return the archive file
118 */
119 public ArchiveFile getSpecifiedArchiveFile() {
120 if (fFileName.length() == 0) {
121 return null;
122 }
123
124 try {
125 ZipFile zipFile = new ZipFile(fFileName);
126 return new ZipArchiveFile(zipFile);
127 } catch (ZipException e) {
128 // ignore
129 } catch (IOException e) {
130 // ignore
131 }
132
133 try {
134 TarFile tarFile = new TarFile(fFileName);
135 return new TarArchiveFile(tarFile);
136 } catch (TarException e) {
137 // ignore
138 } catch (IOException e) {
139 // ignore
140 }
141
142 return null;
143 }
144
145 /**
146 * Get the number of checked elements in the array and the children
147 *
148 * @param elements
149 * the elements to check for checked
150 * @return the number of checked elements
151 */
152 protected int getNbCheckedElements(TracePackageElement[] elements) {
153 int totalWork = 0;
154 for (TracePackageElement tracePackageElement : elements) {
353a8e06
MAL
155 TracePackageElement[] children = tracePackageElement.getChildren();
156 if (children != null && children.length > 0) {
157 totalWork += getNbCheckedElements(children);
6e651d8b
MAL
158 } else if (tracePackageElement.isChecked()) {
159 ++totalWork;
160 }
161 }
162
163 return totalWork;
164 }
165
195355a9
MAL
166 /**
167 * Returns whether or not the Files element is checked under the given trace
168 * package element
169 *
170 * @param tracePackageElement
171 * the trace package element
172 * @return whether or not the Files element is checked under the given trace
173 * package element
174 */
175 public static boolean isFilesChecked(TracePackageElement tracePackageElement) {
176 for (TracePackageElement element : tracePackageElement.getChildren()) {
177 if (element instanceof TracePackageFilesElement) {
178 return element.isChecked();
179 }
180 }
181
182 return false;
183 }
184
6e651d8b
MAL
185 /**
186 * Common interface between ZipEntry and TarEntry
187 */
188 protected interface ArchiveEntry {
189 /**
190 * The name of the entry
191 *
192 * @return The name of the entry
193 */
194 String getName();
195 }
196
197 /**
198 * Common interface between ZipFile and TarFile
199 */
200 protected interface ArchiveFile {
201 /**
202 * Returns an enumeration cataloging the archive.
203 *
204 * @return enumeration of all files in the archive
205 */
206 Enumeration<? extends ArchiveEntry> entries();
207
208 /**
209 * Close the file input stream.
210 *
211 * @throws IOException
212 */
213 void close() throws IOException;
214
215 /**
216 * Returns a new InputStream for the given file in the archive.
217 *
218 * @param entry
219 * the given file
220 * @return an input stream for the given file
221 * @throws TarException
222 * @throws IOException
223 */
224 InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException;
225 }
226
227 /**
228 * Adapter for TarFile to ArchiveFile
229 */
230 protected class TarArchiveFile implements ArchiveFile {
231
232 private TarFile fTarFile;
233
234 /**
235 * Constructs a TarAchiveFile for a TarFile
236 *
237 * @param tarFile
238 * the TarFile
239 */
240 public TarArchiveFile(TarFile tarFile) {
241 this.fTarFile = tarFile;
242 }
243
244 @Override
245 public Enumeration<? extends ArchiveEntry> entries() {
507b1336 246 Vector<ArchiveEntry> v = new Vector<>();
6e651d8b
MAL
247 for (Enumeration<?> e = fTarFile.entries(); e.hasMoreElements();) {
248 v.add(new TarArchiveEntry((TarEntry) e.nextElement()));
249 }
250
251 return v.elements();
252 }
253
254 @Override
255 public void close() throws IOException {
256 fTarFile.close();
257 }
258
259 @Override
260 public InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException {
261 return fTarFile.getInputStream(((TarArchiveEntry) entry).getTarEntry());
262 }
263 }
264
265 /**
266 * Adapter for TarEntry to ArchiveEntry
267 */
268 protected class TarArchiveEntry implements ArchiveEntry {
269 private TarEntry fTarEntry;
270
271 /**
272 * Constructs a TarArchiveEntry for a TarEntry
273 *
274 * @param tarEntry
275 * the TarEntry
276 */
277 public TarArchiveEntry(TarEntry tarEntry) {
278 this.fTarEntry = tarEntry;
279 }
280
281 @Override
282 public String getName() {
283 return fTarEntry.getName();
284 }
285
286 /**
287 * Get the corresponding TarEntry
288 *
289 * @return the corresponding TarEntry
290 */
291 public TarEntry getTarEntry() {
292 return fTarEntry;
293 }
294
295 @Override
296 public String toString() {
297 return getName();
298 }
299 }
300
301 /**
302 * Adapter for ArchiveEntry to ArchiveEntry
303 */
304 protected class ZipAchiveEntry implements ArchiveEntry {
305
306 private ZipEntry fZipEntry;
307
308 /**
309 * Constructs a ZipAchiveEntry for a ZipEntry
310 *
311 * @param zipEntry
312 * the ZipEntry
313 */
314 public ZipAchiveEntry(ZipEntry zipEntry) {
315 this.fZipEntry = zipEntry;
316 }
317
318 @Override
319 public String getName() {
320 return fZipEntry.getName();
321 }
322
323 /**
324 * Get the corresponding ZipEntry
325 *
326 * @return the corresponding ZipEntry
327 */
328 public ZipEntry getZipEntry() {
329 return fZipEntry;
330 }
331
332 @Override
333 public String toString() {
334 return getName();
335 }
336 }
337
338 /**
339 * Adapter for ZipFile to ArchiveFile
340 */
341 protected class ZipArchiveFile implements ArchiveFile {
342
343 private ZipFile fZipFile;
344
345 /**
346 * Constructs a ZipArchiveFile for a ZipFile
347 *
348 * @param zipFile
349 * the ZipFile
350 */
351 public ZipArchiveFile(ZipFile zipFile) {
352 this.fZipFile = zipFile;
353 }
354
355 @Override
356 public Enumeration<? extends ArchiveEntry> entries() {
507b1336 357 Vector<ArchiveEntry> v = new Vector<>();
6e651d8b
MAL
358 for (Enumeration<?> e = fZipFile.entries(); e.hasMoreElements();) {
359 v.add(new ZipAchiveEntry((ZipEntry) e.nextElement()));
360 }
361
362 return v.elements();
363 }
364
365 @Override
366 public void close() throws IOException {
367 fZipFile.close();
368 }
369
370 @Override
371 public InputStream getInputStream(ArchiveEntry entry) throws TarException, IOException {
372 return fZipFile.getInputStream(((ZipAchiveEntry) entry).getZipEntry());
373 }
374 }
375
376}
This page took 0.069351 seconds and 5 git commands to generate.