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