2288bb33cca4cc09d81017f36a80659d18b9bbcc
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.ui / src / org / eclipse / linuxtools / 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.linuxtools.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.linuxtools.internal.tmf.analysis.xml.ui.Activator;
21 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils;
22 import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
23 import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
24 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule;
25 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
26 import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
27 import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisRequirement;
28 import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
29 import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
30 import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
31 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
32 import org.osgi.framework.Bundle;
33 import org.w3c.dom.Element;
34
35 /**
36 * Analysis module helpers for modules provided by XML files
37 *
38 * @author Geneviève Bastien
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 return fSourceElement.getAttribute(TmfXmlStrings.ID);
73 }
74
75 @Override
76 public String getName() {
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
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
114 public boolean appliesToTraceType(Class<? extends ITmfTrace> traceClass) {
115 /* Trace types may be available in XML header */
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()) {
125 return true;
126 }
127
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;
136 }
137
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
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
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);
164
165 break;
166 default:
167 break;
168
169 }
170 if (module != null) {
171 module.setTrace(trace);
172 TmfAnalysisManager.analysisModuleCreated(module);
173 }
174
175 return module;
176 }
177
178 }
This page took 0.04646 seconds and 4 git commands to generate.