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
1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal and others
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.tracecompass.internal.tmf.analysis.xml.ui.module;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20
21 import javax.xml.parsers.ParserConfigurationException;
22
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils;
25 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider.XmlPatternAnalysis;
26 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
27 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator;
28 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings;
29 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternDensityView;
30 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternLatencyTableView;
31 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternScatterGraphView;
32 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternStatisticsView;
33 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.timegraph.XmlTimeGraphView;
34 import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.xychart.XmlXYView;
35 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
36 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisOutput;
37 import org.eclipse.tracecompass.tmf.core.analysis.ITmfNewAnalysisModuleListener;
38 import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Element;
41 import org.w3c.dom.NodeList;
42 import 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 */
50 public class TmfXmlAnalysisOutputSource implements ITmfNewAnalysisModuleListener {
51
52 /** String separating data elements for the output properties */
53 public static final @NonNull String DATA_SEPARATOR = ";;;"; //$NON-NLS-1$
54
55 /**
56 * Enum to match the name of a view's XML element to its view ID.
57 */
58 public static enum ViewType {
59 /**
60 * Time graph view element
61 */
62 TIME_GRAPH_VIEW(TmfXmlUiStrings.TIME_GRAPH_VIEW, XmlTimeGraphView.ID),
63 /**
64 * XY chart view element
65 */
66 XY_VIEW(TmfXmlUiStrings.XY_VIEW, XmlXYView.ID);
67
68 private final @NonNull String fXmlElem;
69 private final String fViewId;
70
71 private ViewType(@NonNull String xmlElem, String viewId) {
72 fXmlElem = xmlElem;
73 fViewId = viewId;
74 }
75
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() {
82 return fXmlElem;
83 }
84
85 private String getViewId() {
86 return fViewId;
87 }
88 }
89
90 /**
91 * Enum for latency view type.
92 *
93 * @author Jean-Christian Kouame
94 */
95 public static enum LatencyViewType {
96
97 /**
98 * Latency Table View type
99 */
100 LATENCY_TABLE(PatternLatencyTableView.ID, Messages.TmfXmlAnalysisOutputSource_LatencyTable),
101
102 /**
103 * Latency Scatter View type
104 */
105 SCATTER_GRAPH(PatternScatterGraphView.ID, Messages.TmfXmlAnalysisOutputSource_ScatterGraphTitle),
106
107 /**
108 * Latency Density View type
109 */
110 DENSITY_VIEW(PatternDensityView.ID, Messages.TmfXmlAnalysisOutputSource_DensityChartTitle),
111
112 /**
113 * Latency Statistic View type
114 */
115 STATISTIC_VIEW(PatternStatisticsView.ID, Messages.TmfXmlAnalysisOutputSource_LatencyStatisticsTitle);
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 }
143
144 @Override
145 public void moduleCreated(IAnalysisModule module) {
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) {
154 if (!XmlUtils.xmlValidate(xmlFile).isOK()) {
155 continue;
156 }
157
158 try {
159 Document doc = XmlUtils.getDocumentFromFile(xmlFile);
160
161 /* get state provider views if the analysis has state systems */
162 if (module instanceof ITmfAnalysisModuleWithStateSystems) {
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 }
173
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();
179 IAnalysisOutput output = new TmfXmlViewOutput(viewId, viewType);
180 output.setOutputProperty(TmfXmlUiStrings.XML_OUTPUT_DATA, node.getAttribute(TmfXmlStrings.ID) + DATA_SEPARATOR + xmlFile.getAbsolutePath(), false);
181 module.registerOutput(output);
182 }
183 }
184 }
185 }
186 }
187
188 // Add the latency views for pattern analysis
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
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.035258 seconds and 5 git commands to generate.