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