Commit | Line | Data |
---|---|---|
048bde85 | 1 | /******************************************************************************* |
38fad53e | 2 | * Copyright (c) 2016 École Polytechnique de Montréal and others |
048bde85 GB |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.analysis.xml.ui.module; |
048bde85 GB |
14 | |
15 | import java.io.File; | |
16 | import java.io.IOException; | |
1a23419e | 17 | import java.util.List; |
537572cd | 18 | import java.util.Map; |
048bde85 GB |
19 | |
20 | import javax.xml.parsers.DocumentBuilder; | |
21 | import javax.xml.parsers.DocumentBuilderFactory; | |
22 | import javax.xml.parsers.ParserConfigurationException; | |
23 | ||
44b06bb9 | 24 | import org.eclipse.jdt.annotation.NonNull; |
38fad53e | 25 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider.XmlPatternAnalysis; |
2bdf0193 AM |
26 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator; |
27 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings; | |
38fad53e JCK |
28 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module.Messages; |
29 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternLatencyTableView; | |
2bdf0193 AM |
30 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.xychart.XmlXYView; |
31 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; | |
32 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
33 | import org.eclipse.tracecompass.tmf.analysis.xml.ui.views.timegraph.XmlTimeGraphView; | |
34 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; | |
35 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisOutput; | |
36 | import org.eclipse.tracecompass.tmf.core.analysis.ITmfNewAnalysisModuleListener; | |
38fad53e | 37 | import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems; |
048bde85 | 38 | import org.w3c.dom.Document; |
1a23419e FW |
39 | import org.w3c.dom.Element; |
40 | import org.w3c.dom.NodeList; | |
048bde85 GB |
41 | import org.xml.sax.SAXException; |
42 | ||
43 | /** | |
44 | * This class searches all XML files to find outputs applicable to the newly | |
45 | * created analysis | |
46 | * | |
47 | * @author Geneviève Bastien | |
48 | */ | |
49 | public class TmfXmlAnalysisOutputSource implements ITmfNewAnalysisModuleListener { | |
50 | ||
51 | /** String separating data elements for the output properties */ | |
8ecd89fa | 52 | public static final @NonNull String DATA_SEPARATOR = ";;;"; //$NON-NLS-1$ |
048bde85 | 53 | |
2e9d15a9 GB |
54 | /** |
55 | * Enum to match the name of a view's XML element to its view ID. | |
56 | */ | |
44b06bb9 GB |
57 | public static enum ViewType { |
58 | /** | |
59 | * Time graph view element | |
60 | */ | |
87c5447c | 61 | TIME_GRAPH_VIEW(TmfXmlUiStrings.TIME_GRAPH_VIEW, XmlTimeGraphView.ID), |
44b06bb9 GB |
62 | /** |
63 | * XY chart view element | |
64 | */ | |
87c5447c | 65 | XY_VIEW(TmfXmlUiStrings.XY_VIEW, XmlXYView.ID); |
2e9d15a9 | 66 | |
44b06bb9 | 67 | private final @NonNull String fXmlElem; |
2e9d15a9 GB |
68 | private final String fViewId; |
69 | ||
44b06bb9 | 70 | private ViewType(@NonNull String xmlElem, String viewId) { |
2e9d15a9 GB |
71 | fXmlElem = xmlElem; |
72 | fViewId = viewId; | |
73 | } | |
74 | ||
44b06bb9 GB |
75 | /** |
76 | * Get the XML element corresponding to this view type | |
77 | * | |
78 | * @return The XML element corresponding to this type | |
79 | */ | |
80 | public @NonNull String getXmlElem() { | |
2e9d15a9 GB |
81 | return fXmlElem; |
82 | } | |
83 | ||
44b06bb9 | 84 | private String getViewId() { |
2e9d15a9 GB |
85 | return fViewId; |
86 | } | |
87 | } | |
88 | ||
38fad53e JCK |
89 | /** |
90 | * Enum for latency view type. | |
91 | * | |
92 | * @author Jean-Christian Kouame | |
93 | * @since 2.0 | |
94 | * | |
95 | */ | |
96 | public static enum LatencyViewType { | |
97 | ||
98 | /** | |
99 | * Latency Table View type | |
100 | */ | |
101 | LATENCY_TABLE(PatternLatencyTableView.ID, Messages.TmfXmlAnalysisOutputSource_LatencyTable); | |
102 | ||
103 | private @NonNull String fLatencyViewId; | |
104 | private String fLatencyViewLabel; | |
105 | ||
106 | private LatencyViewType(@NonNull String viewId, String label) { | |
107 | fLatencyViewId = viewId; | |
108 | fLatencyViewLabel = label; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Get the ID of the latency view | |
113 | * | |
114 | * @return The ID | |
115 | */ | |
116 | public String getViewId() { | |
117 | return fLatencyViewId; | |
118 | } | |
119 | ||
120 | /** | |
121 | * Get the label of the view | |
122 | * | |
123 | * @return The label | |
124 | */ | |
125 | public String getLabel() { | |
126 | return fLatencyViewLabel; | |
127 | } | |
128 | } | |
87c5447c | 129 | |
048bde85 GB |
130 | @Override |
131 | public void moduleCreated(IAnalysisModule module) { | |
537572cd BH |
132 | Map<String, File> files = XmlUtils.listFiles(); |
133 | for (File xmlFile : files.values()) { | |
048bde85 GB |
134 | if (!XmlUtils.xmlValidate(xmlFile).isOK()) { |
135 | continue; | |
136 | } | |
137 | ||
138 | try { | |
139 | /* Load the XML File */ | |
140 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
141 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
142 | Document doc = dBuilder.parse(xmlFile); | |
143 | doc.getDocumentElement().normalize(); | |
144 | ||
145 | /* get state provider views if the analysis has state systems */ | |
38fad53e | 146 | if (module instanceof ITmfAnalysisModuleWithStateSystems) { |
2e9d15a9 GB |
147 | for (ViewType viewType : ViewType.values()) { |
148 | NodeList ssViewNodes = doc.getElementsByTagName(viewType.getXmlElem()); | |
149 | for (int i = 0; i < ssViewNodes.getLength(); i++) { | |
150 | Element node = (Element) ssViewNodes.item(i); | |
151 | ||
152 | /* Check if analysis is the right one */ | |
153 | List<Element> headNodes = XmlUtils.getChildElements(node, TmfXmlStrings.HEAD); | |
154 | if (headNodes.size() != 1) { | |
155 | continue; | |
156 | } | |
1a23419e | 157 | |
2e9d15a9 GB |
158 | List<Element> analysisNodes = XmlUtils.getChildElements(headNodes.get(0), TmfXmlStrings.ANALYSIS); |
159 | for (Element analysis : analysisNodes) { | |
160 | String analysisId = analysis.getAttribute(TmfXmlStrings.ID); | |
161 | if (analysisId.equals(module.getId())) { | |
162 | String viewId = viewType.getViewId(); | |
44b06bb9 | 163 | IAnalysisOutput output = new TmfXmlViewOutput(viewId, viewType); |
2e9d15a9 GB |
164 | output.setOutputProperty(TmfXmlUiStrings.XML_OUTPUT_DATA, node.getAttribute(TmfXmlStrings.ID) + DATA_SEPARATOR + xmlFile.getAbsolutePath(), false); |
165 | module.registerOutput(output); | |
166 | } | |
1a23419e FW |
167 | } |
168 | } | |
169 | } | |
170 | } | |
38fad53e JCK |
171 | |
172 | //Add the latency views for pattern analysis | |
173 | if (module instanceof XmlPatternAnalysis) { | |
174 | for (LatencyViewType viewType : LatencyViewType.values()) { | |
175 | IAnalysisOutput output = new TmfXmlLatencyViewOutput(viewType.getViewId(), viewType.getLabel()); | |
176 | output.setOutputProperty(TmfXmlUiStrings.XML_LATENCY_OUTPUT_DATA, module.getId() + DATA_SEPARATOR + viewType.getLabel(), false); | |
177 | module.registerOutput(output); | |
178 | } | |
179 | } | |
180 | ||
048bde85 GB |
181 | } catch (ParserConfigurationException | SAXException | IOException e) { |
182 | Activator.logError("Error opening XML file", e); //$NON-NLS-1$ | |
183 | } | |
184 | } | |
185 | } | |
186 | ||
187 | } |