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