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