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
.importexport
;
15 import java
.io
.InputStream
;
16 import java
.text
.MessageFormat
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Enumeration
;
19 import java
.util
.HashSet
;
20 import java
.util
.List
;
23 import org
.eclipse
.core
.runtime
.IPath
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.IStatus
;
26 import org
.eclipse
.core
.runtime
.Path
;
27 import org
.eclipse
.core
.runtime
.Status
;
28 import org
.eclipse
.jface
.operation
.ModalContext
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
30 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.AbstractTracePackageOperation
;
31 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.ITracePackageConstants
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageElement
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageFilesElement
;
34 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageTraceElement
;
37 * An operation that extracts information from the manifest located in an
40 * @author Marc-Andre Laperle
42 public class TracePackageExtractManifestOperation
extends AbstractTracePackageOperation
{
45 * Constructs a new import operation for reading the manifest
48 * the output file name
50 public TracePackageExtractManifestOperation(String fileName
) {
55 * Run extract the manifest operation. The status (result) of the operation
56 * can be obtained with {@link #getStatus}
58 * @param progressMonitor
59 * the progress monitor to use to display progress and receive
60 * requests for cancellation
63 public void run(IProgressMonitor progressMonitor
) {
64 TracePackageElement
[] elements
= null;
66 progressMonitor
.worked(1);
67 ArchiveFile archiveFile
= getSpecifiedArchiveFile();
68 progressMonitor
.worked(1);
69 if (archiveFile
== null) {
70 setStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TracePackageExtractManifestOperation_InvalidFormat
));
74 Enumeration
<?
> entries
= archiveFile
.entries();
76 boolean found
= false;
77 while (entries
.hasMoreElements()) {
78 ModalContext
.checkCanceled(progressMonitor
);
80 ArchiveEntry entry
= (ArchiveEntry
) entries
.nextElement();
81 IPath p
= new Path(entry
.getName());
83 p
= p
.removeFirstSegments(1);
85 if (entry
.getName().endsWith(ITracePackageConstants
.MANIFEST_FILENAME
)) {
87 InputStream inputStream
= archiveFile
.getInputStream(entry
);
88 ManifestReader
.validateManifest(inputStream
);
90 inputStream
= archiveFile
.getInputStream(entry
);
91 elements
= ManifestReader
.loadElementsFromManifest(inputStream
);
95 progressMonitor
.worked(1);
99 setStatus(Status
.OK_STATUS
);
102 elements
= generateElementsFromArchive();
103 if (elements
.length
> 0) {
104 setStatus(Status
.OK_STATUS
);
106 setStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, MessageFormat
.format(Messages
.TracePackageExtractManifestOperation_ErrorManifestNotFound
, ITracePackageConstants
.MANIFEST_FILENAME
)));
110 setResultElements(elements
);
112 } catch (InterruptedException e
) {
113 setStatus(Status
.CANCEL_STATUS
);
114 } catch (Exception e
) {
115 setStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TracePackageExtractManifestOperation_ErrorReadingManifest
, e
));
119 private TracePackageElement
[] generateElementsFromArchive() {
120 ArchiveFile archiveFile
= getSpecifiedArchiveFile();
121 Enumeration
<?
> entries
= archiveFile
.entries();
122 Set
<String
> traceFileNames
= new HashSet
<>();
123 while (entries
.hasMoreElements()) {
124 ArchiveEntry entry
= (ArchiveEntry
) entries
.nextElement();
125 String entryName
= entry
.getName();
126 IPath fullArchivePath
= new Path(entryName
);
127 if (!fullArchivePath
.hasTrailingSeparator() && fullArchivePath
.segmentCount() > 0) {
128 traceFileNames
.add(fullArchivePath
.segment(0));
132 List
<TracePackageElement
> packageElements
= new ArrayList
<>();
133 for (String traceFileName
: traceFileNames
) {
134 TracePackageTraceElement traceElement
= new TracePackageTraceElement(null, traceFileName
, null);
135 new TracePackageFilesElement(traceElement
, traceFileName
);
136 packageElements
.add(traceElement
);
139 return packageElements
.toArray(new TracePackageElement
[] {});