1 /*******************************************************************************
2 * Copyright (c) 2014, 2016 École Polytechnique de Montréal
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
;
13 import java
.util
.Collections
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
18 import org
.eclipse
.core
.runtime
.Path
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
21 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.Activator
;
22 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.pattern
.stateprovider
.XmlPatternAnalysis
;
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
.requirements
.TmfAnalysisRequirement
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.ITmfPropertiesProvider
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
34 import org
.osgi
.framework
.Bundle
;
35 import org
.w3c
.dom
.Element
;
38 * Analysis module helpers for modules provided by XML files
40 * @author Geneviève Bastien
43 public class TmfAnalysisModuleHelperXml
implements IAnalysisModuleHelper
, ITmfPropertiesProvider
{
46 * The types of analysis that can be XML-defined
48 public enum XmlAnalysisModuleType
{
49 /** Analysis will be of type XmlStateSystemModule */
53 * Analysis will be of type XmlPatternAnalysisModule
60 private final File fSourceFile
;
61 private final Element fSourceElement
;
62 private final XmlAnalysisModuleType fType
;
68 * The XML file containing the details of this analysis
70 * The XML node element
72 * The type of analysis
74 public TmfAnalysisModuleHelperXml(File xmlFile
, Element node
, XmlAnalysisModuleType type
) {
75 fSourceFile
= xmlFile
;
76 fSourceElement
= node
;
81 public String
getId() {
83 * The attribute ID cannot be null because the XML has been validated
86 return fSourceElement
.getAttribute(TmfXmlStrings
.ID
);
90 public String
getName() {
92 /* Label may be available in XML header */
93 List
<Element
> head
= XmlUtils
.getChildElements(fSourceElement
, TmfXmlStrings
.HEAD
);
94 if (head
.size() == 1) {
95 List
<Element
> labels
= XmlUtils
.getChildElements(head
.get(0), TmfXmlStrings
.LABEL
);
96 if (!labels
.isEmpty()) {
97 name
= labels
.get(0).getAttribute(TmfXmlStrings
.VALUE
);
108 public boolean isAutomatic() {
116 public boolean appliesToExperiment() {
121 public String
getHelpText() {
122 return ""; //$NON-NLS-1$
126 public String
getHelpText(@NonNull ITmfTrace trace
) {
127 return ""; //$NON-NLS-1$
131 public String
getIcon() {
136 public Bundle
getBundle() {
137 return Activator
.getDefault().getBundle();
141 public boolean appliesToTraceType(Class
<?
extends ITmfTrace
> traceClass
) {
142 /* Trace types may be available in XML header */
143 List
<Element
> head
= XmlUtils
.getChildElements(fSourceElement
, TmfXmlStrings
.HEAD
);
144 if (head
.size() != 1) {
148 * TODO: Test with custom trace types
150 List
<Element
> elements
= XmlUtils
.getChildElements(head
.get(0), TmfXmlStrings
.TRACETYPE
);
151 if (elements
.isEmpty()) {
155 for (Element element
: elements
) {
156 String traceTypeId
= element
.getAttribute(TmfXmlStrings
.ID
);
157 traceTypeId
= TmfTraceType
.buildCompatibilityTraceTypeId(traceTypeId
);
158 TraceTypeHelper helper
= TmfTraceType
.getTraceType(traceTypeId
);
159 if ((helper
!= null) && helper
.getTrace().getClass().isAssignableFrom(traceClass
)) {
167 public Iterable
<Class
<?
extends ITmfTrace
>> getValidTraceTypes() {
168 return Collections
.EMPTY_SET
;
172 public Iterable
<TmfAnalysisRequirement
> getAnalysisRequirements() {
173 return Collections
.EMPTY_SET
;
177 public IAnalysisModule
newModule(ITmfTrace trace
) throws TmfAnalysisException
{
178 String analysisid
= getId();
179 IAnalysisModule module
= null;
182 module
= new XmlStateSystemModule();
183 XmlStateSystemModule ssModule
= (XmlStateSystemModule
) module
;
184 module
.setId(analysisid
);
185 ssModule
.setXmlFile(new Path(fSourceFile
.getAbsolutePath()));
188 * FIXME: There is no way to know if a module is automatic, so we
191 ssModule
.setAutomatic(true);
195 module
= new XmlPatternAnalysis();
196 module
.setName(getName());
197 module
.setId(analysisid
);
198 XmlPatternAnalysis paModule
= (XmlPatternAnalysis
) module
;
199 paModule
.setXmlFile(new Path(fSourceFile
.getAbsolutePath()));
202 * FIXME: Maybe the pattern analysis should not be automatic.
204 paModule
.setAutomatic(true);
211 if (module
!= null) {
212 if (module
.setTrace(trace
)) {
213 TmfAnalysisManager
.analysisModuleCreated(module
);
215 /* The analysis does not apply to the trace, dispose of the module */
224 // ------------------------------------------------------------------------
225 // ITmfPropertiesProvider
226 // ------------------------------------------------------------------------
229 public @NonNull Map
<@NonNull String
, @NonNull String
> getProperties() {
230 Map
<@NonNull String
, @NonNull String
> properties
= new HashMap
<>();
231 properties
.put(NonNullUtils
.checkNotNull(Messages
.XmlModuleHelper_PropertyFile
), fSourceFile
.getName());
232 properties
.put(NonNullUtils
.checkNotNull(Messages
.XmlModuleHelper_PropertyType
), fType
.name());