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