1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.ui
.module
;
16 import java
.io
.FileNotFoundException
;
17 import java
.io
.IOException
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
22 import javax
.xml
.parsers
.DocumentBuilder
;
23 import javax
.xml
.parsers
.DocumentBuilderFactory
;
24 import javax
.xml
.parsers
.ParserConfigurationException
;
26 import org
.eclipse
.core
.runtime
.FileLocator
;
27 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
28 import org
.eclipse
.core
.runtime
.IPath
;
29 import org
.eclipse
.core
.runtime
.ISafeRunnable
;
30 import org
.eclipse
.core
.runtime
.Platform
;
31 import org
.eclipse
.core
.runtime
.SafeRunner
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.Activator
;
33 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
34 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
35 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.ui
.module
.TmfAnalysisModuleHelperXml
.XmlAnalysisModuleType
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleSource
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisManager
;
39 import org
.osgi
.framework
.Bundle
;
40 import org
.w3c
.dom
.Document
;
41 import org
.w3c
.dom
.Element
;
42 import org
.w3c
.dom
.NodeList
;
43 import org
.xml
.sax
.SAXException
;
46 * Analysis module source who creates helpers for the analysis modules described
47 * in the imported XML files
49 * @author Geneviève Bastien
52 public class XmlAnalysisModuleSource
implements IAnalysisModuleSource
{
54 /** Extension point ID */
55 private static final String TMF_XML_BUILTIN_ID
= "org.eclipse.linuxtools.tmf.analysis.xml.core.files"; //$NON-NLS-1$
56 private static final String XML_FILE_ELEMENT
= "xmlfile"; //$NON-NLS-1$
58 private static final String XML_FILE_ATTRIB
= "file"; //$NON-NLS-1$
60 private static List
<IAnalysisModuleHelper
> fModules
= null;
63 * Constructor. It adds the new module listener to the analysis manager.
65 public XmlAnalysisModuleSource() {
66 TmfAnalysisManager
.addNewModuleListener(new TmfXmlAnalysisOutputSource());
70 public synchronized Iterable
<IAnalysisModuleHelper
> getAnalysisModules() {
71 if (fModules
== null) {
72 fModules
= new ArrayList
<>();
73 populateBuiltinModules();
74 populateAnalysisModules();
79 private static void processFile(File xmlFile
) {
80 if (!XmlUtils
.xmlValidate(xmlFile
).isOK()) {
85 /* Load the XML File */
86 DocumentBuilderFactory dbFactory
= DocumentBuilderFactory
.newInstance();
87 DocumentBuilder dBuilder
= dbFactory
.newDocumentBuilder();
88 Document doc
= dBuilder
.parse(xmlFile
);
89 doc
.getDocumentElement().normalize();
91 /* get State Providers modules */
92 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
93 for (int i
= 0; i
< stateproviderNodes
.getLength(); i
++) {
94 Element node
= (Element
) stateproviderNodes
.item(i
);
96 IAnalysisModuleHelper helper
= new TmfAnalysisModuleHelperXml(xmlFile
, node
, XmlAnalysisModuleType
.STATE_SYSTEM
);
99 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
100 Activator
.logError("Error opening XML file", e
); //$NON-NLS-1$
104 private static void populateBuiltinModules() {
105 /* Get the XML files advertised through the extension point */
106 IConfigurationElement
[] elements
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TMF_XML_BUILTIN_ID
);
107 for (IConfigurationElement element
: elements
) {
108 if (element
.getName().equals(XML_FILE_ELEMENT
)) {
109 final String filename
= element
.getAttribute(XML_FILE_ATTRIB
);
110 final String name
= element
.getContributor().getName();
111 // Run this in a safe runner in case there is an exception
112 // (IOException, FileNotFoundException, NPE, etc).
113 // This makes sure other extensions are not prevented from
114 // working if one is faulty.
115 SafeRunner
.run(new ISafeRunnable() {
118 public void run() throws Exception
{
120 Bundle bundle
= Platform
.getBundle(name
);
121 if (bundle
!= null) {
122 URL xmlUrl
= bundle
.getResource(filename
);
123 if (xmlUrl
== null) {
124 throw new FileNotFoundException(filename
);
126 URL locatedURL
= FileLocator
.toFileURL(xmlUrl
);
127 processFile(new File(locatedURL
.getFile()));
133 public void handleException(Throwable exception
) {
134 // Handled sufficiently in SafeRunner
141 private static void populateAnalysisModules() {
142 IPath pathToFiles
= XmlUtils
.getXmlFilesPath();
143 File fFolder
= pathToFiles
.toFile();
144 if (!(fFolder
.isDirectory() && fFolder
.exists())) {
147 for (File xmlFile
: fFolder
.listFiles()) {
148 processFile(xmlFile
);
153 * Notifies the main XML analysis module that the executable modules list
154 * may have changed and needs to be refreshed.
156 public static void notifyModuleChange() {
158 TmfAnalysisManager
.refreshModules();