Commit | Line | Data |
---|---|---|
97ed0cf0 FW |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 École Polytechnique de Montréal | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.analysis.xml.ui.module; |
97ed0cf0 FW |
14 | |
15 | import java.io.File; | |
16 | import java.io.IOException; | |
0281ea1c | 17 | import java.net.URL; |
97ed0cf0 FW |
18 | import java.util.ArrayList; |
19 | import java.util.List; | |
20 | ||
21 | import javax.xml.parsers.DocumentBuilder; | |
22 | import javax.xml.parsers.DocumentBuilderFactory; | |
23 | import javax.xml.parsers.ParserConfigurationException; | |
24 | ||
0281ea1c GB |
25 | import org.eclipse.core.runtime.FileLocator; |
26 | import org.eclipse.core.runtime.IConfigurationElement; | |
97ed0cf0 | 27 | import org.eclipse.core.runtime.IPath; |
0281ea1c | 28 | import org.eclipse.core.runtime.Platform; |
2bdf0193 AM |
29 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator; |
30 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; | |
31 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
32 | import org.eclipse.tracecompass.tmf.analysis.xml.ui.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType; | |
33 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper; | |
34 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleSource; | |
35 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager; | |
0281ea1c | 36 | import org.osgi.framework.Bundle; |
97ed0cf0 FW |
37 | import org.w3c.dom.Document; |
38 | import org.w3c.dom.Element; | |
39 | import org.w3c.dom.NodeList; | |
40 | import org.xml.sax.SAXException; | |
41 | ||
42 | /** | |
43 | * Analysis module source who creates helpers for the analysis modules described | |
44 | * in the imported XML files | |
45 | * | |
46 | * @author Geneviève Bastien | |
47 | * @since 3.0 | |
48 | */ | |
49 | public class XmlAnalysisModuleSource implements IAnalysisModuleSource { | |
50 | ||
0281ea1c GB |
51 | /** Extension point ID */ |
52 | private static final String TMF_XML_BUILTIN_ID = "org.eclipse.linuxtools.tmf.analysis.xml.core.files"; //$NON-NLS-1$ | |
53 | private static final String XML_FILE_ELEMENT = "xmlfile"; //$NON-NLS-1$ | |
54 | ||
55 | private static final String XML_FILE_ATTRIB = "file"; //$NON-NLS-1$ | |
56 | ||
97ed0cf0 FW |
57 | private static List<IAnalysisModuleHelper> fModules = null; |
58 | ||
048bde85 GB |
59 | /** |
60 | * Constructor. It adds the new module listener to the analysis manager. | |
61 | */ | |
62 | public XmlAnalysisModuleSource() { | |
63 | TmfAnalysisManager.addNewModuleListener(new TmfXmlAnalysisOutputSource()); | |
64 | } | |
65 | ||
97ed0cf0 FW |
66 | @Override |
67 | public synchronized Iterable<IAnalysisModuleHelper> getAnalysisModules() { | |
68 | if (fModules == null) { | |
0281ea1c GB |
69 | fModules = new ArrayList<>(); |
70 | populateBuiltinModules(); | |
97ed0cf0 FW |
71 | populateAnalysisModules(); |
72 | } | |
73 | return fModules; | |
74 | } | |
75 | ||
0281ea1c GB |
76 | private static void processFile(File xmlFile) { |
77 | if (!XmlUtils.xmlValidate(xmlFile).isOK()) { | |
97ed0cf0 FW |
78 | return; |
79 | } | |
97ed0cf0 | 80 | |
0281ea1c GB |
81 | try { |
82 | /* Load the XML File */ | |
83 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
84 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
85 | Document doc = dBuilder.parse(xmlFile); | |
86 | doc.getDocumentElement().normalize(); | |
87 | ||
88 | /* get State Providers modules */ | |
89 | NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER); | |
90 | for (int i = 0; i < stateproviderNodes.getLength(); i++) { | |
91 | Element node = (Element) stateproviderNodes.item(i); | |
97ed0cf0 | 92 | |
0281ea1c GB |
93 | IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.STATE_SYSTEM); |
94 | fModules.add(helper); | |
95 | } | |
96 | } catch (ParserConfigurationException | SAXException | IOException e) { | |
97 | Activator.logError("Error opening XML file", e); //$NON-NLS-1$ | |
98 | } | |
99 | } | |
97ed0cf0 | 100 | |
0281ea1c GB |
101 | private static void populateBuiltinModules() { |
102 | /* Get the XML files advertised through the extension point */ | |
103 | IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_XML_BUILTIN_ID); | |
104 | for (IConfigurationElement element : elements) { | |
105 | if (element.getName().equals(XML_FILE_ELEMENT)) { | |
106 | String filename = element.getAttribute(XML_FILE_ATTRIB); | |
107 | String name = element.getContributor().getName(); | |
108 | if (name != null) { | |
109 | Bundle bundle = Platform.getBundle(name); | |
110 | if (bundle != null) { | |
111 | try { | |
112 | URL xmlUrl = bundle.getResource(filename); | |
113 | URL locatedURL = FileLocator.toFileURL(xmlUrl); | |
114 | processFile(new File(locatedURL.getFile())); | |
115 | } catch (IOException e) { | |
116 | /* ignore */ | |
117 | } | |
118 | } | |
97ed0cf0 | 119 | } |
97ed0cf0 FW |
120 | } |
121 | } | |
122 | } | |
123 | ||
0281ea1c GB |
124 | private static void populateAnalysisModules() { |
125 | IPath pathToFiles = XmlUtils.getXmlFilesPath(); | |
126 | File fFolder = pathToFiles.toFile(); | |
127 | if (!(fFolder.isDirectory() && fFolder.exists())) { | |
128 | return; | |
129 | } | |
130 | for (File xmlFile : fFolder.listFiles()) { | |
131 | processFile(xmlFile); | |
132 | } | |
133 | } | |
134 | ||
97ed0cf0 FW |
135 | /** |
136 | * Notifies the main XML analysis module that the executable modules list | |
137 | * may have changed and needs to be refreshed. | |
138 | */ | |
139 | public static void notifyModuleChange() { | |
140 | fModules = null; | |
141 | TmfAnalysisManager.refreshModules(); | |
142 | } | |
143 | ||
144 | } |