Commit | Line | Data |
---|---|---|
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg; |
6e651d8b | 14 | |
6120dc63 | 15 | import java.io.File; |
6e651d8b MAL |
16 | import java.io.IOException; |
17 | import java.io.InputStream; | |
18 | import java.util.Enumeration; | |
19 | import java.util.Vector; | |
6e651d8b | 20 | |
6120dc63 MAL |
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; | |
6e651d8b MAL |
24 | import org.eclipse.core.runtime.IProgressMonitor; |
25 | import org.eclipse.core.runtime.IStatus; | |
5b3fe39a | 26 | import org.eclipse.jdt.annotation.NonNull; |
5d5f933e | 27 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.TarFile; |
6e651d8b MAL |
28 | |
29 | /** | |
30 | * An abstract operation containing common code useful for other trace package | |
31 | * operations | |
32 | * | |
33 | * @author Marc-Andre Laperle | |
34 | */ | |
574f43ad | 35 | public abstract class AbstractTracePackageOperation { |
6e651d8b | 36 | private IStatus fStatus; |
8d03681c MAL |
37 | // Result of this operation, if any |
38 | private TracePackageElement[] fResultElements; | |
39 | ||
6e651d8b MAL |
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 | ||
8d03681c MAL |
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 | ||
6e651d8b MAL |
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 | ||
6120dc63 MAL |
123 | File file = new File(fFileName); |
124 | if (file.isDirectory()) { | |
125 | return null; | |
126 | } | |
127 | ||
6e651d8b | 128 | try { |
6120dc63 | 129 | return new ZipArchiveFile(new ZipFile(file)); |
6e651d8b MAL |
130 | } catch (IOException e) { |
131 | // ignore | |
132 | } | |
133 | ||
134 | try { | |
6120dc63 MAL |
135 | return new TarArchiveFile(new TarFile(file)); |
136 | } catch (IOException e) { | |
6e651d8b MAL |
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) { | |
353a8e06 MAL |
153 | TracePackageElement[] children = tracePackageElement.getChildren(); |
154 | if (children != null && children.length > 0) { | |
155 | totalWork += getNbCheckedElements(children); | |
6e651d8b MAL |
156 | } else if (tracePackageElement.isChecked()) { |
157 | ++totalWork; | |
158 | } | |
159 | } | |
160 | ||
161 | return totalWork; | |
162 | } | |
163 | ||
195355a9 MAL |
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 | ||
6e651d8b | 183 | /** |
6120dc63 | 184 | * Common interface between ZipArchiveEntry and TarArchiveEntry |
6e651d8b MAL |
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 | */ | |
5b3fe39a | 204 | Enumeration<@NonNull ? extends ArchiveEntry> entries(); |
6e651d8b MAL |
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 | |
6e651d8b MAL |
219 | * @throws IOException |
220 | */ | |
6120dc63 | 221 | InputStream getInputStream(ArchiveEntry entry) throws IOException; |
6e651d8b MAL |
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 | |
5b3fe39a AM |
242 | public Enumeration<@NonNull ? extends ArchiveEntry> entries() { |
243 | Vector<@NonNull ArchiveEntry> v = new Vector<>(); | |
6e651d8b | 244 | for (Enumeration<?> e = fTarFile.entries(); e.hasMoreElements();) { |
6120dc63 | 245 | v.add(new TarArchiveEntryAdapter((TarArchiveEntry) e.nextElement())); |
6e651d8b MAL |
246 | } |
247 | ||
248 | return v.elements(); | |
249 | } | |
250 | ||
251 | @Override | |
252 | public void close() throws IOException { | |
253 | fTarFile.close(); | |
254 | } | |
255 | ||
256 | @Override | |
6120dc63 MAL |
257 | public InputStream getInputStream(ArchiveEntry entry) throws IOException { |
258 | return fTarFile.getInputStream(((TarArchiveEntryAdapter) entry).getTarEntry()); | |
6e651d8b MAL |
259 | } |
260 | } | |
261 | ||
262 | /** | |
6120dc63 | 263 | * Adapter for TarArchiveEntry to ArchiveEntry |
6e651d8b | 264 | */ |
6120dc63 MAL |
265 | protected class TarArchiveEntryAdapter implements ArchiveEntry { |
266 | private TarArchiveEntry fTarEntry; | |
6e651d8b MAL |
267 | |
268 | /** | |
6120dc63 | 269 | * Constructs a TarArchiveEntry for a TarArchiveEntry |
6e651d8b MAL |
270 | * |
271 | * @param tarEntry | |
6120dc63 | 272 | * the TarArchiveEntry |
6e651d8b | 273 | */ |
6120dc63 | 274 | public TarArchiveEntryAdapter(TarArchiveEntry tarEntry) { |
6e651d8b MAL |
275 | this.fTarEntry = tarEntry; |
276 | } | |
277 | ||
278 | @Override | |
279 | public String getName() { | |
280 | return fTarEntry.getName(); | |
281 | } | |
282 | ||
283 | /** | |
6120dc63 | 284 | * Get the corresponding TarArchiveEntry |
6e651d8b | 285 | * |
6120dc63 | 286 | * @return the corresponding TarArchiveEntry |
6e651d8b | 287 | */ |
6120dc63 | 288 | public TarArchiveEntry getTarEntry() { |
6e651d8b MAL |
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 | */ | |
6120dc63 | 301 | protected class ZipAchiveEntryAdapter implements ArchiveEntry { |
6e651d8b | 302 | |
6120dc63 | 303 | private ZipArchiveEntry fZipEntry; |
6e651d8b MAL |
304 | |
305 | /** | |
6120dc63 | 306 | * Constructs a ZipAchiveEntryAdapter for a ZipArchiveEntry |
6e651d8b MAL |
307 | * |
308 | * @param zipEntry | |
6120dc63 | 309 | * the ZipArchiveEntry |
6e651d8b | 310 | */ |
6120dc63 | 311 | public ZipAchiveEntryAdapter(ZipArchiveEntry zipEntry) { |
6e651d8b MAL |
312 | this.fZipEntry = zipEntry; |
313 | } | |
314 | ||
315 | @Override | |
316 | public String getName() { | |
317 | return fZipEntry.getName(); | |
318 | } | |
319 | ||
320 | /** | |
6120dc63 | 321 | * Get the corresponding ZipArchiveEntry |
6e651d8b | 322 | * |
6120dc63 | 323 | * @return the corresponding ZipArchiveEntry |
6e651d8b | 324 | */ |
6120dc63 | 325 | public ZipArchiveEntry getZipEntry() { |
6e651d8b MAL |
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 | |
5b3fe39a AM |
353 | public Enumeration<@NonNull ? extends ArchiveEntry> entries() { |
354 | Vector<@NonNull ArchiveEntry> v = new Vector<>(); | |
6120dc63 MAL |
355 | for (Enumeration<ZipArchiveEntry> e = fZipFile.getEntries(); e.hasMoreElements();) { |
356 | v.add(new ZipAchiveEntryAdapter(e.nextElement())); | |
6e651d8b MAL |
357 | } |
358 | ||
359 | return v.elements(); | |
360 | } | |
361 | ||
362 | @Override | |
363 | public void close() throws IOException { | |
364 | fZipFile.close(); | |
365 | } | |
366 | ||
367 | @Override | |
6120dc63 MAL |
368 | public InputStream getInputStream(ArchiveEntry entry) throws IOException { |
369 | return fZipFile.getInputStream(((ZipAchiveEntryAdapter) entry).getZipEntry()); | |
6e651d8b MAL |
370 | } |
371 | } | |
372 | ||
373 | } |