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