| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014, 2016 É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 | |
| 10 | package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module; |
| 11 | |
| 12 | import java.io.File; |
| 13 | import java.util.Collections; |
| 14 | import java.util.HashMap; |
| 15 | import java.util.List; |
| 16 | import java.util.Map; |
| 17 | |
| 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.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
| 24 | import org.eclipse.tracecompass.internal.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.TmfAbstractAnalysisRequirement; |
| 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; |
| 36 | |
| 37 | /** |
| 38 | * Analysis module helpers for modules provided by XML files |
| 39 | * |
| 40 | * @author Geneviève Bastien |
| 41 | * @since 2.0 |
| 42 | */ |
| 43 | public class TmfAnalysisModuleHelperXml implements IAnalysisModuleHelper, ITmfPropertiesProvider { |
| 44 | |
| 45 | private static final String ICON_ANALYSIS = "/icons/analysis.png"; //$NON-NLS-1$ |
| 46 | |
| 47 | /** |
| 48 | * The types of analysis that can be XML-defined |
| 49 | */ |
| 50 | public enum XmlAnalysisModuleType { |
| 51 | /** Analysis will be of type XmlStateSystemModule */ |
| 52 | STATE_SYSTEM, |
| 53 | |
| 54 | /** |
| 55 | * Analysis will be of type XmlPatternAnalysisModule |
| 56 | * |
| 57 | * @since 2.0 |
| 58 | */ |
| 59 | PATTERN |
| 60 | } |
| 61 | |
| 62 | private final File fSourceFile; |
| 63 | private final Element fSourceElement; |
| 64 | private final XmlAnalysisModuleType fType; |
| 65 | |
| 66 | /** |
| 67 | * Constructor |
| 68 | * |
| 69 | * @param xmlFile |
| 70 | * The XML file containing the details of this analysis |
| 71 | * @param node |
| 72 | * The XML node element |
| 73 | * @param type |
| 74 | * The type of analysis |
| 75 | */ |
| 76 | public TmfAnalysisModuleHelperXml(File xmlFile, Element node, XmlAnalysisModuleType type) { |
| 77 | fSourceFile = xmlFile; |
| 78 | fSourceElement = node; |
| 79 | fType = type; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public String getId() { |
| 84 | /* |
| 85 | * The attribute ID cannot be null because the XML has been validated |
| 86 | * and it is mandatory |
| 87 | */ |
| 88 | return fSourceElement.getAttribute(TmfXmlStrings.ID); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public String getName() { |
| 93 | String name = null; |
| 94 | /* Label may be available in XML header */ |
| 95 | List<Element> head = XmlUtils.getChildElements(fSourceElement, TmfXmlStrings.HEAD); |
| 96 | if (head.size() == 1) { |
| 97 | List<Element> labels = XmlUtils.getChildElements(head.get(0), TmfXmlStrings.LABEL); |
| 98 | if (!labels.isEmpty()) { |
| 99 | name = labels.get(0).getAttribute(TmfXmlStrings.VALUE); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (name == null) { |
| 104 | name = getId(); |
| 105 | } |
| 106 | return name; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public boolean isAutomatic() { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @since 1.0 |
| 116 | */ |
| 117 | @Override |
| 118 | public boolean appliesToExperiment() { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public String getHelpText() { |
| 124 | return ""; //$NON-NLS-1$ |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String getHelpText(@NonNull ITmfTrace trace) { |
| 129 | return ""; //$NON-NLS-1$ |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String getIcon() { |
| 134 | return ICON_ANALYSIS; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Bundle getBundle() { |
| 139 | return Activator.getDefault().getBundle(); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public boolean appliesToTraceType(Class<? extends ITmfTrace> traceClass) { |
| 144 | /* Trace types may be available in XML header */ |
| 145 | List<Element> head = XmlUtils.getChildElements(fSourceElement, TmfXmlStrings.HEAD); |
| 146 | if (head.size() != 1) { |
| 147 | return true; |
| 148 | } |
| 149 | /* |
| 150 | * TODO: Test with custom trace types |
| 151 | */ |
| 152 | List<Element> elements = XmlUtils.getChildElements(head.get(0), TmfXmlStrings.TRACETYPE); |
| 153 | if (elements.isEmpty()) { |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | for (Element element : elements) { |
| 158 | String traceTypeId = element.getAttribute(TmfXmlStrings.ID); |
| 159 | traceTypeId = TmfTraceType.buildCompatibilityTraceTypeId(traceTypeId); |
| 160 | TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId); |
| 161 | if ((helper != null) && helper.getTrace().getClass().isAssignableFrom(traceClass)) { |
| 162 | return true; |
| 163 | } |
| 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Iterable<Class<? extends ITmfTrace>> getValidTraceTypes() { |
| 170 | return Collections.EMPTY_SET; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Iterable<TmfAbstractAnalysisRequirement> getAnalysisRequirements() { |
| 175 | return Collections.EMPTY_SET; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException { |
| 180 | String analysisid = getId(); |
| 181 | IAnalysisModule module = null; |
| 182 | switch (fType) { |
| 183 | case STATE_SYSTEM: |
| 184 | module = new XmlStateSystemModule(); |
| 185 | XmlStateSystemModule ssModule = (XmlStateSystemModule) module; |
| 186 | module.setId(analysisid); |
| 187 | ssModule.setXmlFile(new Path(fSourceFile.getAbsolutePath())); |
| 188 | |
| 189 | /* |
| 190 | * FIXME: There is no way to know if a module is automatic, so we |
| 191 | * default to true |
| 192 | */ |
| 193 | ssModule.setAutomatic(true); |
| 194 | |
| 195 | break; |
| 196 | case PATTERN: |
| 197 | module = new XmlPatternAnalysis(); |
| 198 | module.setName(getName()); |
| 199 | module.setId(analysisid); |
| 200 | XmlPatternAnalysis paModule = (XmlPatternAnalysis) module; |
| 201 | paModule.setXmlFile(new Path(fSourceFile.getAbsolutePath())); |
| 202 | |
| 203 | /* |
| 204 | * FIXME: Maybe the pattern analysis should not be automatic. |
| 205 | */ |
| 206 | paModule.setAutomatic(true); |
| 207 | |
| 208 | break; |
| 209 | default: |
| 210 | break; |
| 211 | |
| 212 | } |
| 213 | if (module != null) { |
| 214 | if (module.setTrace(trace)) { |
| 215 | TmfAnalysisManager.analysisModuleCreated(module); |
| 216 | } else { |
| 217 | /* The analysis does not apply to the trace, dispose of the module */ |
| 218 | module.dispose(); |
| 219 | module = null; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return module; |
| 224 | } |
| 225 | |
| 226 | // ------------------------------------------------------------------------ |
| 227 | // ITmfPropertiesProvider |
| 228 | // ------------------------------------------------------------------------ |
| 229 | |
| 230 | @Override |
| 231 | public @NonNull Map<@NonNull String, @NonNull String> getProperties() { |
| 232 | Map<@NonNull String, @NonNull String> properties = new HashMap<>(); |
| 233 | properties.put(NonNullUtils.checkNotNull(Messages.XmlModuleHelper_PropertyFile), fSourceFile.getName()); |
| 234 | properties.put(NonNullUtils.checkNotNull(Messages.XmlModuleHelper_PropertyType), fType.name()); |
| 235 | return properties; |
| 236 | } |
| 237 | |
| 238 | } |