| 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.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.pattern.stateprovider.XmlPatternAnalysis; |
| 26 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator; |
| 27 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings; |
| 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; |
| 30 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.latency.PatternScatterGraphView; |
| 31 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.xychart.XmlXYView; |
| 32 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; |
| 33 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
| 34 | import org.eclipse.tracecompass.tmf.analysis.xml.ui.views.timegraph.XmlTimeGraphView; |
| 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 | * @since 2.0 |
| 95 | * |
| 96 | */ |
| 97 | public static enum LatencyViewType { |
| 98 | |
| 99 | /** |
| 100 | * Latency Table View type |
| 101 | */ |
| 102 | LATENCY_TABLE(PatternLatencyTableView.ID, Messages.TmfXmlAnalysisOutputSource_LatencyTable), |
| 103 | |
| 104 | /** |
| 105 | * Latency Scatter View type |
| 106 | */ |
| 107 | SCATTER_GRAPH(PatternScatterGraphView.ID, Messages.TmfXmlAnalysisOutputSource_ScatterGraphTitle); |
| 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 | } |
| 135 | |
| 136 | @Override |
| 137 | public void moduleCreated(IAnalysisModule module) { |
| 138 | Map<String, File> files = XmlUtils.listFiles(); |
| 139 | for (File xmlFile : files.values()) { |
| 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 */ |
| 152 | if (module instanceof ITmfAnalysisModuleWithStateSystems) { |
| 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 | } |
| 163 | |
| 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(); |
| 169 | IAnalysisOutput output = new TmfXmlViewOutput(viewId, viewType); |
| 170 | output.setOutputProperty(TmfXmlUiStrings.XML_OUTPUT_DATA, node.getAttribute(TmfXmlStrings.ID) + DATA_SEPARATOR + xmlFile.getAbsolutePath(), false); |
| 171 | module.registerOutput(output); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Add the latency views for pattern analysis |
| 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 | |
| 187 | } catch (ParserConfigurationException | SAXException | IOException e) { |
| 188 | Activator.logError("Error opening XML file", e); //$NON-NLS-1$ |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | } |