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;
}
/**
- * 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);
}
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]);
}
* @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();
// 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];
}
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];
}