TMF: Add trace stub for TMF unit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / parsers / custom / CustomXmlTraceDefinition.java
index 695fede026cc7d477b0adc4492fbf5ffe0607c35..1e69549ed7fa051029ccc2061696ec99daeb8ce1 100644 (file)
@@ -15,8 +15,10 @@ package org.eclipse.linuxtools.tmf.core.parsers.custom;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -460,11 +462,26 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
     }
 
     /**
-     * Load all the XML trace definitions in the default definitions file.
+     * Load all custom XML trace definitions, including the user-defined and
+     * default (built-in) parsers.
      *
      * @return The loaded trace definitions
      */
     public static CustomXmlTraceDefinition[] loadAll() {
+        return loadAll(true);
+    }
+
+    /**
+     * Load all custom XML trace definitions, including the user-defined and,
+     * optionally, the default (built-in) parsers.
+     *
+     * @param includeDefaults
+     *            if true, the default (built-in) parsers are included
+     *
+     * @return The loaded trace definitions
+     * @since 3.1
+     */
+    public static CustomXmlTraceDefinition[] loadAll(boolean includeDefaults) {
         File defaultFile = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
         File legacyFile = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY);
 
@@ -481,14 +498,19 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
         }
 
         Set<CustomXmlTraceDefinition> defs = new TreeSet<>(new Comparator<CustomXmlTraceDefinition>() {
-
             @Override
             public int compare(CustomXmlTraceDefinition o1, CustomXmlTraceDefinition o2) {
+                int result = o1.categoryName.compareTo(o2.categoryName);
+                if (result != 0) {
+                    return result;
+                }
                 return o1.definitionName.compareTo(o2.definitionName);
             }
         });
         defs.addAll(Arrays.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME)));
-        defs.addAll(Arrays.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME)));
+        if (includeDefaults) {
+            defs.addAll(Arrays.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME)));
+        }
         return defs.toArray(new CustomXmlTraceDefinition[0]);
     }
 
@@ -500,6 +522,27 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
      * @return The loaded trace definitions
      */
     public static CustomXmlTraceDefinition[] loadAll(String path) {
+        File file = new File(path);
+        if (!file.canRead()) {
+            return new CustomXmlTraceDefinition[0];
+        }
+        try (FileInputStream fis = new FileInputStream(file);) {
+            return loadAll(fis);
+        } catch (IOException e) {
+            Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
+        }
+        return new CustomXmlTraceDefinition[0];
+    }
+
+    /**
+     * Load all the XML trace definitions from the given stream
+     *
+     * @param stream
+     *            An input stream from which to read the definitions
+     * @return The loaded trace definitions
+     * @since 3.1
+     */
+    public static CustomXmlTraceDefinition[] loadAll(InputStream stream) {
         try {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
@@ -510,12 +553,7 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
             // The following catches xml parsing exceptions
             db.setErrorHandler(createErrorHandler());
 
-            File file = new File(path);
-            if (!file.canRead()) {
-                return new CustomXmlTraceDefinition[0];
-            }
-            Document doc = db.parse(file);
-
+            Document doc = db.parse(stream);
             Element root = doc.getDocumentElement();
             if (!root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
                 return new CustomXmlTraceDefinition[0];
@@ -534,7 +572,7 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
             }
             return defList.toArray(new CustomXmlTraceDefinition[0]);
         } catch (ParserConfigurationException | SAXException | IOException e) {
-            Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
+            Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + stream, e); //$NON-NLS-1$
         }
         return new CustomXmlTraceDefinition[0];
     }
This page took 0.035936 seconds and 5 git commands to generate.