Commit | Line | Data |
---|---|---|
048bde85 GB |
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; | |
1a23419e | 17 | import java.util.List; |
048bde85 GB |
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; | |
1a23419e | 25 | import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.TmfXmlUiStrings; |
87c5447c | 26 | import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.views.xychart.XmlXYView; |
048bde85 | 27 | import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils; |
1a23419e FW |
28 | import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
29 | import org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph.XmlTimeGraphView; | |
048bde85 | 30 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule; |
1a23419e | 31 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisOutput; |
048bde85 | 32 | import org.eclipse.linuxtools.tmf.core.analysis.ITmfNewAnalysisModuleListener; |
1a23419e | 33 | import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule; |
048bde85 | 34 | import org.w3c.dom.Document; |
1a23419e FW |
35 | import org.w3c.dom.Element; |
36 | import org.w3c.dom.NodeList; | |
048bde85 GB |
37 | import org.xml.sax.SAXException; |
38 | ||
39 | /** | |
40 | * This class searches all XML files to find outputs applicable to the newly | |
41 | * created analysis | |
42 | * | |
43 | * @author Geneviève Bastien | |
44 | */ | |
45 | public class TmfXmlAnalysisOutputSource implements ITmfNewAnalysisModuleListener { | |
46 | ||
47 | /** String separating data elements for the output properties */ | |
48 | public static final String DATA_SEPARATOR = ";;;"; //$NON-NLS-1$ | |
49 | ||
2e9d15a9 GB |
50 | /** |
51 | * Enum to match the name of a view's XML element to its view ID. | |
52 | */ | |
53 | private static enum ViewType { | |
87c5447c GB |
54 | TIME_GRAPH_VIEW(TmfXmlUiStrings.TIME_GRAPH_VIEW, XmlTimeGraphView.ID), |
55 | XY_VIEW(TmfXmlUiStrings.XY_VIEW, XmlXYView.ID); | |
2e9d15a9 GB |
56 | |
57 | private final String fXmlElem; | |
58 | private final String fViewId; | |
59 | ||
60 | private ViewType(String xmlElem, String viewId) { | |
61 | fXmlElem = xmlElem; | |
62 | fViewId = viewId; | |
63 | } | |
64 | ||
65 | public String getXmlElem() { | |
66 | return fXmlElem; | |
67 | } | |
68 | ||
69 | public String getViewId() { | |
70 | return fViewId; | |
71 | } | |
72 | } | |
73 | ||
87c5447c | 74 | |
048bde85 GB |
75 | @Override |
76 | public void moduleCreated(IAnalysisModule module) { | |
77 | IPath pathToFiles = XmlUtils.getXmlFilesPath(); | |
78 | File fFolder = pathToFiles.toFile(); | |
79 | if (!(fFolder.isDirectory() && fFolder.exists())) { | |
80 | return; | |
81 | } | |
82 | for (File xmlFile : fFolder.listFiles()) { | |
83 | if (!XmlUtils.xmlValidate(xmlFile).isOK()) { | |
84 | continue; | |
85 | } | |
86 | ||
87 | try { | |
88 | /* Load the XML File */ | |
89 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
90 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
91 | Document doc = dBuilder.parse(xmlFile); | |
92 | doc.getDocumentElement().normalize(); | |
93 | ||
94 | /* get state provider views if the analysis has state systems */ | |
1a23419e | 95 | if (module instanceof TmfStateSystemAnalysisModule) { |
2e9d15a9 GB |
96 | for (ViewType viewType : ViewType.values()) { |
97 | NodeList ssViewNodes = doc.getElementsByTagName(viewType.getXmlElem()); | |
98 | for (int i = 0; i < ssViewNodes.getLength(); i++) { | |
99 | Element node = (Element) ssViewNodes.item(i); | |
100 | ||
101 | /* Check if analysis is the right one */ | |
102 | List<Element> headNodes = XmlUtils.getChildElements(node, TmfXmlStrings.HEAD); | |
103 | if (headNodes.size() != 1) { | |
104 | continue; | |
105 | } | |
1a23419e | 106 | |
2e9d15a9 GB |
107 | List<Element> analysisNodes = XmlUtils.getChildElements(headNodes.get(0), TmfXmlStrings.ANALYSIS); |
108 | for (Element analysis : analysisNodes) { | |
109 | String analysisId = analysis.getAttribute(TmfXmlStrings.ID); | |
110 | if (analysisId.equals(module.getId())) { | |
111 | String viewId = viewType.getViewId(); | |
112 | IAnalysisOutput output = new TmfXmlViewOutput(viewId); | |
113 | output.setOutputProperty(TmfXmlUiStrings.XML_OUTPUT_DATA, node.getAttribute(TmfXmlStrings.ID) + DATA_SEPARATOR + xmlFile.getAbsolutePath(), false); | |
114 | module.registerOutput(output); | |
115 | } | |
1a23419e FW |
116 | } |
117 | } | |
118 | } | |
119 | } | |
048bde85 GB |
120 | } catch (ParserConfigurationException | SAXException | IOException e) { |
121 | Activator.logError("Error opening XML file", e); //$NON-NLS-1$ | |
122 | } | |
123 | } | |
124 | } | |
125 | ||
126 | } |