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