custom.parsers: bug 494697 Define event names in custom parsers
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomEvent.java
... / ...
CommitLineData
1/*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson
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
13package org.eclipse.tracecompass.tmf.core.parsers.custom;
14
15import static org.eclipse.tracecompass.common.core.NonNullUtils.equalsNullable;
16
17import java.text.ParseException;
18import java.util.HashMap;
19import java.util.Map;
20
21import org.eclipse.jdt.annotation.NonNull;
22import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
23import org.eclipse.tracecompass.tmf.core.event.ITmfEventType;
24import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
25import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
26import org.eclipse.tracecompass.tmf.core.event.TmfEventType;
27import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn;
28import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
29import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
30import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
31import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
32import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
33
34/**
35 * Base event for custom text parsers.
36 *
37 * @author Patrick Tassé
38 */
39public class CustomEvent extends TmfEvent {
40
41 /** Input format key */
42 protected static final String TIMESTAMP_INPUT_FORMAT_KEY = "CE_TS_I_F"; //$NON-NLS-1$
43
44 /** Empty message */
45 protected static final String NO_MESSAGE = ""; //$NON-NLS-1$
46
47 /** Replacement for the super-class' timestamp field */
48 private @NonNull ITmfTimestamp customEventTimestamp;
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
56 /** The trace to which this event belongs */
57 protected CustomTraceDefinition fDefinition;
58
59 /** The payload data of this event, <field name, value> */
60 protected Map<String, String> fData;
61
62 private TmfEventField[] fColumnData;
63
64 /**
65 * Basic constructor.
66 *
67 * @param definition
68 * The trace definition to which this event belongs
69 */
70 public CustomEvent(CustomTraceDefinition definition) {
71 super(null, ITmfContext.UNKNOWN_RANK, null, null, null);
72 fDefinition = definition;
73 fData = new HashMap<>();
74 customEventTimestamp = TmfTimestamp.ZERO;
75 }
76
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 */
85 public CustomEvent(CustomTraceDefinition definition, @NonNull TmfEvent other) {
86 super(other);
87 fDefinition = definition;
88 fData = new HashMap<>();
89
90 /* Set our overridden fields */
91 customEventTimestamp = other.getTimestamp();
92 customEventContent = other.getContent();
93 customEventType = other.getType();
94 }
95
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
105 * @param type
106 * Event type
107 */
108 public CustomEvent(CustomTraceDefinition definition, ITmfTrace parentTrace,
109 ITmfTimestamp timestamp, TmfEventType type) {
110 /* Do not use upstream's fields for stuff we override */
111 super(parentTrace, ITmfContext.UNKNOWN_RANK, null, null, null);
112 fDefinition = definition;
113 fData = new HashMap<>();
114
115 /* Set our overridden fields */
116 if (timestamp == null) {
117 customEventTimestamp = TmfTimestamp.ZERO;
118 } else {
119 customEventTimestamp = timestamp;
120 }
121 customEventContent = null;
122 customEventType = type;
123 }
124
125 // ------------------------------------------------------------------------
126 // Overridden getters
127 // ------------------------------------------------------------------------
128
129 @Override
130 public ITmfTimestamp getTimestamp() {
131 if (fData != null) {
132 processData();
133 }
134 return customEventTimestamp;
135 }
136
137 @Override
138 public ITmfEventField getContent() {
139 if (fData != null) {
140 processData();
141 }
142 return customEventContent;
143 }
144
145 @Override
146 public ITmfEventType getType() {
147 return customEventType;
148 }
149
150 @Override
151 public String getName() {
152 if (fData != null) {
153 processData();
154 }
155 return super.getName();
156 }
157
158 // ------------------------------------------------------------------------
159 // Setters
160 // ------------------------------------------------------------------------
161
162 /**
163 * Set this event's timestamp
164 *
165 * @param timestamp
166 * The new timestamp
167 */
168 protected void setTimestamp(@NonNull ITmfTimestamp timestamp) {
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
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
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
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
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
227 private void processData() {
228 String timestampString = fData.get(CustomTraceDefinition.TAG_TIMESTAMP);
229 String timestampInputFormat = fData.get(TIMESTAMP_INPUT_FORMAT_KEY);
230 ITmfTimestamp timestamp = null;
231 if (timestampInputFormat != null && timestampString != null) {
232 TmfTimestampFormat timestampFormat = new TmfTimestampFormat(timestampInputFormat);
233 try {
234 long time = timestampFormat.parseValue(timestampString);
235 timestamp = TmfTimestamp.fromNanos(getTrace().getTimestampTransform().transform(time));
236 setTimestamp(timestamp);
237 } catch (ParseException e) {
238 setTimestamp(TmfTimestamp.ZERO);
239 }
240 } else {
241 setTimestamp(TmfTimestamp.ZERO);
242 }
243
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
251 int i = 0;
252 fColumnData = new TmfEventField[fDefinition.outputs.size()];
253 for (OutputColumn outputColumn : fDefinition.outputs) {
254 String value = fData.get(outputColumn.name);
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);
258 } else {
259 fColumnData[i++] = new TmfEventField(outputColumn.name, (value != null ? value : ""), null); //$NON-NLS-1$
260 }
261 }
262 setContent(new CustomEventContent(customEventContent.getName(), customEventContent.getValue(), fColumnData));
263 fData = null;
264 }
265
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());
271 result = prime * result + customEventTimestamp.hashCode();
272 result = prime * result + ((customEventContent == null) ? 0 : customEventContent.hashCode());
273 result = prime * result + ((customEventType == null) ? 0 : customEventType.hashCode());
274 return result;
275 }
276
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;
289 if (!equalsNullable(fDefinition, other.fDefinition)) {
290 return false;
291 }
292
293 if (!customEventTimestamp.equals(other.customEventTimestamp)) {
294 return false;
295 }
296
297 if (!equalsNullable(customEventContent, other.customEventContent)) {
298 return false;
299 }
300
301 if (!equalsNullable(customEventType, other.customEventType)) {
302 return false;
303 }
304 return true;
305 }
306
307}
This page took 0.026929 seconds and 5 git commands to generate.