| 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 | |
| 13 | package org.eclipse.tracecompass.tmf.core.parsers.custom; |
| 14 | |
| 15 | import java.util.regex.Matcher; |
| 16 | |
| 17 | import org.eclipse.jdt.annotation.NonNull; |
| 18 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; |
| 19 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; |
| 20 | import org.eclipse.tracecompass.tmf.core.event.TmfEventType; |
| 21 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag; |
| 22 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.InputData; |
| 23 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.InputLine; |
| 24 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
| 25 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 26 | |
| 27 | /** |
| 28 | * Trace event for custom text parsers. |
| 29 | * |
| 30 | * @author Patrick Tassé |
| 31 | */ |
| 32 | public class CustomTxtEvent extends CustomEvent { |
| 33 | |
| 34 | private String fLastExtraFieldName = null; |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | * |
| 39 | * @param definition |
| 40 | * Trace definition |
| 41 | */ |
| 42 | public CustomTxtEvent(CustomTxtTraceDefinition definition) { |
| 43 | super(definition); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Construct a custom text event from an existing TmfEvent. |
| 48 | * |
| 49 | * @param definition |
| 50 | * Trace definition |
| 51 | * @param other |
| 52 | * The TmfEvent object to copy |
| 53 | */ |
| 54 | public CustomTxtEvent(CustomTxtTraceDefinition definition, @NonNull TmfEvent other) { |
| 55 | super(definition, other); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Full constructor. |
| 60 | * |
| 61 | * @param definition |
| 62 | * Trace definition |
| 63 | * @param parentTrace |
| 64 | * Parent trace object |
| 65 | * @param timestamp |
| 66 | * Timestamp of this event |
| 67 | * @param type |
| 68 | * Event type |
| 69 | */ |
| 70 | public CustomTxtEvent(CustomTxtTraceDefinition definition, |
| 71 | ITmfTrace parentTrace, ITmfTimestamp timestamp, TmfEventType type) { |
| 72 | super(definition, parentTrace, timestamp, type); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void setContent(ITmfEventField content) { |
| 77 | super.setContent(content); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Process an entry in the trace file |
| 82 | * |
| 83 | * @param input |
| 84 | * The input line to read |
| 85 | * @param matcher |
| 86 | * The regex matcher to use |
| 87 | */ |
| 88 | public void processGroups(InputLine input, Matcher matcher) { |
| 89 | if (input.eventType != null) { |
| 90 | fData.put(Tag.EVENT_TYPE, input.eventType); |
| 91 | } |
| 92 | if (input.columns == null) { |
| 93 | return; |
| 94 | } |
| 95 | for (int i = 0; i < input.columns.size(); i++) { |
| 96 | InputData column = input.columns.get(i); |
| 97 | if (i < matcher.groupCount() && matcher.group(i + 1) != null) { |
| 98 | String value = matcher.group(i + 1).trim(); |
| 99 | if (value.length() == 0) { |
| 100 | continue; |
| 101 | } |
| 102 | Object key = (column.tag.equals(Tag.OTHER) ? column.name : column.tag); |
| 103 | if (key.equals(Tag.EXTRA_FIELD_NAME)) { |
| 104 | // If tag extra field name, save the extra field name for |
| 105 | // the next extra field value and add the field to the map |
| 106 | fLastExtraFieldName = value; |
| 107 | if (!fData.containsKey(value)) { |
| 108 | fData.put(value, null); |
| 109 | } |
| 110 | continue; |
| 111 | } else if (key.equals(Tag.EXTRA_FIELD_VALUE)) { |
| 112 | // If tag extra field value, use the extra field name as key |
| 113 | if (fLastExtraFieldName == null) { |
| 114 | continue; |
| 115 | } |
| 116 | key = fLastExtraFieldName; |
| 117 | } |
| 118 | if (column.action == CustomTraceDefinition.ACTION_SET) { |
| 119 | fData.put(key, value); |
| 120 | if (key.equals(Tag.TIMESTAMP)) { |
| 121 | fData.put(Key.TIMESTAMP_INPUT_FORMAT, column.format); |
| 122 | } |
| 123 | } else if (column.action == CustomTraceDefinition.ACTION_APPEND) { |
| 124 | String s = fData.get(key); |
| 125 | if (s != null) { |
| 126 | fData.put(key, s + value); |
| 127 | } else { |
| 128 | fData.put(key, value); |
| 129 | } |
| 130 | if (key.equals(Tag.TIMESTAMP)) { |
| 131 | String timeStampInputFormat = fData.get(Key.TIMESTAMP_INPUT_FORMAT); |
| 132 | if (timeStampInputFormat != null) { |
| 133 | fData.put(Key.TIMESTAMP_INPUT_FORMAT, timeStampInputFormat + column.format); |
| 134 | } else { |
| 135 | fData.put(Key.TIMESTAMP_INPUT_FORMAT, column.format); |
| 136 | } |
| 137 | } |
| 138 | } else if (column.action == CustomTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) { |
| 139 | String s = fData.get(key); |
| 140 | if (s != null) { |
| 141 | fData.put(key, s + CustomTraceDefinition.SEPARATOR + value); |
| 142 | } else { |
| 143 | fData.put(key, value); |
| 144 | } |
| 145 | if (key.equals(Tag.TIMESTAMP)) { |
| 146 | String timeStampInputFormat = fData.get(Key.TIMESTAMP_INPUT_FORMAT); |
| 147 | if (timeStampInputFormat != null) { |
| 148 | fData.put(Key.TIMESTAMP_INPUT_FORMAT, timeStampInputFormat + " | " + column.format); //$NON-NLS-1$ |
| 149 | } else { |
| 150 | fData.put(Key.TIMESTAMP_INPUT_FORMAT, column.format); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | } |