1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
;
16 import java
.io
.IOException
;
17 import java
.io
.InputStream
;
18 import java
.util
.Enumeration
;
19 import java
.util
.Vector
;
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
;
30 * An abstract operation containing common code useful for other trace package
33 * @author Marc-Andre Laperle
35 public abstract class AbstractTracePackageOperation
{
36 private IStatus fStatus
;
37 // Result of this operation, if any
38 private TracePackageElement
[] fResultElements
;
40 private final String fFileName
;
43 * Constructs a new trace package operation
46 * the output file name
48 public AbstractTracePackageOperation(String fileName
) {
53 * Run the operation. The status (result) of the operation can be obtained
54 * with {@link #getStatus}
56 * @param progressMonitor
57 * the progress monitor to use to display progress and receive
58 * requests for cancellation
60 public abstract void run(IProgressMonitor progressMonitor
);
63 * Returns the status of the operation (result)
65 * @return the status of the operation
67 public IStatus
getStatus() {
72 * Get the resulting elements for this operation, if any
74 * @return the resulting elements or null if no result is produced by this
77 public TracePackageElement
[] getResultElements() {
78 return fResultElements
;
82 * Set the resulting elements for this operation, if any
85 * the resulting elements produced by this operation, can be set
88 public void setResultElements(TracePackageElement
[] elements
) {
89 fResultElements
= elements
;
93 * Set the status for this operation
98 protected void setStatus(IStatus status
) {
103 * Get the file name of the package
105 * @return the file name
107 protected String
getFileName() {
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
116 * @return the archive file
118 public ArchiveFile
getSpecifiedArchiveFile() {
119 if (fFileName
.length() == 0) {
123 File file
= new File(fFileName
);
124 if (file
.isDirectory()) {
129 return new ZipArchiveFile(new ZipFile(file
));
130 } catch (IOException e
) {
135 return new TarArchiveFile(new TarFile(file
));
136 } catch (IOException e
) {
144 * Get the number of checked elements in the array and the children
147 * the elements to check for checked
148 * @return the number of checked elements
150 protected int getNbCheckedElements(TracePackageElement
[] elements
) {
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()) {
165 * Returns whether or not the Files element is checked under the given trace
168 * @param tracePackageElement
169 * the trace package element
170 * @return whether or not the Files element is checked under the given trace
173 public static boolean isFilesChecked(TracePackageElement tracePackageElement
) {
174 for (TracePackageElement element
: tracePackageElement
.getChildren()) {
175 if (element
instanceof TracePackageFilesElement
) {
176 return element
.isChecked();
184 * Common interface between ZipArchiveEntry and TarArchiveEntry
186 protected interface ArchiveEntry
{
188 * The name of the entry
190 * @return The name of the entry
196 * Common interface between ZipFile and TarFile
198 protected interface ArchiveFile
{
200 * Returns an enumeration cataloging the archive.
202 * @return enumeration of all files in the archive
204 Enumeration
<@NonNull ?
extends ArchiveEntry
> entries();
207 * Close the file input stream.
209 * @throws IOException
211 void close() throws IOException
;
214 * Returns a new InputStream for the given file in the archive.
218 * @return an input stream for the given file
219 * @throws IOException
221 InputStream
getInputStream(ArchiveEntry entry
) throws IOException
;
225 * Adapter for TarFile to ArchiveFile
227 protected class TarArchiveFile
implements ArchiveFile
{
229 private TarFile fTarFile
;
232 * Constructs a TarAchiveFile for a TarFile
237 public TarArchiveFile(TarFile tarFile
) {
238 this.fTarFile
= tarFile
;
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()));
252 public void close() throws IOException
{
257 public InputStream
getInputStream(ArchiveEntry entry
) throws IOException
{
258 return fTarFile
.getInputStream(((TarArchiveEntryAdapter
) entry
).getTarEntry());
263 * Adapter for TarArchiveEntry to ArchiveEntry
265 protected class TarArchiveEntryAdapter
implements ArchiveEntry
{
266 private TarArchiveEntry fTarEntry
;
269 * Constructs a TarArchiveEntry for a TarArchiveEntry
272 * the TarArchiveEntry
274 public TarArchiveEntryAdapter(TarArchiveEntry tarEntry
) {
275 this.fTarEntry
= tarEntry
;
279 public String
getName() {
280 return fTarEntry
.getName();
284 * Get the corresponding TarArchiveEntry
286 * @return the corresponding TarArchiveEntry
288 public TarArchiveEntry
getTarEntry() {
293 public String
toString() {
299 * Adapter for ArchiveEntry to ArchiveEntry
301 protected class ZipAchiveEntryAdapter
implements ArchiveEntry
{
303 private ZipArchiveEntry fZipEntry
;
306 * Constructs a ZipAchiveEntryAdapter for a ZipArchiveEntry
309 * the ZipArchiveEntry
311 public ZipAchiveEntryAdapter(ZipArchiveEntry zipEntry
) {
312 this.fZipEntry
= zipEntry
;
316 public String
getName() {
317 return fZipEntry
.getName();
321 * Get the corresponding ZipArchiveEntry
323 * @return the corresponding ZipArchiveEntry
325 public ZipArchiveEntry
getZipEntry() {
330 public String
toString() {
336 * Adapter for ZipFile to ArchiveFile
338 protected class ZipArchiveFile
implements ArchiveFile
{
340 private ZipFile fZipFile
;
343 * Constructs a ZipArchiveFile for a ZipFile
348 public ZipArchiveFile(ZipFile zipFile
) {
349 this.fZipFile
= zipFile
;
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()));
363 public void close() throws IOException
{
368 public InputStream
getInputStream(ArchiveEntry entry
) throws IOException
{
369 return fZipFile
.getInputStream(((ZipAchiveEntryAdapter
) entry
).getZipEntry());