Make XML analysis using custom parsers backwards compatible
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / project / model / TmfTraceType.java
CommitLineData
4bf17f4a 1/*******************************************************************************
2b0005f0 2 * Copyright (c) 2011, 2015 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
c9b31f60 13 * Bernd Hufmann - Updated custom trace type ID handling
4bf17f4a 14 *******************************************************************************/
15
2bdf0193 16package org.eclipse.tracecompass.tmf.core.project.model;
4bf17f4a 17
d04ec5a7 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
47aafe74 55 /** Extension point ID */
a926c25c 56 public static final String TMF_TRACE_TYPE_ID = "org.eclipse.linuxtools.tmf.core.tracetype"; //$NON-NLS-1$
4bf17f4a 57
47aafe74 58 /** Extension point element 'Category' */
4bf17f4a 59 public static final String CATEGORY_ELEM = "category"; //$NON-NLS-1$
47aafe74
AM
60
61 /** Extension point element 'Type' */
4bf17f4a 62 public static final String TYPE_ELEM = "type"; //$NON-NLS-1$
47aafe74 63
1ec2ca01
GB
64 /** Extension point element 'Experiment' */
65 public static final String EXPERIMENT_ELEM = "experiment"; //$NON-NLS-1$
66
47aafe74 67 /** Extension point attribute 'ID' */
4bf17f4a 68 public static final String ID_ATTR = "id"; //$NON-NLS-1$
47aafe74
AM
69
70 /** Extension point attribute 'name' */
4bf17f4a 71 public static final String NAME_ATTR = "name"; //$NON-NLS-1$
47aafe74
AM
72
73 /** Extension point attribute 'category' */
4bf17f4a 74 public static final String CATEGORY_ATTR = "category"; //$NON-NLS-1$
47aafe74
AM
75
76 /** Extension point attribute 'trace_type' */
4bf17f4a 77 public static final String TRACE_TYPE_ATTR = "trace_type"; //$NON-NLS-1$
47aafe74
AM
78
79 /** Extension point attribute 'event_type' */
4bf17f4a 80 public static final String EVENT_TYPE_ATTR = "event_type"; //$NON-NLS-1$
47aafe74 81
1ec2ca01
GB
82 /** Extension point attribute 'experiment_type' */
83 public static final String EXPERIMENT_TYPE_ATTR = "experiment_type"; //$NON-NLS-1$
84
d8aba2e2
BH
85 /** Extension point attribute 'isDirectory' */
86 public static final String IS_DIR_ATTR = "isDirectory"; //$NON-NLS-1$
87
1ec2ca01
GB
88 /** Default experiment type */
89 public static final String DEFAULT_EXPERIMENT_TYPE = "org.eclipse.linuxtools.tmf.core.experiment.generic"; //$NON-NLS-1$
90
d04ec5a7
MK
91 // The mapping of available trace type IDs to their corresponding
92 // configuration element
a4a116c3
PT
93 private static final Map<String, IConfigurationElement> TRACE_TYPE_ATTRIBUTES = new HashMap<>();
94 private static final Map<String, IConfigurationElement> TRACE_CATEGORIES = new HashMap<>();
95 private static final Map<String, TraceTypeHelper> TRACE_TYPES = new LinkedHashMap<>();
d04ec5a7 96
a4a116c3
PT
97 static {
98 populateCategoriesAndTraceTypes();
99 populateCustomTraceTypes();
100 }
d04ec5a7 101
376fdfbd
GB
102 /**
103 * Enum to say whether a type applies to a trace or experiment
104 *
105 * @author Geneviève Bastien
106 */
107 public enum TraceElementType {
108 /** Trace type applies to trace */
109 TRACE,
110 /** Trace type applies to experiment */
111 EXPERIMENT,
112 }
113
a4a116c3
PT
114 // ------------------------------------------------------------------
115 // Constructor
116 // ------------------------------------------------------------------
117
118 private TmfTraceType() {
119 }
120
121 // ------------------------------------------------------------------
122 // Operations
123 // ------------------------------------------------------------------
124
d04ec5a7
MK
125 /**
126 * Retrieves the category name from the platform extension registry based on
127 * the category ID
128 *
129 * @param categoryId
130 * The category ID
b544077e
BH
131 * @return the category name or empty string if not found
132 */
4bf17f4a 133 public static String getCategoryName(String categoryId) {
134 IConfigurationElement[] elements = Platform.getExtensionRegistry()
135 .getConfigurationElementsFor(TMF_TRACE_TYPE_ID);
136 for (IConfigurationElement element : elements) {
09154cfe 137 if (element.getName().equals(CATEGORY_ELEM) && element.getAttribute(ID_ATTR).equals(categoryId)) {
4bf17f4a 138 return element.getAttribute(NAME_ATTR);
139 }
140 }
141 return ""; //$NON-NLS-1$
142 }
143
b544077e 144 /**
cfd22ad0 145 * Retrieves all configuration elements from the platform extension registry
376fdfbd 146 * for the trace type extension that apply to traces and not experiments.
cfd22ad0
MD
147 *
148 * @return an array of trace type configuration elements
b544077e 149 */
4bf17f4a 150 public static IConfigurationElement[] getTypeElements() {
151 IConfigurationElement[] elements = Platform.getExtensionRegistry()
152 .getConfigurationElementsFor(TMF_TRACE_TYPE_ID);
507b1336 153 List<IConfigurationElement> typeElements = new LinkedList<>();
4bf17f4a 154 for (IConfigurationElement element : elements) {
155 if (element.getName().equals(TYPE_ELEM)) {
156 typeElements.add(element);
157 }
158 }
beae214a 159 return typeElements.toArray(new IConfigurationElement[typeElements.size()]);
4bf17f4a 160 }
d04ec5a7 161
47aafe74
AM
162 /**
163 * Get an iterable view of the existing trace type IDs.
164 *
165 * @return The currently registered trace type IDs
166 */
a4a116c3
PT
167 public static Iterable<String> getTraceTypeIDs() {
168 return TRACE_TYPES.keySet();
47aafe74
AM
169 }
170
252c602c
BH
171 /**
172 * Get an iterable view of the existing trace type helpers.
173 *
174 * @return The currently registered trace type helpers
175 */
a4a116c3
PT
176 public static Iterable<TraceTypeHelper> getTraceTypeHelpers() {
177 return TRACE_TYPES.values();
252c602c
BH
178 }
179
d04ec5a7 180 /**
2b0005f0 181 * Returns a list of trace type labels "category : name", ...
d04ec5a7 182 *
376fdfbd
GB
183 * Returns only trace types, not experiment types
184 *
2b0005f0 185 * @return a list of trace type labels
d04ec5a7 186 */
a4a116c3 187 public static String[] getAvailableTraceTypes() {
252c602c
BH
188 return getAvailableTraceTypes(null);
189 }
190
191 /**
2b0005f0 192 * Returns a list of trace type labels "category : name", ... sorted by given comparator.
252c602c 193 *
376fdfbd
GB
194 * Returns only trace types, not experiment types
195 *
252c602c
BH
196 * @param comparator
197 * Comparator class (type String) or null for alphabetical order.
2b0005f0 198 * @return a list of trace type labels sorted according to the given comparator
252c602c 199 */
a4a116c3 200 public static String[] getAvailableTraceTypes(Comparator<String> comparator) {
d04ec5a7
MK
201
202 // Generate the list of Category:TraceType to populate the ComboBox
507b1336 203 List<String> traceTypes = new ArrayList<>();
d04ec5a7 204
a4a116c3
PT
205 for (String key : TRACE_TYPES.keySet()) {
206 TraceTypeHelper tt = TRACE_TYPES.get(key);
376fdfbd 207 if (!tt.isExperimentType()) {
2b0005f0 208 traceTypes.add(tt.getLabel());
376fdfbd 209 }
d04ec5a7 210 }
d04ec5a7 211
252c602c
BH
212 if (comparator == null) {
213 Collections.sort(traceTypes);
214 } else {
215 Collections.sort(traceTypes, comparator);
216 }
217
d04ec5a7
MK
218 // Format result
219 return traceTypes.toArray(new String[traceTypes.size()]);
220 }
221
222 /**
cfe2f40c 223<<<<<<< Upstream, based on 3097c4b013b2ea6ae2f6e8c1fa76e5aeaf2a1cbf
d04ec5a7
MK
224 * Gets the custom trace types (custom text and friends)
225 *
226 * @param type
227 * the type to get (Text, xml or other...)
228 * @return the list of custom trace types
d04ec5a7
MK
229 */
230 public static List<String> getCustomTraceTypes(String type) {
507b1336 231 List<String> traceTypes = new ArrayList<>();
c9b31f60 232 if (type.equals(CustomTxtTraceDefinition.CUSTOM_TXT_CATEGORY)) {
d04ec5a7
MK
233 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
234 String traceTypeName = def.definitionName;
235 traceTypes.add(traceTypeName);
236 }
237 }
c9b31f60 238 if (type.equals(CustomXmlTraceDefinition.CUSTOM_XML_CATEGORY)) {
d04ec5a7
MK
239 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
240 String traceTypeName = def.definitionName;
241 traceTypes.add(traceTypeName);
242 }
243 }
244 return traceTypes;
245 }
246
247 /**
cfe2f40c
BH
248=======
249>>>>>>> 930e20d Remove deprecated and unused metheds related to custom traces
d04ec5a7
MK
250 * Gets all the custom trace types
251 *
252 * @return the list of custom trace types
d04ec5a7 253 */
52885aeb
PT
254 public static List<String> getCustomTraceTypes() {
255
507b1336 256 List<String> traceTypes = new ArrayList<>();
52885aeb
PT
257 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
258 String traceTypeName = def.definitionName;
259 traceTypes.add(traceTypeName);
260 }
261 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
262 String traceTypeName = def.definitionName;
263 traceTypes.add(traceTypeName);
d04ec5a7 264 }
52885aeb
PT
265 return traceTypes;
266 }
d04ec5a7 267
a4a116c3 268 private static void populateCustomTraceTypes() {
d04ec5a7
MK
269 // add the custom trace types
270 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
2b0005f0
PT
271 CustomTxtTrace trace = new CustomTxtTrace(def);
272 String traceTypeId = trace.getTraceTypeId();
4b3b667b 273 TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, def.categoryName, def.definitionName, trace, false, TraceElementType.TRACE);
a4a116c3 274 TRACE_TYPES.put(traceTypeId, tt);
72807127
BH
275 // Deregister trace as signal handler because it is only used for validation
276 TmfSignalManager.deregister(trace);
d04ec5a7
MK
277 }
278 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
2b0005f0
PT
279 CustomXmlTrace trace = new CustomXmlTrace(def);
280 String traceTypeId = trace.getTraceTypeId();
4b3b667b 281 TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, def.categoryName, def.definitionName, trace, false, TraceElementType.TRACE);
a4a116c3 282 TRACE_TYPES.put(traceTypeId, tt);
72807127
BH
283 // Deregister trace as signal handler because it is only used for validation
284 TmfSignalManager.deregister(trace);
d04ec5a7 285 }
52885aeb
PT
286 }
287
288 /**
289 * Add or replace a custom trace type
290 *
cfe2f40c 291<<<<<<< Upstream, based on 3097c4b013b2ea6ae2f6e8c1fa76e5aeaf2a1cbf
52885aeb
PT
292 * @param category
293 * The custom parser category
294 * @param definitionName
295 * The custom parser definition name to add or replace
4b3b667b 296 * @deprecated Use {@link #addCustomTraceType(Class, String, String)}
52885aeb 297 */
4b3b667b 298 @Deprecated
a4a116c3 299 public static void addCustomTraceType(String category, String definitionName) {
c9b31f60 300 if (category.equals(CustomTxtTraceDefinition.CUSTOM_TXT_CATEGORY)) {
4b3b667b 301 addCustomTraceType(CustomTxtTrace.class, category, definitionName);
c9b31f60 302 } else if (category.equals(CustomXmlTraceDefinition.CUSTOM_XML_CATEGORY)) {
4b3b667b
PT
303 addCustomTraceType(CustomXmlTrace.class, category, definitionName);
304 }
305 }
306
307 /**
308 * Add or replace a custom trace type
309 *
cfe2f40c
BH
310=======
311>>>>>>> 930e20d Remove deprecated and unused metheds related to custom traces
4b3b667b
PT
312 * @param traceClass
313 * The custom trace class, either {@link CustomTxtTrace} or
314 * {@link CustomXmlTrace}
315 * @param category
316 * The custom parser category
317 * @param definitionName
318 * The custom parser definition name to add or replace
319 */
320 public static void addCustomTraceType(Class<? extends ITmfTrace> traceClass, String category, String definitionName) {
52885aeb 321 String traceTypeId = null;
c9b31f60 322 ITmfTrace trace = null;
52885aeb 323
4b3b667b 324 if (traceClass.equals(CustomTxtTrace.class)) {
332527a4 325 CustomTxtTraceDefinition def = CustomTxtTraceDefinition.load(category, definitionName);
52885aeb
PT
326 if (def != null) {
327 trace = new CustomTxtTrace(def);
2b0005f0 328 traceTypeId = trace.getTraceTypeId();
52885aeb 329 }
4b3b667b 330 } else if (traceClass.equals(CustomXmlTrace.class)) {
332527a4 331 CustomXmlTraceDefinition def = CustomXmlTraceDefinition.load(category, definitionName);
52885aeb
PT
332 if (def != null) {
333 trace = new CustomXmlTrace(def);
2b0005f0 334 traceTypeId = trace.getTraceTypeId();
52885aeb
PT
335 }
336 }
337
338 if (traceTypeId != null && trace != null) {
a4a116c3 339 TraceTypeHelper helper = TRACE_TYPES.get(traceTypeId);
52885aeb
PT
340 if (helper != null) {
341 helper.getTrace().dispose();
342 }
376fdfbd 343 TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, category, definitionName, trace, false, TraceElementType.TRACE);
a4a116c3 344 TRACE_TYPES.put(traceTypeId, tt);
52885aeb
PT
345 // Deregister trace as signal handler because it is only used for validation
346 TmfSignalManager.deregister(trace);
347 }
348 }
349
4b3b667b
PT
350 /**
351 * Remove a custom trace type
352 *
353 * @param traceClass
354 * The custom trace class, either {@link CustomTxtTrace} or
355 * {@link CustomXmlTrace}
356 * @param category
357 * The custom parser category
358 * @param definitionName
359 * The custom parser definition name to add or replace
360 */
361 public static void removeCustomTraceType(Class<? extends ITmfTrace> traceClass, String category, String definitionName) {
c9b31f60
BH
362 String traceTypeId = null;
363 if (traceClass.equals(CustomTxtTrace.class)) {
364 traceTypeId = CustomTxtTrace.buildTraceTypeId(category, definitionName);
365 } else if (traceClass.equals(CustomXmlTrace.class)) {
366 traceTypeId = CustomXmlTrace.buildTraceTypeId(category, definitionName);
367 }
368 if (traceTypeId != null) {
369 TraceTypeHelper helper = TRACE_TYPES.remove(traceTypeId);
370 if (helper != null) {
371 helper.getTrace().dispose();
372 }
52885aeb 373 }
d04ec5a7
MK
374 }
375
376 /**
377 * Gets a trace type for a given canonical id
378 *
379 * @param id
380 * the ID of the trace
381 * @return the return type
d04ec5a7 382 */
a4a116c3
PT
383 public static TraceTypeHelper getTraceType(String id) {
384 return TRACE_TYPES.get(id);
d04ec5a7
MK
385 }
386
a4a116c3
PT
387 private static void populateCategoriesAndTraceTypes() {
388 if (TRACE_TYPES.isEmpty()) {
d04ec5a7
MK
389 // Populate the Categories and Trace Types
390 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
391 for (IConfigurationElement ce : config) {
392 String elementName = ce.getName();
393 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
394 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
a4a116c3 395 TRACE_TYPE_ATTRIBUTES.put(traceTypeId, ce);
d04ec5a7
MK
396 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
397 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
a4a116c3 398 TRACE_CATEGORIES.put(categoryId, ce);
376fdfbd
GB
399 } else if (elementName.equals(TmfTraceType.EXPERIMENT_ELEM)) {
400 String experimentTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
a4a116c3 401 TRACE_TYPE_ATTRIBUTES.put(experimentTypeId, ce);
d04ec5a7
MK
402 }
403 }
404 // create the trace types
a4a116c3
PT
405 for (String typeId : TRACE_TYPE_ATTRIBUTES.keySet()) {
406 IConfigurationElement ce = TRACE_TYPE_ATTRIBUTES.get(typeId);
d04ec5a7
MK
407 final String category = getCategory(ce);
408 final String attribute = ce.getAttribute(TmfTraceType.NAME_ATTR);
409 ITmfTrace trace = null;
376fdfbd 410 TraceElementType elementType = TraceElementType.TRACE;
d04ec5a7 411 try {
376fdfbd
GB
412 if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) {
413 trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
414 } else if (ce.getName().equals(TmfTraceType.EXPERIMENT_ELEM)) {
415 trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.EXPERIMENT_TYPE_ATTR);
416 elementType = TraceElementType.EXPERIMENT;
417 }
a8ddd783
GB
418 if (trace == null) {
419 break;
376fdfbd 420 }
a8ddd783
GB
421 // Deregister trace as signal handler because it is only
422 // used for validation
423 TmfSignalManager.deregister(trace);
424
425 final String dirString = ce.getAttribute(TmfTraceType.IS_DIR_ATTR);
426 boolean isDir = Boolean.parseBoolean(dirString);
427
428 TraceTypeHelper tt = new TraceTypeHelper(typeId, category, attribute, trace, isDir, elementType);
a4a116c3 429 TRACE_TYPES.put(typeId, tt);
d04ec5a7 430 } catch (CoreException e) {
d77f31da 431 Activator.logError("Unexpected error during populating trace types", e); //$NON-NLS-1$
d04ec5a7 432 }
d8aba2e2 433
d04ec5a7
MK
434 }
435 }
436 }
437
a4a116c3 438 private static String getCategory(IConfigurationElement ce) {
d04ec5a7
MK
439 final String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
440 if (categoryId != null) {
a4a116c3 441 IConfigurationElement category = TRACE_CATEGORIES.get(categoryId);
d04ec5a7
MK
442 if (category != null && !category.getName().equals("")) { //$NON-NLS-1$
443 return category.getAttribute(TmfTraceType.NAME_ATTR);
444 }
445 }
2b0005f0 446 return ""; //$NON-NLS-1$
d04ec5a7
MK
447 }
448
449 /**
450 * Returns the list of trace categories
451 *
452 * @return the list of trace categories
d04ec5a7 453 */
a4a116c3 454 public static List<String> getTraceCategories() {
507b1336 455 List<String> categoryNames = new ArrayList<>();
a4a116c3
PT
456 for (String key : TRACE_TYPES.keySet()) {
457 final String categoryName = TRACE_TYPES.get(key).getCategoryName();
d04ec5a7
MK
458 if (!categoryNames.contains(categoryName)) {
459 categoryNames.add(categoryName);
460 }
461 }
462 return categoryNames;
463 }
464
465 /**
376fdfbd
GB
466 * Get the trace type helper classes from category name. Return only the
467 * trace types, not the experiment types
d04ec5a7 468 *
cd9821de
BH
469 * @param categoryName
470 * the categoryName to lookup
471 * @return a list of trace type helper classes {@link TraceTypeHelper}
d04ec5a7 472 */
a4a116c3 473 public static List<TraceTypeHelper> getTraceTypes(String categoryName) {
507b1336 474 List<TraceTypeHelper> traceNames = new ArrayList<>();
a4a116c3
PT
475 for (String key : TRACE_TYPES.keySet()) {
476 if (!TRACE_TYPES.get(key).isExperimentType()) {
477 final String storedCategoryName = TRACE_TYPES.get(key).getCategoryName();
376fdfbd 478 if (storedCategoryName.equals(categoryName)) {
a4a116c3 479 traceNames.add(TRACE_TYPES.get(key));
376fdfbd 480 }
d04ec5a7
MK
481 }
482 }
483 return traceNames;
484 }
485
d04ec5a7
MK
486 /**
487 * Validate a trace type
488 *
489 * @param traceTypeName
490 * the trace category (canonical name)
491 * @param fileName
492 * the file name (and path)
493 * @return true if the trace is of a valid type
d04ec5a7 494 */
a4a116c3 495 public static boolean validate(String traceTypeName, String fileName) {
76fccfb0 496 if (traceTypeName != null && !traceTypeName.isEmpty()) {
a4a116c3 497 final TraceTypeHelper traceTypeHelper = TRACE_TYPES.get(traceTypeName);
0621dbae 498 if (traceTypeHelper == null || !traceTypeHelper.validate(fileName).isOK()) {
d04ec5a7
MK
499 return false;
500 }
501 }
502 return true;
503 }
504
505 /**
506 * Validate a trace
507 *
508 * @param traceToValidate
509 * the trace category (canonical name)
510 * @return true if the trace is of a valid type
d04ec5a7 511 */
a4a116c3 512 public static boolean validate(TraceValidationHelper traceToValidate) {
d04ec5a7
MK
513 return validate(traceToValidate.getTraceType(), traceToValidate.getTraceToScan());
514 }
515
d04ec5a7
MK
516 /**
517 * Get a configuration element for a given name
518 *
519 * @param traceType
520 * the name canonical
521 * @return the configuration element, can be null
d04ec5a7 522 */
a4a116c3
PT
523 public static IConfigurationElement getTraceAttributes(String traceType) {
524 return TRACE_TYPE_ATTRIBUTES.get(traceType);
d04ec5a7
MK
525 }
526
527 /**
2b0005f0 528 * Find the id of a trace type by its label "category : name"
d04ec5a7 529 *
2b0005f0
PT
530 * @param label
531 * the trace type label
532 * @return the trace type id
d04ec5a7 533 */
2b0005f0 534 public static String getTraceTypeId(String label) {
a4a116c3 535 for (String key : TRACE_TYPES.keySet()) {
2b0005f0 536 if (TRACE_TYPES.get(key).getLabel().equals(label)) {
d04ec5a7
MK
537 return key;
538 }
539 }
540 return null;
541 }
76fccfb0 542
252c602c 543 /**
d8aba2e2
BH
544 * Checks if a trace is a valid directory trace
545 * @param path
252c602c 546 * the file name (and path)
d8aba2e2 547 * @return <code>true</code> if the trace is a valid directory trace else <code>false</code>
252c602c 548 */
a4a116c3 549 public static boolean isDirectoryTrace(String path) {
d8aba2e2
BH
550 final Iterable<TraceTypeHelper> traceTypeHelpers = getTraceTypeHelpers();
551 for (TraceTypeHelper traceTypeHelper : traceTypeHelpers) {
552 if (traceTypeHelper.isDirectoryTraceType() &&
a8ddd783 553 traceTypeHelper.validate(path).isOK()) {
d8aba2e2
BH
554 return true;
555 }
252c602c
BH
556 }
557 return false;
558 }
559
560 /**
561 * @param traceType
562 * the trace type
d8aba2e2 563 * @return <code>true</code> it is a directory trace type else else <code>false</code>
252c602c 564 */
a4a116c3 565 public static boolean isDirectoryTraceType(String traceType) {
d8aba2e2
BH
566 if (traceType != null) {
567 TraceTypeHelper traceTypeHelper = getTraceType(traceType);
568 if (traceTypeHelper != null) {
569 return traceTypeHelper.isDirectoryTraceType();
570 }
1dec1830 571 return false;
252c602c 572 }
d8aba2e2 573 throw new IllegalArgumentException("Invalid trace type string: " + traceType); //$NON-NLS-1$
252c602c 574 }
d8aba2e2 575
4b3b667b
PT
576 /**
577 * Get the trace type id for a resource
578 *
579 * @param resource
580 * the resource
581 * @return the trace type id or null if it is not set
582 * @throws CoreException
583 * if the trace type id cannot be accessed
a465519a 584 * @since 3.2
4b3b667b
PT
585 */
586 public static String getTraceTypeId(IResource resource) throws CoreException {
587 String traceTypeId = resource.getPersistentProperties().get(TmfCommonConstants.TRACETYPE);
e86f7ac4
BH
588 return buildCompatibilityTraceTypeId(traceTypeId);
589 }
590
591 /**
592 * This methods builds a trace type ID from a given ID taking into
593 * consideration any format changes that were done for the IDs of custom
594 * text or XML traces. For example, such format change took place when
595 * moving to Trace Compass. Trace type IDs that are part of the plug-in
596 * extension for trace types won't be changed.
597 *
598 * This method is useful for IDs that were persisted in the workspace before
599 * the format changes (e.g. in the persistent properties of a trace
600 * resource).
601 *
602 * It ensures backwards compatibility of the workspace for custom text and
603 * XML traces.
604 *
605 * @param traceTypeId
606 * the legacy trace type ID
607 * @return the trace type ID in Trace Compass format
608 */
609 public static String buildCompatibilityTraceTypeId(String traceTypeId) {
4b3b667b
PT
610 // Fix custom trace type id with old class name or without category name for backward compatibility
611 if (traceTypeId != null) {
c9b31f60
BH
612 String newTraceType = CustomTxtTrace.buildCompatibilityTraceTypeId(traceTypeId);
613 if (newTraceType.equals(traceTypeId)) {
614 newTraceType = CustomXmlTrace.buildCompatibilityTraceTypeId(traceTypeId);
4b3b667b 615 }
c9b31f60 616 return newTraceType;
4b3b667b
PT
617 }
618 return traceTypeId;
619 }
c9b31f60 620
4bf17f4a 621}
This page took 0.101199 seconds and 5 git commands to generate.