1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.ui
.module
;
16 import java
.io
.IOException
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import javax
.xml
.parsers
.DocumentBuilder
;
22 import javax
.xml
.parsers
.DocumentBuilderFactory
;
23 import javax
.xml
.parsers
.ParserConfigurationException
;
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
;
43 * Analysis module source who creates helpers for the analysis modules described
44 * in the imported XML files
46 * @author Geneviève Bastien
49 public class XmlAnalysisModuleSource
implements IAnalysisModuleSource
{
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$
55 private static final String XML_FILE_ATTRIB
= "file"; //$NON-NLS-1$
57 private static List
<IAnalysisModuleHelper
> fModules
= null;
60 * Constructor. It adds the new module listener to the analysis manager.
62 public XmlAnalysisModuleSource() {
63 TmfAnalysisManager
.addNewModuleListener(new TmfXmlAnalysisOutputSource());
67 public synchronized Iterable
<IAnalysisModuleHelper
> getAnalysisModules() {
68 if (fModules
== null) {
69 fModules
= new ArrayList
<>();
70 populateBuiltinModules();
71 populateAnalysisModules();
76 private static void processFile(File xmlFile
) {
77 if (!XmlUtils
.xmlValidate(xmlFile
).isOK()) {
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();
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
);
93 IAnalysisModuleHelper helper
= new TmfAnalysisModuleHelperXml(xmlFile
, node
, XmlAnalysisModuleType
.STATE_SYSTEM
);
96 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
97 Activator
.logError("Error opening XML file", e
); //$NON-NLS-1$
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();
109 Bundle bundle
= Platform
.getBundle(name
);
110 if (bundle
!= null) {
112 URL xmlUrl
= bundle
.getResource(filename
);
113 URL locatedURL
= FileLocator
.toFileURL(xmlUrl
);
114 processFile(new File(locatedURL
.getFile()));
115 } catch (IOException e
) {
124 private static void populateAnalysisModules() {
125 IPath pathToFiles
= XmlUtils
.getXmlFilesPath();
126 File fFolder
= pathToFiles
.toFile();
127 if (!(fFolder
.isDirectory() && fFolder
.exists())) {
130 for (File xmlFile
: fFolder
.listFiles()) {
131 processFile(xmlFile
);
136 * Notifies the main XML analysis module that the executable modules list
137 * may have changed and needs to be refreshed.
139 public static void notifyModuleChange() {
141 TmfAnalysisManager
.refreshModules();