Make XML analysis using custom parsers backwards compatible
[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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.File;
18 import java.util.Collections;
19 import java.util.List;
20
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jdt.annotation.NonNull;
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;
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 */
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;
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;
71 }
72
73 @Override
74 public String getId() {
75 /*
76 * The attribute ID cannot be null because the XML has been validated
77 * and it is mandatory
78 */
79 return checkNotNull(fSourceElement.getAttribute(TmfXmlStrings.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 traceTypeId = TmfTraceType.buildCompatibilityTraceTypeId(traceTypeId);
143 TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId);
144 if ((helper != null) && helper.getTrace().getClass().isAssignableFrom(traceClass)) {
145 return true;
146 }
147 }
148 return false;
149 }
150
151 @Override
152 public Iterable<Class<? extends ITmfTrace>> getValidTraceTypes() {
153 return Collections.EMPTY_SET;
154 }
155
156 @Override
157 public Iterable<TmfAnalysisRequirement> getAnalysisRequirements() {
158 return Collections.EMPTY_SET;
159 }
160
161 @Override
162 public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException {
163 String analysisid = getId();
164 IAnalysisModule module = null;
165 switch (fType) {
166 case STATE_SYSTEM:
167 module = new XmlStateSystemModule();
168 XmlStateSystemModule ssModule = (XmlStateSystemModule) module;
169 module.setId(analysisid);
170 ssModule.setXmlFile(new Path(fSourceFile.getAbsolutePath()));
171
172 /*
173 * FIXME: There is no way to know if a module is automatic, so we
174 * default to true
175 */
176 ssModule.setAutomatic(true);
177
178 break;
179 default:
180 break;
181
182 }
183 if (module != null) {
184 module.setTrace(trace);
185 TmfAnalysisManager.analysisModuleCreated(module);
186 }
187
188 return module;
189 }
190
191 }
This page took 0.036713 seconds and 5 git commands to generate.