1 /*******************************************************************************
2 * Copyright (c) 2013 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
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
;
15 import java
.io
.IOException
;
16 import java
.io
.InputStream
;
17 import java
.util
.Enumeration
;
18 import java
.util
.Vector
;
19 import java
.util
.zip
.ZipEntry
;
20 import java
.util
.zip
.ZipException
;
21 import java
.util
.zip
.ZipFile
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.ui
.internal
.wizards
.datatransfer
.TarEntry
;
26 import org
.eclipse
.ui
.internal
.wizards
.datatransfer
.TarException
;
27 import org
.eclipse
.ui
.internal
.wizards
.datatransfer
.TarFile
;
30 * An abstract operation containing common code useful for other trace package
33 * @author Marc-Andre Laperle
35 @SuppressWarnings("restriction")
36 abstract public class AbstractTracePackageOperation
{
37 private IStatus fStatus
;
38 private final String fFileName
;
41 * Constructs a new trace package operation
44 * the output file name
46 public AbstractTracePackageOperation(String fileName
) {
51 * Run the operation. The status (result) of the operation can be obtained
52 * with {@link #getStatus}
54 * @param progressMonitor
55 * the progress monitor to use to display progress and receive
56 * requests for cancellation
58 public abstract void run(IProgressMonitor progressMonitor
);
61 * Returns the status of the operation (result)
63 * @return the status of the operation
65 public IStatus
getStatus() {
70 * Set the status for this operation
75 protected void setStatus(IStatus status
) {
80 * Get the file name of the package
82 * @return the file name
84 protected String
getFileName() {
89 * Answer a handle to the archive file currently specified as being the
90 * source. Return null if this file does not exist or is not of valid
93 * @return the archive file
95 public ArchiveFile
getSpecifiedArchiveFile() {
96 if (fFileName
.length() == 0) {
101 ZipFile zipFile
= new ZipFile(fFileName
);
102 return new ZipArchiveFile(zipFile
);
103 } catch (ZipException e
) {
105 } catch (IOException e
) {
110 TarFile tarFile
= new TarFile(fFileName
);
111 return new TarArchiveFile(tarFile
);
112 } catch (TarException e
) {
114 } catch (IOException e
) {
122 * Get the number of checked elements in the array and the children
125 * the elements to check for checked
126 * @return the number of checked elements
128 protected int getNbCheckedElements(TracePackageElement
[] elements
) {
130 for (TracePackageElement tracePackageElement
: elements
) {
131 if (tracePackageElement
.getChildren() != null) {
132 totalWork
+= getNbCheckedElements(tracePackageElement
.getChildren());
133 } else if (tracePackageElement
.isChecked()) {
142 * Common interface between ZipEntry and TarEntry
144 protected interface ArchiveEntry
{
146 * The name of the entry
148 * @return The name of the entry
154 * Common interface between ZipFile and TarFile
156 protected interface ArchiveFile
{
158 * Returns an enumeration cataloging the archive.
160 * @return enumeration of all files in the archive
162 Enumeration
<?
extends ArchiveEntry
> entries();
165 * Close the file input stream.
167 * @throws IOException
169 void close() throws IOException
;
172 * Returns a new InputStream for the given file in the archive.
176 * @return an input stream for the given file
177 * @throws TarException
178 * @throws IOException
180 InputStream
getInputStream(ArchiveEntry entry
) throws TarException
, IOException
;
184 * Adapter for TarFile to ArchiveFile
186 protected class TarArchiveFile
implements ArchiveFile
{
188 private TarFile fTarFile
;
191 * Constructs a TarAchiveFile for a TarFile
196 public TarArchiveFile(TarFile tarFile
) {
197 this.fTarFile
= tarFile
;
201 public Enumeration
<?
extends ArchiveEntry
> entries() {
202 Vector
<ArchiveEntry
> v
= new Vector
<ArchiveEntry
>();
203 for (Enumeration
<?
> e
= fTarFile
.entries(); e
.hasMoreElements();) {
204 v
.add(new TarArchiveEntry((TarEntry
) e
.nextElement()));
211 public void close() throws IOException
{
216 public InputStream
getInputStream(ArchiveEntry entry
) throws TarException
, IOException
{
217 return fTarFile
.getInputStream(((TarArchiveEntry
) entry
).getTarEntry());
222 * Adapter for TarEntry to ArchiveEntry
224 protected class TarArchiveEntry
implements ArchiveEntry
{
225 private TarEntry fTarEntry
;
228 * Constructs a TarArchiveEntry for a TarEntry
233 public TarArchiveEntry(TarEntry tarEntry
) {
234 this.fTarEntry
= tarEntry
;
238 public String
getName() {
239 return fTarEntry
.getName();
243 * Get the corresponding TarEntry
245 * @return the corresponding TarEntry
247 public TarEntry
getTarEntry() {
252 public String
toString() {
258 * Adapter for ArchiveEntry to ArchiveEntry
260 protected class ZipAchiveEntry
implements ArchiveEntry
{
262 private ZipEntry fZipEntry
;
265 * Constructs a ZipAchiveEntry for a ZipEntry
270 public ZipAchiveEntry(ZipEntry zipEntry
) {
271 this.fZipEntry
= zipEntry
;
275 public String
getName() {
276 return fZipEntry
.getName();
280 * Get the corresponding ZipEntry
282 * @return the corresponding ZipEntry
284 public ZipEntry
getZipEntry() {
289 public String
toString() {
295 * Adapter for ZipFile to ArchiveFile
297 protected class ZipArchiveFile
implements ArchiveFile
{
299 private ZipFile fZipFile
;
302 * Constructs a ZipArchiveFile for a ZipFile
307 public ZipArchiveFile(ZipFile zipFile
) {
308 this.fZipFile
= zipFile
;
312 public Enumeration
<?
extends ArchiveEntry
> entries() {
313 Vector
<ArchiveEntry
> v
= new Vector
<ArchiveEntry
>();
314 for (Enumeration
<?
> e
= fZipFile
.entries(); e
.hasMoreElements();) {
315 v
.add(new ZipAchiveEntry((ZipEntry
) e
.nextElement()));
322 public void close() throws IOException
{
327 public InputStream
getInputStream(ArchiveEntry entry
) throws TarException
, IOException
{
328 return fZipFile
.getInputStream(((ZipAchiveEntry
) entry
).getZipEntry());