xml: bug 497272 Populate views from built-in XML files
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / core / module / XmlAnalysisModuleSource.java
CommitLineData
97ed0cf0 1/*******************************************************************************
0dcf01f1 2 * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others
97ed0cf0
FW
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
97ed0cf0
FW
8 *******************************************************************************/
9
6eca054d 10package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module;
97ed0cf0
FW
11
12import java.io.File;
2aebf5f3 13import java.io.FileInputStream;
2aebf5f3 14import java.io.FileOutputStream;
97ed0cf0 15import java.io.IOException;
2aebf5f3 16import java.nio.channels.FileChannel;
97ed0cf0
FW
17import java.util.ArrayList;
18import java.util.List;
537572cd 19import java.util.Map;
97ed0cf0 20
97ed0cf0
FW
21import javax.xml.parsers.ParserConfigurationException;
22
23import org.eclipse.core.runtime.IPath;
aa353506 24import org.eclipse.jdt.annotation.NonNull;
0dcf01f1 25import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
6eca054d
GB
26import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType;
27import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
2bdf0193
AM
28import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
29import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleSource;
30import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
97ed0cf0
FW
31import org.w3c.dom.Document;
32import org.w3c.dom.Element;
33import org.w3c.dom.NodeList;
34import org.xml.sax.SAXException;
35
36/**
37 * Analysis module source who creates helpers for the analysis modules described
38 * in the imported XML files
39 *
40 * @author Geneviève Bastien
0dcf01f1 41 * @since 2.0
97ed0cf0
FW
42 */
43public class XmlAnalysisModuleSource implements IAnalysisModuleSource {
44
0281ea1c 45
2aebf5f3
BH
46 /*
47 * Legacy (Linux Tools) XML directory.
48 * TODO Remove once we feel the transition phase is over.
49 */
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$
54
aa353506 55 private static List<@NonNull IAnalysisModuleHelper> fModules = null;
97ed0cf0 56
048bde85
GB
57 /**
58 * Constructor. It adds the new module listener to the analysis manager.
59 */
60 public XmlAnalysisModuleSource() {
03b8cd0e 61
048bde85
GB
62 }
63
97ed0cf0
FW
64 @Override
65 public synchronized Iterable<IAnalysisModuleHelper> getAnalysisModules() {
aa353506
AM
66 List<@NonNull IAnalysisModuleHelper> modules = fModules;
67 if (modules == null) {
68 modules = new ArrayList<>();
69 fModules = modules;
0281ea1c 70 populateBuiltinModules();
97ed0cf0
FW
71 populateAnalysisModules();
72 }
aa353506 73 return modules;
97ed0cf0
FW
74 }
75
0281ea1c
GB
76 private static void processFile(File xmlFile) {
77 if (!XmlUtils.xmlValidate(xmlFile).isOK()) {
97ed0cf0
FW
78 return;
79 }
97ed0cf0 80
0281ea1c 81 try {
f10786aa 82 Document doc = XmlUtils.getDocumentFromFile(xmlFile);
0281ea1c
GB
83
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);
97ed0cf0 88
0281ea1c
GB
89 IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.STATE_SYSTEM);
90 fModules.add(helper);
91 }
38e2a2e9
JCK
92
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);
97
98 IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.PATTERN);
99 fModules.add(helper);
100 }
0281ea1c
GB
101 } catch (ParserConfigurationException | SAXException | IOException e) {
102 Activator.logError("Error opening XML file", e); //$NON-NLS-1$
103 }
104 }
97ed0cf0 105
0281ea1c 106 private static void populateBuiltinModules() {
d2452723
GB
107 Map<String, IPath> files = XmlUtils.listBuiltinFiles();
108 for (IPath xmlPath : files.values()) {
109 processFile(xmlPath.toFile());
97ed0cf0
FW
110 }
111 }
112
0281ea1c
GB
113 private static void populateAnalysisModules() {
114 IPath pathToFiles = XmlUtils.getXmlFilesPath();
bc21b431
MK
115 File folder = pathToFiles.toFile();
116 if (!(folder.isDirectory() && folder.exists())) {
0281ea1c
GB
117 return;
118 }
2aebf5f3
BH
119 /*
120 * Transfer files from Linux Tools directory.
121 */
bc21b431
MK
122 File oldFolder = XML_DIRECTORY_LEGACY.toFile();
123 final File[] oldAnalysisFiles = oldFolder.listFiles();
124 if (oldAnalysisFiles != null) {
125 for (File fromFile : oldAnalysisFiles) {
2aebf5f3
BH
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);
136 }
137 }
138 }
139 }
537572cd
BH
140 Map<String, File> files = XmlUtils.listFiles();
141 for (File xmlFile : files.values()) {
142 processFile(xmlFile);
0281ea1c
GB
143 }
144 }
145
97ed0cf0
FW
146 /**
147 * Notifies the main XML analysis module that the executable modules list
148 * may have changed and needs to be refreshed.
149 */
150 public static void notifyModuleChange() {
151 fModules = null;
152 TmfAnalysisManager.refreshModules();
153 }
154
155}
This page took 0.095085 seconds and 5 git commands to generate.