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 | ||
13 | package org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.importexport; | |
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; | |
28 | import org.eclipse.jface.operation.ModalContext; | |
29 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
30 | import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.AbstractTracePackageOperation; | |
31 | import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.ITracePackageConstants; | |
6e651d8b MAL |
32 | import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement; |
33 | import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement; | |
6e651d8b | 34 | import 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 | */ | |
42 | public 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 | } |