From: Geneviève Bastien Date: Tue, 9 Aug 2016 20:14:35 +0000 (-0400) Subject: xml: bug 497272 Populate views from built-in XML files X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=d2452723c4085c5261addc215bd91466d3340558;hp=3ef48ce154faf24f26f66ca76699c44f21b7f15c;p=deliverable%2Ftracecompass.git xml: bug 497272 Populate views from built-in XML files This patch makes sure that outputs defined in built-in XML files advertised through the extension point are added under their analyses. It moves the method to list the built-in files from the analysis module source to the XmlUtils class and adds a unit test for this use case. Change-Id: I5984d1cc9adbbc7e6348632c0f58390f73db41bd Signed-off-by: Geneviève Bastien Reviewed-on: https://git.eclipse.org/r/78723 Reviewed-by: Jean-Christian Kouame Tested-by: Jean-Christian Kouame Reviewed-by: Hudson CI --- diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_builtin.xml b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_builtin.xml index 3e7a1e8715..b4f433c04a 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_builtin.xml +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/test_xml_files/test_builtin.xml @@ -25,4 +25,14 @@ + + + + + \ No newline at end of file diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/META-INF/MANIFEST.MF b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/META-INF/MANIFEST.MF index 3f9d0bb48d..ce566b032a 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/META-INF/MANIFEST.MF +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/META-INF/MANIFEST.MF @@ -17,8 +17,11 @@ Export-Package: org.eclipse.tracecompass.internal.tmf.analysis.xml.core;x-friend org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,org.eclipse.tracecompass.tmf.analysis.xml.ui", org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.readonly;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,org.eclipse.tracecompass.tmf.analysis.xml.ui", org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.readwrite;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,org.eclipse.tracecompass.tmf.analysis.xml.ui", - org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,org.eclipse.tracecompass.tmf.analysis.xml.ui, - org.eclipse.tracecompass.tmf.analysis.xml.ui.swtbot.tests", + org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module; + x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests, + org.eclipse.tracecompass.tmf.analysis.xml.ui, + org.eclipse.tracecompass.tmf.analysis.xml.ui.swtbot.tests, + org.eclipse.tracecompass.tmf.analysis.xml.ui.tests", org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.ui,org.eclipse.tracecompass.tmf.analysis.xml.core.tests", org.eclipse.tracecompass.internal.tmf.analysis.xml.core.segment;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests", org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.core.tests,org.eclipse.tracecompass.tmf.analysis.xml.ui" diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlAnalysisModuleSource.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlAnalysisModuleSource.java index 8900602e60..ac3e3ffdc6 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlAnalysisModuleSource.java +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlAnalysisModuleSource.java @@ -11,10 +11,8 @@ package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.net.URL; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.List; @@ -22,12 +20,7 @@ import java.util.Map; import javax.xml.parsers.ParserConfigurationException; -import org.eclipse.core.runtime.FileLocator; -import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IPath; -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.internal.tmf.analysis.xml.core.Activator; import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType; @@ -35,7 +28,6 @@ import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.Tmf import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper; import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleSource; import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager; -import org.osgi.framework.Bundle; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -50,11 +42,6 @@ import org.xml.sax.SAXException; */ public class XmlAnalysisModuleSource implements IAnalysisModuleSource { - /** Extension point ID */ - private static final String TMF_XML_BUILTIN_ID = "org.eclipse.linuxtools.tmf.analysis.xml.core.files"; //$NON-NLS-1$ - private static final String XML_FILE_ELEMENT = "xmlfile"; //$NON-NLS-1$ - - private static final String XML_FILE_ATTRIB = "file"; //$NON-NLS-1$ /* * Legacy (Linux Tools) XML directory. @@ -117,39 +104,9 @@ public class XmlAnalysisModuleSource implements IAnalysisModuleSource { } private static void populateBuiltinModules() { - /* Get the XML files advertised through the extension point */ - IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_XML_BUILTIN_ID); - for (IConfigurationElement element : elements) { - if (element.getName().equals(XML_FILE_ELEMENT)) { - final String filename = element.getAttribute(XML_FILE_ATTRIB); - final String name = element.getContributor().getName(); - // Run this in a safe runner in case there is an exception - // (IOException, FileNotFoundException, NPE, etc). - // This makes sure other extensions are not prevented from - // working if one is faulty. - 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); - if (xmlUrl == null) { - throw new FileNotFoundException(filename); - } - URL locatedURL = FileLocator.toFileURL(xmlUrl); - processFile(new File(locatedURL.getFile())); - } - } - } - - @Override - public void handleException(Throwable exception) { - // Handled sufficiently in SafeRunner - } - }); - } + Map files = XmlUtils.listBuiltinFiles(); + for (IPath xmlPath : files.values()) { + processFile(xmlPath.toFile()); } } diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlUtils.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlUtils.java index 28105b0444..626e73c744 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlUtils.java +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlUtils.java @@ -16,6 +16,7 @@ import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyStrin import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; @@ -35,15 +36,21 @@ import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.ISafeRunnable; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.SafeRunner; import org.eclipse.core.runtime.Status; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.osgi.util.NLS; import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator; import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; +import org.osgi.framework.Bundle; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -65,9 +72,13 @@ public class XmlUtils { /** Name of the XSD schema file */ private static final String XSD = "xmlDefinition.xsd"; //$NON-NLS-1$ + /** Extension point ID and attributes */ + private static final String TMF_XML_BUILTIN_ID = "org.eclipse.linuxtools.tmf.analysis.xml.core.files"; //$NON-NLS-1$ + private static final String XML_FILE_ELEMENT = "xmlfile"; //$NON-NLS-1$ + private static final String XML_FILE_ATTRIB = "file"; //$NON-NLS-1$ + /** * Extension for XML files - * @since 2.0 */ public static final String XML_EXTENSION = "xml"; //$NON-NLS-1$ @@ -148,7 +159,6 @@ public class XmlUtils { * the key is the file name. * * @return A map with all the XML analysis files - * @since 2.0 */ public static synchronized @NonNull Map listFiles() { IPath pathToFiles = XmlUtils.getXmlFilesPath(); @@ -171,12 +181,55 @@ public class XmlUtils { return Collections.unmodifiableMap(fileMap); } + /** + * List all files advertised through the builtin extension point. It returns a map where the key is the file name. + * + * @return A map with all the XMl analysis builtin files + */ + public static synchronized @NonNull Map listBuiltinFiles() { + /* Get the XML files advertised through the extension point */ + IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_XML_BUILTIN_ID); + Map map = new HashMap<>(); + for (IConfigurationElement element : elements) { + if (element.getName().equals(XML_FILE_ELEMENT)) { + final String filename = element.getAttribute(XML_FILE_ATTRIB); + final String name = element.getContributor().getName(); + // Run this in a safe runner in case there is an exception + // (IOException, FileNotFoundException, NPE, etc). + // This makes sure other extensions are not prevented from + // working if one is faulty. + 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); + if (xmlUrl == null) { + throw new FileNotFoundException(filename); + } + URL locatedURL = FileLocator.toFileURL(xmlUrl); + map.put(filename, new Path(locatedURL.getPath())); + } + } + } + + @Override + public void handleException(Throwable exception) { + // Handled sufficiently in SafeRunner + } + }); + } + } + return map; + } + /** * Delete an XML analysis file * * @param name * The XML file to delete - * @since 2.0 */ public static void deleteFile(String name) { Map files = listFiles(); @@ -195,7 +248,6 @@ public class XmlUtils { * @param to * The full path of the file to write to * @return Whether the file was successfully exported - * @since 2.0 */ public static IStatus exportXmlFile(String from, String to) { @@ -242,7 +294,6 @@ public class XmlUtils { * @param fileName * The file name * @return The list of IDs - * @since 2.0 */ public static List getAnalysisIdsFromFile(String fileName) { List ids = new ArrayList<>(); @@ -281,7 +332,6 @@ public class XmlUtils { * If any parse errors occur. * @throws IOException * If any IO errors occur. - * @since 2.0 */ public static Document getDocumentFromFile(File file) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/META-INF/MANIFEST.MF b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/META-INF/MANIFEST.MF index e8368206ba..a1137eae44 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/META-INF/MANIFEST.MF +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/META-INF/MANIFEST.MF @@ -14,7 +14,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.tracecompass.tmf.analysis.xml.core, org.eclipse.tracecompass.tmf.analysis.xml.ui, org.eclipse.tracecompass.tmf.ui, - org.eclipse.tracecompass.tmf.analysis.xml.core.tests + org.eclipse.tracecompass.tmf.analysis.xml.core.tests, + org.eclipse.tracecompass.tmf.core.tests Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Bundle-ActivationPolicy: lazy Import-Package: com.google.common.collect;version="15.0.0", diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisOutputSourceTest.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisOutputSourceTest.java new file mode 100644 index 0000000000..eb37dea420 --- /dev/null +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui.tests/src/org/eclipse/tracecompass/tmf/analysis/xml/ui/tests/module/XmlAnalysisOutputSourceTest.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2016 École Polytechnique de Montréal + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ + +package org.eclipse.tracecompass.tmf.analysis.xml.ui.tests.module; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Iterator; + +import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module.TmfXmlAnalysisOutputSource; +import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; +import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisOutput; +import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; +import org.eclipse.tracecompass.tmf.core.trace.TmfTrace; +import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub; +import org.junit.Test; + +/** + * Test that XML-defined outputs are added to the analysis modules they are for + * + * @author Geneviève Bastien + */ +public class XmlAnalysisOutputSourceTest { + + private static final String BUILTIN_MODULE = "test.builtin.sp"; + private static final String BUILTIN_OUTPUT= "Test output of XML builtin module"; + + /** + * Test the + * {@link TmfXmlAnalysisOutputSource#moduleCreated(IAnalysisModule)} method + */ + @Test + public void testBuiltinOutput() { + + TmfTrace trace = new TmfXmlTraceStub(); + try { + trace.traceOpened(new TmfTraceOpenedSignal(this, trace, null)); + + IAnalysisModule module = trace.getAnalysisModule(BUILTIN_MODULE); + assertNotNull(module); + + Iterator iterator = module.getOutputs().iterator(); + assertTrue(iterator.hasNext()); + IAnalysisOutput output = iterator.next(); + assertEquals(BUILTIN_OUTPUT, output.getName()); + + } finally { + trace.dispose(); + } + + } + +} \ No newline at end of file diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/META-INF/MANIFEST.MF b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/META-INF/MANIFEST.MF index 4111c9334e..cb72168756 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/META-INF/MANIFEST.MF +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/META-INF/MANIFEST.MF @@ -24,7 +24,7 @@ Require-Bundle: org.eclipse.ui, org.eclipse.core.filesystem Export-Package: org.eclipse.tracecompass.internal.tmf.analysis.xml.ui;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.ui.tests", org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.handler;x-internal:=true, - org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module;x-internal:=true, + org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.ui.tests", org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views;x-internal:=true, org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency;x-friends:="org.eclipse.tracecompass.tmf.analysis.xml.ui.swtbot.tests", org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.timegraph;x-internal:=true, diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/module/TmfXmlAnalysisOutputSource.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/module/TmfXmlAnalysisOutputSource.java index bf668a74d3..9e09080c29 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/module/TmfXmlAnalysisOutputSource.java +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/module/TmfXmlAnalysisOutputSource.java @@ -15,7 +15,8 @@ package org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module; import java.io.File; import java.io.IOException; import java.util.List; -import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; import javax.xml.parsers.ParserConfigurationException; @@ -142,8 +143,14 @@ public class TmfXmlAnalysisOutputSource implements ITmfNewAnalysisModuleListener @Override public void moduleCreated(IAnalysisModule module) { - Map files = XmlUtils.listFiles(); - for (File xmlFile : files.values()) { + // Get all the XML files, builtin and not builtin + Set files = XmlUtils.listBuiltinFiles().values().stream() + .map(p -> p.toFile()) + .collect(Collectors.toSet()); + XmlUtils.listFiles().values().stream() + .forEach(f -> files.add(f)); + + for (File xmlFile : files) { if (!XmlUtils.xmlValidate(xmlFile).isOK()) { continue; }