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