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 | ||
13 | package org.eclipse.linuxtools.tmf.analysis.xml.ui.module; | |
14 | ||
15 | import java.io.File; | |
16 | import java.io.IOException; | |
17 | import java.util.ArrayList; | |
18 | import java.util.List; | |
19 | ||
20 | import javax.xml.parsers.DocumentBuilder; | |
21 | import javax.xml.parsers.DocumentBuilderFactory; | |
22 | import javax.xml.parsers.ParserConfigurationException; | |
23 | ||
24 | import org.eclipse.core.runtime.IPath; | |
25 | import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.Activator; | |
26 | import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils; | |
27 | import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
28 | import org.eclipse.linuxtools.tmf.analysis.xml.ui.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType; | |
29 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper; | |
30 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleSource; | |
31 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager; | |
32 | import org.w3c.dom.Document; | |
33 | import org.w3c.dom.Element; | |
34 | import org.w3c.dom.NodeList; | |
35 | import org.xml.sax.SAXException; | |
36 | ||
37 | /** | |
38 | * Analysis module source who creates helpers for the analysis modules described | |
39 | * in the imported XML files | |
40 | * | |
41 | * @author Geneviève Bastien | |
42 | * @since 3.0 | |
43 | */ | |
44 | public class XmlAnalysisModuleSource implements IAnalysisModuleSource { | |
45 | ||
46 | private static List<IAnalysisModuleHelper> fModules = null; | |
47 | ||
48 | @Override | |
49 | public synchronized Iterable<IAnalysisModuleHelper> getAnalysisModules() { | |
50 | if (fModules == null) { | |
51 | populateAnalysisModules(); | |
52 | } | |
53 | return fModules; | |
54 | } | |
55 | ||
56 | private static void populateAnalysisModules() { | |
57 | fModules = new ArrayList<>(); | |
58 | IPath pathToFiles = XmlUtils.getXmlFilesPath(); | |
59 | File fFolder = pathToFiles.toFile(); | |
60 | if (!(fFolder.isDirectory() && fFolder.exists())) { | |
61 | return; | |
62 | } | |
63 | for (File xmlFile : fFolder.listFiles()) { | |
64 | if (!XmlUtils.xmlValidate(xmlFile).isOK()) { | |
65 | continue; | |
66 | } | |
67 | ||
68 | try { | |
69 | /* Load the XML File */ | |
70 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
71 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
72 | Document doc = dBuilder.parse(xmlFile); | |
73 | doc.getDocumentElement().normalize(); | |
74 | ||
75 | /* get State Providers modules */ | |
76 | NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER); | |
77 | for (int i = 0; i < stateproviderNodes.getLength(); i++) { | |
78 | Element node = (Element) stateproviderNodes.item(i); | |
79 | ||
80 | IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.STATE_SYSTEM); | |
81 | fModules.add(helper); | |
82 | } | |
83 | } catch (ParserConfigurationException | SAXException | IOException e) { | |
84 | Activator.logError("Error opening XML file", e); //$NON-NLS-1$ | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
89 | /** | |
90 | * Notifies the main XML analysis module that the executable modules list | |
91 | * may have changed and needs to be refreshed. | |
92 | */ | |
93 | public static void notifyModuleChange() { | |
94 | fModules = null; | |
95 | TmfAnalysisManager.refreshModules(); | |
96 | } | |
97 | ||
98 | } |