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