1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation
11 * Matthew Khouzam - Add support for default xml parsers
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
;
16 import java
.io
.ByteArrayInputStream
;
18 import java
.io
.FileWriter
;
19 import java
.io
.IOException
;
20 import java
.io
.StringWriter
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Arrays
;
23 import java
.util
.Comparator
;
24 import java
.util
.List
;
26 import java
.util
.TreeSet
;
28 import javax
.xml
.parsers
.DocumentBuilder
;
29 import javax
.xml
.parsers
.DocumentBuilderFactory
;
30 import javax
.xml
.parsers
.ParserConfigurationException
;
31 import javax
.xml
.transform
.OutputKeys
;
32 import javax
.xml
.transform
.Transformer
;
33 import javax
.xml
.transform
.TransformerException
;
34 import javax
.xml
.transform
.TransformerFactory
;
35 import javax
.xml
.transform
.TransformerFactoryConfigurationError
;
36 import javax
.xml
.transform
.dom
.DOMSource
;
37 import javax
.xml
.transform
.stream
.StreamResult
;
39 import org
.eclipse
.core
.runtime
.Platform
;
40 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
42 import org
.w3c
.dom
.Document
;
43 import org
.w3c
.dom
.Element
;
44 import org
.w3c
.dom
.Node
;
45 import org
.w3c
.dom
.NodeList
;
46 import org
.xml
.sax
.EntityResolver
;
47 import org
.xml
.sax
.ErrorHandler
;
48 import org
.xml
.sax
.InputSource
;
49 import org
.xml
.sax
.SAXException
;
50 import org
.xml
.sax
.SAXParseException
;
53 * Trace definition for custom XML traces.
55 * @author Patrick Tassé
58 public class CustomXmlTraceDefinition
extends CustomTraceDefinition
{
61 public static final String TAG_IGNORE
= Messages
.CustomXmlTraceDefinition_ignoreTag
;
63 /** Name of the default XML definitions file */
64 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME
= "custom_xml_default_parsers.xml"; //$NON-NLS-1$
66 /** Name of the XML definitions file */
67 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
= "custom_xml_parsers.xml"; //$NON-NLS-1$
69 /** Path to the XML definitions file */
70 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
=
71 Platform
.getInstallLocation().getURL().getPath() + "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
72 CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME
;
74 /** Path to the XML definitions file */
75 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
=
76 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
79 * Legacy path to the XML definitions file (in the UI plug-in) TODO Remove
80 * once we feel the transition phase is over.
82 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY
=
83 Activator
.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
84 .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
85 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
87 private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
= Messages
.CustomXmlTraceDefinition_definitionRootElement
;
88 private static final String DEFINITION_ELEMENT
= Messages
.CustomXmlTraceDefinition_definition
;
89 private static final String NAME_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_name
;
90 private static final String LOG_ENTRY_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_logEntry
;
91 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= Messages
.CustomXmlTraceDefinition_timestampOutputFormat
;
92 private static final String INPUT_ELEMENT_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputElement
;
93 private static final String ATTRIBUTE_ELEMENT
= Messages
.CustomXmlTraceDefinition_attribute
;
94 private static final String INPUT_DATA_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputData
;
95 private static final String ACTION_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_action
;
96 private static final String FORMAT_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_format
;
97 private static final String OUTPUT_COLUMN_ELEMENT
= Messages
.CustomXmlTraceDefinition_outputColumn
;
99 /** Top-level input element */
100 public InputElement rootInputElement
;
103 * Default constructor
105 public CustomXmlTraceDefinition() {
106 this("", null, new ArrayList
<OutputColumn
>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
115 * The top-level XML element
117 * The list of output columns
118 * @param timeStampOutputFormat
119 * The timestamp format to use
121 public CustomXmlTraceDefinition(String logtype
, InputElement rootElement
,
122 List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
123 this.definitionName
= logtype
;
124 this.rootInputElement
= rootElement
;
125 this.outputs
= outputs
;
126 this.timeStampOutputFormat
= timeStampOutputFormat
;
130 * Wrapper for input XML elements
132 public static class InputElement
{
134 /** Name of the element */
135 public String elementName
;
137 /** Indicates if this is a log entry */
138 public boolean logEntry
;
140 /** Name of the input element */
141 public String inputName
;
144 public int inputAction
;
147 public String inputFormat
;
149 /** XML attributes of this element */
150 public List
<InputAttribute
> attributes
;
152 /** Parent element */
153 public InputElement parentElement
;
155 /** Following element in the file */
156 public InputElement nextElement
;
158 /** Child elements */
159 public List
<InputElement
> childElements
;
162 * Default (empty) constructor
164 public InputElement() {
173 * If this element is a log entry
175 * Name of the the input
181 * XML attributes of this element
183 public InputElement(String elementName
, boolean logEntry
,
184 String inputName
, int inputAction
, String inputFormat
,
185 List
<InputAttribute
> attributes
) {
186 this.elementName
= elementName
;
187 this.logEntry
= logEntry
;
188 this.inputName
= inputName
;
189 this.inputAction
= inputAction
;
190 this.inputFormat
= inputFormat
;
191 this.attributes
= attributes
;
195 * Add a XML attribute to the element
198 * The attribute to add
200 public void addAttribute(InputAttribute attribute
) {
201 if (attributes
== null) {
202 attributes
= new ArrayList
<>(1);
204 attributes
.add(attribute
);
208 * Add a child element to this one.
211 * The input element to add as child
213 public void addChild(InputElement input
) {
214 if (childElements
== null) {
215 childElements
= new ArrayList
<>(1);
216 } else if (childElements
.size() > 0) {
217 InputElement last
= childElements
.get(childElements
.size() - 1);
218 last
.nextElement
= input
;
220 childElements
.add(input
);
221 input
.parentElement
= this;
225 * Set the following input element.
228 * The input element to add as next element
230 public void addNext(InputElement input
) {
231 if (parentElement
!= null) {
232 int index
= parentElement
.childElements
.indexOf(this);
233 parentElement
.childElements
.add(index
+ 1, input
);
234 InputElement next
= nextElement
;
236 input
.nextElement
= next
;
238 input
.parentElement
= this.parentElement
;
242 * Move this element up in its parent's list of children.
244 public void moveUp() {
245 if (parentElement
!= null) {
246 int index
= parentElement
.childElements
.indexOf(this);
248 parentElement
.childElements
.add(index
- 1, parentElement
.childElements
.remove(index
));
249 parentElement
.childElements
.get(index
).nextElement
= nextElement
;
250 nextElement
= parentElement
.childElements
.get(index
);
256 * Move this element down in its parent's list of children.
258 public void moveDown() {
259 if (parentElement
!= null) {
260 int index
= parentElement
.childElements
.indexOf(this);
261 if (index
< parentElement
.childElements
.size() - 1) {
262 parentElement
.childElements
.add(index
+ 1, parentElement
.childElements
.remove(index
));
263 nextElement
= parentElement
.childElements
.get(index
).nextElement
;
264 parentElement
.childElements
.get(index
).nextElement
= this;
272 * Wrapper for XML element attributes
274 public static class InputAttribute
{
276 /** Name of the XML attribute */
277 public String attributeName
;
280 public String inputName
;
283 public int inputAction
;
286 public String inputFormat
;
289 * Default (empty) constructor
291 public InputAttribute() {
297 * @param attributeName
298 * Name of the XML attribute
306 public InputAttribute(String attributeName
, String inputName
,
307 int inputAction
, String inputFormat
) {
308 this.attributeName
= attributeName
;
309 this.inputName
= inputName
;
310 this.inputAction
= inputAction
;
311 this.inputFormat
= inputFormat
;
317 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
321 public void save(String path
) {
323 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
324 DocumentBuilder db
= dbf
.newDocumentBuilder();
326 // The following allows xml parsing without access to the dtd
327 db
.setEntityResolver(createEmptyEntityResolver());
329 // The following catches xml parsing exceptions
330 db
.setErrorHandler(createErrorHandler());
333 File file
= new File(path
);
334 if (file
.canRead()) {
335 doc
= db
.parse(file
);
336 if (!doc
.getDocumentElement().getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
340 doc
= db
.newDocument();
341 Node node
= doc
.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
);
342 doc
.appendChild(node
);
345 Element root
= doc
.getDocumentElement();
347 NodeList nodeList
= root
.getChildNodes();
348 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
349 Node node
= nodeList
.item(i
);
350 if (node
instanceof Element
&&
351 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
352 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
353 root
.removeChild(node
);
356 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
357 root
.appendChild(definitionElement
);
358 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
360 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
361 definitionElement
.appendChild(formatElement
);
362 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
364 if (rootInputElement
!= null) {
365 definitionElement
.appendChild(createInputElementElement(rootInputElement
, doc
));
368 if (outputs
!= null) {
369 for (OutputColumn output
: outputs
) {
370 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
371 definitionElement
.appendChild(outputColumnElement
);
372 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
376 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
377 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
379 // initialize StreamResult with File object to save to file
380 StreamResult result
= new StreamResult(new StringWriter());
381 DOMSource source
= new DOMSource(doc
);
382 transformer
.transform(source
, result
);
383 String xmlString
= result
.getWriter().toString();
385 try (FileWriter writer
= new FileWriter(file
);) {
386 writer
.write(xmlString
);
389 TmfTraceType
.addCustomTraceType(TmfTraceType
.CUSTOM_XML_CATEGORY
, definitionName
);
391 } catch (ParserConfigurationException
| TransformerFactoryConfigurationError
| TransformerException
| IOException
| SAXException e
) {
392 Activator
.logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
396 private Element
createInputElementElement(InputElement inputElement
, Document doc
) {
397 Element inputElementElement
= doc
.createElement(INPUT_ELEMENT_ELEMENT
);
398 inputElementElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.elementName
);
400 if (inputElement
.logEntry
) {
401 inputElementElement
.setAttribute(LOG_ENTRY_ATTRIBUTE
, Boolean
.toString(inputElement
.logEntry
));
404 if (inputElement
.parentElement
!= null) {
405 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
406 inputElementElement
.appendChild(inputDataElement
);
407 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.inputName
);
408 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputElement
.inputAction
));
409 if (inputElement
.inputFormat
!= null) {
410 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputElement
.inputFormat
);
414 if (inputElement
.attributes
!= null) {
415 for (InputAttribute attribute
: inputElement
.attributes
) {
416 Element inputAttributeElement
= doc
.createElement(ATTRIBUTE_ELEMENT
);
417 inputElementElement
.appendChild(inputAttributeElement
);
418 inputAttributeElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.attributeName
);
419 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
420 inputAttributeElement
.appendChild(inputDataElement
);
421 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.inputName
);
422 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(attribute
.inputAction
));
423 if (attribute
.inputFormat
!= null) {
424 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, attribute
.inputFormat
);
429 if (inputElement
.childElements
!= null) {
430 for (InputElement childInputElement
: inputElement
.childElements
) {
431 inputElementElement
.appendChild(createInputElementElement(childInputElement
, doc
));
435 return inputElementElement
;
439 * Load all the XML trace definitions in the default definitions file.
441 * @return The loaded trace definitions
443 public static CustomXmlTraceDefinition
[] loadAll() {
444 File defaultFile
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
445 File legacyFile
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY
);
448 * If there is no file at the expected location, check the legacy
451 if (!defaultFile
.exists() && legacyFile
.exists()) {
452 CustomXmlTraceDefinition
[] oldDefs
= loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY
);
453 for (CustomXmlTraceDefinition def
: oldDefs
) {
454 /* Save in the new location */
459 Set
<CustomXmlTraceDefinition
> defs
= new TreeSet
<>(new Comparator
<CustomXmlTraceDefinition
>() {
462 public int compare(CustomXmlTraceDefinition o1
, CustomXmlTraceDefinition o2
) {
463 return o1
.definitionName
.compareTo(o2
.definitionName
);
466 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
)));
467 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
)));
468 return defs
.toArray(new CustomXmlTraceDefinition
[0]);
472 * Load all the XML trace definitions in the given definitions file.
475 * Path to the definitions file to load
476 * @return The loaded trace definitions
478 public static CustomXmlTraceDefinition
[] loadAll(String path
) {
480 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
481 DocumentBuilder db
= dbf
.newDocumentBuilder();
483 // The following allows xml parsing without access to the dtd
484 db
.setEntityResolver(createEmptyEntityResolver());
486 // The following catches xml parsing exceptions
487 db
.setErrorHandler(createErrorHandler());
489 File file
= new File(path
);
490 if (!file
.canRead()) {
491 return new CustomXmlTraceDefinition
[0];
493 Document doc
= db
.parse(file
);
495 Element root
= doc
.getDocumentElement();
496 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
497 return new CustomXmlTraceDefinition
[0];
500 ArrayList
<CustomXmlTraceDefinition
> defList
= new ArrayList
<>();
501 NodeList nodeList
= root
.getChildNodes();
502 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
503 Node node
= nodeList
.item(i
);
504 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
505 CustomXmlTraceDefinition def
= extractDefinition((Element
) node
);
511 return defList
.toArray(new CustomXmlTraceDefinition
[0]);
512 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
513 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
515 return new CustomXmlTraceDefinition
[0];
519 * Load the given trace definition.
521 * @param definitionName
522 * Name of the XML trace definition to load
523 * @return The loaded trace definition
525 public static CustomXmlTraceDefinition
load(String definitionName
) {
527 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
528 DocumentBuilder db
= dbf
.newDocumentBuilder();
530 // The following allows xml parsing without access to the dtd
531 EntityResolver resolver
= new EntityResolver() {
533 public InputSource
resolveEntity(String publicId
, String systemId
) {
534 String empty
= ""; //$NON-NLS-1$
535 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
536 return new InputSource(bais
);
539 db
.setEntityResolver(resolver
);
541 // The following catches xml parsing exceptions
542 db
.setErrorHandler(new ErrorHandler() {
544 public void error(SAXParseException saxparseexception
) throws SAXException
{
548 public void warning(SAXParseException saxparseexception
) throws SAXException
{
552 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
553 throw saxparseexception
;
557 CustomXmlTraceDefinition value
= lookupXmlDefinition(definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
559 value
= lookupXmlDefinition(definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
);
562 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
563 Activator
.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
568 private static CustomXmlTraceDefinition
lookupXmlDefinition(String definitionName
, DocumentBuilder db
, String source
) throws SAXException
, IOException
{
569 File file
= new File(source
);
570 if (!file
.exists()) {
574 Document doc
= db
.parse(file
);
576 Element root
= doc
.getDocumentElement();
577 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
581 NodeList nodeList
= root
.getChildNodes();
582 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
583 Node node
= nodeList
.item(i
);
584 if (node
instanceof Element
&&
585 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
586 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
587 return extractDefinition((Element
) node
);
594 * Extract a trace definition from an XML element.
596 * @param definitionElement
598 * @return The extracted trace definition
600 public static CustomXmlTraceDefinition
extractDefinition(Element definitionElement
) {
601 CustomXmlTraceDefinition def
= new CustomXmlTraceDefinition();
603 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
604 if (def
.definitionName
== null) {
608 NodeList nodeList
= definitionElement
.getChildNodes();
609 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
610 Node node
= nodeList
.item(i
);
611 String nodeName
= node
.getNodeName();
612 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
613 Element formatElement
= (Element
) node
;
614 def
.timeStampOutputFormat
= formatElement
.getTextContent();
615 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
616 InputElement inputElement
= extractInputElement((Element
) node
);
617 if (inputElement
!= null) {
618 if (def
.rootInputElement
== null) {
619 def
.rootInputElement
= inputElement
;
624 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
625 Element outputColumnElement
= (Element
) node
;
626 OutputColumn outputColumn
= new OutputColumn();
627 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
628 def
.outputs
.add(outputColumn
);
634 private static InputElement
extractInputElement(Element inputElementElement
) {
635 InputElement inputElement
= new InputElement();
636 inputElement
.elementName
= inputElementElement
.getAttribute(NAME_ATTRIBUTE
);
637 inputElement
.logEntry
= (Boolean
.toString(true).equals(inputElementElement
.getAttribute(LOG_ENTRY_ATTRIBUTE
))) ?
true : false;
638 NodeList nodeList
= inputElementElement
.getChildNodes();
639 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
640 Node node
= nodeList
.item(i
);
641 String nodeName
= node
.getNodeName();
642 if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
643 Element inputDataElement
= (Element
) node
;
644 inputElement
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
645 inputElement
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
646 inputElement
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
647 } else if (nodeName
.equals(ATTRIBUTE_ELEMENT
)) {
648 Element attributeElement
= (Element
) node
;
649 InputAttribute attribute
= new InputAttribute();
650 attribute
.attributeName
= attributeElement
.getAttribute(NAME_ATTRIBUTE
);
651 NodeList attributeNodeList
= attributeElement
.getChildNodes();
652 for (int j
= 0; j
< attributeNodeList
.getLength(); j
++) {
653 Node attributeNode
= attributeNodeList
.item(j
);
654 String attributeNodeName
= attributeNode
.getNodeName();
655 if (attributeNodeName
.equals(INPUT_DATA_ELEMENT
)) {
656 Element inputDataElement
= (Element
) attributeNode
;
657 attribute
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
658 attribute
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
659 attribute
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
662 inputElement
.addAttribute(attribute
);
663 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
664 Element childInputElementElement
= (Element
) node
;
665 InputElement childInputElement
= extractInputElement(childInputElementElement
);
666 if (childInputElement
!= null) {
667 inputElement
.addChild(childInputElement
);
675 * Delete the given trace definition from the list of currently loaded ones.
677 * @param definitionName
678 * Name of the trace definition to delete
680 public static void delete(String definitionName
) {
682 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
683 DocumentBuilder db
= dbf
.newDocumentBuilder();
685 // The following allows xml parsing without access to the dtd
686 EntityResolver resolver
= new EntityResolver() {
688 public InputSource
resolveEntity(String publicId
, String systemId
) {
689 String empty
= ""; //$NON-NLS-1$
690 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
691 return new InputSource(bais
);
694 db
.setEntityResolver(resolver
);
696 // The following catches xml parsing exceptions
697 db
.setErrorHandler(new ErrorHandler() {
699 public void error(SAXParseException saxparseexception
) throws SAXException
{
703 public void warning(SAXParseException saxparseexception
) throws SAXException
{
707 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
708 throw saxparseexception
;
712 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
713 Document doc
= db
.parse(file
);
715 Element root
= doc
.getDocumentElement();
716 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
720 NodeList nodeList
= root
.getChildNodes();
721 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
722 Node node
= nodeList
.item(i
);
723 if (node
instanceof Element
&&
724 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
725 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
726 root
.removeChild(node
);
730 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
731 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
733 // initialize StreamResult with File object to save to file
734 StreamResult result
= new StreamResult(new StringWriter());
735 DOMSource source
= new DOMSource(doc
);
736 transformer
.transform(source
, result
);
737 String xmlString
= result
.getWriter().toString();
739 try (FileWriter writer
= new FileWriter(file
);) {
740 writer
.write(xmlString
);
743 TmfTraceType
.removeCustomTraceType(TmfTraceType
.CUSTOM_XML_CATEGORY
, definitionName
);
744 // Check if default definition needs to be reloaded
745 TmfTraceType
.addCustomTraceType(TmfTraceType
.CUSTOM_XML_CATEGORY
, definitionName
);
747 } catch (ParserConfigurationException
| SAXException
| IOException
| TransformerFactoryConfigurationError
| TransformerException e
) {
748 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$