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