Commit | Line | Data |
---|---|---|
6151d86c | 1 | /******************************************************************************* |
53f17e49 | 2 | * Copyright (c) 2010, 2016 Ericsson |
6151d86c PT |
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 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.parsers.custom; |
6151d86c PT |
14 | |
15 | import java.text.ParseException; | |
6151d86c PT |
16 | import java.util.HashMap; |
17 | import java.util.Map; | |
7a0ebe01 | 18 | import java.util.Objects; |
6151d86c | 19 | |
ca5b04ad | 20 | import org.eclipse.jdt.annotation.NonNull; |
2bdf0193 AM |
21 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; |
22 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventType; | |
23 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; | |
24 | import org.eclipse.tracecompass.tmf.core.event.TmfEventField; | |
25 | import org.eclipse.tracecompass.tmf.core.event.TmfEventType; | |
26 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn; | |
f5cc6ed1 | 27 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag; |
2bdf0193 | 28 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
2bdf0193 AM |
29 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; |
30 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat; | |
0c7471fb | 31 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
2bdf0193 | 32 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
6151d86c | 33 | |
a0a88f65 AM |
34 | /** |
35 | * Base event for custom text parsers. | |
36 | * | |
37 | * @author Patrick Tassé | |
38 | */ | |
6151d86c PT |
39 | public class CustomEvent extends TmfEvent { |
40 | ||
f5cc6ed1 PT |
41 | /** Payload data map key |
42 | * @since 2.1*/ | |
43 | protected enum Key { | |
44 | /** Timestamp input format */ | |
45 | TIMESTAMP_INPUT_FORMAT | |
46 | } | |
47 | ||
48 | /** Input format key | |
49 | * @deprecated Use {@link Key#TIMESTAMP_INPUT_FORMAT} instead. */ | |
50 | @Deprecated | |
6151d86c | 51 | protected static final String TIMESTAMP_INPUT_FORMAT_KEY = "CE_TS_I_F"; //$NON-NLS-1$ |
a0a88f65 AM |
52 | |
53 | /** Empty message */ | |
6151d86c | 54 | protected static final String NO_MESSAGE = ""; //$NON-NLS-1$ |
6151d86c | 55 | |
bd54d363 | 56 | /** Replacement for the super-class' timestamp field */ |
cbf0057c | 57 | private @NonNull ITmfTimestamp customEventTimestamp; |
bd54d363 AM |
58 | |
59 | /** Replacement for the super-class' content field */ | |
60 | private ITmfEventField customEventContent; | |
61 | ||
62 | /** Replacement for the super-class' type field */ | |
63 | private ITmfEventType customEventType; | |
64 | ||
a0a88f65 | 65 | /** The trace to which this event belongs */ |
6151d86c | 66 | protected CustomTraceDefinition fDefinition; |
a0a88f65 | 67 | |
f5cc6ed1 PT |
68 | /** |
69 | * The payload data of this event, where the key is one of: the {@link Tag}, | |
70 | * the field name string if the tag is {@link Tag#OTHER}, or | |
71 | * {@link Key#TIMESTAMP_INPUT_FORMAT}. | |
72 | */ | |
73 | protected Map<Object, String> fData; | |
a0a88f65 | 74 | |
6151d86c PT |
75 | private TmfEventField[] fColumnData; |
76 | ||
a0a88f65 AM |
77 | /** |
78 | * Basic constructor. | |
79 | * | |
80 | * @param definition | |
81 | * The trace definition to which this event belongs | |
82 | */ | |
6151d86c | 83 | public CustomEvent(CustomTraceDefinition definition) { |
e1de2fd4 | 84 | super(null, ITmfContext.UNKNOWN_RANK, null, null, null); |
6151d86c | 85 | fDefinition = definition; |
507b1336 | 86 | fData = new HashMap<>(); |
cbf0057c | 87 | customEventTimestamp = TmfTimestamp.ZERO; |
6151d86c PT |
88 | } |
89 | ||
a0a88f65 AM |
90 | /** |
91 | * Build a new CustomEvent from an existing TmfEvent. | |
92 | * | |
93 | * @param definition | |
94 | * The trace definition to which this event belongs | |
95 | * @param other | |
96 | * The TmfEvent to copy | |
97 | */ | |
ca5b04ad | 98 | public CustomEvent(CustomTraceDefinition definition, @NonNull TmfEvent other) { |
6151d86c PT |
99 | super(other); |
100 | fDefinition = definition; | |
507b1336 | 101 | fData = new HashMap<>(); |
bd54d363 AM |
102 | |
103 | /* Set our overridden fields */ | |
104 | customEventTimestamp = other.getTimestamp(); | |
105 | customEventContent = other.getContent(); | |
106 | customEventType = other.getType(); | |
6151d86c PT |
107 | } |
108 | ||
a0a88f65 AM |
109 | /** |
110 | * Full constructor | |
111 | * | |
112 | * @param definition | |
113 | * Trace definition of this event | |
114 | * @param parentTrace | |
115 | * Parent trace object | |
116 | * @param timestamp | |
117 | * Timestamp of this event | |
a0a88f65 AM |
118 | * @param type |
119 | * Event type | |
a0a88f65 AM |
120 | */ |
121 | public CustomEvent(CustomTraceDefinition definition, ITmfTrace parentTrace, | |
e1de2fd4 | 122 | ITmfTimestamp timestamp, TmfEventType type) { |
bd54d363 | 123 | /* Do not use upstream's fields for stuff we override */ |
e1de2fd4 | 124 | super(parentTrace, ITmfContext.UNKNOWN_RANK, null, null, null); |
6151d86c | 125 | fDefinition = definition; |
507b1336 | 126 | fData = new HashMap<>(); |
bd54d363 AM |
127 | |
128 | /* Set our overridden fields */ | |
cbf0057c GB |
129 | if (timestamp == null) { |
130 | customEventTimestamp = TmfTimestamp.ZERO; | |
131 | } else { | |
132 | customEventTimestamp = timestamp; | |
133 | } | |
bd54d363 AM |
134 | customEventContent = null; |
135 | customEventType = type; | |
6151d86c PT |
136 | } |
137 | ||
bd54d363 AM |
138 | // ------------------------------------------------------------------------ |
139 | // Overridden getters | |
140 | // ------------------------------------------------------------------------ | |
141 | ||
6151d86c PT |
142 | @Override |
143 | public ITmfTimestamp getTimestamp() { | |
144 | if (fData != null) { | |
145 | processData(); | |
146 | } | |
bd54d363 | 147 | return customEventTimestamp; |
6151d86c PT |
148 | } |
149 | ||
bd54d363 AM |
150 | @Override |
151 | public ITmfEventField getContent() { | |
0ff3b4eb PT |
152 | if (fData != null) { |
153 | processData(); | |
154 | } | |
bd54d363 AM |
155 | return customEventContent; |
156 | } | |
157 | ||
158 | @Override | |
159 | public ITmfEventType getType() { | |
160 | return customEventType; | |
161 | } | |
162 | ||
53f17e49 GB |
163 | @Override |
164 | public String getName() { | |
165 | if (fData != null) { | |
166 | processData(); | |
167 | } | |
168 | return super.getName(); | |
169 | } | |
170 | ||
bd54d363 AM |
171 | // ------------------------------------------------------------------------ |
172 | // Setters | |
173 | // ------------------------------------------------------------------------ | |
174 | ||
175 | /** | |
176 | * Set this event's timestamp | |
177 | * | |
178 | * @param timestamp | |
179 | * The new timestamp | |
180 | */ | |
cbf0057c | 181 | protected void setTimestamp(@NonNull ITmfTimestamp timestamp) { |
bd54d363 AM |
182 | customEventTimestamp = timestamp; |
183 | } | |
184 | ||
185 | /** | |
186 | * Set this event's content | |
187 | * | |
188 | * @param content | |
189 | * The new content | |
190 | */ | |
191 | protected void setContent(ITmfEventField content) { | |
192 | customEventContent = content; | |
193 | } | |
194 | ||
eab78906 PT |
195 | /** |
196 | * Get this event's content value. | |
197 | * <p> | |
198 | * This does not process the payload data and is therefore safe to call in | |
199 | * the middle of parsing an event. | |
200 | * | |
201 | * @return the event's content value. | |
202 | */ | |
203 | Object getContentValue() { | |
204 | return customEventContent.getValue(); | |
205 | } | |
206 | ||
bd54d363 AM |
207 | /** |
208 | * Set this event's type | |
209 | * | |
210 | * @param type | |
211 | * The new type | |
212 | */ | |
213 | protected void setType(ITmfEventType type) { | |
214 | customEventType = type; | |
215 | } | |
216 | ||
217 | // ------------------------------------------------------------------------ | |
218 | // Other operations | |
219 | // ------------------------------------------------------------------------ | |
220 | ||
baafe54c AM |
221 | /** |
222 | * Get the contents of an event table cell for this event's row. | |
223 | * | |
224 | * @param index | |
225 | * The ID/index of the field to display. This corresponds to the | |
226 | * index in the event content. | |
227 | * @return The String to display in the cell | |
baafe54c AM |
228 | */ |
229 | public String getEventString(int index) { | |
230 | if (fData != null) { | |
231 | processData(); | |
232 | } | |
233 | if (index < 0 || index >= fColumnData.length) { | |
234 | return ""; //$NON-NLS-1$ | |
235 | } | |
236 | ||
237 | return fColumnData[index].getValue().toString(); | |
238 | } | |
239 | ||
6151d86c | 240 | private void processData() { |
f5cc6ed1 PT |
241 | String timestampString = fData.get(Tag.TIMESTAMP); |
242 | String timestampInputFormat = fData.get(Key.TIMESTAMP_INPUT_FORMAT); | |
b2c971ec | 243 | ITmfTimestamp timestamp = null; |
738beb68 PT |
244 | if (timestampInputFormat != null && timestampString != null) { |
245 | TmfTimestampFormat timestampFormat = new TmfTimestampFormat(timestampInputFormat); | |
6151d86c | 246 | try { |
738beb68 | 247 | long time = timestampFormat.parseValue(timestampString); |
b2c971ec | 248 | timestamp = TmfTimestamp.fromNanos(getTrace().getTimestampTransform().transform(time)); |
738beb68 | 249 | setTimestamp(timestamp); |
6151d86c PT |
250 | } catch (ParseException e) { |
251 | setTimestamp(TmfTimestamp.ZERO); | |
252 | } | |
253 | } else { | |
254 | setTimestamp(TmfTimestamp.ZERO); | |
255 | } | |
256 | ||
53f17e49 | 257 | // Update the custom event type of this event if set |
f5cc6ed1 | 258 | String eventName = fData.get(Tag.EVENT_TYPE); |
53f17e49 GB |
259 | ITmfEventType type = getType(); |
260 | if (eventName != null && type instanceof CustomEventType) { | |
261 | ((CustomEventType) type).setName(eventName); | |
262 | } | |
263 | ||
6151d86c PT |
264 | int i = 0; |
265 | fColumnData = new TmfEventField[fDefinition.outputs.size()]; | |
266 | for (OutputColumn outputColumn : fDefinition.outputs) { | |
f5cc6ed1 PT |
267 | Object key = (outputColumn.tag.equals(Tag.OTHER) ? outputColumn.name : outputColumn.tag); |
268 | String value = fData.get(key); | |
269 | if (outputColumn.tag.equals(Tag.TIMESTAMP) && timestamp != null) { | |
738beb68 PT |
270 | TmfTimestampFormat timestampFormat = new TmfTimestampFormat(fDefinition.timeStampOutputFormat); |
271 | fColumnData[i++] = new TmfEventField(outputColumn.name, timestampFormat.format(timestamp.getValue()), null); | |
6151d86c | 272 | } else { |
214cc822 | 273 | fColumnData[i++] = new TmfEventField(outputColumn.name, (value != null ? value : ""), null); //$NON-NLS-1$ |
6151d86c PT |
274 | } |
275 | } | |
0ff3b4eb | 276 | setContent(new CustomEventContent(customEventContent.getName(), customEventContent.getValue(), fColumnData)); |
6151d86c PT |
277 | fData = null; |
278 | } | |
279 | ||
6151d86c PT |
280 | @Override |
281 | public int hashCode() { | |
282 | final int prime = 31; | |
283 | int result = super.hashCode(); | |
284 | result = prime * result + ((fDefinition == null) ? 0 : fDefinition.hashCode()); | |
cbf0057c | 285 | result = prime * result + customEventTimestamp.hashCode(); |
bd54d363 AM |
286 | result = prime * result + ((customEventContent == null) ? 0 : customEventContent.hashCode()); |
287 | result = prime * result + ((customEventType == null) ? 0 : customEventType.hashCode()); | |
6151d86c PT |
288 | return result; |
289 | } | |
290 | ||
6151d86c PT |
291 | @Override |
292 | public boolean equals(Object obj) { | |
293 | if (this == obj) { | |
294 | return true; | |
295 | } | |
296 | if (!super.equals(obj)) { | |
297 | return false; | |
298 | } | |
299 | if (!(obj instanceof CustomEvent)) { | |
300 | return false; | |
301 | } | |
302 | CustomEvent other = (CustomEvent) obj; | |
7a0ebe01 | 303 | if (!Objects.equals(fDefinition, other.fDefinition)) { |
6151d86c PT |
304 | return false; |
305 | } | |
bd54d363 | 306 | |
cbf0057c | 307 | if (!customEventTimestamp.equals(other.customEventTimestamp)) { |
bd54d363 AM |
308 | return false; |
309 | } | |
310 | ||
7a0ebe01 | 311 | if (!Objects.equals(customEventContent, other.customEventContent)) { |
bd54d363 AM |
312 | return false; |
313 | } | |
314 | ||
7a0ebe01 | 315 | if (!Objects.equals(customEventType, other.customEventType)) { |
bd54d363 AM |
316 | return false; |
317 | } | |
6151d86c PT |
318 | return true; |
319 | } | |
320 | ||
321 | } |