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
1 /*******************************************************************************
2 * Copyright (c) 2014, 2016 É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
10 package org.eclipse.tracecompass.tmf.analysis.xml.core.module;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.io.File;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.tracecompass.common.core.NonNullUtils;
23 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
24 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
25 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
26 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
27 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
28 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
29 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisRequirement;
30 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
31 import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
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;
35 import org.osgi.framework.Bundle;
36 import org.w3c.dom.Element;
37
38 /**
39 * Analysis module helpers for modules provided by XML files
40 *
41 * @author Geneviève Bastien
42 * @since 2.0
43 */
44 public class TmfAnalysisModuleHelperXml implements IAnalysisModuleHelper, ITmfPropertiesProvider {
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;
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;
72 }
73
74 @Override
75 public String getId() {
76 /*
77 * The attribute ID cannot be null because the XML has been validated
78 * and it is mandatory
79 */
80 return checkNotNull(fSourceElement.getAttribute(TmfXmlStrings.ID));
81 }
82
83 @Override
84 public String getName() {
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
95 if (name == null) {
96 name = getId();
97 }
98 return name;
99 }
100
101 @Override
102 public boolean isAutomatic() {
103 return false;
104 }
105
106 /**
107 * @since 1.0
108 */
109 @Override
110 public boolean appliesToExperiment() {
111 return false;
112 }
113
114 @Override
115 public String getHelpText() {
116 return ""; //$NON-NLS-1$
117 }
118
119 @Override
120 public String getHelpText(@NonNull ITmfTrace trace) {
121 return ""; //$NON-NLS-1$
122 }
123
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
135 public boolean appliesToTraceType(Class<? extends ITmfTrace> traceClass) {
136 /* Trace types may be available in XML header */
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()) {
146 return true;
147 }
148
149 for (Element element : elements) {
150 String traceTypeId = element.getAttribute(TmfXmlStrings.ID);
151 traceTypeId = TmfTraceType.buildCompatibilityTraceTypeId(traceTypeId);
152 TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId);
153 if ((helper != null) && helper.getTrace().getClass().isAssignableFrom(traceClass)) {
154 return true;
155 }
156 }
157 return false;
158 }
159
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
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
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);
186
187 break;
188 default:
189 break;
190
191 }
192 if (module != null) {
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 }
200 }
201
202 return module;
203 }
204
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
217 }
This page took 0.036002 seconds and 5 git commands to generate.