Fix some null warnings
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / tmf / analysis / xml / ui / module / XmlAnalysisModuleSource.java
CommitLineData
97ed0cf0 1/*******************************************************************************
2aebf5f3 2 * Copyright (c) 2014, 2015 É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
8 *
9 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
2aebf5f3 11 * Bernd Hufmann - Ensure backwards compatibility to Linux Tools
97ed0cf0
FW
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.tmf.analysis.xml.ui.module;
97ed0cf0
FW
15
16import java.io.File;
2aebf5f3 17import java.io.FileInputStream;
4884b393 18import java.io.FileNotFoundException;
2aebf5f3 19import java.io.FileOutputStream;
97ed0cf0 20import java.io.IOException;
0281ea1c 21import java.net.URL;
2aebf5f3 22import java.nio.channels.FileChannel;
97ed0cf0
FW
23import java.util.ArrayList;
24import java.util.List;
25
26import javax.xml.parsers.DocumentBuilder;
27import javax.xml.parsers.DocumentBuilderFactory;
28import javax.xml.parsers.ParserConfigurationException;
29
0281ea1c
GB
30import org.eclipse.core.runtime.FileLocator;
31import org.eclipse.core.runtime.IConfigurationElement;
97ed0cf0 32import org.eclipse.core.runtime.IPath;
4884b393 33import org.eclipse.core.runtime.ISafeRunnable;
0281ea1c 34import org.eclipse.core.runtime.Platform;
4884b393 35import org.eclipse.core.runtime.SafeRunner;
aa353506 36import org.eclipse.jdt.annotation.NonNull;
2bdf0193 37import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator;
2aebf5f3 38import org.eclipse.tracecompass.tmf.analysis.xml.core.module.Messages;
2bdf0193
AM
39import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
40import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
41import org.eclipse.tracecompass.tmf.analysis.xml.ui.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType;
42import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
43import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleSource;
44import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
0281ea1c 45import org.osgi.framework.Bundle;
97ed0cf0
FW
46import org.w3c.dom.Document;
47import org.w3c.dom.Element;
48import org.w3c.dom.NodeList;
49import org.xml.sax.SAXException;
50
51/**
52 * Analysis module source who creates helpers for the analysis modules described
53 * in the imported XML files
54 *
55 * @author Geneviève Bastien
97ed0cf0
FW
56 */
57public class XmlAnalysisModuleSource implements IAnalysisModuleSource {
58
0281ea1c
GB
59 /** Extension point ID */
60 private static final String TMF_XML_BUILTIN_ID = "org.eclipse.linuxtools.tmf.analysis.xml.core.files"; //$NON-NLS-1$
61 private static final String XML_FILE_ELEMENT = "xmlfile"; //$NON-NLS-1$
62
63 private static final String XML_FILE_ATTRIB = "file"; //$NON-NLS-1$
64
2aebf5f3
BH
65 /*
66 * Legacy (Linux Tools) XML directory.
67 * TODO Remove once we feel the transition phase is over.
68 */
69 private static final IPath XML_DIRECTORY_LEGACY =
70 Activator.getDefault().getStateLocation().removeLastSegments(1)
71 .append("org.eclipse.linuxtools.tmf.analysis.xml.core") //$NON-NLS-1$
72 .append("xml_files"); //$NON-NLS-1$
73
aa353506 74 private static List<@NonNull IAnalysisModuleHelper> fModules = null;
97ed0cf0 75
048bde85
GB
76 /**
77 * Constructor. It adds the new module listener to the analysis manager.
78 */
79 public XmlAnalysisModuleSource() {
80 TmfAnalysisManager.addNewModuleListener(new TmfXmlAnalysisOutputSource());
81 }
82
97ed0cf0
FW
83 @Override
84 public synchronized Iterable<IAnalysisModuleHelper> getAnalysisModules() {
aa353506
AM
85 List<@NonNull IAnalysisModuleHelper> modules = fModules;
86 if (modules == null) {
87 modules = new ArrayList<>();
88 fModules = modules;
0281ea1c 89 populateBuiltinModules();
97ed0cf0
FW
90 populateAnalysisModules();
91 }
aa353506 92 return modules;
97ed0cf0
FW
93 }
94
0281ea1c
GB
95 private static void processFile(File xmlFile) {
96 if (!XmlUtils.xmlValidate(xmlFile).isOK()) {
97ed0cf0
FW
97 return;
98 }
97ed0cf0 99
0281ea1c
GB
100 try {
101 /* Load the XML File */
102 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
103 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
104 Document doc = dBuilder.parse(xmlFile);
105 doc.getDocumentElement().normalize();
106
107 /* get State Providers modules */
108 NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
109 for (int i = 0; i < stateproviderNodes.getLength(); i++) {
110 Element node = (Element) stateproviderNodes.item(i);
97ed0cf0 111
0281ea1c
GB
112 IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.STATE_SYSTEM);
113 fModules.add(helper);
114 }
115 } catch (ParserConfigurationException | SAXException | IOException e) {
116 Activator.logError("Error opening XML file", e); //$NON-NLS-1$
117 }
118 }
97ed0cf0 119
0281ea1c
GB
120 private static void populateBuiltinModules() {
121 /* Get the XML files advertised through the extension point */
122 IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_XML_BUILTIN_ID);
123 for (IConfigurationElement element : elements) {
124 if (element.getName().equals(XML_FILE_ELEMENT)) {
4884b393
MAL
125 final String filename = element.getAttribute(XML_FILE_ATTRIB);
126 final String name = element.getContributor().getName();
127 // Run this in a safe runner in case there is an exception
128 // (IOException, FileNotFoundException, NPE, etc).
129 // This makes sure other extensions are not prevented from
130 // working if one is faulty.
131 SafeRunner.run(new ISafeRunnable() {
132
133 @Override
134 public void run() throws Exception {
135 if (name != null) {
136 Bundle bundle = Platform.getBundle(name);
137 if (bundle != null) {
138 URL xmlUrl = bundle.getResource(filename);
139 if (xmlUrl == null) {
140 throw new FileNotFoundException(filename);
141 }
142 URL locatedURL = FileLocator.toFileURL(xmlUrl);
143 processFile(new File(locatedURL.getFile()));
144 }
0281ea1c
GB
145 }
146 }
4884b393
MAL
147
148 @Override
149 public void handleException(Throwable exception) {
150 // Handled sufficiently in SafeRunner
151 }
152 });
97ed0cf0
FW
153 }
154 }
155 }
156
0281ea1c
GB
157 private static void populateAnalysisModules() {
158 IPath pathToFiles = XmlUtils.getXmlFilesPath();
159 File fFolder = pathToFiles.toFile();
160 if (!(fFolder.isDirectory() && fFolder.exists())) {
161 return;
162 }
2aebf5f3
BH
163
164 /*
165 * Transfer files from Linux Tools directory.
166 */
167 File fOldFolder = XML_DIRECTORY_LEGACY.toFile();
168 if ((fOldFolder.isDirectory() && fOldFolder.exists())) {
169 for (File fromFile : fOldFolder.listFiles()) {
170 File toFile = pathToFiles.append(fromFile.getName()).toFile();
171 if (!toFile.exists() && !fromFile.isDirectory()) {
172 try (FileInputStream fis = new FileInputStream(fromFile);
173 FileOutputStream fos = new FileOutputStream(toFile);
174 FileChannel source = fis.getChannel();
175 FileChannel destination = fos.getChannel();) {
176 destination.transferFrom(source, 0, source.size());
177 } catch (IOException e) {
178 String error = Messages.XmlUtils_ErrorCopyingFile;
179 Activator.logError(error, e);
180 }
181 }
182 }
183 }
184
0281ea1c
GB
185 for (File xmlFile : fFolder.listFiles()) {
186 processFile(xmlFile);
187 }
188 }
189
97ed0cf0
FW
190 /**
191 * Notifies the main XML analysis module that the executable modules list
192 * may have changed and needs to be refreshed.
193 */
194 public static void notifyModuleChange() {
195 fModules = null;
196 TmfAnalysisManager.refreshModules();
197 }
198
199}
This page took 0.07201 seconds and 5 git commands to generate.