Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / wizards / tracepkg / importexport / TracePackageExtractManifestOperation.java
CommitLineData
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
13package org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.importexport;
14
6e651d8b 15import java.io.InputStream;
6e651d8b
MAL
16import java.text.MessageFormat;
17import java.util.ArrayList;
18import java.util.Enumeration;
f7885d6d 19import java.util.HashSet;
6e651d8b 20import java.util.List;
f7885d6d 21import java.util.Set;
6e651d8b 22
6e651d8b
MAL
23import org.eclipse.core.runtime.IPath;
24import org.eclipse.core.runtime.IProgressMonitor;
25import org.eclipse.core.runtime.IStatus;
26import org.eclipse.core.runtime.Path;
27import org.eclipse.core.runtime.Status;
28import org.eclipse.jface.operation.ModalContext;
29import org.eclipse.linuxtools.internal.tmf.ui.Activator;
30import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.AbstractTracePackageOperation;
31import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.ITracePackageConstants;
6e651d8b
MAL
32import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement;
33import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement;
6e651d8b 34import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement;
6e651d8b
MAL
35
36/**
37 * An operation that extracts information from the manifest located in an
38 * archive
39 *
40 * @author Marc-Andre Laperle
41 */
42public class TracePackageExtractManifestOperation extends AbstractTracePackageOperation {
43
6e651d8b
MAL
44 /**
45 * Constructs a new import operation for reading the manifest
46 *
47 * @param fileName
48 * the output file name
49 */
50 public TracePackageExtractManifestOperation(String fileName) {
51 super(fileName);
52 }
53
54 /**
55 * Run extract the manifest operation. The status (result) of the operation
56 * can be obtained with {@link #getStatus}
57 *
58 * @param progressMonitor
59 * the progress monitor to use to display progress and receive
60 * requests for cancellation
61 */
62 @Override
63 public void run(IProgressMonitor progressMonitor) {
195355a9 64 TracePackageElement[] elements = null;
6e651d8b
MAL
65 try {
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));
71 return;
72 }
73
74 Enumeration<?> entries = archiveFile.entries();
75
76 boolean found = false;
77 while (entries.hasMoreElements()) {
78 ModalContext.checkCanceled(progressMonitor);
79
80 ArchiveEntry entry = (ArchiveEntry) entries.nextElement();
81 IPath p = new Path(entry.getName());
82 //Remove project name
83 p = p.removeFirstSegments(1);
84
85 if (entry.getName().endsWith(ITracePackageConstants.MANIFEST_FILENAME)) {
86 found = true;
87 InputStream inputStream = archiveFile.getInputStream(entry);
30bf6b07 88 ManifestReader.validateManifest(inputStream);
6e651d8b
MAL
89
90 inputStream = archiveFile.getInputStream(entry);
30bf6b07 91 elements = ManifestReader.loadElementsFromManifest(inputStream);
6e651d8b
MAL
92 break;
93 }
94
95 progressMonitor.worked(1);
96 }
97
98 if (found) {
99 setStatus(Status.OK_STATUS);
100 }
101 else {
f7885d6d
MAL
102 elements = generateElementsFromArchive();
103 if (elements.length > 0) {
104 setStatus(Status.OK_STATUS);
105 } else {
106 setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.TracePackageExtractManifestOperation_ErrorManifestNotFound, ITracePackageConstants.MANIFEST_FILENAME)));
107 }
6e651d8b
MAL
108 }
109
8d03681c 110 setResultElements(elements);
6e651d8b
MAL
111
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));
116 }
117 }
118
f7885d6d
MAL
119 private TracePackageElement[] generateElementsFromArchive() {
120 ArchiveFile archiveFile = getSpecifiedArchiveFile();
121 Enumeration<?> entries = archiveFile.entries();
507b1336 122 Set<String> traceFileNames = new HashSet<>();
f7885d6d
MAL
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));
129 }
130 }
131
507b1336 132 List<TracePackageElement> packageElements = new ArrayList<>();
f7885d6d
MAL
133 for (String traceFileName : traceFileNames) {
134 TracePackageTraceElement traceElement = new TracePackageTraceElement(null, traceFileName, null);
135 traceElement.setChildren(new TracePackageElement[] { new TracePackageFilesElement(traceElement, traceFileName) });
136 packageElements.add(traceElement);
137 }
138
139 return packageElements.toArray(new TracePackageElement[] {});
140 }
141
6e651d8b 142}
This page took 0.057048 seconds and 5 git commands to generate.