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