tmf: Fix custom parser wizard preview table
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / wizards / tracepkg / importexport / TracePackageImportOperation.java
CommitLineData
6e651d8b 1/*******************************************************************************
60ae41e1 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
89730b51 11 * Patrick Tasse - Add support for source location
6e651d8b
MAL
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.importexport;
15
89730b51 16import java.io.File;
6e651d8b
MAL
17import java.io.IOException;
18import java.io.InputStream;
19import java.lang.reflect.InvocationTargetException;
89730b51 20import java.net.URI;
6e651d8b
MAL
21import java.text.MessageFormat;
22import java.util.ArrayList;
23import java.util.Enumeration;
24import java.util.List;
25import java.util.Map;
26
27import org.eclipse.core.resources.IFile;
89730b51 28import org.eclipse.core.resources.IFolder;
6e651d8b
MAL
29import org.eclipse.core.resources.IMarker;
30import org.eclipse.core.resources.IResource;
31import org.eclipse.core.runtime.CoreException;
32import org.eclipse.core.runtime.IPath;
33import org.eclipse.core.runtime.IProgressMonitor;
34import org.eclipse.core.runtime.IStatus;
35import org.eclipse.core.runtime.Path;
36import org.eclipse.core.runtime.Status;
37import org.eclipse.core.runtime.SubProgressMonitor;
89730b51 38import org.eclipse.core.runtime.URIUtil;
6e651d8b
MAL
39import org.eclipse.jface.operation.ModalContext;
40import org.eclipse.linuxtools.internal.tmf.ui.Activator;
41import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.AbstractTracePackageOperation;
42import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageBookmarkElement;
43import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement;
44import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement;
45import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFileElement;
46import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFilesElement;
47import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement;
89730b51 48import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
47aafe74
AM
49import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceImportException;
50import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
51import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
7441221c 52import org.eclipse.linuxtools.tmf.core.util.Pair;
6e651d8b 53import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
6e651d8b
MAL
54import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
55import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
47aafe74 56import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils;
6e651d8b
MAL
57import org.eclipse.ui.dialogs.IOverwriteQuery;
58import org.eclipse.ui.ide.IDE;
59import org.eclipse.ui.internal.wizards.datatransfer.TarException;
60import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
61import org.eclipse.ui.wizards.datatransfer.ImportOperation;
62
63/**
64 * An operation that imports a trace package from an archive
65 *
66 * @author Marc-Andre Laperle
67 */
68@SuppressWarnings("restriction")
69public class TracePackageImportOperation extends AbstractTracePackageOperation implements IOverwriteQuery {
70
195355a9 71 private final TracePackageElement[] fImportTraceElements;
6e651d8b
MAL
72 private final TmfTraceFolder fTmfTraceFolder;
73
6e651d8b
MAL
74 /**
75 * Constructs a new import operation
76 *
195355a9 77 * @param importTraceElements
6e651d8b
MAL
78 * the trace element to be imported
79 * @param fileName
80 * the output file name
81 * @param tmfTraceFolder
82 * the destination folder
83 */
195355a9 84 public TracePackageImportOperation(String fileName, TracePackageElement[] importTraceElements, TmfTraceFolder tmfTraceFolder) {
6e651d8b 85 super(fileName);
195355a9 86 fImportTraceElements = importTraceElements;
6e651d8b
MAL
87 fTmfTraceFolder = tmfTraceFolder;
88 }
89
90 private class ImportProvider implements IImportStructureProvider {
91
92 private Exception fException;
93
94 @Override
95 public List getChildren(Object element) {
96 return null;
97 }
98
99 @Override
100 public InputStream getContents(Object element) {
101 InputStream inputStream = null;
102 // We can add throws
103 try {
104 inputStream = ((ArchiveProviderElement) element).getContents();
105 } catch (IOException e) {
106 fException = e;
107 } catch (TarException e) {
108 fException = e;
109 }
110 return inputStream;
111 }
112
113 @Override
114 public String getFullPath(Object element) {
115 return ((ArchiveProviderElement) element).getFullPath();
116 }
117
118 @Override
119 public String getLabel(Object element) {
120 return ((ArchiveProviderElement) element).getLabel();
121 }
122
123 @Override
124 public boolean isFolder(Object element) {
125 return ((ArchiveProviderElement) element).isFolder();
126 }
127
128 public Exception getException() {
129 return fException;
130 }
131 }
132
133 private class ArchiveProviderElement {
134
135 private final String fPath;
136 private final String fLabel;
137
138 private ArchiveFile fArchiveFile;
139 private ArchiveEntry fEntry;
140
141 public ArchiveProviderElement(String destinationPath, String label, ArchiveFile archiveFile, ArchiveEntry entry) {
142 fPath = destinationPath;
143 fLabel = label;
144 this.fArchiveFile = archiveFile;
145 this.fEntry = entry;
146 }
147
148 public InputStream getContents() throws TarException, IOException {
149 return fArchiveFile.getInputStream(fEntry);
150 }
151
152 public String getFullPath() {
153 return fPath;
154 }
155
156 public String getLabel() {
157 return fLabel;
158 }
159
160 public boolean isFolder() {
161 return false;
162 }
163 }
164
165 /**
166 * Run the operation. The status (result) of the operation can be obtained
167 * with {@link #getStatus}
168 *
169 * @param progressMonitor
170 * the progress monitor to use to display progress and receive
171 * requests for cancellation
172 */
173 @Override
174 public void run(IProgressMonitor progressMonitor) {
195355a9 175 int totalWork = getNbCheckedElements(fImportTraceElements) * 2;
6e651d8b
MAL
176 progressMonitor.beginTask(Messages.TracePackageImportOperation_ImportingPackage, totalWork);
177 doRun(progressMonitor);
178 progressMonitor.done();
179 }
180
181 private void doRun(IProgressMonitor progressMonitor) {
182 try {
195355a9 183 setStatus(deleteExistingTraces(progressMonitor));
6e651d8b
MAL
184 if (getStatus().getSeverity() != IStatus.OK) {
185 return;
186 }
187
89730b51 188 TracePackageFilesElement traceFilesElement = null;
195355a9
MAL
189 for (TracePackageElement packageElement : fImportTraceElements) {
190 TracePackageTraceElement traceElement = (TracePackageTraceElement) packageElement;
191 if (!isFilesChecked(packageElement)) {
192 continue;
193 }
194
195 TracePackageElement[] children = traceElement.getChildren();
196 for (TracePackageElement element : children) {
197 ModalContext.checkCanceled(progressMonitor);
198
199 if (element instanceof TracePackageFilesElement) {
89730b51 200 traceFilesElement = (TracePackageFilesElement) element;
a6ee485c 201 setStatus(importTraceFiles(traceFilesElement, traceElement, progressMonitor));
6e651d8b 202
195355a9
MAL
203 } else if (element instanceof TracePackageSupplFilesElement) {
204 TracePackageSupplFilesElement suppFilesElement = (TracePackageSupplFilesElement) element;
205 setStatus(importSupplFiles(suppFilesElement, traceElement, progressMonitor));
206 }
6e651d8b 207
195355a9
MAL
208 if (getStatus().getSeverity() != IStatus.OK) {
209 return;
210 }
6e651d8b 211 }
89730b51 212 }
6e651d8b
MAL
213
214 } catch (InterruptedException e) {
215 setStatus(Status.CANCEL_STATUS);
216 }
217 }
218
a6ee485c
MAL
219 /**
220 * Returns whether or not the Files element is checked under the given trace
221 * package element
222 *
223 * @param tracePackageElement
224 * the trace package element
225 * @return whether or not the Files element is checked under the given trace
226 * package element
227 */
228 public static boolean isFilesChecked(TracePackageElement tracePackageElement) {
229 for (TracePackageElement element : tracePackageElement.getChildren()) {
230 if (element instanceof TracePackageFilesElement) {
231 return element.isChecked();
232 }
233 }
234
235 return false;
236 }
237
238 /**
239 * Return the matching TmfTraceElement for a given trace element.
240 */
241 private TmfTraceElement getMatchingTraceElement(TracePackageTraceElement tracePackageElement) {
242 IPath tracePath = fTmfTraceFolder.getPath().append(tracePackageElement.getDestinationElementPath());
6e651d8b 243 List<TmfTraceElement> traces = fTmfTraceFolder.getTraces();
a6ee485c
MAL
244 for (TmfTraceElement t : traces) {
245 if (t.getPath().equals(tracePath)) {
246 return t;
247 }
248 }
249
250 return null;
251 }
6e651d8b 252
a6ee485c 253 private IStatus deleteExistingTraces(IProgressMonitor progressMonitor) {
195355a9
MAL
254 for (TracePackageElement packageElement : fImportTraceElements) {
255 TracePackageTraceElement traceElement = (TracePackageTraceElement) packageElement;
256 if (!isFilesChecked(traceElement)) {
257 continue;
6e651d8b 258 }
6e651d8b 259
a6ee485c 260 TmfTraceElement existingTrace = getMatchingTraceElement(traceElement);
195355a9
MAL
261 if (existingTrace != null) {
262 try {
263 existingTrace.delete(new SubProgressMonitor(progressMonitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
264 } catch (CoreException e) {
265 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.Messages.TracePackage_ErrorOperation, e);
266 }
6e651d8b
MAL
267 }
268 }
269
270 return Status.OK_STATUS;
271 }
272
195355a9
MAL
273 private void importBookmarks(IResource traceRes, TracePackageTraceElement traceElement, IProgressMonitor monitor) {
274 for (TracePackageElement o : traceElement.getChildren()) {
6e651d8b
MAL
275 if (o instanceof TracePackageBookmarkElement && o.isChecked()) {
276
277 // Get element
278 IFile bookmarksFile = null;
a6ee485c
MAL
279 TmfTraceElement tmfTraceElement = getMatchingTraceElement(traceElement);
280 if (tmfTraceElement != null) {
281 try {
282 bookmarksFile = tmfTraceElement.createBookmarksFile();
6e651d8b 283
a6ee485c
MAL
284 // Make sure that if a bookmark is double-clicked first
285 // before opening the trace, it opens the right editor
6e651d8b 286
a6ee485c
MAL
287 // Get the editor id from the extension point
288 String traceEditorId = tmfTraceElement.getEditorId();
289 final String editorId = (traceEditorId != null) ? traceEditorId : TmfEventsEditor.ID;
290 IDE.setDefaultEditor(bookmarksFile, editorId);
6e651d8b 291
a6ee485c
MAL
292 } catch (CoreException e) {
293 Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmarkFile, traceRes.getName()), e);
6e651d8b
MAL
294 }
295 }
296
297 if (bookmarksFile == null) {
298 break;
299 }
300
301 TracePackageBookmarkElement bookmarkElement = (TracePackageBookmarkElement) o;
302
303 List<Map<String, String>> bookmarks = bookmarkElement.getBookmarks();
304 for (Map<String, String> attrs : bookmarks) {
305 IMarker createMarker = null;
306 try {
307 createMarker = bookmarksFile.createMarker(IMarker.BOOKMARK);
308 } catch (CoreException e) {
309 Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmark, traceRes.getName()), e);
310 }
311 if (createMarker != null && createMarker.exists()) {
312 try {
313 for (String key : attrs.keySet()) {
314 String value = attrs.get(key);
315 if (key.equals(IMarker.LOCATION)) {
316 createMarker.setAttribute(IMarker.LOCATION, Integer.valueOf(value).intValue());
317 } else {
318 createMarker.setAttribute(key, value);
319 }
320 }
321 } catch (CoreException e) {
322 Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmark, traceRes.getName()), e);
323 }
324 }
325 }
326 }
327 }
328
329 monitor.worked(1);
330 }
331
a6ee485c 332 private IStatus importTraceFiles(TracePackageFilesElement traceFilesElement, TracePackageTraceElement traceElement, IProgressMonitor monitor) {
7441221c
MAL
333 List<Pair<String, String>> fileNameAndLabelPairs = new ArrayList<>();
334
335 String sourceName = traceFilesElement.getFileName();
a6ee485c 336 String destinationName = traceElement.getImportName();
7441221c
MAL
337
338 fileNameAndLabelPairs.add(new Pair<>(sourceName, destinationName));
339
6e651d8b 340 IPath containerPath = fTmfTraceFolder.getPath();
a6ee485c
MAL
341 IStatus status = importFiles(getSpecifiedArchiveFile(), fileNameAndLabelPairs, containerPath, Path.EMPTY, monitor);
342 if (getStatus().getSeverity() != IStatus.OK) {
343 return status;
344 }
345
346 // We need to set the trace type before importing the supplementary files so we do it here
347 IResource traceRes = fTmfTraceFolder.getResource().findMember(traceElement.getDestinationElementPath());
348 if (traceRes == null || !traceRes.exists()) {
349 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorFindingImportedTrace, destinationName));
350 }
351
352 TraceTypeHelper traceType = null;
353 String traceTypeStr = traceElement.getTraceType();
354 if (traceTypeStr != null) {
355 traceType = TmfTraceType.getInstance().getTraceType(traceTypeStr);
356 if (traceType == null) {
357 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorSettingTraceType, traceElement.getTraceType(), destinationName));
358 }
359 } else {
360 try {
361 monitor.subTask(MessageFormat.format(Messages.TracePackageImportOperation_DetectingTraceType, destinationName));
362 traceType = TmfTraceTypeUIUtils.selectTraceType(traceRes.getLocation().toOSString(), null, null);
363 } catch (TmfTraceImportException e) {
364 // Could not figure out the type
365 }
366 }
367
368 if (traceType != null) {
369 try {
370 TmfTraceTypeUIUtils.setTraceType(traceRes, traceType);
371 } catch (CoreException e) {
372 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorSettingTraceType, traceElement.getTraceType(), destinationName), e);
373 }
374 }
375
376 importBookmarks(traceRes, traceElement, monitor);
377
378 try {
379 URI uri = new File(getFileName()).toURI();
380 IPath entryPath = new Path(traceFilesElement.getFileName());
381 if (traceRes instanceof IFolder) {
382 entryPath = entryPath.addTrailingSeparator();
383 }
384 String sourceLocation = URIUtil.toUnencodedString(URIUtil.toJarURI(uri, entryPath));
385 traceRes.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation);
386 } catch (CoreException e) {
387 }
388
6e651d8b
MAL
389 return status;
390 }
391
195355a9 392 private IStatus importSupplFiles(TracePackageSupplFilesElement suppFilesElement, TracePackageTraceElement traceElement, IProgressMonitor monitor) {
7441221c 393 List<Pair<String, String>> fileNameAndLabelPairs = new ArrayList<>();
6e651d8b
MAL
394 for (TracePackageElement child : suppFilesElement.getChildren()) {
395 TracePackageSupplFileElement supplFile = (TracePackageSupplFileElement) child;
7441221c 396 fileNameAndLabelPairs.add(new Pair<>(supplFile.getText(), new Path(supplFile.getText()).lastSegment()));
6e651d8b
MAL
397 }
398
7441221c 399 if (!fileNameAndLabelPairs.isEmpty()) {
a6ee485c
MAL
400 TmfTraceElement existingTrace = getMatchingTraceElement(traceElement);
401 if (existingTrace != null) {
6e651d8b 402 ArchiveFile archiveFile = getSpecifiedArchiveFile();
a6ee485c
MAL
403 existingTrace.refreshSupplementaryFolder();
404 // Project/Traces/A/B -> A/B
405 IPath traceFolderRelativePath = fTmfTraceFolder.getPath().makeRelativeTo(fTmfTraceFolder.getProject().getTracesFolder().getPath());
406 // Project/.tracing/A/B/
407 IFolder traceSupplementaryFolder = fTmfTraceFolder.getTraceSupplementaryFolder(traceFolderRelativePath.toString());
408 IPath destinationContainerPath = traceSupplementaryFolder.getFullPath();
409 // Remove the .tracing segment at the beginnin so that a file in folder .tracing/A/B/ imports destinationContainerPath/A/B/
410 Path baseSourcePath = new Path(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER_NAME);
411 return importFiles(archiveFile, fileNameAndLabelPairs, destinationContainerPath, baseSourcePath, monitor);
6e651d8b
MAL
412 }
413 }
414
415 return Status.OK_STATUS;
416 }
417
a6ee485c 418 private IStatus importFiles(ArchiveFile archiveFile, List<Pair<String, String>> fileNameAndLabelPairs, IPath destinationContainerPath, IPath baseSourcePath, IProgressMonitor monitor) {
507b1336 419 List<ArchiveProviderElement> objects = new ArrayList<>();
6e651d8b
MAL
420 Enumeration<?> entries = archiveFile.entries();
421 while (entries.hasMoreElements()) {
422 ArchiveEntry entry = (ArchiveEntry) entries.nextElement();
423 String entryName = entry.getName();
424 IPath fullArchivePath = new Path(entryName);
425 if (fullArchivePath.hasTrailingSeparator()) {
426 // We only care about file entries as the folders will get created by the ImportOperation
427 continue;
428 }
429
7441221c 430 for (Pair<String, String> fileNameAndLabel : fileNameAndLabelPairs) {
f7885d6d 431
a6ee485c 432 // Examples: Traces/aaa/kernel/ .tracing/aaa/testtexttrace.txt/statistics.ht
7441221c 433 IPath searchedArchivePath = new Path(fileNameAndLabel.getFirst());
f7885d6d 434
6e651d8b 435 // Check if this archive entry matches the searched file name at this archive location
7441221c 436 boolean fileMatch = entryName.equalsIgnoreCase(searchedArchivePath.toString());
a6ee485c 437 // For example Traces/aaa/kernel/metadata matches Traces/aaa/kernel/
7441221c
MAL
438 boolean folderMatch = entryName.startsWith(searchedArchivePath + "/"); //$NON-NLS-1$
439
440 if (fileMatch || folderMatch) {
a6ee485c
MAL
441 // .tracing/aaa/testtexttrace.txt/statistics.ht -> aaa/testtexttrace.txt/statistics.ht
442 IPath destinationPath = fullArchivePath.makeRelativeTo(baseSourcePath);
7441221c 443
f7885d6d 444 // metadata statistics.ht
7441221c
MAL
445 // We don't use the label when the entry is a folder match because the labels for individual files
446 // under the folder are not specified in the manifest so just use the last segment.
447 String resourceLabel = folderMatch ? fullArchivePath.lastSegment() : fileNameAndLabel.getSecond();
6e651d8b
MAL
448
449 ArchiveProviderElement pe = new ArchiveProviderElement(destinationPath.toString(), resourceLabel, archiveFile, entry);
450 objects.add(pe);
451 break;
452 }
453 }
454 }
455
456 ImportProvider provider = new ImportProvider();
457
458 ImportOperation operation = new ImportOperation(destinationContainerPath,
459 null, provider, this,
460 objects);
461 operation.setCreateContainerStructure(true);
462 operation.setOverwriteResources(true);
463
464 try {
7441221c 465 operation.run(new SubProgressMonitor(monitor, fileNameAndLabelPairs.size(), SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
6e651d8b
MAL
466 archiveFile.close();
467 } catch (InvocationTargetException e) {
468 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.Messages.TracePackage_ErrorOperation, e);
469 } catch (InterruptedException e) {
470 return Status.CANCEL_STATUS;
471 } catch (IOException e) {
472 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.Messages.TracePackage_ErrorOperation, e);
473 }
474
475 if (provider.getException() != null) {
476 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.Messages.TracePackage_ErrorOperation, provider.getException());
477 }
478
479 return operation.getStatus();
480 }
481
482 @Override
483 public String queryOverwrite(String pathString) {
484 // We always overwrite once we reach this point
485 return null;
486 }
6e651d8b 487}
This page took 0.053985 seconds and 5 git commands to generate.