Make Custom Parser trace type backwards compatible to Linux Tools
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomXmlTrace.java
index aa38c7093fb6e692ea1337c9122ac69b25cdb51d..5c91bcb3819c8c794b50d66ae642270b0c4863b9 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2014 Ericsson
+ * Copyright (c) 2010, 2015 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -8,6 +8,7 @@
  *
  * Contributors:
  *   Patrick Tasse - Initial API and implementation
+ *   Bernd Hufmann - Add trace type id handling
  *******************************************************************************/
 
 package org.eclipse.tracecompass.tmf.core.parsers.custom;
@@ -26,6 +27,7 @@ import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.internal.tmf.core.Activator;
 import org.eclipse.tracecompass.internal.tmf.core.parsers.custom.CustomEventAspects;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
@@ -35,6 +37,7 @@ import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
@@ -59,7 +62,7 @@ import org.xml.sax.SAXParseException;
  * @author Patrick Tassé
  * @since 3.0
  */
-public class CustomXmlTrace extends CustomTrace implements ITmfPersistentlyIndexable {
+public class CustomXmlTrace extends TmfTrace implements ITmfPersistentlyIndexable {
 
     private static final TmfLongLocation NULL_LOCATION = new TmfLongLocation(-1L);
     private static final int DEFAULT_CACHE_SIZE = 100;
@@ -70,6 +73,12 @@ public class CustomXmlTrace extends CustomTrace implements ITmfPersistentlyIndex
     private final CustomXmlEventType fEventType;
     private final CustomXmlInputElement fRecordInputElement;
     private BufferedRandomAccessFile fFile;
+    private final String fTraceTypeId;
+
+    private static final char SEPARATOR = ':';
+    private static final String CUSTOM_XML_TRACE_TYPE_PREFIX = "custom.xml.trace" + SEPARATOR; //$NON-NLS-1$
+    private static final String LINUX_TOOLS_CUSTOM_XML_TRACE_TYPE_PREFIX = "org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTrace" + SEPARATOR; //$NON-NLS-1$
+    private static final String EARLY_TRACE_COMPASS_CUSTOM_XML_TRACE_TYPE_PREFIX = "org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace" + SEPARATOR; //$NON-NLS-1$
 
     /**
      * Basic constructor
@@ -78,10 +87,10 @@ public class CustomXmlTrace extends CustomTrace implements ITmfPersistentlyIndex
      *            Trace definition
      */
     public CustomXmlTrace(final CustomXmlTraceDefinition definition) {
-        super(definition);
         fDefinition = definition;
         fEventType = new CustomXmlEventType(fDefinition);
         fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);
+        fTraceTypeId = buildTraceTypeId(definition.categoryName, definition.definitionName);
         setCacheSize(DEFAULT_CACHE_SIZE);
     }
 
@@ -588,4 +597,69 @@ public class CustomXmlTrace extends CustomTrace implements ITmfPersistentlyIndex
     protected ITmfTraceIndexer createIndexer(int interval) {
         return new TmfBTreeTraceIndexer(this, interval);
     }
+
+    @Override
+    public String getTraceTypeId() {
+        return fTraceTypeId;
+    }
+
+    /**
+     * Build the trace type id for a custom XML trace
+     *
+     * @param category
+     *            the category
+     * @param definitionName
+     *            the definition name
+     * @return the trace type id
+     */
+    public static @NonNull String buildTraceTypeId(String category, String definitionName) {
+        return CUSTOM_XML_TRACE_TYPE_PREFIX + category + SEPARATOR + definitionName;
+    }
+
+    /**
+     * Checks whether the given trace type ID is a custom XML trace type ID
+     *
+     * @param traceTypeId
+     *                the trace type ID to check
+     * @return <code>true</code> if it's a custom text trace type ID else <code>false</code>
+     */
+    public static boolean isCustomTraceTypeId(@NonNull String traceTypeId) {
+        return traceTypeId.startsWith(CUSTOM_XML_TRACE_TYPE_PREFIX);
+    }
+
+    /**
+     * This methods builds a trace type ID from a given ID taking into
+     * consideration any format changes that were done for the IDs of custom
+     * XML traces. For example, such format change took place when moving to
+     * Trace Compass. Trace type IDs that are part of the plug-in extension for
+     * trace types won't be changed.
+     *
+     * This method is useful for IDs that were persisted in the workspace before
+     * the format changes (e.g. in the persistent properties of a trace
+     * resource).
+     *
+     * It ensures backwards compatibility of the workspace for custom XML
+     * traces.
+     *
+     * @param traceTypeId
+     *            the legacy trace type ID
+     * @return the trace type id in Trace Compass format
+     */
+    public static @NonNull String buildCompatibilityTraceTypeId(@NonNull String traceTypeId) {
+        // Handle early Trace Compass custom XML trace type IDs
+        if (traceTypeId.startsWith(EARLY_TRACE_COMPASS_CUSTOM_XML_TRACE_TYPE_PREFIX)) {
+            return CUSTOM_XML_TRACE_TYPE_PREFIX + traceTypeId.substring(EARLY_TRACE_COMPASS_CUSTOM_XML_TRACE_TYPE_PREFIX.length());
+        }
+
+        // Handle Linux Tools custom XML trace type IDs (with and without category)
+        int index = traceTypeId.lastIndexOf(SEPARATOR);
+        if ((index != -1) && (traceTypeId.startsWith(LINUX_TOOLS_CUSTOM_XML_TRACE_TYPE_PREFIX))) {
+            String definitionName = index < traceTypeId.length() ? traceTypeId.substring(index + 1) : ""; //$NON-NLS-1$
+            if (traceTypeId.contains(CustomXmlTrace.class.getSimpleName() + SEPARATOR) && traceTypeId.indexOf(SEPARATOR) == index) {
+                return buildTraceTypeId(CustomXmlTraceDefinition.CUSTOM_XML_CATEGORY, definitionName);
+            }
+            return CUSTOM_XML_TRACE_TYPE_PREFIX + traceTypeId.substring(LINUX_TOOLS_CUSTOM_XML_TRACE_TYPE_PREFIX.length());
+        }
+        return traceTypeId;
+    }
 }
This page took 0.027963 seconds and 5 git commands to generate.