0960c824dc39adb9c147e468c9486e7348e24eed
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.ui / src / org / eclipse / linuxtools / tmf / analysis / xml / ui / module / TmfXmlAnalysisOutputSource.java
1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.analysis.xml.ui.module;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.List;
18
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
22
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.Activator;
25 import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.TmfXmlUiStrings;
26 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils;
27 import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
28 import org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph.XmlTimeGraphView;
29 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule;
30 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisOutput;
31 import org.eclipse.linuxtools.tmf.core.analysis.ITmfNewAnalysisModuleListener;
32 import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.NodeList;
36 import org.xml.sax.SAXException;
37
38 /**
39 * This class searches all XML files to find outputs applicable to the newly
40 * created analysis
41 *
42 * @author Geneviève Bastien
43 */
44 public class TmfXmlAnalysisOutputSource implements ITmfNewAnalysisModuleListener {
45
46 /** String separating data elements for the output properties */
47 public static final String DATA_SEPARATOR = ";;;"; //$NON-NLS-1$
48
49 @Override
50 public void moduleCreated(IAnalysisModule module) {
51 IPath pathToFiles = XmlUtils.getXmlFilesPath();
52 File fFolder = pathToFiles.toFile();
53 if (!(fFolder.isDirectory() && fFolder.exists())) {
54 return;
55 }
56 for (File xmlFile : fFolder.listFiles()) {
57 if (!XmlUtils.xmlValidate(xmlFile).isOK()) {
58 continue;
59 }
60
61 try {
62 /* Load the XML File */
63 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
64 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
65 Document doc = dBuilder.parse(xmlFile);
66 doc.getDocumentElement().normalize();
67
68 /* get state provider views if the analysis has state systems */
69 if (module instanceof TmfStateSystemAnalysisModule) {
70 NodeList ssViewNodes = doc.getElementsByTagName(TmfXmlUiStrings.TIME_GRAPH_VIEW);
71 for (int i = 0; i < ssViewNodes.getLength(); i++) {
72 Element node = (Element) ssViewNodes.item(i);
73
74 /* Check if analysis is the right one */
75 List<Element> headNodes = XmlUtils.getChildElements(node, TmfXmlStrings.HEAD);
76 if (headNodes.size() != 1) {
77 continue;
78 }
79
80 List<Element> analysisNodes = XmlUtils.getChildElements(headNodes.get(0), TmfXmlStrings.ANALYSIS);
81 for (Element analysis : analysisNodes) {
82 String analysisId = analysis.getAttribute(TmfXmlStrings.ID);
83 if (analysisId.equals(module.getId())) {
84 IAnalysisOutput output = new TmfXmlViewOutput(XmlTimeGraphView.ID);
85 output.setOutputProperty(TmfXmlUiStrings.XML_OUTPUT_DATA, node.getAttribute(TmfXmlStrings.ID) + DATA_SEPARATOR + xmlFile.getAbsolutePath(), false);
86 module.registerOutput(output);
87 }
88 }
89 }
90 }
91 } catch (ParserConfigurationException | SAXException | IOException e) {
92 Activator.logError("Error opening XML file", e); //$NON-NLS-1$
93 }
94 }
95 }
96
97 }
This page took 0.03312 seconds and 5 git commands to generate.