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