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
index d2b9935b591c6c63048079ac92cd0323c428ccb8..00f23f998749fdfca55a0612ee86e969db9e89c8 100644 (file)
 
 package org.eclipse.tracecompass.tmf.core.parsers.custom;
 
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
 import java.io.ByteArrayInputStream;
 import java.text.SimpleDateFormat;
+import java.util.AbstractMap.SimpleEntry;
 import java.util.List;
+import java.util.Map.Entry;
 
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
+import org.w3c.dom.Element;
 import org.xml.sax.EntityResolver;
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.InputSource;
@@ -40,19 +46,104 @@ public abstract class CustomTraceDefinition {
     /** "append with separator" action */
     public static final int ACTION_APPEND_WITH_SEPARATOR = 2;
 
-    /** Timestamp tag */
+    /**
+     * Input tag
+     *
+     * @since 2.1
+     */
+    public enum Tag {
+        /** Ignore */
+        IGNORE(Messages.CustomXmlTraceDefinition_ignoreTag),
+        /** Timestamp */
+        TIMESTAMP(TmfBaseAspects.getTimestampAspect().getName()),
+        /** Event type */
+        EVENT_TYPE(TmfBaseAspects.getEventTypeAspect().getName()),
+        /** Message */
+        MESSAGE(Messages.CustomTraceDefinition_messageTag),
+        /** Extra field name
+         * @since 2.2*/
+        EXTRA_FIELD_NAME(Messages.CustomTraceDefinition_extraFieldNameTag),
+        /** Extra field value
+         * @since 2.2*/
+        EXTRA_FIELD_VALUE(Messages.CustomTraceDefinition_extraFieldValueTag),
+        /**
+         * Extra fields
+         * <p>
+         * Used as output tag corresponding to the {@link #EXTRA_FIELD_NAME} and
+         * {@link #EXTRA_FIELD_VALUE} input tags.
+         * @since 2.2
+         */
+        EXTRA_FIELDS(Messages.CustomExtraFieldsAspect_extraFieldsAspectName),
+        /** Other */
+        OTHER(Messages.CustomTraceDefinition_otherTag);
+
+        private final String fLabel;
+
+        private Tag(String label) {
+            fLabel = label;
+        }
+
+        @Override
+        public String toString() {
+            return fLabel;
+        }
+
+        /**
+         * Get a tag from its label (toString).
+         *
+         * @param label
+         *            the label
+         * @return the corresponding tag, or null
+         */
+        public static Tag fromLabel(String label) {
+            for (Tag tag : Tag.values()) {
+                if (tag.toString().equals(label)) {
+                    return tag;
+                }
+            }
+            return null;
+        }
+
+        /**
+         * Get a tag from its name (identifier).
+         *
+         * @param name
+         *            the name
+         * @return the corresponding tag, or null
+         */
+        public static Tag fromName(String name) {
+            for (Tag tag : Tag.values()) {
+                if (tag.name().equals(name)) {
+                    return tag;
+                }
+            }
+            return null;
+        }
+    }
+
+    /**
+     * Separator to use with the
+     * {@link CustomTraceDefinition#ACTION_APPEND_WITH_SEPARATOR}
+     *
+     * @since 2.2
+     */
+    public static final @NonNull String SEPARATOR = " | ";  //$NON-NLS-1$
+
+    /** Timestamp tag
+     * @deprecated Use {@link Tag#TIMESTAMP} instead. */
+    @Deprecated
     public static final String TAG_TIMESTAMP = Messages.CustomTraceDefinition_timestampTag;
 
-    /** Message tag */
+    /** Message tag
+     * @deprecated Use {@link Tag#MESSAGE} instead. */
+    @Deprecated
     public static final String TAG_MESSAGE = Messages.CustomTraceDefinition_messageTag;
 
-    /** "Other" tag */
+    /** "Other" tag
+     * @deprecated Use {@link Tag#OTHER} instead. */
+    @Deprecated
     public static final String TAG_OTHER = Messages.CustomTraceDefinition_otherTag;
 
-    /** Event type tag
-     * @since 2.1*/
-    public static final String TAG_EVENT_TYPE = Messages.CustomTraceDefinition_eventTypeTag;
-
     /** Category of this trace definition */
     public String categoryName;
 
@@ -70,22 +161,48 @@ public abstract class CustomTraceDefinition {
      */
     public static class OutputColumn {
 
+        /** Tag of this input
+         * @since 2.1*/
+        public @NonNull Tag tag;
+
         /** Name of this column */
         public @NonNull String name;
 
         /**
          * Default constructor (empty)
+         * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)}
+         *             instead.
          */
+        @Deprecated
         public OutputColumn() {
-            this(""); //$NON-NLS-1$
+            this(Tag.IGNORE, ""); //$NON-NLS-1$
         }
 
         /**
          * Constructor
          *
-         * @param name Name of this output column
+         * @param name
+         *            Name of this output column
+         * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)}
+         *             instead.
          */
+        @Deprecated
         public OutputColumn(@NonNull String name) {
+            this.tag = Tag.OTHER;
+            this.name = name;
+        }
+
+        /**
+         * Constructor
+         *
+         * @param tag
+         *            Tag of this output column
+         * @param name
+         *            Name of this output column
+         * @since 2.1
+         */
+        public OutputColumn(@NonNull Tag tag, @NonNull String name) {
+            this.tag = tag;
             this.name = name;
         }
 
@@ -157,4 +274,39 @@ public abstract class CustomTraceDefinition {
             }
         };
     }
+
+    /**
+     * Extract the tag and name from an XML element
+     *
+     * @param element
+     *            the XML element
+     * @param tagAttribute
+     *            the tag attribute
+     * @param nameAttribute
+     *            the name attribute
+     * @return an entry where the key is the tag and the value is the name
+     * @since 2.1
+     */
+    protected static Entry<@NonNull Tag, @NonNull String> extractTagAndName(Element element, String tagAttribute, String nameAttribute) {
+        Tag tag = Tag.fromName(element.getAttribute(tagAttribute));
+        String name = element.getAttribute(nameAttribute);
+        if (tag == null) {
+            // Backward compatibility
+            if (name.equals(Messages.CustomTraceDefinition_timestampTag)) {
+                tag = Tag.TIMESTAMP;
+                name = checkNotNull(Tag.TIMESTAMP.toString());
+            } else if (name.equals(Messages.CustomTraceDefinition_messageTag)) {
+                tag = Tag.MESSAGE;
+                name = checkNotNull(Tag.MESSAGE.toString());
+            } else if (name.equals(Messages.CustomXmlTraceDefinition_ignoreTag)) {
+                tag = Tag.IGNORE;
+                name = checkNotNull(Tag.IGNORE.toString());
+            } else {
+                tag = Tag.OTHER;
+            }
+        } else if (name.isEmpty()) {
+            name = checkNotNull(tag.toString());
+        }
+        return new SimpleEntry<>(tag, name);
+    }
 }
This page took 0.045123 seconds and 5 git commands to generate.