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