Commit | Line | Data |
---|---|---|
d3de0920 | 1 | /******************************************************************************* |
53bae902 | 2 | * Copyright (c) 2013, 2014 Kalray, Ericsson |
d3de0920 XR |
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 | * Xavier Raynaud - Initial API and implementation | |
53bae902 | 11 | * Bernd Hufmann - Adapted to new events table column API |
d3de0920 XR |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.internal.tmf.ui.commands; | |
15 | ||
16 | import java.io.IOException; | |
17 | import java.io.Writer; | |
53bae902 | 18 | import java.util.List; |
d3de0920 XR |
19 | |
20 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; | |
22 | import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter; | |
2740e05c | 23 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
fd3f1eff AM |
24 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; |
25 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
53bae902 | 26 | import org.eclipse.linuxtools.tmf.ui.viewers.events.columns.TmfEventTableColumn; |
d3de0920 XR |
27 | |
28 | /** | |
29 | * This TMF Requests exports traces to text files. | |
30 | * @author Xavier Raynaud <xavier.raynaud@kalray.eu> | |
31 | */ | |
fd3f1eff | 32 | public class ExportToTextRequest extends TmfEventRequest { |
d3de0920 XR |
33 | |
34 | private final Writer fWriter; | |
35 | private final ITmfFilter fFilter; | |
53bae902 | 36 | private final List<TmfEventTableColumn> fColumns; |
d3de0920 XR |
37 | private IOException fIOException; |
38 | ||
39 | /** | |
40 | * Constructor | |
41 | * @param w | |
42 | * a Writer, typically a FileWriter. | |
43 | * @param filter | |
44 | * a TmfFilter, if we want to filter some events. May be <code>null</code>. | |
53bae902 BH |
45 | * @param columns |
46 | * the {@link TmfEventTableColumn} requesting the export (may be <code>null</code>) | |
d3de0920 | 47 | */ |
53bae902 | 48 | public ExportToTextRequest(Writer w, ITmfFilter filter, List<TmfEventTableColumn> columns) { |
2740e05c | 49 | super(ITmfEvent.class, TmfTimeRange.ETERNITY, 0, ITmfEventRequest.ALL_DATA, ExecutionType.FOREGROUND); |
d3de0920 XR |
50 | this.fWriter = w; |
51 | this.fFilter = filter; | |
53bae902 | 52 | this.fColumns = columns; |
d3de0920 XR |
53 | } |
54 | ||
55 | /** | |
56 | * Gets the IOException thrown by this export request, if any. | |
57 | * @return the fIoException | |
58 | */ | |
59 | public IOException getIOException() { | |
60 | return fIOException; | |
61 | } | |
62 | ||
63 | @Override | |
64 | public void handleData(final ITmfEvent event) { | |
65 | super.handleData(event); | |
66 | if (isCancelled()) { | |
67 | return; | |
68 | } | |
69 | try { | |
70 | if (fFilter == null || fFilter.matches(event)) { | |
53bae902 | 71 | if (fColumns != null) { |
d3de0920 | 72 | boolean needTab = false; |
53bae902 | 73 | for (TmfEventTableColumn column : fColumns) { |
d3de0920 XR |
74 | if (needTab) { |
75 | fWriter.write('\t'); | |
76 | } | |
53bae902 | 77 | fWriter.write(column.getItemString(event)); |
d3de0920 XR |
78 | needTab = true; |
79 | } | |
80 | } else { // fallback to default formatting | |
81 | fWriter.write(event.getTimestamp().toString()); | |
82 | fWriter.write('\t'); | |
83 | fWriter.write(event.getSource()); | |
84 | fWriter.write('\t'); | |
85 | fWriter.write(event.getType().getName()); | |
86 | fWriter.write('\t'); | |
87 | fWriter.write(event.getReference()); | |
88 | fWriter.write('\t'); | |
89 | ITmfEventField content = event.getContent(); | |
53bae902 BH |
90 | Object value = content.getValue(); |
91 | if (value != null) { | |
92 | fWriter.write(value.toString()); | |
93 | } | |
d3de0920 XR |
94 | } |
95 | fWriter.write('\n'); | |
96 | } | |
97 | } catch (IOException ex) { | |
98 | fIOException = ex; | |
99 | fail(); | |
100 | } | |
101 | } | |
102 | ||
d3de0920 | 103 | } |