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