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;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
/** Path to the XML definitions file */
protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME =
- Platform.getInstallLocation().getURL().getPath() +
- "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
- CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME;
+ Platform.getInstallLocation().getURL().getPath() + "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
+ CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME;
/** Path to the XML definitions file */
protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME =
Activator.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();
- /** Legacy path to the XML definitions file (in the UI plug-in)
- * TODO Remove once we feel the transition phase is over. */
+ /**
+ * Legacy path to the XML definitions file (in the UI plug-in) TODO Remove
+ * once we feel the transition phase is over.
+ */
private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY =
Activator.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
.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;
* 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 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)}
+ */
+ @Deprecated
+ public CustomXmlTraceDefinition(String traceType, InputElement rootElement,
+ List<OutputColumn> outputs, String timeStampOutputFormat) {
+ this.definitionName = traceType;
+ this.rootInputElement = rootElement;
+ this.outputs = outputs;
+ this.timeStampOutputFormat = timeStampOutputFormat;
}
/**
* Full constructor
*
- * @param logtype
- * Type of trace type
+ * @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 logtype, InputElement rootElement,
+ public CustomXmlTraceDefinition(String category, String traceType, InputElement rootElement,
List<OutputColumn> outputs, String timeStampOutputFormat) {
- this.definitionName = logtype;
+ this.categoryName = category;
+ this.definitionName = traceType;
this.rootInputElement = rootElement;
this.outputs = outputs;
this.timeStampOutputFormat = timeStampOutputFormat;
DocumentBuilder db = dbf.newDocumentBuilder();
// The following allows xml parsing without access to the dtd
- EntityResolver resolver = new EntityResolver() {
- @Override
- public InputSource resolveEntity(String publicId, String systemId) {
- String empty = ""; //$NON-NLS-1$
- ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
- return new InputSource(bais);
- }
- };
- db.setEntityResolver(resolver);
+ db.setEntityResolver(createEmptyEntityResolver());
// The following catches xml parsing exceptions
- db.setErrorHandler(new ErrorHandler() {
- @Override
- public void error(SAXParseException saxparseexception) throws SAXException {
- }
-
- @Override
- public void warning(SAXParseException saxparseexception) throws SAXException {
- }
-
- @Override
- public void fatalError(SAXParseException saxparseexception) throws SAXException {
- throw saxparseexception;
- }
- });
+ db.setErrorHandler(createErrorHandler());
Document doc = null;
File file = new File(path);
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);
writer.write(xmlString);
}
- TmfTraceType.getInstance().addCustomTraceType(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
+ TmfTraceType.addCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
- } catch (ParserConfigurationException e) {
- Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
- } catch (TransformerConfigurationException e) {
- Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
- } catch (TransformerFactoryConfigurationError e) {
- Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
- } catch (TransformerException e) {
- Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
- } catch (IOException e) {
- Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
- } catch (SAXException e) {
+ } catch (ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException | IOException | SAXException e) {
Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
}
}
}
/**
- * 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);
}
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.2
+ */
+ public static CustomXmlTraceDefinition[] loadAll(InputStream stream) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// The following allows xml parsing without access to the dtd
- EntityResolver resolver = new EntityResolver() {
- @Override
- public InputSource resolveEntity(String publicId, String systemId) {
- String empty = ""; //$NON-NLS-1$
- ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
- return new InputSource(bais);
- }
- };
- db.setEntityResolver(resolver);
+ db.setEntityResolver(createEmptyEntityResolver());
// The following catches xml parsing exceptions
- db.setErrorHandler(new ErrorHandler() {
- @Override
- public void error(SAXParseException saxparseexception) throws SAXException {
- }
-
- @Override
- public void warning(SAXParseException saxparseexception) throws SAXException {
- }
-
- @Override
- public void fatalError(SAXParseException saxparseexception) throws SAXException {
- throw saxparseexception;
- }
- });
-
- File file = new File(path);
- if (!file.canRead()) {
- return new CustomXmlTraceDefinition[0];
- }
- Document doc = db.parse(file);
+ db.setErrorHandler(createErrorHandler());
+ 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 e) {
- Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
- } catch (SAXException e) {
- Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
- } catch (IOException e) {
- Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
+ } catch (ParserConfigurationException | SAXException | IOException e) {
+ Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + stream, e); //$NON-NLS-1$
}
return new CustomXmlTraceDefinition[0];
}
* @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();
}
});
- 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 e) {
- Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
- } catch (SAXException e) {
- Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
- } catch (IOException e) {
+ } catch (ParserConfigurationException | SAXException | IOException e) {
Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
}
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;
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;
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;
}
}
/**
- * 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();
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();
writer.write(xmlString);
}
- TmfTraceType.getInstance().removeCustomTraceType(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
+ TmfTraceType.removeCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
+ // Check if default definition needs to be reloaded
+ TmfTraceType.addCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
- } catch (ParserConfigurationException e) {
- Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
- } catch (SAXException e) {
- Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
- } catch (IOException e) {
- Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
- } catch (TransformerConfigurationException e) {
- Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
- } catch (TransformerFactoryConfigurationError e) {
- Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
- } catch (TransformerException e) {
+ } catch (ParserConfigurationException | SAXException | IOException | TransformerFactoryConfigurationError | TransformerException e) {
Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
}
}