xml: bug 497272 Populate views from built-in XML files
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / internal / 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
6eca054d 13package org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module;
048bde85
GB
14
15import java.io.File;
16import java.io.IOException;
1a23419e 17import java.util.List;
d2452723
GB
18import java.util.Set;
19import java.util.stream.Collectors;
048bde85 20
048bde85
GB
21import javax.xml.parsers.ParserConfigurationException;
22
44b06bb9 23import org.eclipse.jdt.annotation.NonNull;
6eca054d 24import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils;
38fad53e 25import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider.XmlPatternAnalysis;
6eca054d 26import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
2bdf0193
AM
27import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator;
28import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings;
3b5dc745 29import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternDensityView;
38fad53e 30import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternLatencyTableView;
c8e5d00e 31import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternScatterGraphView;
5b901f94 32import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternStatisticsView;
6eca054d 33import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.timegraph.XmlTimeGraphView;
2bdf0193 34import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.xychart.XmlXYView;
2bdf0193
AM
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
38fad53e
JCK
94 */
95 public static enum LatencyViewType {
96
97 /**
98 * Latency Table View type
99 */
c8e5d00e
JCK
100 LATENCY_TABLE(PatternLatencyTableView.ID, Messages.TmfXmlAnalysisOutputSource_LatencyTable),
101
102 /**
103 * Latency Scatter View type
104 */
3b5dc745
JCK
105 SCATTER_GRAPH(PatternScatterGraphView.ID, Messages.TmfXmlAnalysisOutputSource_ScatterGraphTitle),
106
107 /**
108 * Latency Density View type
109 */
5b901f94
JCK
110 DENSITY_VIEW(PatternDensityView.ID, Messages.TmfXmlAnalysisOutputSource_DensityChartTitle),
111
112 /**
113 * Latency Statistic View type
114 */
115 STATISTIC_VIEW(PatternStatisticsView.ID, Messages.TmfXmlAnalysisOutputSource_LatencyStatisticsTitle);
38fad53e
JCK
116
117 private @NonNull String fLatencyViewId;
118 private String fLatencyViewLabel;
119
120 private LatencyViewType(@NonNull String viewId, String label) {
121 fLatencyViewId = viewId;
122 fLatencyViewLabel = label;
123 }
124
125 /**
126 * Get the ID of the latency view
127 *
128 * @return The ID
129 */
130 public String getViewId() {
131 return fLatencyViewId;
132 }
133
134 /**
135 * Get the label of the view
136 *
137 * @return The label
138 */
139 public String getLabel() {
140 return fLatencyViewLabel;
141 }
142 }
87c5447c 143
048bde85
GB
144 @Override
145 public void moduleCreated(IAnalysisModule module) {
d2452723
GB
146 // Get all the XML files, builtin and not builtin
147 Set<File> files = XmlUtils.listBuiltinFiles().values().stream()
148 .map(p -> p.toFile())
149 .collect(Collectors.toSet());
150 XmlUtils.listFiles().values().stream()
151 .forEach(f -> files.add(f));
152
153 for (File xmlFile : files) {
048bde85
GB
154 if (!XmlUtils.xmlValidate(xmlFile).isOK()) {
155 continue;
156 }
157
158 try {
f10786aa 159 Document doc = XmlUtils.getDocumentFromFile(xmlFile);
048bde85
GB
160
161 /* get state provider views if the analysis has state systems */
38fad53e 162 if (module instanceof ITmfAnalysisModuleWithStateSystems) {
2e9d15a9
GB
163 for (ViewType viewType : ViewType.values()) {
164 NodeList ssViewNodes = doc.getElementsByTagName(viewType.getXmlElem());
165 for (int i = 0; i < ssViewNodes.getLength(); i++) {
166 Element node = (Element) ssViewNodes.item(i);
167
168 /* Check if analysis is the right one */
169 List<Element> headNodes = XmlUtils.getChildElements(node, TmfXmlStrings.HEAD);
170 if (headNodes.size() != 1) {
171 continue;
172 }
1a23419e 173
2e9d15a9
GB
174 List<Element> analysisNodes = XmlUtils.getChildElements(headNodes.get(0), TmfXmlStrings.ANALYSIS);
175 for (Element analysis : analysisNodes) {
176 String analysisId = analysis.getAttribute(TmfXmlStrings.ID);
177 if (analysisId.equals(module.getId())) {
178 String viewId = viewType.getViewId();
44b06bb9 179 IAnalysisOutput output = new TmfXmlViewOutput(viewId, viewType);
2e9d15a9
GB
180 output.setOutputProperty(TmfXmlUiStrings.XML_OUTPUT_DATA, node.getAttribute(TmfXmlStrings.ID) + DATA_SEPARATOR + xmlFile.getAbsolutePath(), false);
181 module.registerOutput(output);
182 }
1a23419e
FW
183 }
184 }
185 }
186 }
38fad53e 187
c8e5d00e 188 // Add the latency views for pattern analysis
38fad53e
JCK
189 if (module instanceof XmlPatternAnalysis) {
190 for (LatencyViewType viewType : LatencyViewType.values()) {
191 IAnalysisOutput output = new TmfXmlLatencyViewOutput(viewType.getViewId(), viewType.getLabel());
192 output.setOutputProperty(TmfXmlUiStrings.XML_LATENCY_OUTPUT_DATA, module.getId() + DATA_SEPARATOR + viewType.getLabel(), false);
193 module.registerOutput(output);
194 }
195 }
196
048bde85
GB
197 } catch (ParserConfigurationException | SAXException | IOException e) {
198 Activator.logError("Error opening XML file", e); //$NON-NLS-1$
199 }
200 }
201 }
202
203}
This page took 0.078096 seconds and 5 git commands to generate.