Commit | Line | Data |
---|---|---|
048bde85 GB |
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.linuxtools.tmf.analysis.xml.ui.module; | |
14 | ||
15 | import java.io.File; | |
16 | import java.io.IOException; | |
1a23419e | 17 | import java.util.List; |
048bde85 GB |
18 | |
19 | import javax.xml.parsers.DocumentBuilder; | |
20 | import javax.xml.parsers.DocumentBuilderFactory; | |
21 | import javax.xml.parsers.ParserConfigurationException; | |
22 | ||
23 | import org.eclipse.core.runtime.IPath; | |
44b06bb9 | 24 | import org.eclipse.jdt.annotation.NonNull; |
048bde85 | 25 | import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.Activator; |
1a23419e | 26 | import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.TmfXmlUiStrings; |
87c5447c | 27 | import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.views.xychart.XmlXYView; |
048bde85 | 28 | import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils; |
1a23419e FW |
29 | import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
30 | import org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph.XmlTimeGraphView; | |
048bde85 | 31 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule; |
1a23419e | 32 | import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisOutput; |
048bde85 | 33 | import org.eclipse.linuxtools.tmf.core.analysis.ITmfNewAnalysisModuleListener; |
1a23419e | 34 | import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule; |
048bde85 | 35 | import org.w3c.dom.Document; |
1a23419e FW |
36 | import org.w3c.dom.Element; |
37 | import org.w3c.dom.NodeList; | |
048bde85 GB |
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 String DATA_SEPARATOR = ";;;"; //$NON-NLS-1$ | |
50 | ||
2e9d15a9 GB |
51 | /** |
52 | * Enum to match the name of a view's XML element to its view ID. | |
a465519a | 53 | * @since 1.2 |
2e9d15a9 | 54 | */ |
44b06bb9 GB |
55 | public static enum ViewType { |
56 | /** | |
57 | * Time graph view element | |
58 | */ | |
87c5447c | 59 | TIME_GRAPH_VIEW(TmfXmlUiStrings.TIME_GRAPH_VIEW, XmlTimeGraphView.ID), |
44b06bb9 GB |
60 | /** |
61 | * XY chart view element | |
62 | */ | |
87c5447c | 63 | XY_VIEW(TmfXmlUiStrings.XY_VIEW, XmlXYView.ID); |
2e9d15a9 | 64 | |
44b06bb9 | 65 | private final @NonNull String fXmlElem; |
2e9d15a9 GB |
66 | private final String fViewId; |
67 | ||
44b06bb9 | 68 | private ViewType(@NonNull String xmlElem, String viewId) { |
2e9d15a9 GB |
69 | fXmlElem = xmlElem; |
70 | fViewId = viewId; | |
71 | } | |
72 | ||
44b06bb9 GB |
73 | /** |
74 | * Get the XML element corresponding to this view type | |
75 | * | |
76 | * @return The XML element corresponding to this type | |
77 | */ | |
78 | public @NonNull String getXmlElem() { | |
2e9d15a9 GB |
79 | return fXmlElem; |
80 | } | |
81 | ||
44b06bb9 | 82 | private String getViewId() { |
2e9d15a9 GB |
83 | return fViewId; |
84 | } | |
85 | } | |
86 | ||
87c5447c | 87 | |
048bde85 GB |
88 | @Override |
89 | public void moduleCreated(IAnalysisModule module) { | |
90 | IPath pathToFiles = XmlUtils.getXmlFilesPath(); | |
91 | File fFolder = pathToFiles.toFile(); | |
92 | if (!(fFolder.isDirectory() && fFolder.exists())) { | |
93 | return; | |
94 | } | |
95 | for (File xmlFile : fFolder.listFiles()) { | |
96 | if (!XmlUtils.xmlValidate(xmlFile).isOK()) { | |
97 | continue; | |
98 | } | |
99 | ||
100 | try { | |
101 | /* Load the XML File */ | |
102 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
103 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
104 | Document doc = dBuilder.parse(xmlFile); | |
105 | doc.getDocumentElement().normalize(); | |
106 | ||
107 | /* get state provider views if the analysis has state systems */ | |
1a23419e | 108 | if (module instanceof TmfStateSystemAnalysisModule) { |
2e9d15a9 GB |
109 | for (ViewType viewType : ViewType.values()) { |
110 | NodeList ssViewNodes = doc.getElementsByTagName(viewType.getXmlElem()); | |
111 | for (int i = 0; i < ssViewNodes.getLength(); i++) { | |
112 | Element node = (Element) ssViewNodes.item(i); | |
113 | ||
114 | /* Check if analysis is the right one */ | |
115 | List<Element> headNodes = XmlUtils.getChildElements(node, TmfXmlStrings.HEAD); | |
116 | if (headNodes.size() != 1) { | |
117 | continue; | |
118 | } | |
1a23419e | 119 | |
2e9d15a9 GB |
120 | List<Element> analysisNodes = XmlUtils.getChildElements(headNodes.get(0), TmfXmlStrings.ANALYSIS); |
121 | for (Element analysis : analysisNodes) { | |
122 | String analysisId = analysis.getAttribute(TmfXmlStrings.ID); | |
123 | if (analysisId.equals(module.getId())) { | |
124 | String viewId = viewType.getViewId(); | |
44b06bb9 | 125 | IAnalysisOutput output = new TmfXmlViewOutput(viewId, viewType); |
2e9d15a9 GB |
126 | output.setOutputProperty(TmfXmlUiStrings.XML_OUTPUT_DATA, node.getAttribute(TmfXmlStrings.ID) + DATA_SEPARATOR + xmlFile.getAbsolutePath(), false); |
127 | module.registerOutput(output); | |
128 | } | |
1a23419e FW |
129 | } |
130 | } | |
131 | } | |
132 | } | |
048bde85 GB |
133 | } catch (ParserConfigurationException | SAXException | IOException e) { |
134 | Activator.logError("Error opening XML file", e); //$NON-NLS-1$ | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | } |