tmf: Add some nonNull annotation to the tmf.core.analysis package
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / tmf / analysis / xml / ui / module / TmfAnalysisModuleHelperXml.java
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
13 package org.eclipse.tracecompass.tmf.analysis.xml.ui.module;
14
15 import java.io.File;
16 import java.util.Collections;
17 import java.util.List;
18
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jdt.annotation.NonNull;
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;
33 import org.osgi.framework.Bundle;
34 import org.w3c.dom.Element;
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;
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;
69 }
70
71 @Override
72 public String getId() {
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;
80 }
81
82 @Override
83 public String getName() {
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
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
110 @Override
111 public String getHelpText(@NonNull ITmfTrace trace) {
112 return ""; //$NON-NLS-1$
113 }
114
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
126 public boolean appliesToTraceType(Class<? extends ITmfTrace> traceClass) {
127 /* Trace types may be available in XML header */
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()) {
137 return true;
138 }
139
140 for (Element element : elements) {
141 String traceTypeId = element.getAttribute(TmfXmlStrings.ID);
142 TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId);
143 if ((helper != null) && helper.getTrace().getClass().isAssignableFrom(traceClass)) {
144 return true;
145 }
146 }
147 return false;
148 }
149
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
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
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);
176
177 break;
178 default:
179 break;
180
181 }
182 if (module != null) {
183 module.setTrace(trace);
184 TmfAnalysisManager.analysisModuleCreated(module);
185 }
186
187 return module;
188 }
189
190 }
This page took 0.036142 seconds and 6 git commands to generate.