tmf: Add XML analysis helper properties
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / tmf / analysis / xml / core / module / TmfAnalysisModuleHelperXml.java
CommitLineData
97ed0cf0 1/*******************************************************************************
0dcf01f1 2 * Copyright (c) 2014, 2016 École Polytechnique de Montréal
97ed0cf0
FW
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
97ed0cf0
FW
8 *******************************************************************************/
9
0dcf01f1 10package org.eclipse.tracecompass.tmf.analysis.xml.core.module;
97ed0cf0 11
5db5a3a4
AM
12import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
97ed0cf0 14import java.io.File;
63c43609 15import java.util.Collections;
145efc1f 16import java.util.HashMap;
aa89118c 17import java.util.List;
145efc1f 18import java.util.Map;
97ed0cf0
FW
19
20import org.eclipse.core.runtime.Path;
54eae41f 21import org.eclipse.jdt.annotation.NonNull;
145efc1f 22import org.eclipse.tracecompass.common.core.NonNullUtils;
0dcf01f1 23import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
2bdf0193
AM
24import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
25import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
26import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
27import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
28import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
29import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisRequirement;
30import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
145efc1f 31import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
2bdf0193
AM
32import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
33import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
34import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
97ed0cf0
FW
35import org.osgi.framework.Bundle;
36import org.w3c.dom.Element;
97ed0cf0
FW
37
38/**
39 * Analysis module helpers for modules provided by XML files
40 *
41 * @author Geneviève Bastien
0dcf01f1 42 * @since 2.0
97ed0cf0 43 */
145efc1f 44public class TmfAnalysisModuleHelperXml implements IAnalysisModuleHelper, ITmfPropertiesProvider {
97ed0cf0
FW
45
46 /**
47 * The types of analysis that can be XML-defined
48 */
49 public enum XmlAnalysisModuleType {
50 /** Analysis will be of type XmlStateSystemModule */
51 STATE_SYSTEM
52 }
53
54 private final File fSourceFile;
55 private final Element fSourceElement;
56 private final XmlAnalysisModuleType fType;
97ed0cf0
FW
57
58 /**
59 * Constructor
60 *
61 * @param xmlFile
62 * The XML file containing the details of this analysis
63 * @param node
64 * The XML node element
65 * @param type
66 * The type of analysis
67 */
68 public TmfAnalysisModuleHelperXml(File xmlFile, Element node, XmlAnalysisModuleType type) {
69 fSourceFile = xmlFile;
70 fSourceElement = node;
71 fType = type;
97ed0cf0
FW
72 }
73
74 @Override
75 public String getId() {
ba27dd38
GB
76 /*
77 * The attribute ID cannot be null because the XML has been validated
78 * and it is mandatory
79 */
5db5a3a4 80 return checkNotNull(fSourceElement.getAttribute(TmfXmlStrings.ID));
97ed0cf0
FW
81 }
82
83 @Override
84 public String getName() {
aa89118c
GB
85 String name = null;
86 /* Label may be available in XML header */
87 List<Element> head = XmlUtils.getChildElements(fSourceElement, TmfXmlStrings.HEAD);
88 if (head.size() == 1) {
89 List<Element> labels = XmlUtils.getChildElements(head.get(0), TmfXmlStrings.LABEL);
90 if (!labels.isEmpty()) {
91 name = labels.get(0).getAttribute(TmfXmlStrings.VALUE);
92 }
93 }
94
97ed0cf0
FW
95 if (name == null) {
96 name = getId();
97 }
98 return name;
99 }
100
101 @Override
102 public boolean isAutomatic() {
103 return false;
104 }
105
ff7b95a5
GB
106 /**
107 * @since 1.0
108 */
109 @Override
110 public boolean appliesToExperiment() {
111 return false;
112 }
113
97ed0cf0
FW
114 @Override
115 public String getHelpText() {
ab9ca7ea 116 return ""; //$NON-NLS-1$
97ed0cf0
FW
117 }
118
54eae41f
GB
119 @Override
120 public String getHelpText(@NonNull ITmfTrace trace) {
121 return ""; //$NON-NLS-1$
122 }
123
97ed0cf0
FW
124 @Override
125 public String getIcon() {
126 return null;
127 }
128
129 @Override
130 public Bundle getBundle() {
131 return Activator.getDefault().getBundle();
132 }
133
134 @Override
aa89118c 135 public boolean appliesToTraceType(Class<? extends ITmfTrace> traceClass) {
97ed0cf0 136 /* Trace types may be available in XML header */
aa89118c
GB
137 List<Element> head = XmlUtils.getChildElements(fSourceElement, TmfXmlStrings.HEAD);
138 if (head.size() != 1) {
139 return true;
140 }
141 /*
142 * TODO: Test with custom trace types
143 */
144 List<Element> elements = XmlUtils.getChildElements(head.get(0), TmfXmlStrings.TRACETYPE);
145 if (elements.isEmpty()) {
97ed0cf0
FW
146 return true;
147 }
148
aa89118c
GB
149 for (Element element : elements) {
150 String traceTypeId = element.getAttribute(TmfXmlStrings.ID);
e86f7ac4 151 traceTypeId = TmfTraceType.buildCompatibilityTraceTypeId(traceTypeId);
a4a116c3 152 TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId);
aa89118c
GB
153 if ((helper != null) && helper.getTrace().getClass().isAssignableFrom(traceClass)) {
154 return true;
155 }
156 }
157 return false;
97ed0cf0
FW
158 }
159
63c43609
MR
160 @Override
161 public Iterable<Class<? extends ITmfTrace>> getValidTraceTypes() {
162 return Collections.EMPTY_SET;
163 }
164
165 @Override
166 public Iterable<TmfAnalysisRequirement> getAnalysisRequirements() {
167 return Collections.EMPTY_SET;
168 }
169
97ed0cf0
FW
170 @Override
171 public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException {
172 String analysisid = getId();
173 IAnalysisModule module = null;
174 switch (fType) {
175 case STATE_SYSTEM:
176 module = new XmlStateSystemModule();
177 XmlStateSystemModule ssModule = (XmlStateSystemModule) module;
178 module.setId(analysisid);
179 ssModule.setXmlFile(new Path(fSourceFile.getAbsolutePath()));
180
048bde85
GB
181 /*
182 * FIXME: There is no way to know if a module is automatic, so we
183 * default to true
184 */
185 ssModule.setAutomatic(true);
97ed0cf0 186
97ed0cf0
FW
187 break;
188 default:
189 break;
190
191 }
192 if (module != null) {
f479550c
GB
193 if (module.setTrace(trace)) {
194 TmfAnalysisManager.analysisModuleCreated(module);
195 } else {
196 /* The analysis does not apply to the trace, dispose of the module */
197 module.dispose();
198 module = null;
199 }
97ed0cf0
FW
200 }
201
202 return module;
203 }
204
145efc1f
GB
205 // ------------------------------------------------------------------------
206 // ITmfPropertiesProvider
207 // ------------------------------------------------------------------------
208
209 @Override
210 public @NonNull Map<@NonNull String, @NonNull String> getProperties() {
211 Map<@NonNull String, @NonNull String> properties = new HashMap<>();
212 properties.put(NonNullUtils.checkNotNull(Messages.XmlModuleHelper_PropertyFile), fSourceFile.getName());
213 properties.put(NonNullUtils.checkNotNull(Messages.XmlModuleHelper_PropertyType), fType.name());
214 return properties;
215 }
216
97ed0cf0 217}
This page took 0.071891 seconds and 5 git commands to generate.