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
.FileOutputStream
;
15 import java
.io
.IOException
;
16 import java
.nio
.channels
.FileChannel
;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
21 import javax
.xml
.parsers
.ParserConfigurationException
;
23 import org
.eclipse
.core
.runtime
.IPath
;
24 import org
.eclipse
.jdt
.annotation
.NonNull
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.Activator
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.TmfAnalysisModuleHelperXml
.XmlAnalysisModuleType
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleSource
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisManager
;
31 import org
.w3c
.dom
.Document
;
32 import org
.w3c
.dom
.Element
;
33 import org
.w3c
.dom
.NodeList
;
34 import org
.xml
.sax
.SAXException
;
37 * Analysis module source who creates helpers for the analysis modules described
38 * in the imported XML files
40 * @author Geneviève Bastien
43 public class XmlAnalysisModuleSource
implements IAnalysisModuleSource
{
47 * Legacy (Linux Tools) XML directory.
48 * TODO Remove once we feel the transition phase is over.
50 private static final IPath XML_DIRECTORY_LEGACY
=
51 Activator
.getDefault().getStateLocation().removeLastSegments(1)
52 .append("org.eclipse.linuxtools.tmf.analysis.xml.core") //$NON-NLS-1$
53 .append("xml_files"); //$NON-NLS-1$
55 private static List
<@NonNull IAnalysisModuleHelper
> fModules
= null;
58 * Constructor. It adds the new module listener to the analysis manager.
60 public XmlAnalysisModuleSource() {
65 public synchronized Iterable
<IAnalysisModuleHelper
> getAnalysisModules() {
66 List
<@NonNull IAnalysisModuleHelper
> modules
= fModules
;
67 if (modules
== null) {
68 modules
= new ArrayList
<>();
70 populateBuiltinModules();
71 populateAnalysisModules();
76 private static void processFile(File xmlFile
) {
77 if (!XmlUtils
.xmlValidate(xmlFile
).isOK()) {
82 Document doc
= XmlUtils
.getDocumentFromFile(xmlFile
);
84 /* get State Providers modules */
85 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
86 for (int i
= 0; i
< stateproviderNodes
.getLength(); i
++) {
87 Element node
= (Element
) stateproviderNodes
.item(i
);
89 IAnalysisModuleHelper helper
= new TmfAnalysisModuleHelperXml(xmlFile
, node
, XmlAnalysisModuleType
.STATE_SYSTEM
);
93 /* get pattern modules */
94 NodeList patternNodes
= doc
.getElementsByTagName(TmfXmlStrings
.PATTERN
);
95 for (int i
= 0; i
< patternNodes
.getLength(); i
++) {
96 Element node
= (Element
) patternNodes
.item(i
);
98 IAnalysisModuleHelper helper
= new TmfAnalysisModuleHelperXml(xmlFile
, node
, XmlAnalysisModuleType
.PATTERN
);
101 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
102 Activator
.logError("Error opening XML file", e
); //$NON-NLS-1$
106 private static void populateBuiltinModules() {
107 Map
<String
, IPath
> files
= XmlUtils
.listBuiltinFiles();
108 for (IPath xmlPath
: files
.values()) {
109 processFile(xmlPath
.toFile());
113 private static void populateAnalysisModules() {
114 IPath pathToFiles
= XmlUtils
.getXmlFilesPath();
115 File folder
= pathToFiles
.toFile();
116 if (!(folder
.isDirectory() && folder
.exists())) {
120 * Transfer files from Linux Tools directory.
122 File oldFolder
= XML_DIRECTORY_LEGACY
.toFile();
123 final File
[] oldAnalysisFiles
= oldFolder
.listFiles();
124 if (oldAnalysisFiles
!= null) {
125 for (File fromFile
: oldAnalysisFiles
) {
126 File toFile
= pathToFiles
.append(fromFile
.getName()).toFile();
127 if (!toFile
.exists() && !fromFile
.isDirectory()) {
128 try (FileInputStream fis
= new FileInputStream(fromFile
);
129 FileOutputStream fos
= new FileOutputStream(toFile
);
130 FileChannel source
= fis
.getChannel();
131 FileChannel destination
= fos
.getChannel();) {
132 destination
.transferFrom(source
, 0, source
.size());
133 } catch (IOException e
) {
134 String error
= Messages
.XmlUtils_ErrorCopyingFile
;
135 Activator
.logError(error
, e
);
140 Map
<String
, File
> files
= XmlUtils
.listFiles();
141 for (File xmlFile
: files
.values()) {
142 processFile(xmlFile
);
147 * Notifies the main XML analysis module that the executable modules list
148 * may have changed and needs to be refreshed.
150 public static void notifyModuleChange() {
152 TmfAnalysisManager
.refreshModules();