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