Commit | Line | Data |
---|---|---|
97ed0cf0 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal |
97ed0cf0 FW |
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; | |
4884b393 | 16 | import java.io.FileNotFoundException; |
97ed0cf0 | 17 | import java.io.IOException; |
0281ea1c | 18 | import java.net.URL; |
97ed0cf0 FW |
19 | import java.util.ArrayList; |
20 | import java.util.List; | |
21 | ||
22 | import javax.xml.parsers.DocumentBuilder; | |
23 | import javax.xml.parsers.DocumentBuilderFactory; | |
24 | import javax.xml.parsers.ParserConfigurationException; | |
25 | ||
0281ea1c GB |
26 | import org.eclipse.core.runtime.FileLocator; |
27 | import org.eclipse.core.runtime.IConfigurationElement; | |
97ed0cf0 | 28 | import org.eclipse.core.runtime.IPath; |
4884b393 | 29 | import org.eclipse.core.runtime.ISafeRunnable; |
0281ea1c | 30 | import org.eclipse.core.runtime.Platform; |
4884b393 | 31 | import org.eclipse.core.runtime.SafeRunner; |
2bdf0193 AM |
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; | |
0281ea1c | 39 | import org.osgi.framework.Bundle; |
97ed0cf0 FW |
40 | import org.w3c.dom.Document; |
41 | import org.w3c.dom.Element; | |
42 | import org.w3c.dom.NodeList; | |
43 | import org.xml.sax.SAXException; | |
44 | ||
45 | /** | |
46 | * Analysis module source who creates helpers for the analysis modules described | |
47 | * in the imported XML files | |
48 | * | |
49 | * @author Geneviève Bastien | |
50 | * @since 3.0 | |
51 | */ | |
52 | public class XmlAnalysisModuleSource implements IAnalysisModuleSource { | |
53 | ||
0281ea1c GB |
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$ | |
57 | ||
58 | private static final String XML_FILE_ATTRIB = "file"; //$NON-NLS-1$ | |
59 | ||
97ed0cf0 FW |
60 | private static List<IAnalysisModuleHelper> fModules = null; |
61 | ||
048bde85 GB |
62 | /** |
63 | * Constructor. It adds the new module listener to the analysis manager. | |
64 | */ | |
65 | public XmlAnalysisModuleSource() { | |
66 | TmfAnalysisManager.addNewModuleListener(new TmfXmlAnalysisOutputSource()); | |
67 | } | |
68 | ||
97ed0cf0 FW |
69 | @Override |
70 | public synchronized Iterable<IAnalysisModuleHelper> getAnalysisModules() { | |
71 | if (fModules == null) { | |
0281ea1c GB |
72 | fModules = new ArrayList<>(); |
73 | populateBuiltinModules(); | |
97ed0cf0 FW |
74 | populateAnalysisModules(); |
75 | } | |
76 | return fModules; | |
77 | } | |
78 | ||
0281ea1c GB |
79 | private static void processFile(File xmlFile) { |
80 | if (!XmlUtils.xmlValidate(xmlFile).isOK()) { | |
97ed0cf0 FW |
81 | return; |
82 | } | |
97ed0cf0 | 83 | |
0281ea1c GB |
84 | try { |
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(); | |
90 | ||
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); | |
97ed0cf0 | 95 | |
0281ea1c GB |
96 | IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.STATE_SYSTEM); |
97 | fModules.add(helper); | |
98 | } | |
99 | } catch (ParserConfigurationException | SAXException | IOException e) { | |
100 | Activator.logError("Error opening XML file", e); //$NON-NLS-1$ | |
101 | } | |
102 | } | |
97ed0cf0 | 103 | |
0281ea1c GB |
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)) { | |
4884b393 MAL |
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() { | |
116 | ||
117 | @Override | |
118 | public void run() throws Exception { | |
119 | if (name != null) { | |
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); | |
125 | } | |
126 | URL locatedURL = FileLocator.toFileURL(xmlUrl); | |
127 | processFile(new File(locatedURL.getFile())); | |
128 | } | |
0281ea1c GB |
129 | } |
130 | } | |
4884b393 MAL |
131 | |
132 | @Override | |
133 | public void handleException(Throwable exception) { | |
134 | // Handled sufficiently in SafeRunner | |
135 | } | |
136 | }); | |
97ed0cf0 FW |
137 | } |
138 | } | |
139 | } | |
140 | ||
0281ea1c GB |
141 | private static void populateAnalysisModules() { |
142 | IPath pathToFiles = XmlUtils.getXmlFilesPath(); | |
143 | File fFolder = pathToFiles.toFile(); | |
144 | if (!(fFolder.isDirectory() && fFolder.exists())) { | |
145 | return; | |
146 | } | |
147 | for (File xmlFile : fFolder.listFiles()) { | |
148 | processFile(xmlFile); | |
149 | } | |
150 | } | |
151 | ||
97ed0cf0 FW |
152 | /** |
153 | * Notifies the main XML analysis module that the executable modules list | |
154 | * may have changed and needs to be refreshed. | |
155 | */ | |
156 | public static void notifyModuleChange() { | |
157 | fModules = null; | |
158 | TmfAnalysisManager.refreshModules(); | |
159 | } | |
160 | ||
161 | } |