0e7cdff7a5ee103a06ccb45d2e2d52e8c493c32a
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / core / module / XmlAnalysisModuleSource.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others
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
10 package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module;
11
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.net.URL;
18 import java.nio.channels.FileChannel;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
22
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
26
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;
45
46 /**
47 * Analysis module source who creates helpers for the analysis modules described
48 * in the imported XML files
49 *
50 * @author Geneviève Bastien
51 * @since 2.0
52 */
53 public class XmlAnalysisModuleSource implements IAnalysisModuleSource {
54
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$
58
59 private static final String XML_FILE_ATTRIB = "file"; //$NON-NLS-1$
60
61 /*
62 * Legacy (Linux Tools) XML directory.
63 * TODO Remove once we feel the transition phase is over.
64 */
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$
69
70 private static List<@NonNull IAnalysisModuleHelper> fModules = null;
71
72 /**
73 * Constructor. It adds the new module listener to the analysis manager.
74 */
75 public XmlAnalysisModuleSource() {
76
77 }
78
79 @Override
80 public synchronized Iterable<IAnalysisModuleHelper> getAnalysisModules() {
81 List<@NonNull IAnalysisModuleHelper> modules = fModules;
82 if (modules == null) {
83 modules = new ArrayList<>();
84 fModules = modules;
85 populateBuiltinModules();
86 populateAnalysisModules();
87 }
88 return modules;
89 }
90
91 private static void processFile(File xmlFile) {
92 if (!XmlUtils.xmlValidate(xmlFile).isOK()) {
93 return;
94 }
95
96 try {
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();
102
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);
107
108 IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.STATE_SYSTEM);
109 fModules.add(helper);
110 }
111
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);
116
117 IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.PATTERN);
118 fModules.add(helper);
119 }
120 } catch (ParserConfigurationException | SAXException | IOException e) {
121 Activator.logError("Error opening XML file", e); //$NON-NLS-1$
122 }
123 }
124
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() {
137
138 @Override
139 public void run() throws IOException {
140 if (name != null) {
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);
146 }
147 URL locatedURL = FileLocator.toFileURL(xmlUrl);
148 processFile(new File(locatedURL.getFile()));
149 }
150 }
151 }
152
153 @Override
154 public void handleException(Throwable exception) {
155 // Handled sufficiently in SafeRunner
156 }
157 });
158 }
159 }
160 }
161
162 private static void populateAnalysisModules() {
163 IPath pathToFiles = XmlUtils.getXmlFilesPath();
164 File folder = pathToFiles.toFile();
165 if (!(folder.isDirectory() && folder.exists())) {
166 return;
167 }
168 /*
169 * Transfer files from Linux Tools directory.
170 */
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);
185 }
186 }
187 }
188 }
189 Map<String, File> files = XmlUtils.listFiles();
190 for (File xmlFile : files.values()) {
191 processFile(xmlFile);
192 }
193 }
194
195 /**
196 * Notifies the main XML analysis module that the executable modules list
197 * may have changed and needs to be refreshed.
198 */
199 public static void notifyModuleChange() {
200 fModules = null;
201 TmfAnalysisManager.refreshModules();
202 }
203
204 }
This page took 0.035236 seconds and 4 git commands to generate.