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