TMF XML: Add support of the XML file extension point to the modules source
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.ui / src / org / eclipse / linuxtools / tmf / analysis / xml / ui / module / XmlAnalysisModuleSource.java
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
13 package org.eclipse.linuxtools.tmf.analysis.xml.ui.module;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.net.URL;
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
25 import org.eclipse.core.runtime.FileLocator;
26 import org.eclipse.core.runtime.IConfigurationElement;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.Platform;
29 import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.Activator;
30 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils;
31 import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
32 import org.eclipse.linuxtools.tmf.analysis.xml.ui.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType;
33 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
34 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleSource;
35 import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
36 import org.osgi.framework.Bundle;
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
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
57 private static List<IAnalysisModuleHelper> fModules = null;
58
59 @Override
60 public synchronized Iterable<IAnalysisModuleHelper> getAnalysisModules() {
61 if (fModules == null) {
62 fModules = new ArrayList<>();
63 populateBuiltinModules();
64 populateAnalysisModules();
65 }
66 return fModules;
67 }
68
69 private static void processFile(File xmlFile) {
70 if (!XmlUtils.xmlValidate(xmlFile).isOK()) {
71 return;
72 }
73
74 try {
75 /* Load the XML File */
76 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
77 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
78 Document doc = dBuilder.parse(xmlFile);
79 doc.getDocumentElement().normalize();
80
81 /* get State Providers modules */
82 NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
83 for (int i = 0; i < stateproviderNodes.getLength(); i++) {
84 Element node = (Element) stateproviderNodes.item(i);
85
86 IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.STATE_SYSTEM);
87 fModules.add(helper);
88 }
89 } catch (ParserConfigurationException | SAXException | IOException e) {
90 Activator.logError("Error opening XML file", e); //$NON-NLS-1$
91 }
92 }
93
94 private static void populateBuiltinModules() {
95 /* Get the XML files advertised through the extension point */
96 IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_XML_BUILTIN_ID);
97 for (IConfigurationElement element : elements) {
98 if (element.getName().equals(XML_FILE_ELEMENT)) {
99 String filename = element.getAttribute(XML_FILE_ATTRIB);
100 String name = element.getContributor().getName();
101 if (name != null) {
102 Bundle bundle = Platform.getBundle(name);
103 if (bundle != null) {
104 try {
105 URL xmlUrl = bundle.getResource(filename);
106 URL locatedURL = FileLocator.toFileURL(xmlUrl);
107 processFile(new File(locatedURL.getFile()));
108 } catch (IOException e) {
109 /* ignore */
110 }
111 }
112 }
113 }
114 }
115 }
116
117 private static void populateAnalysisModules() {
118 IPath pathToFiles = XmlUtils.getXmlFilesPath();
119 File fFolder = pathToFiles.toFile();
120 if (!(fFolder.isDirectory() && fFolder.exists())) {
121 return;
122 }
123 for (File xmlFile : fFolder.listFiles()) {
124 processFile(xmlFile);
125 }
126 }
127
128 /**
129 * Notifies the main XML analysis module that the executable modules list
130 * may have changed and needs to be refreshed.
131 */
132 public static void notifyModuleChange() {
133 fModules = null;
134 TmfAnalysisManager.refreshModules();
135 }
136
137 }
This page took 0.032433 seconds and 5 git commands to generate.