TMF: Add the XML analysis module source
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.ui / src / org / eclipse / linuxtools / tmf / analysis / xml / ui / module / XmlAnalysisModuleSource.java
CommitLineData
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
13package org.eclipse.linuxtools.tmf.analysis.xml.ui.module;
14
15import java.io.File;
16import java.io.IOException;
17import java.util.ArrayList;
18import java.util.List;
19
20import javax.xml.parsers.DocumentBuilder;
21import javax.xml.parsers.DocumentBuilderFactory;
22import javax.xml.parsers.ParserConfigurationException;
23
24import org.eclipse.core.runtime.IPath;
25import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.Activator;
26import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils;
27import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
28import org.eclipse.linuxtools.tmf.analysis.xml.ui.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType;
29import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
30import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleSource;
31import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
32import org.w3c.dom.Document;
33import org.w3c.dom.Element;
34import org.w3c.dom.NodeList;
35import 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 */
44public 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}
This page took 0.02795 seconds and 5 git commands to generate.