ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / wizards / tracepkg / importexport / TracePackageExtractManifestOperation.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
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;
21 import java.util.Set;
22
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;
32 import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement;
33 import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement;
34 import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement;
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
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) {
64 TracePackageElement[] elements = null;
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);
88 ManifestReader.validateManifest(inputStream);
89
90 inputStream = archiveFile.getInputStream(entry);
91 elements = ManifestReader.loadElementsFromManifest(inputStream);
92 break;
93 }
94
95 progressMonitor.worked(1);
96 }
97
98 if (found) {
99 setStatus(Status.OK_STATUS);
100 }
101 else {
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 }
108 }
109
110 setResultElements(elements);
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
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));
129 }
130 }
131
132 List<TracePackageElement> packageElements = new ArrayList<>();
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
142 }
This page took 0.041086 seconds and 5 git commands to generate.