TMF: Validate if an analysis can be executed using the requirements
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.ui / src / org / eclipse / linuxtools / tmf / analysis / xml / ui / module / TmfAnalysisModuleHelperXml.java
CommitLineData
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
13package org.eclipse.linuxtools.tmf.analysis.xml.ui.module;
14
15import java.io.File;
63c43609 16import java.util.Collections;
aa89118c 17import java.util.List;
97ed0cf0
FW
18
19import org.eclipse.core.runtime.Path;
20import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.Activator;
aa89118c 21import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils;
97ed0cf0
FW
22import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
23import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
24import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule;
25import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
048bde85 26import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
63c43609 27import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisRequirement;
97ed0cf0 28import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
aa89118c
GB
29import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
30import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
97ed0cf0 31import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
97ed0cf0
FW
32import org.osgi.framework.Bundle;
33import org.w3c.dom.Element;
97ed0cf0
FW
34
35/**
36 * Analysis module helpers for modules provided by XML files
37 *
38 * @author Geneviève Bastien
39 */
40public 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;
97ed0cf0
FW
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;
97ed0cf0
FW
68 }
69
70 @Override
71 public String getId() {
f47f1bbe 72 return fSourceElement.getAttribute(TmfXmlStrings.ID);
97ed0cf0
FW
73 }
74
75 @Override
76 public String getName() {
aa89118c
GB
77 String name = null;
78 /* Label may be available in XML header */
79 List<Element> head = XmlUtils.getChildElements(fSourceElement, TmfXmlStrings.HEAD);
80 if (head.size() == 1) {
81 List<Element> labels = XmlUtils.getChildElements(head.get(0), TmfXmlStrings.LABEL);
82 if (!labels.isEmpty()) {
83 name = labels.get(0).getAttribute(TmfXmlStrings.VALUE);
84 }
85 }
86
97ed0cf0
FW
87 if (name == null) {
88 name = getId();
89 }
90 return name;
91 }
92
93 @Override
94 public boolean isAutomatic() {
95 return false;
96 }
97
98 @Override
99 public String getHelpText() {
100 return new String();
101 }
102
103 @Override
104 public String getIcon() {
105 return null;
106 }
107
108 @Override
109 public Bundle getBundle() {
110 return Activator.getDefault().getBundle();
111 }
112
113 @Override
aa89118c 114 public boolean appliesToTraceType(Class<? extends ITmfTrace> traceClass) {
97ed0cf0 115 /* Trace types may be available in XML header */
aa89118c
GB
116 List<Element> head = XmlUtils.getChildElements(fSourceElement, TmfXmlStrings.HEAD);
117 if (head.size() != 1) {
118 return true;
119 }
120 /*
121 * TODO: Test with custom trace types
122 */
123 List<Element> elements = XmlUtils.getChildElements(head.get(0), TmfXmlStrings.TRACETYPE);
124 if (elements.isEmpty()) {
97ed0cf0
FW
125 return true;
126 }
127
aa89118c
GB
128 for (Element element : elements) {
129 String traceTypeId = element.getAttribute(TmfXmlStrings.ID);
130 TraceTypeHelper helper = TmfTraceType.getInstance().getTraceType(traceTypeId);
131 if ((helper != null) && helper.getTrace().getClass().isAssignableFrom(traceClass)) {
132 return true;
133 }
134 }
135 return false;
97ed0cf0
FW
136 }
137
63c43609
MR
138 @Override
139 public Iterable<Class<? extends ITmfTrace>> getValidTraceTypes() {
140 return Collections.EMPTY_SET;
141 }
142
143 @Override
144 public Iterable<TmfAnalysisRequirement> getAnalysisRequirements() {
145 return Collections.EMPTY_SET;
146 }
147
97ed0cf0
FW
148 @Override
149 public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException {
150 String analysisid = getId();
151 IAnalysisModule module = null;
152 switch (fType) {
153 case STATE_SYSTEM:
154 module = new XmlStateSystemModule();
155 XmlStateSystemModule ssModule = (XmlStateSystemModule) module;
156 module.setId(analysisid);
157 ssModule.setXmlFile(new Path(fSourceFile.getAbsolutePath()));
158
048bde85
GB
159 /*
160 * FIXME: There is no way to know if a module is automatic, so we
161 * default to true
162 */
163 ssModule.setAutomatic(true);
97ed0cf0 164
97ed0cf0
FW
165 break;
166 default:
167 break;
168
169 }
170 if (module != null) {
171 module.setTrace(trace);
048bde85 172 TmfAnalysisManager.analysisModuleCreated(module);
97ed0cf0
FW
173 }
174
175 return module;
176 }
177
178}
This page took 0.035197 seconds and 5 git commands to generate.