tmf: bug 494698 Add per-event fields to custom parsers
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomTxtEvent.java
CommitLineData
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 13package org.eclipse.tracecompass.tmf.core.parsers.custom;
6151d86c
PT
14
15import java.util.regex.Matcher;
16
ca5b04ad 17import org.eclipse.jdt.annotation.NonNull;
2bdf0193
AM
18import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
19import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
20import org.eclipse.tracecompass.tmf.core.event.TmfEventType;
f5cc6ed1 21import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag;
2bdf0193
AM
22import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.InputData;
23import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.InputLine;
24import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
25import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
6151d86c 26
a0a88f65
AM
27/**
28 * Trace event for custom text parsers.
29 *
30 * @author Patrick Tassé
31 */
6151d86c
PT
32public class CustomTxtEvent extends CustomEvent {
33
efeeb733
GB
34 private String fLastExtraFieldName = null;
35
a0a88f65
AM
36 /**
37 * Constructor
38 *
39 * @param definition
40 * Trace definition
41 */
6151d86c
PT
42 public CustomTxtEvent(CustomTxtTraceDefinition definition) {
43 super(definition);
6151d86c
PT
44 }
45
a0a88f65
AM
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 */
ca5b04ad 54 public CustomTxtEvent(CustomTxtTraceDefinition definition, @NonNull TmfEvent other) {
6151d86c
PT
55 super(definition, other);
56 }
57
a0a88f65
AM
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
a0a88f65
AM
67 * @param type
68 * Event type
a0a88f65
AM
69 */
70 public CustomTxtEvent(CustomTxtTraceDefinition definition,
e1de2fd4
AM
71 ITmfTrace parentTrace, ITmfTimestamp timestamp, TmfEventType type) {
72 super(definition, parentTrace, timestamp, type);
6151d86c
PT
73 }
74
75 @Override
76 public void setContent(ITmfEventField content) {
77 super.setContent(content);
78 }
79
a0a88f65
AM
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 */
6151d86c 88 public void processGroups(InputLine input, Matcher matcher) {
eddf2682 89 if (input.eventType != null) {
f5cc6ed1 90 fData.put(Tag.EVENT_TYPE, input.eventType);
eddf2682 91 }
d5efe032
AF
92 if (input.columns == null) {
93 return;
94 }
6151d86c
PT
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 }
f5cc6ed1 102 Object key = (column.tag.equals(Tag.OTHER) ? column.name : column.tag);
efeeb733
GB
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 }
6151d86c 118 if (column.action == CustomTraceDefinition.ACTION_SET) {
f5cc6ed1
PT
119 fData.put(key, value);
120 if (key.equals(Tag.TIMESTAMP)) {
121 fData.put(Key.TIMESTAMP_INPUT_FORMAT, column.format);
6151d86c
PT
122 }
123 } else if (column.action == CustomTraceDefinition.ACTION_APPEND) {
f5cc6ed1 124 String s = fData.get(key);
6151d86c 125 if (s != null) {
f5cc6ed1 126 fData.put(key, s + value);
6151d86c 127 } else {
f5cc6ed1 128 fData.put(key, value);
6151d86c 129 }
f5cc6ed1
PT
130 if (key.equals(Tag.TIMESTAMP)) {
131 String timeStampInputFormat = fData.get(Key.TIMESTAMP_INPUT_FORMAT);
6151d86c 132 if (timeStampInputFormat != null) {
f5cc6ed1 133 fData.put(Key.TIMESTAMP_INPUT_FORMAT, timeStampInputFormat + column.format);
6151d86c 134 } else {
f5cc6ed1 135 fData.put(Key.TIMESTAMP_INPUT_FORMAT, column.format);
6151d86c
PT
136 }
137 }
138 } else if (column.action == CustomTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
f5cc6ed1 139 String s = fData.get(key);
6151d86c 140 if (s != null) {
efeeb733 141 fData.put(key, s + CustomTraceDefinition.SEPARATOR + value);
6151d86c 142 } else {
f5cc6ed1 143 fData.put(key, value);
6151d86c 144 }
f5cc6ed1
PT
145 if (key.equals(Tag.TIMESTAMP)) {
146 String timeStampInputFormat = fData.get(Key.TIMESTAMP_INPUT_FORMAT);
6151d86c 147 if (timeStampInputFormat != null) {
f5cc6ed1 148 fData.put(Key.TIMESTAMP_INPUT_FORMAT, timeStampInputFormat + " | " + column.format); //$NON-NLS-1$
6151d86c 149 } else {
f5cc6ed1 150 fData.put(Key.TIMESTAMP_INPUT_FORMAT, column.format);
6151d86c
PT
151 }
152 }
153 }
154 }
155 }
156 }
157
158}
This page took 0.0943 seconds and 5 git commands to generate.