1 /*******************************************************************************
2 * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
;
13 import java
.io
.FileInputStream
;
14 import java
.io
.FileNotFoundException
;
15 import java
.io
.FileOutputStream
;
16 import java
.io
.IOException
;
18 import java
.nio
.channels
.FileChannel
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
23 import javax
.xml
.parsers
.DocumentBuilder
;
24 import javax
.xml
.parsers
.DocumentBuilderFactory
;
25 import javax
.xml
.parsers
.ParserConfigurationException
;
27 import org
.eclipse
.core
.runtime
.FileLocator
;
28 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
29 import org
.eclipse
.core
.runtime
.IPath
;
30 import org
.eclipse
.core
.runtime
.ISafeRunnable
;
31 import org
.eclipse
.core
.runtime
.Platform
;
32 import org
.eclipse
.core
.runtime
.SafeRunner
;
33 import org
.eclipse
.jdt
.annotation
.NonNull
;
34 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.Activator
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.TmfAnalysisModuleHelperXml
.XmlAnalysisModuleType
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleSource
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisManager
;
40 import org
.osgi
.framework
.Bundle
;
41 import org
.w3c
.dom
.Document
;
42 import org
.w3c
.dom
.Element
;
43 import org
.w3c
.dom
.NodeList
;
44 import org
.xml
.sax
.SAXException
;
47 * Analysis module source who creates helpers for the analysis modules described
48 * in the imported XML files
50 * @author Geneviève Bastien
53 public class XmlAnalysisModuleSource
implements IAnalysisModuleSource
{
55 /** Extension point ID */
56 private static final String TMF_XML_BUILTIN_ID
= "org.eclipse.linuxtools.tmf.analysis.xml.core.files"; //$NON-NLS-1$
57 private static final String XML_FILE_ELEMENT
= "xmlfile"; //$NON-NLS-1$
59 private static final String XML_FILE_ATTRIB
= "file"; //$NON-NLS-1$
62 * Legacy (Linux Tools) XML directory.
63 * TODO Remove once we feel the transition phase is over.
65 private static final IPath XML_DIRECTORY_LEGACY
=
66 Activator
.getDefault().getStateLocation().removeLastSegments(1)
67 .append("org.eclipse.linuxtools.tmf.analysis.xml.core") //$NON-NLS-1$
68 .append("xml_files"); //$NON-NLS-1$
70 private static List
<@NonNull IAnalysisModuleHelper
> fModules
= null;
73 * Constructor. It adds the new module listener to the analysis manager.
75 public XmlAnalysisModuleSource() {
80 public synchronized Iterable
<IAnalysisModuleHelper
> getAnalysisModules() {
81 List
<@NonNull IAnalysisModuleHelper
> modules
= fModules
;
82 if (modules
== null) {
83 modules
= new ArrayList
<>();
85 populateBuiltinModules();
86 populateAnalysisModules();
91 private static void processFile(File xmlFile
) {
92 if (!XmlUtils
.xmlValidate(xmlFile
).isOK()) {
97 /* Load the XML File */
98 DocumentBuilderFactory dbFactory
= DocumentBuilderFactory
.newInstance();
99 DocumentBuilder dBuilder
= dbFactory
.newDocumentBuilder();
100 Document doc
= dBuilder
.parse(xmlFile
);
101 doc
.getDocumentElement().normalize();
103 /* get State Providers modules */
104 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
105 for (int i
= 0; i
< stateproviderNodes
.getLength(); i
++) {
106 Element node
= (Element
) stateproviderNodes
.item(i
);
108 IAnalysisModuleHelper helper
= new TmfAnalysisModuleHelperXml(xmlFile
, node
, XmlAnalysisModuleType
.STATE_SYSTEM
);
109 fModules
.add(helper
);
112 /* get pattern modules */
113 NodeList patternNodes
= doc
.getElementsByTagName(TmfXmlStrings
.PATTERN
);
114 for (int i
= 0; i
< patternNodes
.getLength(); i
++) {
115 Element node
= (Element
) patternNodes
.item(i
);
117 IAnalysisModuleHelper helper
= new TmfAnalysisModuleHelperXml(xmlFile
, node
, XmlAnalysisModuleType
.PATTERN
);
118 fModules
.add(helper
);
120 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
121 Activator
.logError("Error opening XML file", e
); //$NON-NLS-1$
125 private static void populateBuiltinModules() {
126 /* Get the XML files advertised through the extension point */
127 IConfigurationElement
[] elements
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TMF_XML_BUILTIN_ID
);
128 for (IConfigurationElement element
: elements
) {
129 if (element
.getName().equals(XML_FILE_ELEMENT
)) {
130 final String filename
= element
.getAttribute(XML_FILE_ATTRIB
);
131 final String name
= element
.getContributor().getName();
132 // Run this in a safe runner in case there is an exception
133 // (IOException, FileNotFoundException, NPE, etc).
134 // This makes sure other extensions are not prevented from
135 // working if one is faulty.
136 SafeRunner
.run(new ISafeRunnable() {
139 public void run() throws IOException
{
141 Bundle bundle
= Platform
.getBundle(name
);
142 if (bundle
!= null) {
143 URL xmlUrl
= bundle
.getResource(filename
);
144 if (xmlUrl
== null) {
145 throw new FileNotFoundException(filename
);
147 URL locatedURL
= FileLocator
.toFileURL(xmlUrl
);
148 processFile(new File(locatedURL
.getFile()));
154 public void handleException(Throwable exception
) {
155 // Handled sufficiently in SafeRunner
162 private static void populateAnalysisModules() {
163 IPath pathToFiles
= XmlUtils
.getXmlFilesPath();
164 File folder
= pathToFiles
.toFile();
165 if (!(folder
.isDirectory() && folder
.exists())) {
169 * Transfer files from Linux Tools directory.
171 File oldFolder
= XML_DIRECTORY_LEGACY
.toFile();
172 final File
[] oldAnalysisFiles
= oldFolder
.listFiles();
173 if (oldAnalysisFiles
!= null) {
174 for (File fromFile
: oldAnalysisFiles
) {
175 File toFile
= pathToFiles
.append(fromFile
.getName()).toFile();
176 if (!toFile
.exists() && !fromFile
.isDirectory()) {
177 try (FileInputStream fis
= new FileInputStream(fromFile
);
178 FileOutputStream fos
= new FileOutputStream(toFile
);
179 FileChannel source
= fis
.getChannel();
180 FileChannel destination
= fos
.getChannel();) {
181 destination
.transferFrom(source
, 0, source
.size());
182 } catch (IOException e
) {
183 String error
= Messages
.XmlUtils_ErrorCopyingFile
;
184 Activator
.logError(error
, e
);
189 Map
<String
, File
> files
= XmlUtils
.listFiles();
190 for (File xmlFile
: files
.values()) {
191 processFile(xmlFile
);
196 * Notifies the main XML analysis module that the executable modules list
197 * may have changed and needs to be refreshed.
199 public static void notifyModuleChange() {
201 TmfAnalysisManager
.refreshModules();