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