Commit | Line | Data |
---|---|---|
4bf17f4a | 1 | /******************************************************************************* |
1ec2ca01 | 2 | * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal |
cfd22ad0 | 3 | * |
4bf17f4a | 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 | |
cfd22ad0 | 8 | * |
4bf17f4a | 9 | * Contributors: |
10 | * Patrick Tasse - Initial API and implementation | |
d04ec5a7 | 11 | * Matthew Khouzam - Added import functionalities |
1ec2ca01 | 12 | * Geneviève Bastien - Added support for experiment types |
4bf17f4a | 13 | *******************************************************************************/ |
14 | ||
47aafe74 | 15 | package org.eclipse.linuxtools.tmf.core.project.model; |
4bf17f4a | 16 | |
d04ec5a7 MK |
17 | import java.io.File; |
18 | import java.util.ArrayList; | |
252c602c BH |
19 | import java.util.Collections; |
20 | import java.util.Comparator; | |
d04ec5a7 | 21 | import java.util.HashMap; |
d04ec5a7 | 22 | import java.util.LinkedHashMap; |
4bf17f4a | 23 | import java.util.LinkedList; |
24 | import java.util.List; | |
d04ec5a7 | 25 | import java.util.Map; |
4bf17f4a | 26 | |
05627bda | 27 | import org.eclipse.core.runtime.CoreException; |
4bf17f4a | 28 | import org.eclipse.core.runtime.IConfigurationElement; |
29 | import org.eclipse.core.runtime.Platform; | |
47aafe74 AM |
30 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTrace; |
31 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTraceDefinition; | |
32 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTrace; | |
33 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition; | |
72807127 | 34 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; |
d04ec5a7 | 35 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
4bf17f4a | 36 | |
b544077e | 37 | /** |
d04ec5a7 MK |
38 | * Utility class for accessing TMF trace type extensions from the platform's |
39 | * extensions registry. | |
cfd22ad0 | 40 | * |
b544077e BH |
41 | * @version 1.0 |
42 | * @author Patrick Tasse | |
d04ec5a7 | 43 | * @author Matthew Khouzam |
47aafe74 | 44 | * @since 3.0 |
b544077e | 45 | */ |
d04ec5a7 | 46 | public final class TmfTraceType { |
4bf17f4a | 47 | |
a2d29ca1 MK |
48 | private static final char SEPARATOR = ':'; |
49 | ||
47aafe74 | 50 | /** Extension point ID */ |
a926c25c | 51 | public static final String TMF_TRACE_TYPE_ID = "org.eclipse.linuxtools.tmf.core.tracetype"; //$NON-NLS-1$ |
4bf17f4a | 52 | |
47aafe74 | 53 | /** Extension point element 'Category' */ |
4bf17f4a | 54 | public static final String CATEGORY_ELEM = "category"; //$NON-NLS-1$ |
47aafe74 AM |
55 | |
56 | /** Extension point element 'Type' */ | |
4bf17f4a | 57 | public static final String TYPE_ELEM = "type"; //$NON-NLS-1$ |
47aafe74 | 58 | |
1ec2ca01 GB |
59 | /** Extension point element 'Experiment' */ |
60 | public static final String EXPERIMENT_ELEM = "experiment"; //$NON-NLS-1$ | |
61 | ||
47aafe74 | 62 | /** Extension point attribute 'ID' */ |
4bf17f4a | 63 | public static final String ID_ATTR = "id"; //$NON-NLS-1$ |
47aafe74 AM |
64 | |
65 | /** Extension point attribute 'name' */ | |
4bf17f4a | 66 | public static final String NAME_ATTR = "name"; //$NON-NLS-1$ |
47aafe74 AM |
67 | |
68 | /** Extension point attribute 'category' */ | |
4bf17f4a | 69 | public static final String CATEGORY_ATTR = "category"; //$NON-NLS-1$ |
47aafe74 AM |
70 | |
71 | /** Extension point attribute 'trace_type' */ | |
4bf17f4a | 72 | public static final String TRACE_TYPE_ATTR = "trace_type"; //$NON-NLS-1$ |
47aafe74 AM |
73 | |
74 | /** Extension point attribute 'event_type' */ | |
4bf17f4a | 75 | public static final String EVENT_TYPE_ATTR = "event_type"; //$NON-NLS-1$ |
47aafe74 | 76 | |
1ec2ca01 GB |
77 | /** Extension point attribute 'experiment_type' */ |
78 | public static final String EXPERIMENT_TYPE_ATTR = "experiment_type"; //$NON-NLS-1$ | |
79 | ||
d8aba2e2 BH |
80 | /** Extension point attribute 'isDirectory' */ |
81 | public static final String IS_DIR_ATTR = "isDirectory"; //$NON-NLS-1$ | |
82 | ||
b544077e | 83 | /** |
d04ec5a7 MK |
84 | * Custom text label used internally and therefore should not be |
85 | * externalized | |
d04ec5a7 MK |
86 | */ |
87 | public static final String CUSTOM_TXT_CATEGORY = "Custom Text"; //$NON-NLS-1$ | |
47aafe74 | 88 | |
d04ec5a7 MK |
89 | /** |
90 | * Custom XML label used internally and therefore should not be externalized | |
d04ec5a7 MK |
91 | */ |
92 | public static final String CUSTOM_XML_CATEGORY = "Custom XML"; //$NON-NLS-1$ | |
93 | ||
1ec2ca01 GB |
94 | /** Default experiment type */ |
95 | public static final String DEFAULT_EXPERIMENT_TYPE = "org.eclipse.linuxtools.tmf.core.experiment.generic"; //$NON-NLS-1$ | |
96 | ||
d04ec5a7 MK |
97 | // The mapping of available trace type IDs to their corresponding |
98 | // configuration element | |
507b1336 AM |
99 | private final Map<String, IConfigurationElement> fTraceTypeAttributes = new HashMap<>(); |
100 | private final Map<String, IConfigurationElement> fTraceCategories = new HashMap<>(); | |
101 | private final Map<String, TraceTypeHelper> fTraceTypes = new LinkedHashMap<>(); | |
d04ec5a7 MK |
102 | |
103 | private static TmfTraceType fInstance = null; | |
104 | ||
376fdfbd GB |
105 | /** |
106 | * Enum to say whether a type applies to a trace or experiment | |
107 | * | |
108 | * @author Geneviève Bastien | |
109 | */ | |
110 | public enum TraceElementType { | |
111 | /** Trace type applies to trace */ | |
112 | TRACE, | |
113 | /** Trace type applies to experiment */ | |
114 | EXPERIMENT, | |
115 | } | |
116 | ||
d04ec5a7 MK |
117 | /** |
118 | * Retrieves the category name from the platform extension registry based on | |
119 | * the category ID | |
120 | * | |
121 | * @param categoryId | |
122 | * The category ID | |
b544077e BH |
123 | * @return the category name or empty string if not found |
124 | */ | |
4bf17f4a | 125 | public static String getCategoryName(String categoryId) { |
126 | IConfigurationElement[] elements = Platform.getExtensionRegistry() | |
127 | .getConfigurationElementsFor(TMF_TRACE_TYPE_ID); | |
128 | for (IConfigurationElement element : elements) { | |
129 | if (element.getName().equals(CATEGORY_ELEM) && categoryId.equals(element.getAttribute(ID_ATTR))) { | |
130 | return element.getAttribute(NAME_ATTR); | |
131 | } | |
132 | } | |
133 | return ""; //$NON-NLS-1$ | |
134 | } | |
135 | ||
b544077e | 136 | /** |
cfd22ad0 | 137 | * Retrieves all configuration elements from the platform extension registry |
376fdfbd | 138 | * for the trace type extension that apply to traces and not experiments. |
cfd22ad0 MD |
139 | * |
140 | * @return an array of trace type configuration elements | |
b544077e | 141 | */ |
4bf17f4a | 142 | public static IConfigurationElement[] getTypeElements() { |
143 | IConfigurationElement[] elements = Platform.getExtensionRegistry() | |
144 | .getConfigurationElementsFor(TMF_TRACE_TYPE_ID); | |
507b1336 | 145 | List<IConfigurationElement> typeElements = new LinkedList<>(); |
4bf17f4a | 146 | for (IConfigurationElement element : elements) { |
147 | if (element.getName().equals(TYPE_ELEM)) { | |
148 | typeElements.add(element); | |
149 | } | |
150 | } | |
beae214a | 151 | return typeElements.toArray(new IConfigurationElement[typeElements.size()]); |
4bf17f4a | 152 | } |
d04ec5a7 MK |
153 | |
154 | private TmfTraceType() { | |
376fdfbd GB |
155 | populateCategoriesAndTraceTypes(); |
156 | populateCustomTraceTypes(); | |
d04ec5a7 MK |
157 | } |
158 | ||
159 | /** | |
160 | * The import utils instance | |
161 | * | |
162 | * @return the import utils instance | |
d04ec5a7 MK |
163 | */ |
164 | public static TmfTraceType getInstance() { | |
165 | if (fInstance == null) { | |
166 | fInstance = new TmfTraceType(); | |
167 | } | |
168 | return fInstance; | |
169 | } | |
170 | ||
171 | // ------------------------------------------------------------------ | |
172 | // Get trace types | |
173 | // ------------------------------------------------------------------ | |
174 | ||
47aafe74 AM |
175 | /** |
176 | * Retrieve the TraceTypeHelper for a given trace type ID | |
177 | * | |
178 | * @param id | |
179 | * The trace type ID | |
180 | * @return The corresponding TraceTypeHelper, or null if there is none for | |
181 | * the specified ID | |
182 | */ | |
183 | public TraceTypeHelper getTraceTypeHelper(String id) { | |
184 | return fTraceTypes.get(id); | |
185 | } | |
186 | ||
187 | /** | |
188 | * Get an iterable view of the existing trace type IDs. | |
189 | * | |
190 | * @return The currently registered trace type IDs | |
191 | */ | |
192 | public Iterable<String> getTraceTypeIDs() { | |
193 | return fTraceTypes.keySet(); | |
194 | } | |
195 | ||
252c602c BH |
196 | /** |
197 | * Get an iterable view of the existing trace type helpers. | |
198 | * | |
199 | * @return The currently registered trace type helpers | |
200 | */ | |
201 | public Iterable<TraceTypeHelper> getTraceTypeHelpers() { | |
202 | return fTraceTypes.values(); | |
203 | } | |
204 | ||
d04ec5a7 | 205 | /** |
a2d29ca1 | 206 | * Returns a list of "category:tracetype , ..." |
d04ec5a7 | 207 | * |
376fdfbd GB |
208 | * Returns only trace types, not experiment types |
209 | * | |
a2d29ca1 | 210 | * @return returns a list of "category:tracetype , ..." |
d04ec5a7 MK |
211 | */ |
212 | public String[] getAvailableTraceTypes() { | |
252c602c BH |
213 | return getAvailableTraceTypes(null); |
214 | } | |
215 | ||
216 | /** | |
217 | * Returns a list of "category:tracetype , ..." sorted by given comparator. | |
218 | * | |
376fdfbd GB |
219 | * Returns only trace types, not experiment types |
220 | * | |
252c602c BH |
221 | * @param comparator |
222 | * Comparator class (type String) or null for alphabetical order. | |
223 | * @return sorted list according to the given comparator | |
224 | */ | |
225 | public String[] getAvailableTraceTypes(Comparator<String> comparator) { | |
d04ec5a7 MK |
226 | |
227 | // Generate the list of Category:TraceType to populate the ComboBox | |
507b1336 | 228 | List<String> traceTypes = new ArrayList<>(); |
d04ec5a7 | 229 | |
d04ec5a7 MK |
230 | for (String key : this.fTraceTypes.keySet()) { |
231 | TraceTypeHelper tt = this.fTraceTypes.get(key); | |
376fdfbd GB |
232 | if (!tt.isExperimentType()) { |
233 | traceTypes.add(tt.getCategoryName() + SEPARATOR + tt.getName()); | |
234 | } | |
d04ec5a7 | 235 | } |
d04ec5a7 | 236 | |
252c602c BH |
237 | if (comparator == null) { |
238 | Collections.sort(traceTypes); | |
239 | } else { | |
240 | Collections.sort(traceTypes, comparator); | |
241 | } | |
242 | ||
d04ec5a7 MK |
243 | // Format result |
244 | return traceTypes.toArray(new String[traceTypes.size()]); | |
245 | } | |
246 | ||
247 | /** | |
248 | * Gets the custom trace types (custom text and friends) | |
249 | * | |
250 | * @param type | |
251 | * the type to get (Text, xml or other...) | |
252 | * @return the list of custom trace types | |
d04ec5a7 MK |
253 | */ |
254 | public static List<String> getCustomTraceTypes(String type) { | |
507b1336 | 255 | List<String> traceTypes = new ArrayList<>(); |
d04ec5a7 MK |
256 | if (type.equals(CUSTOM_TXT_CATEGORY)) { |
257 | for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) { | |
258 | String traceTypeName = def.definitionName; | |
259 | traceTypes.add(traceTypeName); | |
260 | } | |
261 | } | |
262 | if (type.equals(CUSTOM_XML_CATEGORY)) { | |
263 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) { | |
264 | String traceTypeName = def.definitionName; | |
265 | traceTypes.add(traceTypeName); | |
266 | } | |
267 | } | |
268 | return traceTypes; | |
269 | } | |
270 | ||
271 | /** | |
272 | * Gets all the custom trace types | |
273 | * | |
274 | * @return the list of custom trace types | |
d04ec5a7 | 275 | */ |
52885aeb PT |
276 | public static List<String> getCustomTraceTypes() { |
277 | ||
507b1336 | 278 | List<String> traceTypes = new ArrayList<>(); |
52885aeb PT |
279 | for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) { |
280 | String traceTypeName = def.definitionName; | |
281 | traceTypes.add(traceTypeName); | |
282 | } | |
283 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) { | |
284 | String traceTypeName = def.definitionName; | |
285 | traceTypes.add(traceTypeName); | |
d04ec5a7 | 286 | } |
52885aeb PT |
287 | return traceTypes; |
288 | } | |
d04ec5a7 | 289 | |
52885aeb | 290 | private void populateCustomTraceTypes() { |
d04ec5a7 MK |
291 | // add the custom trace types |
292 | for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) { | |
26e33e67 | 293 | String traceTypeId = CustomTxtTrace.class.getCanonicalName() + SEPARATOR + def.definitionName; |
72807127 | 294 | ITmfTrace trace = new CustomTxtTrace(def); |
376fdfbd | 295 | TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, CUSTOM_TXT_CATEGORY, def.definitionName, trace, false, TraceElementType.TRACE); |
26e33e67 | 296 | fTraceTypes.put(traceTypeId, tt); |
72807127 BH |
297 | // Deregister trace as signal handler because it is only used for validation |
298 | TmfSignalManager.deregister(trace); | |
d04ec5a7 MK |
299 | } |
300 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) { | |
26e33e67 | 301 | String traceTypeId = CustomXmlTrace.class.getCanonicalName() + SEPARATOR + def.definitionName; |
72807127 | 302 | ITmfTrace trace = new CustomXmlTrace(def); |
376fdfbd | 303 | TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, CUSTOM_XML_CATEGORY, def.definitionName, trace, false, TraceElementType.TRACE); |
26e33e67 | 304 | fTraceTypes.put(traceTypeId, tt); |
72807127 BH |
305 | // Deregister trace as signal handler because it is only used for validation |
306 | TmfSignalManager.deregister(trace); | |
d04ec5a7 | 307 | } |
52885aeb PT |
308 | } |
309 | ||
310 | /** | |
311 | * Add or replace a custom trace type | |
312 | * | |
313 | * @param category | |
314 | * The custom parser category | |
315 | * @param definitionName | |
316 | * The custom parser definition name to add or replace | |
52885aeb PT |
317 | */ |
318 | public void addCustomTraceType(String category, String definitionName) { | |
319 | String traceTypeId = null; | |
320 | ITmfTrace trace = null; | |
321 | ||
322 | if (category.equals(CUSTOM_TXT_CATEGORY)) { | |
323 | traceTypeId = CustomTxtTrace.class.getCanonicalName() + SEPARATOR + definitionName; | |
324 | CustomTxtTraceDefinition def = CustomTxtTraceDefinition.load(definitionName); | |
325 | if (def != null) { | |
326 | trace = new CustomTxtTrace(def); | |
327 | } | |
328 | } else if (category.equals(CUSTOM_XML_CATEGORY)) { | |
329 | traceTypeId = CustomXmlTrace.class.getCanonicalName() + SEPARATOR + definitionName; | |
330 | CustomXmlTraceDefinition def = CustomXmlTraceDefinition.load(definitionName); | |
331 | if (def != null) { | |
332 | trace = new CustomXmlTrace(def); | |
333 | } | |
334 | } | |
335 | ||
336 | if (traceTypeId != null && trace != null) { | |
337 | TraceTypeHelper helper = fTraceTypes.get(traceTypeId); | |
338 | if (helper != null) { | |
339 | helper.getTrace().dispose(); | |
340 | } | |
376fdfbd | 341 | TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, category, definitionName, trace, false, TraceElementType.TRACE); |
52885aeb PT |
342 | fTraceTypes.put(traceTypeId, tt); |
343 | // Deregister trace as signal handler because it is only used for validation | |
344 | TmfSignalManager.deregister(trace); | |
345 | } | |
346 | } | |
347 | ||
348 | /** | |
349 | * Remove a custom trace type | |
350 | * | |
351 | * @param category | |
352 | * The custom parser category | |
353 | * @param definitionName | |
354 | * The custom parser definition name to add or replace | |
52885aeb PT |
355 | */ |
356 | public void removeCustomTraceType(String category, String definitionName) { | |
357 | if (category.equals(CUSTOM_TXT_CATEGORY)) { | |
358 | String traceTypeId = CustomTxtTrace.class.getCanonicalName() + SEPARATOR + definitionName; | |
359 | TraceTypeHelper helper = fTraceTypes.remove(traceTypeId); | |
360 | if (helper != null) { | |
361 | helper.getTrace().dispose(); | |
362 | } | |
363 | } else if (category.equals(CUSTOM_XML_CATEGORY)) { | |
364 | String traceTypeId = CustomXmlTrace.class.getCanonicalName() + SEPARATOR + definitionName; | |
365 | TraceTypeHelper helper = fTraceTypes.remove(traceTypeId); | |
366 | if (helper != null) { | |
367 | helper.getTrace().dispose(); | |
368 | } | |
369 | } | |
d04ec5a7 MK |
370 | } |
371 | ||
372 | /** | |
373 | * Gets a trace type for a given canonical id | |
374 | * | |
375 | * @param id | |
376 | * the ID of the trace | |
377 | * @return the return type | |
d04ec5a7 MK |
378 | */ |
379 | public TraceTypeHelper getTraceType(String id) { | |
380 | return fTraceTypes.get(id); | |
381 | } | |
382 | ||
383 | private void populateCategoriesAndTraceTypes() { | |
384 | if (fTraceTypes.isEmpty()) { | |
385 | // Populate the Categories and Trace Types | |
386 | IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID); | |
387 | for (IConfigurationElement ce : config) { | |
388 | String elementName = ce.getName(); | |
389 | if (elementName.equals(TmfTraceType.TYPE_ELEM)) { | |
390 | String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR); | |
391 | fTraceTypeAttributes.put(traceTypeId, ce); | |
392 | } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) { | |
393 | String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR); | |
394 | fTraceCategories.put(categoryId, ce); | |
376fdfbd GB |
395 | } else if (elementName.equals(TmfTraceType.EXPERIMENT_ELEM)) { |
396 | String experimentTypeId = ce.getAttribute(TmfTraceType.ID_ATTR); | |
397 | fTraceTypeAttributes.put(experimentTypeId, ce); | |
d04ec5a7 MK |
398 | } |
399 | } | |
400 | // create the trace types | |
401 | for (String typeId : fTraceTypeAttributes.keySet()) { | |
402 | IConfigurationElement ce = fTraceTypeAttributes.get(typeId); | |
403 | final String category = getCategory(ce); | |
404 | final String attribute = ce.getAttribute(TmfTraceType.NAME_ATTR); | |
405 | ITmfTrace trace = null; | |
376fdfbd | 406 | TraceElementType elementType = TraceElementType.TRACE; |
d04ec5a7 | 407 | try { |
376fdfbd GB |
408 | if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) { |
409 | trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR); | |
410 | } else if (ce.getName().equals(TmfTraceType.EXPERIMENT_ELEM)) { | |
411 | trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.EXPERIMENT_TYPE_ATTR); | |
412 | elementType = TraceElementType.EXPERIMENT; | |
413 | } | |
a8ddd783 GB |
414 | if (trace == null) { |
415 | break; | |
376fdfbd | 416 | } |
a8ddd783 GB |
417 | // Deregister trace as signal handler because it is only |
418 | // used for validation | |
419 | TmfSignalManager.deregister(trace); | |
420 | ||
421 | final String dirString = ce.getAttribute(TmfTraceType.IS_DIR_ATTR); | |
422 | boolean isDir = Boolean.parseBoolean(dirString); | |
423 | ||
424 | TraceTypeHelper tt = new TraceTypeHelper(typeId, category, attribute, trace, isDir, elementType); | |
425 | fTraceTypes.put(typeId, tt); | |
d04ec5a7 MK |
426 | } catch (CoreException e) { |
427 | } | |
d8aba2e2 | 428 | |
d04ec5a7 MK |
429 | } |
430 | } | |
431 | } | |
432 | ||
433 | private String getCategory(IConfigurationElement ce) { | |
434 | final String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR); | |
435 | if (categoryId != null) { | |
436 | IConfigurationElement category = fTraceCategories.get(categoryId); | |
437 | if (category != null && !category.getName().equals("")) { //$NON-NLS-1$ | |
438 | return category.getAttribute(TmfTraceType.NAME_ATTR); | |
439 | } | |
440 | } | |
441 | return "[no category]"; //$NON-NLS-1$ | |
442 | } | |
443 | ||
444 | /** | |
445 | * Returns the list of trace categories | |
446 | * | |
447 | * @return the list of trace categories | |
d04ec5a7 MK |
448 | */ |
449 | public List<String> getTraceCategories() { | |
507b1336 | 450 | List<String> categoryNames = new ArrayList<>(); |
d04ec5a7 MK |
451 | for (String key : fTraceTypes.keySet()) { |
452 | final String categoryName = fTraceTypes.get(key).getCategoryName(); | |
453 | if (!categoryNames.contains(categoryName)) { | |
454 | categoryNames.add(categoryName); | |
455 | } | |
456 | } | |
457 | return categoryNames; | |
458 | } | |
459 | ||
460 | /** | |
376fdfbd GB |
461 | * Get the trace type helper classes from category name. Return only the |
462 | * trace types, not the experiment types | |
d04ec5a7 | 463 | * |
cd9821de BH |
464 | * @param categoryName |
465 | * the categoryName to lookup | |
466 | * @return a list of trace type helper classes {@link TraceTypeHelper} | |
d04ec5a7 | 467 | */ |
cd9821de | 468 | public List<TraceTypeHelper> getTraceTypes(String categoryName) { |
507b1336 | 469 | List<TraceTypeHelper> traceNames = new ArrayList<>(); |
d04ec5a7 | 470 | for (String key : fTraceTypes.keySet()) { |
376fdfbd GB |
471 | if (!fTraceTypes.get(key).isExperimentType()) { |
472 | final String storedCategoryName = fTraceTypes.get(key).getCategoryName(); | |
473 | if (storedCategoryName.equals(categoryName)) { | |
474 | traceNames.add(fTraceTypes.get(key)); | |
475 | } | |
d04ec5a7 MK |
476 | } |
477 | } | |
478 | return traceNames; | |
479 | } | |
480 | ||
d04ec5a7 MK |
481 | /** |
482 | * Validate a trace type | |
483 | * | |
484 | * @param traceTypeName | |
485 | * the trace category (canonical name) | |
486 | * @param fileName | |
487 | * the file name (and path) | |
488 | * @return true if the trace is of a valid type | |
d04ec5a7 MK |
489 | */ |
490 | public boolean validate(String traceTypeName, String fileName) { | |
76fccfb0 | 491 | if (traceTypeName != null && !traceTypeName.isEmpty()) { |
de2501f8 | 492 | final TraceTypeHelper traceTypeHelper = fTraceTypes.get(traceTypeName); |
a8ddd783 | 493 | if (!traceTypeHelper.validate(fileName).isOK()) { |
d04ec5a7 MK |
494 | return false; |
495 | } | |
496 | } | |
497 | return true; | |
498 | } | |
499 | ||
500 | /** | |
501 | * Validate a trace | |
502 | * | |
503 | * @param traceToValidate | |
504 | * the trace category (canonical name) | |
505 | * @return true if the trace is of a valid type | |
d04ec5a7 MK |
506 | */ |
507 | public boolean validate(TraceValidationHelper traceToValidate) { | |
508 | return validate(traceToValidate.getTraceType(), traceToValidate.getTraceToScan()); | |
509 | } | |
510 | ||
d04ec5a7 MK |
511 | /** |
512 | * Validate a list of files with a tracetype | |
513 | * | |
514 | * @param traceTypeName | |
515 | * the trace category (canonical name) | |
516 | * @param traces | |
517 | * the list of files to check if they are trace | |
518 | * @return true if all the traces are valid | |
d04ec5a7 MK |
519 | */ |
520 | public boolean validateTraceFiles(String traceTypeName, List<File> traces) { | |
521 | if (traceTypeName != null && !"".equals(traceTypeName) && //$NON-NLS-1$ | |
522 | !traceTypeName.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY) && !traceTypeName.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY)) { | |
523 | for (File trace : traces) { | |
edd51555 MK |
524 | if (!validate(traceTypeName, trace.getAbsolutePath())) { |
525 | return false; | |
526 | } | |
d04ec5a7 MK |
527 | } |
528 | } | |
529 | return true; | |
530 | } | |
531 | ||
532 | /** | |
533 | * Get a configuration element for a given name | |
534 | * | |
535 | * @param traceType | |
536 | * the name canonical | |
537 | * @return the configuration element, can be null | |
d04ec5a7 MK |
538 | */ |
539 | public IConfigurationElement getTraceAttributes(String traceType) { | |
540 | return fTraceTypeAttributes.get(traceType); | |
541 | } | |
542 | ||
543 | /** | |
544 | * Find the id of a trace type by its parameters | |
545 | * | |
546 | * @param category | |
547 | * like "ctf" or "custom text" | |
548 | * @param traceType | |
549 | * like "kernel" | |
550 | * @return an id like "org.eclipse.linuxtools.blabla... | |
d04ec5a7 MK |
551 | */ |
552 | public String getTraceTypeId(String category, String traceType) { | |
553 | for (String key : fTraceTypes.keySet()) { | |
554 | if (fTraceTypes.get(key).getCategoryName().equals(category.trim()) && fTraceTypes.get(key).getName().equals(traceType.trim())) { | |
555 | return key; | |
556 | } | |
557 | } | |
558 | return null; | |
559 | } | |
76fccfb0 | 560 | |
76fccfb0 MK |
561 | /** |
562 | * Gets the custom trace type ID from the custom trace name | |
563 | * | |
564 | * @param traceType | |
565 | * The trace type in human form (category:name) | |
566 | * @return the trace type ID or null if the trace is not a custom one | |
76fccfb0 MK |
567 | */ |
568 | public static String getCustomTraceTypeId(String traceType) { | |
569 | String traceTypeId = null; | |
570 | ||
571 | // do custom trace stuff here | |
572 | String traceTypeToken[] = traceType.split(":", 2); //$NON-NLS-1$ | |
573 | if (traceTypeToken.length == 2) { | |
574 | final boolean startsWithTxt = traceType.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY); | |
575 | final boolean startsWithXML = traceType.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY); | |
576 | if (startsWithTxt) { | |
577 | traceTypeId = CustomTxtTrace.class.getCanonicalName() + SEPARATOR + traceTypeToken[1]; | |
578 | } else if (startsWithXML) { | |
579 | traceTypeId = CustomXmlTrace.class.getCanonicalName() + SEPARATOR + traceTypeToken[1]; | |
580 | } | |
581 | } | |
582 | return traceTypeId; | |
583 | } | |
584 | ||
585 | /** | |
47aafe74 AM |
586 | * Is the trace a custom (user-defined) trace type. These are the traces |
587 | * like : text and xml defined by the custom trace wizard. | |
76fccfb0 | 588 | * |
76fccfb0 | 589 | * @param traceType |
47aafe74 AM |
590 | * the trace type in human form (category:name) |
591 | * @return true if the trace is a custom type | |
76fccfb0 | 592 | */ |
47aafe74 AM |
593 | public static boolean isCustomTrace(String traceType) { |
594 | final boolean startsWithTxt = traceType.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY); | |
595 | final boolean startsWithXML = traceType.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY); | |
596 | return (startsWithTxt || startsWithXML); | |
76fccfb0 | 597 | } |
252c602c BH |
598 | |
599 | /** | |
d8aba2e2 BH |
600 | * Checks if a trace is a valid directory trace |
601 | * @param path | |
252c602c | 602 | * the file name (and path) |
d8aba2e2 | 603 | * @return <code>true</code> if the trace is a valid directory trace else <code>false</code> |
252c602c | 604 | */ |
d8aba2e2 BH |
605 | public boolean isDirectoryTrace(String path) { |
606 | final Iterable<TraceTypeHelper> traceTypeHelpers = getTraceTypeHelpers(); | |
607 | for (TraceTypeHelper traceTypeHelper : traceTypeHelpers) { | |
608 | if (traceTypeHelper.isDirectoryTraceType() && | |
a8ddd783 | 609 | traceTypeHelper.validate(path).isOK()) { |
d8aba2e2 BH |
610 | return true; |
611 | } | |
252c602c BH |
612 | } |
613 | return false; | |
614 | } | |
615 | ||
616 | /** | |
617 | * @param traceType | |
618 | * the trace type | |
d8aba2e2 | 619 | * @return <code>true</code> it is a directory trace type else else <code>false</code> |
252c602c BH |
620 | */ |
621 | public boolean isDirectoryTraceType(String traceType) { | |
d8aba2e2 BH |
622 | if (traceType != null) { |
623 | TraceTypeHelper traceTypeHelper = getTraceType(traceType); | |
624 | if (traceTypeHelper != null) { | |
625 | return traceTypeHelper.isDirectoryTraceType(); | |
626 | } | |
252c602c | 627 | } |
d8aba2e2 | 628 | throw new IllegalArgumentException("Invalid trace type string: " + traceType); //$NON-NLS-1$ |
252c602c | 629 | } |
d8aba2e2 | 630 | |
4bf17f4a | 631 | } |