1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
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
;
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
;
34 * Base class for custom trace definitions.
36 * @author Patrick Tassé
38 public abstract class CustomTraceDefinition
{
41 public static final int ACTION_SET
= 0;
43 /** "append" action */
44 public static final int ACTION_APPEND
= 1;
46 /** "append with separator" action */
47 public static final int ACTION_APPEND_WITH_SEPARATOR
= 2;
56 IGNORE(Messages
.CustomXmlTraceDefinition_ignoreTag
),
58 TIMESTAMP(TmfBaseAspects
.getTimestampAspect().getName()),
60 EVENT_TYPE(TmfBaseAspects
.getEventTypeAspect().getName()),
62 MESSAGE(Messages
.CustomTraceDefinition_messageTag
),
65 EXTRA_FIELD_NAME(Messages
.CustomTraceDefinition_extraFieldNameTag
),
68 EXTRA_FIELD_VALUE(Messages
.CustomTraceDefinition_extraFieldValueTag
),
72 * Used as output tag corresponding to the {@link #EXTRA_FIELD_NAME} and
73 * {@link #EXTRA_FIELD_VALUE} input tags.
76 EXTRA_FIELDS(Messages
.CustomExtraFieldsAspect_extraFieldsAspectName
),
78 OTHER(Messages
.CustomTraceDefinition_otherTag
);
80 private final String fLabel
;
82 private Tag(String label
) {
87 public String
toString() {
92 * Get a tag from its label (toString).
96 * @return the corresponding tag, or null
98 public static Tag
fromLabel(String label
) {
99 for (Tag tag
: Tag
.values()) {
100 if (tag
.toString().equals(label
)) {
108 * Get a tag from its name (identifier).
112 * @return the corresponding tag, or null
114 public static Tag
fromName(String name
) {
115 for (Tag tag
: Tag
.values()) {
116 if (tag
.name().equals(name
)) {
125 * Separator to use with the
126 * {@link CustomTraceDefinition#ACTION_APPEND_WITH_SEPARATOR}
130 public static final @NonNull String SEPARATOR
= " | "; //$NON-NLS-1$
133 * @deprecated Use {@link Tag#TIMESTAMP} instead. */
135 public static final String TAG_TIMESTAMP
= Messages
.CustomTraceDefinition_timestampTag
;
138 * @deprecated Use {@link Tag#MESSAGE} instead. */
140 public static final String TAG_MESSAGE
= Messages
.CustomTraceDefinition_messageTag
;
143 * @deprecated Use {@link Tag#OTHER} instead. */
145 public static final String TAG_OTHER
= Messages
.CustomTraceDefinition_otherTag
;
147 /** Category of this trace definition */
148 public String categoryName
;
150 /** Name of this trace definition */
151 public String definitionName
;
153 /** List of output columns */
154 public List
<OutputColumn
> outputs
;
156 /** Timestamp format */
157 public String timeStampOutputFormat
;
160 * Definition of an output column
162 public static class OutputColumn
{
164 /** Tag of this input
166 public @NonNull Tag tag
;
168 /** Name of this column */
169 public @NonNull String name
;
172 * Default constructor (empty)
173 * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)}
177 public OutputColumn() {
178 this(Tag
.IGNORE
, ""); //$NON-NLS-1$
185 * Name of this output column
186 * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)}
190 public OutputColumn(@NonNull String name
) {
191 this.tag
= Tag
.OTHER
;
199 * Tag of this output column
201 * Name of this output column
204 public OutputColumn(@NonNull Tag tag
, @NonNull String name
) {
210 public String
toString() {
216 * Format a timestamp in this trace's current time stamp format.
219 * The timestamp to format
220 * @return The same timestamp as a formatted string
222 public String
formatTimeStamp(TmfTimestamp timestamp
) {
223 SimpleDateFormat simpleDateFormat
= new SimpleDateFormat(timeStampOutputFormat
);
224 return simpleDateFormat
.format(timestamp
.getValue());
228 * Save this custom trace in the default path.
230 public abstract void save();
233 * Save this custom trace in the supplied path.
236 * The path to save to
238 public abstract void save(String path
);
241 * Creates a new empty entity resolver
243 * @return a new entity resolver
245 protected static EntityResolver
createEmptyEntityResolver() {
246 return new EntityResolver() {
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
);
257 * Creates an error handler for parse exceptions
259 * @return a new error handler
261 protected static ErrorHandler
createErrorHandler() {
262 return new ErrorHandler() {
264 public void error(SAXParseException saxparseexception
) throws SAXException
{
268 public void warning(SAXParseException saxparseexception
) throws SAXException
{
272 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
273 throw saxparseexception
;
279 * Extract the tag and name from an XML element
283 * @param tagAttribute
285 * @param nameAttribute
287 * @return an entry where the key is the tag and the value is the name
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
);
294 // Backward compatibility
295 if (name
.equals(Messages
.CustomTraceDefinition_timestampTag
)) {
297 name
= checkNotNull(Tag
.TIMESTAMP
.toString());
298 } else if (name
.equals(Messages
.CustomTraceDefinition_messageTag
)) {
300 name
= checkNotNull(Tag
.MESSAGE
.toString());
301 } else if (name
.equals(Messages
.CustomXmlTraceDefinition_ignoreTag
)) {
303 name
= checkNotNull(Tag
.IGNORE
.toString());
307 } else if (name
.isEmpty()) {
308 name
= checkNotNull(tag
.toString());
310 return new SimpleEntry
<>(tag
, name
);