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