tmf: Add extension point to add custom trace types to plugins
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomTraceDefinition.java
index 00f23f998749fdfca55a0612ee86e969db9e89c8..55efec0badedfd51a3d55f0de5d366f372efdc9c 100644 (file)
@@ -15,14 +15,24 @@ package org.eclipse.tracecompass.tmf.core.parsers.custom;
 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.net.URL;
 import java.text.SimpleDateFormat;
 import java.util.AbstractMap.SimpleEntry;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map.Entry;
 
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.ISafeRunnable;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.SafeRunner;
 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.osgi.framework.Bundle;
 import org.w3c.dom.Element;
 import org.xml.sax.EntityResolver;
 import org.xml.sax.ErrorHandler;
@@ -144,6 +154,11 @@ public abstract class CustomTraceDefinition {
     @Deprecated
     public static final String TAG_OTHER = Messages.CustomTraceDefinition_otherTag;
 
+    private static final String TMF_CUSTOM_TRACE_BUILTIN_EXTENSION_ID = "org.eclipse.tracecompass.tmf.core.custom.trace"; //$NON-NLS-1$
+    private static final String ATTRIBUTE_NAME_FILE = "file"; //$NON-NLS-1$
+    private static final String ATTRIBUTE_NAME_TRACE_CONTENT_TYPE = "traceContentType"; //$NON-NLS-1$
+    private static final String ELEMENT_NAME_CUSTOM_TRACE = "customTrace"; //$NON-NLS-1$
+
     /** Category of this trace definition */
     public String categoryName;
 
@@ -309,4 +324,52 @@ public abstract class CustomTraceDefinition {
         }
         return new SimpleEntry<>(tag, name);
     }
+
+    /**
+     * Get all the custom trace definition paths contributed by extensions, for
+     * a given content type (XML or Text).
+     *
+     * @param traceContentTypeToLoad
+     *            XML or Text (extension attribute value)
+     * @return the paths
+     *
+     * Note: This method is package-visible by design.
+     */
+    static final Collection<String> getExtensionDefinitionsPaths(String traceContentTypeToLoad) {
+        List<String> extensionDefinitionsPaths = new ArrayList<>();
+        IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_CUSTOM_TRACE_BUILTIN_EXTENSION_ID);
+        for (IConfigurationElement element : elements) {
+            if (!element.getName().equals(ELEMENT_NAME_CUSTOM_TRACE)) {
+                continue;
+            }
+
+            final String traceContentType = element.getAttribute(ATTRIBUTE_NAME_TRACE_CONTENT_TYPE);
+            if (!traceContentType.equals(traceContentTypeToLoad)) {
+                continue;
+            }
+
+            final String filename = element.getAttribute(ATTRIBUTE_NAME_FILE);
+            final String name = element.getContributor().getName();
+            SafeRunner.run(new ISafeRunnable() {
+                @Override
+                public void run() throws IOException {
+                    if (name != null) {
+                        Bundle bundle = Platform.getBundle(name);
+                        if (bundle != null) {
+                            URL xmlUrl = bundle.getResource(filename);
+                            URL locatedURL = FileLocator.toFileURL(xmlUrl);
+                            extensionDefinitionsPaths.add(locatedURL.getPath());
+                        }
+                    }
+                }
+
+                @Override
+                public void handleException(Throwable exception) {
+                    // Handled sufficiently in SafeRunner
+                }
+            });
+
+        }
+        return extensionDefinitionsPaths;
+    }
 }
This page took 0.024557 seconds and 5 git commands to generate.