ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / parsers / custom / CustomXmlTraceDefinition.java
index a8124f5f2bba0cb1c2266b6d6b253bb2efaef3ea..d2df6bf590303ce520dbd9565e661e2452f9b9aa 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;
@@ -84,8 +86,10 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
                     .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
                     .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();
 
+    // TODO: These strings should not be externalized
     private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT = Messages.CustomXmlTraceDefinition_definitionRootElement;
     private static final String DEFINITION_ELEMENT = Messages.CustomXmlTraceDefinition_definition;
+    private static final String CATEGORY_ATTRIBUTE = Messages.CustomXmlTraceDefinition_category;
     private static final String NAME_ATTRIBUTE = Messages.CustomXmlTraceDefinition_name;
     private static final String LOG_ENTRY_ATTRIBUTE = Messages.CustomXmlTraceDefinition_logEntry;
     private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT = Messages.CustomXmlTraceDefinition_timestampOutputFormat;
@@ -103,24 +107,50 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
      * Default constructor
      */
     public CustomXmlTraceDefinition() {
-        this("", null, new ArrayList<OutputColumn>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
+        this(TmfTraceType.CUSTOM_XML_CATEGORY, "", null, new ArrayList<OutputColumn>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
     }
 
     /**
      * Full constructor
      *
-     * @param logtype
-     *            Type of trace type
+     * @param traceType
+     *            Name of the trace type
      * @param rootElement
      *            The top-level XML element
      * @param outputs
      *            The list of output columns
      * @param timeStampOutputFormat
      *            The timestamp format to use
+     * @deprecated Use {@link #CustomXmlTraceDefinition(String, String, InputElement, List, String)}
      */
-    public CustomXmlTraceDefinition(String logtype, InputElement rootElement,
+    @Deprecated
+    public CustomXmlTraceDefinition(String traceType, InputElement rootElement,
             List<OutputColumn> outputs, String timeStampOutputFormat) {
-        this.definitionName = logtype;
+        this.definitionName = traceType;
+        this.rootInputElement = rootElement;
+        this.outputs = outputs;
+        this.timeStampOutputFormat = timeStampOutputFormat;
+    }
+
+    /**
+     * Full constructor
+     *
+     * @param category
+     *            Category of the trace type
+     * @param traceType
+     *            Name of the trace type
+     * @param rootElement
+     *            The top-level XML element
+     * @param outputs
+     *            The list of output columns
+     * @param timeStampOutputFormat
+     *            The timestamp format to use
+     * @since 3.2
+     */
+    public CustomXmlTraceDefinition(String category, String traceType, InputElement rootElement,
+            List<OutputColumn> outputs, String timeStampOutputFormat) {
+        this.categoryName = category;
+        this.definitionName = traceType;
         this.rootInputElement = rootElement;
         this.outputs = outputs;
         this.timeStampOutputFormat = timeStampOutputFormat;
@@ -344,17 +374,13 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
 
             Element root = doc.getDocumentElement();
 
-            NodeList nodeList = root.getChildNodes();
-            for (int i = 0; i < nodeList.getLength(); i++) {
-                Node node = nodeList.item(i);
-                if (node instanceof Element &&
-                        node.getNodeName().equals(DEFINITION_ELEMENT) &&
-                        definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {
-                    root.removeChild(node);
-                }
+            Element oldDefinitionElement = findDefinitionElement(root, categoryName, definitionName);
+            if (oldDefinitionElement != null) {
+                root.removeChild(oldDefinitionElement);
             }
             Element definitionElement = doc.createElement(DEFINITION_ELEMENT);
             root.appendChild(definitionElement);
+            definitionElement.setAttribute(CATEGORY_ATTRIBUTE, categoryName);
             definitionElement.setAttribute(NAME_ATTRIBUTE, definitionName);
 
             Element formatElement = doc.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT);
@@ -386,7 +412,7 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
                 writer.write(xmlString);
             }
 
-            TmfTraceType.addCustomTraceType(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
+            TmfTraceType.addCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
 
         } catch (ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException | IOException | SAXException e) {
             Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
@@ -436,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.2
+     */
+    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);
 
@@ -457,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]);
     }
 
@@ -476,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.2
+     */
+    public static CustomXmlTraceDefinition[] loadAll(InputStream stream) {
         try {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
@@ -486,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];
@@ -510,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];
     }
@@ -521,8 +583,24 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
      * @param definitionName
      *            Name of the XML trace definition to load
      * @return The loaded trace definition
+     * @deprecated Use {@link #load(String, String)}
      */
+    @Deprecated
     public static CustomXmlTraceDefinition load(String definitionName) {
+        return load(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
+    }
+
+    /**
+     * Load the given trace definition.
+     *
+     * @param categoryName
+     *            Category of the definition to load
+     * @param definitionName
+     *            Name of the XML trace definition to load
+     * @return The loaded trace definition
+     * @since 3.2
+     */
+    public static CustomXmlTraceDefinition load(String categoryName, String definitionName) {
         try {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
@@ -554,9 +632,9 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
                 }
             });
 
-            CustomXmlTraceDefinition value = lookupXmlDefinition(definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
+            CustomXmlTraceDefinition value = lookupXmlDefinition(categoryName, definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
             if (value == null) {
-                value = lookupXmlDefinition(definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME);
+                value = lookupXmlDefinition(categoryName, definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME);
             }
             return value;
         } catch (ParserConfigurationException | SAXException | IOException e) {
@@ -565,7 +643,7 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
         return null;
     }
 
-    private static CustomXmlTraceDefinition lookupXmlDefinition(String definitionName, DocumentBuilder db, String source) throws SAXException, IOException {
+    private static CustomXmlTraceDefinition lookupXmlDefinition(String categoryName, String definitionName, DocumentBuilder db, String source) throws SAXException, IOException {
         File file = new File(source);
         if (!file.exists()) {
             return null;
@@ -578,13 +656,28 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
             return null;
         }
 
+        Element definitionElement = findDefinitionElement(root, categoryName, definitionName);
+        if (definitionElement != null) {
+            return extractDefinition(definitionElement);
+        }
+        return null;
+    }
+
+    private static Element findDefinitionElement(Element root, String categoryName, String definitionName) {
         NodeList nodeList = root.getChildNodes();
         for (int i = 0; i < nodeList.getLength(); i++) {
             Node node = nodeList.item(i);
-            if (node instanceof Element &&
-                    node.getNodeName().equals(DEFINITION_ELEMENT) &&
-                    definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {
-                return extractDefinition((Element) node);
+            if (node instanceof Element && node.getNodeName().equals(DEFINITION_ELEMENT)) {
+                Element element = (Element) node;
+                String categoryAttribute = element.getAttribute(CATEGORY_ATTRIBUTE);
+                if (categoryAttribute.isEmpty()) {
+                    categoryAttribute = TmfTraceType.CUSTOM_XML_CATEGORY;
+                }
+                String nameAttribute = element.getAttribute(NAME_ATTRIBUTE);
+                if (categoryName.equals(categoryAttribute) &&
+                        definitionName.equals(nameAttribute)) {
+                    return element;
+                }
             }
         }
         return null;
@@ -600,8 +693,12 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
     public static CustomXmlTraceDefinition extractDefinition(Element definitionElement) {
         CustomXmlTraceDefinition def = new CustomXmlTraceDefinition();
 
+        def.categoryName = definitionElement.getAttribute(CATEGORY_ATTRIBUTE);
+        if (def.categoryName.isEmpty()) {
+            def.categoryName = TmfTraceType.CUSTOM_XML_CATEGORY;
+        }
         def.definitionName = definitionElement.getAttribute(NAME_ATTRIBUTE);
-        if (def.definitionName == null) {
+        if (def.definitionName.isEmpty()) {
             return null;
         }
 
@@ -672,12 +769,27 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
     }
 
     /**
-     * Delete the given trace definition from the list of currently loaded ones.
+     * Delete a definition from the currently loaded ones.
      *
      * @param definitionName
-     *            Name of the trace definition to delete
+     *            The name of the definition to delete
+     * @deprecated Use {@link #delete(String, String)}
      */
+    @Deprecated
     public static void delete(String definitionName) {
+        delete(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
+    }
+
+    /**
+     * Delete a definition from the currently loaded ones.
+     *
+     * @param categoryName
+     *            The category of the definition to delete
+     * @param definitionName
+     *            The name of the definition to delete
+     * @since 3.2
+     */
+    public static void delete(String categoryName, String definitionName) {
         try {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
@@ -717,14 +829,9 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
                 return;
             }
 
-            NodeList nodeList = root.getChildNodes();
-            for (int i = 0; i < nodeList.getLength(); i++) {
-                Node node = nodeList.item(i);
-                if (node instanceof Element &&
-                        node.getNodeName().equals(DEFINITION_ELEMENT) &&
-                        definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {
-                    root.removeChild(node);
-                }
+            Element definitionElement = findDefinitionElement(root, categoryName, definitionName);
+            if (definitionElement != null) {
+                root.removeChild(definitionElement);
             }
 
             Transformer transformer = TransformerFactory.newInstance().newTransformer();
@@ -740,9 +847,9 @@ public class CustomXmlTraceDefinition extends CustomTraceDefinition {
                 writer.write(xmlString);
             }
 
-            TmfTraceType.removeCustomTraceType(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
+            TmfTraceType.removeCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
             // Check if default definition needs to be reloaded
-            TmfTraceType.addCustomTraceType(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
+            TmfTraceType.addCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
 
         } catch (ParserConfigurationException | SAXException | IOException | TransformerFactoryConfigurationError | TransformerException e) {
             Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
This page took 0.029283 seconds and 5 git commands to generate.