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
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.FileOutputStream;
15 import java.io.IOException;
16 import java.nio.channels.FileChannel;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Map;
20
21 import javax.xml.parsers.ParserConfigurationException;
22
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;
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
41 * @since 2.0
42 */
43 public class XmlAnalysisModuleSource implements IAnalysisModuleSource {
44
45
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
55 private static List<@NonNull IAnalysisModuleHelper> fModules = null;
56
57 /**
58 * Constructor. It adds the new module listener to the analysis manager.
59 */
60 public XmlAnalysisModuleSource() {
61
62 }
63
64 @Override
65 public synchronized Iterable<IAnalysisModuleHelper> getAnalysisModules() {
66 List<@NonNull IAnalysisModuleHelper> modules = fModules;
67 if (modules == null) {
68 modules = new ArrayList<>();
69 fModules = modules;
70 populateBuiltinModules();
71 populateAnalysisModules();
72 }
73 return modules;
74 }
75
76 private static void processFile(File xmlFile) {
77 if (!XmlUtils.xmlValidate(xmlFile).isOK()) {
78 return;
79 }
80
81 try {
82 Document doc = XmlUtils.getDocumentFromFile(xmlFile);
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);
88
89 IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.STATE_SYSTEM);
90 fModules.add(helper);
91 }
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 }
101 } catch (ParserConfigurationException | SAXException | IOException e) {
102 Activator.logError("Error opening XML file", e); //$NON-NLS-1$
103 }
104 }
105
106 private static void populateBuiltinModules() {
107 Map<String, IPath> files = XmlUtils.listBuiltinFiles();
108 for (IPath xmlPath : files.values()) {
109 processFile(xmlPath.toFile());
110 }
111 }
112
113 private static void populateAnalysisModules() {
114 IPath pathToFiles = XmlUtils.getXmlFilesPath();
115 File folder = pathToFiles.toFile();
116 if (!(folder.isDirectory() && folder.exists())) {
117 return;
118 }
119 /*
120 * Transfer files from Linux Tools directory.
121 */
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);
136 }
137 }
138 }
139 }
140 Map<String, File> files = XmlUtils.listFiles();
141 for (File xmlFile : files.values()) {
142 processFile(xmlFile);
143 }
144 }
145
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.035706 seconds and 5 git commands to generate.