+
+ private TmfTraceType() {
+ init();
+ }
+
+ /**
+ * The import utils instance
+ *
+ * @return the import utils instance
+ * @since 2.0
+ */
+ public static TmfTraceType getInstance() {
+ if (fInstance == null) {
+ fInstance = new TmfTraceType();
+ }
+ return fInstance;
+ }
+
+ // ------------------------------------------------------------------
+ // Get trace types
+ // ------------------------------------------------------------------
+
+ /**
+ * Returns a list of "category : tracetype , ..."
+ *
+ * @return returns a list of "category : tracetype , ..."
+ * @since 2.0
+ */
+ public String[] getAvailableTraceTypes() {
+
+ // Generate the list of Category:TraceType to populate the ComboBox
+ List<String> traceTypes = new ArrayList<String>();
+
+ List<String> customTypes = getCustomTraceTypes();
+ for (String key : this.fTraceTypes.keySet()) {
+ TraceTypeHelper tt = this.fTraceTypes.get(key);
+ traceTypes.add(tt.getCategoryName() + " : " + tt.getName()); //$NON-NLS-1$
+ }
+ traceTypes.addAll(customTypes);
+
+ // Format result
+ return traceTypes.toArray(new String[traceTypes.size()]);
+ }
+
+ /**
+ * Gets the custom trace types (custom text and friends)
+ *
+ * @param type
+ * the type to get (Text, xml or other...)
+ * @return the list of custom trace types
+ * @since 2.0
+ */
+ public static List<String> getCustomTraceTypes(String type) {
+ List<String> traceTypes = new ArrayList<String>();
+ if (type.equals(CUSTOM_TXT_CATEGORY)) {
+ for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
+ String traceTypeName = def.definitionName;
+ traceTypes.add(traceTypeName);
+ }
+ }
+ if (type.equals(CUSTOM_XML_CATEGORY)) {
+ for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
+ String traceTypeName = def.definitionName;
+ traceTypes.add(traceTypeName);
+ }
+ }
+ return traceTypes;
+ }
+
+ /**
+ * Gets all the custom trace types
+ *
+ * @return the list of custom trace types
+ * @since 2.0
+ */
+ public List<String> getCustomTraceTypes() {
+ List<String> traceTypes = new ArrayList<String>();
+ // remove the customTraceTypes
+ final String[] keySet = fTraceTypes.keySet().toArray(new String[0]);
+ for (String key : keySet) {
+ if (fTraceTypes.get(key).getCategoryName().equals(CUSTOM_TXT_CATEGORY) || fTraceTypes.get(key).getCategoryName().equals(CUSTOM_XML_CATEGORY)) {
+ fTraceTypes.remove(key);
+ }
+ }
+
+ // add the custom trace types
+ for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
+ String traceTypeName = CUSTOM_TXT_CATEGORY + " : " + def.definitionName; //$NON-NLS-1$
+ TraceTypeHelper tt = new TraceTypeHelper(traceTypeName, CUSTOM_TXT_CATEGORY, def.definitionName, null);
+ fTraceTypes.put(traceTypeName, tt);
+ traceTypes.add(traceTypeName);
+ }
+ for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
+ String traceTypeName = CUSTOM_XML_CATEGORY + " : " + def.definitionName; //$NON-NLS-1$
+ TraceTypeHelper tt = new TraceTypeHelper(traceTypeName, CUSTOM_TXT_CATEGORY, def.definitionName, null);
+ fTraceTypes.put(traceTypeName, tt);
+ traceTypes.add(traceTypeName);
+ }
+ return traceTypes;
+ }
+
+ /**
+ * Gets a trace type for a given canonical id
+ *
+ * @param id
+ * the ID of the trace
+ * @return the return type
+ * @since 2.0
+ */
+ public TraceTypeHelper getTraceType(String id) {
+ return fTraceTypes.get(id);
+ }
+
+ private void populateCategoriesAndTraceTypes() {
+ if (fTraceTypes.isEmpty()) {
+ // Populate the Categories and Trace Types
+ IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
+ for (IConfigurationElement ce : config) {
+ String elementName = ce.getName();
+ if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
+ String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
+ fTraceTypeAttributes.put(traceTypeId, ce);
+ } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
+ String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
+ fTraceCategories.put(categoryId, ce);
+ }
+ }
+ // create the trace types
+ for (String typeId : fTraceTypeAttributes.keySet()) {
+ IConfigurationElement ce = fTraceTypeAttributes.get(typeId);
+ final String category = getCategory(ce);
+ final String attribute = ce.getAttribute(TmfTraceType.NAME_ATTR);
+ ITmfTrace trace = null;
+ try {
+ trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
+ } catch (CoreException e) {
+ }
+ TraceTypeHelper tt = new TraceTypeHelper(typeId, category, attribute, trace);
+ fTraceTypes.put(typeId, tt);
+ }
+ }
+ }
+
+ private String getCategory(IConfigurationElement ce) {
+ final String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
+ if (categoryId != null) {
+ IConfigurationElement category = fTraceCategories.get(categoryId);
+ if (category != null && !category.getName().equals("")) { //$NON-NLS-1$
+ return category.getAttribute(TmfTraceType.NAME_ATTR);
+ }
+ }
+ return "[no category]"; //$NON-NLS-1$
+ }
+
+ /**
+ * Returns the list of trace categories
+ *
+ * @return the list of trace categories
+ * @since 2.0
+ */
+ public List<String> getTraceCategories() {
+ List<String> categoryNames = new ArrayList<String>();
+ for (String key : fTraceTypes.keySet()) {
+ final String categoryName = fTraceTypes.get(key).getCategoryName();
+ if (!categoryNames.contains(categoryName)) {
+ categoryNames.add(categoryName);
+ }
+ }
+ return categoryNames;
+ }
+
+ /**
+ * Get the trace types
+ *
+ * @param category
+ * the category to lookup
+ * @return the trace types
+ * @since 2.0
+ */
+
+ public List<TraceTypeHelper> getTraceTypes(String category) {
+ List<TraceTypeHelper> traceNames = new ArrayList<TraceTypeHelper>();
+ for (String key : fTraceTypes.keySet()) {
+ final String categoryName = fTraceTypes.get(key).getCategoryName();
+ if (categoryName.equals(category)) {
+ traceNames.add(fTraceTypes.get(key));
+ }
+ }
+ return traceNames;
+ }
+
+ private void init() {
+ populateCategoriesAndTraceTypes();
+ getCustomTraceTypes();
+
+ }
+
+ private static List<File> isolateTraces(List<FileSystemElement> selectedResources) {
+
+ List<File> traces = new ArrayList<File>();
+
+ // Get the selection
+ Iterator<FileSystemElement> resources = selectedResources.iterator();
+
+ // Get the sorted list of unique entries
+ Map<String, File> fileSystemObjects = new HashMap<String, File>();
+ while (resources.hasNext()) {
+ File resource = (File) resources.next().getFileSystemObject();
+ String key = resource.getAbsolutePath();
+ fileSystemObjects.put(key, resource);
+ }
+ List<String> files = new ArrayList<String>(fileSystemObjects.keySet());
+ Collections.sort(files);
+
+ // After sorting, traces correspond to the unique prefixes
+ String prefix = null;
+ for (int i = 0; i < files.size(); i++) {
+ File file = fileSystemObjects.get(files.get(i));
+ String name = file.getAbsolutePath();
+ if (prefix == null || !name.startsWith(prefix)) {
+ prefix = name; // new prefix
+ traces.add(file);
+ }
+ }
+
+ return traces;
+ }
+
+ /**
+ * Validate a trace type
+ *
+ * @param traceTypeName
+ * the trace category (canonical name)
+ * @param fileName
+ * the file name (and path)
+ * @return true if the trace is of a valid type
+ * @since 2.0
+ */
+ public boolean validate(String traceTypeName, String fileName) {
+ if (traceTypeName != null && !"".equals(traceTypeName) && //$NON-NLS-1$
+ !traceTypeName.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY) && !traceTypeName.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY)) {
+ if (!fTraceTypes.get(traceTypeName).validate(fileName)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Validate a trace
+ *
+ * @param traceToValidate
+ * the trace category (canonical name)
+ * @return true if the trace is of a valid type
+ * @since 2.0
+ */
+ public boolean validate(TraceValidationHelper traceToValidate) {
+ return validate(traceToValidate.getTraceType(), traceToValidate.getTraceToScan());
+ }
+
+ /**
+ * validate list of traces with a tracetype
+ *
+ * @param traceTypeName
+ * the trace category (canonical name)
+ * @param selectedResources
+ * List of traces to validate
+ * @return true if all the traces are valid
+ * @since 2.0
+ */
+ public boolean validateTrace(String traceTypeName, List<FileSystemElement> selectedResources) {
+ List<File> traces = isolateTraces(selectedResources);
+ return validateTraceFiles(traceTypeName, traces);
+ }
+
+ /**
+ * Validate a list of files with a tracetype
+ *
+ * @param traceTypeName
+ * the trace category (canonical name)
+ * @param traces
+ * the list of files to check if they are trace
+ * @return true if all the traces are valid
+ * @since 2.0
+ */
+ public boolean validateTraceFiles(String traceTypeName, List<File> traces) {
+ if (traceTypeName != null && !"".equals(traceTypeName) && //$NON-NLS-1$
+ !traceTypeName.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY) && !traceTypeName.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY)) {
+ for (File trace : traces) {
+ validate(traceTypeName, trace.getAbsolutePath());
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Get a configuration element for a given name
+ *
+ * @param traceType
+ * the name canonical
+ * @return the configuration element, can be null
+ * @since 2.0
+ */
+ public IConfigurationElement getTraceAttributes(String traceType) {
+ return fTraceTypeAttributes.get(traceType);
+ }
+
+ /**
+ * Find the id of a trace type by its parameters
+ *
+ * @param category
+ * like "ctf" or "custom text"
+ * @param traceType
+ * like "kernel"
+ * @return an id like "org.eclipse.linuxtools.blabla...
+ * @since 2.0
+ */
+ public String getTraceTypeId(String category, String traceType) {
+ for (String key : fTraceTypes.keySet()) {
+ if (fTraceTypes.get(key).getCategoryName().equals(category.trim()) && fTraceTypes.get(key).getName().equals(traceType.trim())) {
+ return key;
+ }
+ }
+ return null;
+ }