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 / CustomTraceDefinition.java
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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.ByteArrayInputStream;
18 import java.text.SimpleDateFormat;
19 import java.util.AbstractMap.SimpleEntry;
20 import java.util.List;
21 import java.util.Map.Entry;
22
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
25 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
26 import org.w3c.dom.Element;
27 import org.xml.sax.EntityResolver;
28 import org.xml.sax.ErrorHandler;
29 import org.xml.sax.InputSource;
30 import org.xml.sax.SAXException;
31 import org.xml.sax.SAXParseException;
32
33 /**
34 * Base class for custom trace definitions.
35 *
36 * @author Patrick Tassé
37 */
38 public abstract class CustomTraceDefinition {
39
40 /** "set" action */
41 public static final int ACTION_SET = 0;
42
43 /** "append" action */
44 public static final int ACTION_APPEND = 1;
45
46 /** "append with separator" action */
47 public static final int ACTION_APPEND_WITH_SEPARATOR = 2;
48
49 /**
50 * Input tag
51 *
52 * @since 2.1
53 */
54 public enum Tag {
55 /** Ignore */
56 IGNORE(Messages.CustomXmlTraceDefinition_ignoreTag),
57 /** Timestamp */
58 TIMESTAMP(TmfBaseAspects.getTimestampAspect().getName()),
59 /** Event type */
60 EVENT_TYPE(TmfBaseAspects.getEventTypeAspect().getName()),
61 /** Message */
62 MESSAGE(Messages.CustomTraceDefinition_messageTag),
63 /** Extra field name
64 * @since 2.2*/
65 EXTRA_FIELD_NAME(Messages.CustomTraceDefinition_extraFieldNameTag),
66 /** Extra field value
67 * @since 2.2*/
68 EXTRA_FIELD_VALUE(Messages.CustomTraceDefinition_extraFieldValueTag),
69 /**
70 * Extra fields
71 * <p>
72 * Used as output tag corresponding to the {@link #EXTRA_FIELD_NAME} and
73 * {@link #EXTRA_FIELD_VALUE} input tags.
74 * @since 2.2
75 */
76 EXTRA_FIELDS(Messages.CustomExtraFieldsAspect_extraFieldsAspectName),
77 /** Other */
78 OTHER(Messages.CustomTraceDefinition_otherTag);
79
80 private final String fLabel;
81
82 private Tag(String label) {
83 fLabel = label;
84 }
85
86 @Override
87 public String toString() {
88 return fLabel;
89 }
90
91 /**
92 * Get a tag from its label (toString).
93 *
94 * @param label
95 * the label
96 * @return the corresponding tag, or null
97 */
98 public static Tag fromLabel(String label) {
99 for (Tag tag : Tag.values()) {
100 if (tag.toString().equals(label)) {
101 return tag;
102 }
103 }
104 return null;
105 }
106
107 /**
108 * Get a tag from its name (identifier).
109 *
110 * @param name
111 * the name
112 * @return the corresponding tag, or null
113 */
114 public static Tag fromName(String name) {
115 for (Tag tag : Tag.values()) {
116 if (tag.name().equals(name)) {
117 return tag;
118 }
119 }
120 return null;
121 }
122 }
123
124 /**
125 * Separator to use with the
126 * {@link CustomTraceDefinition#ACTION_APPEND_WITH_SEPARATOR}
127 *
128 * @since 2.2
129 */
130 public static final @NonNull String SEPARATOR = " | "; //$NON-NLS-1$
131
132 /** Timestamp tag
133 * @deprecated Use {@link Tag#TIMESTAMP} instead. */
134 @Deprecated
135 public static final String TAG_TIMESTAMP = Messages.CustomTraceDefinition_timestampTag;
136
137 /** Message tag
138 * @deprecated Use {@link Tag#MESSAGE} instead. */
139 @Deprecated
140 public static final String TAG_MESSAGE = Messages.CustomTraceDefinition_messageTag;
141
142 /** "Other" tag
143 * @deprecated Use {@link Tag#OTHER} instead. */
144 @Deprecated
145 public static final String TAG_OTHER = Messages.CustomTraceDefinition_otherTag;
146
147 /** Category of this trace definition */
148 public String categoryName;
149
150 /** Name of this trace definition */
151 public String definitionName;
152
153 /** List of output columns */
154 public List<OutputColumn> outputs;
155
156 /** Timestamp format */
157 public String timeStampOutputFormat;
158
159 /**
160 * Definition of an output column
161 */
162 public static class OutputColumn {
163
164 /** Tag of this input
165 * @since 2.1*/
166 public @NonNull Tag tag;
167
168 /** Name of this column */
169 public @NonNull String name;
170
171 /**
172 * Default constructor (empty)
173 * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)}
174 * instead.
175 */
176 @Deprecated
177 public OutputColumn() {
178 this(Tag.IGNORE, ""); //$NON-NLS-1$
179 }
180
181 /**
182 * Constructor
183 *
184 * @param name
185 * Name of this output column
186 * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)}
187 * instead.
188 */
189 @Deprecated
190 public OutputColumn(@NonNull String name) {
191 this.tag = Tag.OTHER;
192 this.name = name;
193 }
194
195 /**
196 * Constructor
197 *
198 * @param tag
199 * Tag of this output column
200 * @param name
201 * Name of this output column
202 * @since 2.1
203 */
204 public OutputColumn(@NonNull Tag tag, @NonNull String name) {
205 this.tag = tag;
206 this.name = name;
207 }
208
209 @Override
210 public String toString() {
211 return name;
212 }
213 }
214
215 /**
216 * Format a timestamp in this trace's current time stamp format.
217 *
218 * @param timestamp
219 * The timestamp to format
220 * @return The same timestamp as a formatted string
221 */
222 public String formatTimeStamp(TmfTimestamp timestamp) {
223 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeStampOutputFormat);
224 return simpleDateFormat.format(timestamp.getValue());
225 }
226
227 /**
228 * Save this custom trace in the default path.
229 */
230 public abstract void save();
231
232 /**
233 * Save this custom trace in the supplied path.
234 *
235 * @param path
236 * The path to save to
237 */
238 public abstract void save(String path);
239
240 /**
241 * Creates a new empty entity resolver
242 *
243 * @return a new entity resolver
244 */
245 protected static EntityResolver createEmptyEntityResolver() {
246 return new EntityResolver() {
247 @Override
248 public InputSource resolveEntity(String publicId, String systemId) {
249 String empty = ""; //$NON-NLS-1$
250 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
251 return new InputSource(bais);
252 }
253 };
254 }
255
256 /**
257 * Creates an error handler for parse exceptions
258 *
259 * @return a new error handler
260 */
261 protected static ErrorHandler createErrorHandler() {
262 return new ErrorHandler() {
263 @Override
264 public void error(SAXParseException saxparseexception) throws SAXException {
265 }
266
267 @Override
268 public void warning(SAXParseException saxparseexception) throws SAXException {
269 }
270
271 @Override
272 public void fatalError(SAXParseException saxparseexception) throws SAXException {
273 throw saxparseexception;
274 }
275 };
276 }
277
278 /**
279 * Extract the tag and name from an XML element
280 *
281 * @param element
282 * the XML element
283 * @param tagAttribute
284 * the tag attribute
285 * @param nameAttribute
286 * the name attribute
287 * @return an entry where the key is the tag and the value is the name
288 * @since 2.1
289 */
290 protected static Entry<@NonNull Tag, @NonNull String> extractTagAndName(Element element, String tagAttribute, String nameAttribute) {
291 Tag tag = Tag.fromName(element.getAttribute(tagAttribute));
292 String name = element.getAttribute(nameAttribute);
293 if (tag == null) {
294 // Backward compatibility
295 if (name.equals(Messages.CustomTraceDefinition_timestampTag)) {
296 tag = Tag.TIMESTAMP;
297 name = checkNotNull(Tag.TIMESTAMP.toString());
298 } else if (name.equals(Messages.CustomTraceDefinition_messageTag)) {
299 tag = Tag.MESSAGE;
300 name = checkNotNull(Tag.MESSAGE.toString());
301 } else if (name.equals(Messages.CustomXmlTraceDefinition_ignoreTag)) {
302 tag = Tag.IGNORE;
303 name = checkNotNull(Tag.IGNORE.toString());
304 } else {
305 tag = Tag.OTHER;
306 }
307 } else if (name.isEmpty()) {
308 name = checkNotNull(tag.toString());
309 }
310 return new SimpleEntry<>(tag, name);
311 }
312 }
This page took 0.037771 seconds and 5 git commands to generate.