| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014 École Polytechnique de Montréal |
| 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.ui.Activator; |
| 26 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings; |
| 27 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.xychart.XmlXYView; |
| 28 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; |
| 29 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
| 30 | import org.eclipse.tracecompass.tmf.analysis.xml.ui.views.timegraph.XmlTimeGraphView; |
| 31 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; |
| 32 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisOutput; |
| 33 | import org.eclipse.tracecompass.tmf.core.analysis.ITmfNewAnalysisModuleListener; |
| 34 | import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule; |
| 35 | import org.w3c.dom.Document; |
| 36 | import org.w3c.dom.Element; |
| 37 | import org.w3c.dom.NodeList; |
| 38 | import org.xml.sax.SAXException; |
| 39 | |
| 40 | /** |
| 41 | * This class searches all XML files to find outputs applicable to the newly |
| 42 | * created analysis |
| 43 | * |
| 44 | * @author Geneviève Bastien |
| 45 | */ |
| 46 | public class TmfXmlAnalysisOutputSource implements ITmfNewAnalysisModuleListener { |
| 47 | |
| 48 | /** String separating data elements for the output properties */ |
| 49 | public static final @NonNull String DATA_SEPARATOR = ";;;"; //$NON-NLS-1$ |
| 50 | |
| 51 | /** |
| 52 | * Enum to match the name of a view's XML element to its view ID. |
| 53 | */ |
| 54 | public static enum ViewType { |
| 55 | /** |
| 56 | * Time graph view element |
| 57 | */ |
| 58 | TIME_GRAPH_VIEW(TmfXmlUiStrings.TIME_GRAPH_VIEW, XmlTimeGraphView.ID), |
| 59 | /** |
| 60 | * XY chart view element |
| 61 | */ |
| 62 | XY_VIEW(TmfXmlUiStrings.XY_VIEW, XmlXYView.ID); |
| 63 | |
| 64 | private final @NonNull String fXmlElem; |
| 65 | private final String fViewId; |
| 66 | |
| 67 | private ViewType(@NonNull String xmlElem, String viewId) { |
| 68 | fXmlElem = xmlElem; |
| 69 | fViewId = viewId; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get the XML element corresponding to this view type |
| 74 | * |
| 75 | * @return The XML element corresponding to this type |
| 76 | */ |
| 77 | public @NonNull String getXmlElem() { |
| 78 | return fXmlElem; |
| 79 | } |
| 80 | |
| 81 | private String getViewId() { |
| 82 | return fViewId; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | @Override |
| 88 | public void moduleCreated(IAnalysisModule module) { |
| 89 | Map<String, File> files = XmlUtils.listFiles(); |
| 90 | for (File xmlFile : files.values()) { |
| 91 | if (!XmlUtils.xmlValidate(xmlFile).isOK()) { |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | try { |
| 96 | /* Load the XML File */ |
| 97 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 98 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 99 | Document doc = dBuilder.parse(xmlFile); |
| 100 | doc.getDocumentElement().normalize(); |
| 101 | |
| 102 | /* get state provider views if the analysis has state systems */ |
| 103 | if (module instanceof TmfStateSystemAnalysisModule) { |
| 104 | for (ViewType viewType : ViewType.values()) { |
| 105 | NodeList ssViewNodes = doc.getElementsByTagName(viewType.getXmlElem()); |
| 106 | for (int i = 0; i < ssViewNodes.getLength(); i++) { |
| 107 | Element node = (Element) ssViewNodes.item(i); |
| 108 | |
| 109 | /* Check if analysis is the right one */ |
| 110 | List<Element> headNodes = XmlUtils.getChildElements(node, TmfXmlStrings.HEAD); |
| 111 | if (headNodes.size() != 1) { |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | List<Element> analysisNodes = XmlUtils.getChildElements(headNodes.get(0), TmfXmlStrings.ANALYSIS); |
| 116 | for (Element analysis : analysisNodes) { |
| 117 | String analysisId = analysis.getAttribute(TmfXmlStrings.ID); |
| 118 | if (analysisId.equals(module.getId())) { |
| 119 | String viewId = viewType.getViewId(); |
| 120 | IAnalysisOutput output = new TmfXmlViewOutput(viewId, viewType); |
| 121 | output.setOutputProperty(TmfXmlUiStrings.XML_OUTPUT_DATA, node.getAttribute(TmfXmlStrings.ID) + DATA_SEPARATOR + xmlFile.getAbsolutePath(), false); |
| 122 | module.registerOutput(output); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } catch (ParserConfigurationException | SAXException | IOException e) { |
| 129 | Activator.logError("Error opening XML file", e); //$NON-NLS-1$ |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |