1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
;
15 import java
.io
.ByteArrayInputStream
;
17 import java
.io
.FileWriter
;
18 import java
.io
.IOException
;
19 import java
.io
.StringWriter
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 import javax
.xml
.parsers
.DocumentBuilder
;
24 import javax
.xml
.parsers
.DocumentBuilderFactory
;
25 import javax
.xml
.parsers
.ParserConfigurationException
;
26 import javax
.xml
.transform
.OutputKeys
;
27 import javax
.xml
.transform
.Transformer
;
28 import javax
.xml
.transform
.TransformerConfigurationException
;
29 import javax
.xml
.transform
.TransformerException
;
30 import javax
.xml
.transform
.TransformerFactory
;
31 import javax
.xml
.transform
.TransformerFactoryConfigurationError
;
32 import javax
.xml
.transform
.dom
.DOMSource
;
33 import javax
.xml
.transform
.stream
.StreamResult
;
35 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
36 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceType
;
38 import org
.w3c
.dom
.Document
;
39 import org
.w3c
.dom
.Element
;
40 import org
.w3c
.dom
.Node
;
41 import org
.w3c
.dom
.NodeList
;
42 import org
.xml
.sax
.EntityResolver
;
43 import org
.xml
.sax
.ErrorHandler
;
44 import org
.xml
.sax
.InputSource
;
45 import org
.xml
.sax
.SAXException
;
46 import org
.xml
.sax
.SAXParseException
;
49 * Trace definition for custom XML traces.
51 * @author Patrick Tassé
53 public class CustomXmlTraceDefinition
extends CustomTraceDefinition
{
56 public static final String TAG_IGNORE
= Messages
.CustomXmlTraceDefinition_ignoreTag
;
58 /** Name of the XML definitions file */
59 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
= "custom_xml_parsers.xml"; //$NON-NLS-1$
61 /** Path to the XML definitions file */
62 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
=
63 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
65 private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
= Messages
.CustomXmlTraceDefinition_definitionRootElement
;
66 private static final String DEFINITION_ELEMENT
= Messages
.CustomXmlTraceDefinition_definition
;
67 private static final String NAME_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_name
;
68 private static final String LOG_ENTRY_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_logEntry
;
69 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= Messages
.CustomXmlTraceDefinition_timestampOutputFormat
;
70 private static final String INPUT_ELEMENT_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputElement
;
71 private static final String ATTRIBUTE_ELEMENT
= Messages
.CustomXmlTraceDefinition_attribute
;
72 private static final String INPUT_DATA_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputData
;
73 private static final String ACTION_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_action
;
74 private static final String FORMAT_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_format
;
75 private static final String OUTPUT_COLUMN_ELEMENT
= Messages
.CustomXmlTraceDefinition_outputColumn
;
77 /** Top-level input element */
78 public InputElement rootInputElement
;
83 public CustomXmlTraceDefinition() {
84 this("", null, new ArrayList
<OutputColumn
>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
93 * The top-level XML element
95 * The list of output columns
96 * @param timeStampOutputFormat
97 * The timestamp format to use
99 public CustomXmlTraceDefinition(String logtype
, InputElement rootElement
,
100 List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
101 this.definitionName
= logtype
;
102 this.rootInputElement
= rootElement
;
103 this.outputs
= outputs
;
104 this.timeStampOutputFormat
= timeStampOutputFormat
;
108 * Wrapper for input XML elements
110 public static class InputElement
{
112 /** Name of the element */
113 public String elementName
;
115 /** Indicates if this is a log entry */
116 public boolean logEntry
;
118 /** Name of the input element */
119 public String inputName
;
122 public int inputAction
;
125 public String inputFormat
;
127 /** XML attributes of this element */
128 public List
<InputAttribute
> attributes
;
130 /** Parent element */
131 public InputElement parentElement
;
133 /** Following element in the file */
134 public InputElement nextElement
;
136 /** Child elements */
137 public List
<InputElement
> childElements
;
140 * Default (empty) constructor
142 public InputElement() {}
150 * If this element is a log entry
152 * Name of the the input
158 * XML attributes of this element
160 public InputElement(String elementName
, boolean logEntry
,
161 String inputName
, int inputAction
, String inputFormat
,
162 List
<InputAttribute
> attributes
) {
163 this.elementName
= elementName
;
164 this.logEntry
= logEntry
;
165 this.inputName
= inputName
;
166 this.inputAction
= inputAction
;
167 this.inputFormat
= inputFormat
;
168 this.attributes
= attributes
;
172 * Add a XML attribute to the element
175 * The attribute to add
177 public void addAttribute(InputAttribute attribute
) {
178 if (attributes
== null) {
179 attributes
= new ArrayList
<>(1);
181 attributes
.add(attribute
);
185 * Add a child element to this one.
188 * The input element to add as child
190 public void addChild(InputElement input
) {
191 if (childElements
== null) {
192 childElements
= new ArrayList
<>(1);
193 } else if (childElements
.size() > 0) {
194 InputElement last
= childElements
.get(childElements
.size() - 1);
195 last
.nextElement
= input
;
197 childElements
.add(input
);
198 input
.parentElement
= this;
202 * Set the following input element.
205 * The input element to add as next element
207 public void addNext(InputElement input
) {
208 if (parentElement
!= null) {
209 int index
= parentElement
.childElements
.indexOf(this);
210 parentElement
.childElements
.add(index
+ 1, input
);
211 InputElement next
= nextElement
;
213 input
.nextElement
= next
;
215 input
.parentElement
= this.parentElement
;
219 * Move this element up in its parent's list of children.
221 public void moveUp() {
222 if (parentElement
!= null) {
223 int index
= parentElement
.childElements
.indexOf(this);
225 parentElement
.childElements
.add(index
- 1 , parentElement
.childElements
.remove(index
));
226 parentElement
.childElements
.get(index
).nextElement
= nextElement
;
227 nextElement
= parentElement
.childElements
.get(index
);
233 * Move this element down in its parent's list of children.
235 public void moveDown() {
236 if (parentElement
!= null) {
237 int index
= parentElement
.childElements
.indexOf(this);
238 if (index
< parentElement
.childElements
.size() - 1) {
239 parentElement
.childElements
.add(index
+ 1 , parentElement
.childElements
.remove(index
));
240 nextElement
= parentElement
.childElements
.get(index
).nextElement
;
241 parentElement
.childElements
.get(index
).nextElement
= this;
249 * Wrapper for XML element attributes
251 public static class InputAttribute
{
253 /** Name of the XML attribute */
254 public String attributeName
;
257 public String inputName
;
260 public int inputAction
;
263 public String inputFormat
;
266 * Default (empty) constructor
268 public InputAttribute() {}
273 * @param attributeName
274 * Name of the XML attribute
282 public InputAttribute(String attributeName
, String inputName
,
283 int inputAction
, String inputFormat
) {
284 this.attributeName
= attributeName
;
285 this.inputName
= inputName
;
286 this.inputAction
= inputAction
;
287 this.inputFormat
= inputFormat
;
293 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
297 public void save(String path
) {
299 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
300 DocumentBuilder db
= dbf
.newDocumentBuilder();
302 // The following allows xml parsing without access to the dtd
303 EntityResolver resolver
= new EntityResolver() {
305 public InputSource
resolveEntity(String publicId
, String systemId
) {
306 String empty
= ""; //$NON-NLS-1$
307 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
308 return new InputSource(bais
);
311 db
.setEntityResolver(resolver
);
313 // The following catches xml parsing exceptions
314 db
.setErrorHandler(new ErrorHandler() {
316 public void error(SAXParseException saxparseexception
) throws SAXException
{}
319 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
322 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
323 throw saxparseexception
;
328 File file
= new File(path
);
329 if (file
.canRead()) {
330 doc
= db
.parse(file
);
331 if (! doc
.getDocumentElement().getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
335 doc
= db
.newDocument();
336 Node node
= doc
.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
);
337 doc
.appendChild(node
);
340 Element root
= doc
.getDocumentElement();
342 NodeList nodeList
= root
.getChildNodes();
343 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
344 Node node
= nodeList
.item(i
);
345 if (node
instanceof Element
&&
346 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
347 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
348 root
.removeChild(node
);
351 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
352 root
.appendChild(definitionElement
);
353 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
355 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
356 definitionElement
.appendChild(formatElement
);
357 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
359 if (rootInputElement
!= null) {
360 definitionElement
.appendChild(createInputElementElement(rootInputElement
, doc
));
363 if (outputs
!= null) {
364 for (OutputColumn output
: outputs
) {
365 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
366 definitionElement
.appendChild(outputColumnElement
);
367 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
371 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
372 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
374 //initialize StreamResult with File object to save to file
375 StreamResult result
= new StreamResult(new StringWriter());
376 DOMSource source
= new DOMSource(doc
);
377 transformer
.transform(source
, result
);
378 String xmlString
= result
.getWriter().toString();
380 try (FileWriter writer
= new FileWriter(file
);) {
381 writer
.write(xmlString
);
384 TmfTraceType
.getInstance().addCustomTraceType(TmfTraceType
.CUSTOM_XML_CATEGORY
, definitionName
);
386 } catch (ParserConfigurationException e
) {
387 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
388 } catch (TransformerConfigurationException e
) {
389 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
390 } catch (TransformerFactoryConfigurationError e
) {
391 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
392 } catch (TransformerException e
) {
393 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
394 } catch (IOException e
) {
395 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
396 } catch (SAXException e
) {
397 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
401 private Element
createInputElementElement(InputElement inputElement
, Document doc
) {
402 Element inputElementElement
= doc
.createElement(INPUT_ELEMENT_ELEMENT
);
403 inputElementElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.elementName
);
405 if (inputElement
.logEntry
) {
406 inputElementElement
.setAttribute(LOG_ENTRY_ATTRIBUTE
, Boolean
.toString(inputElement
.logEntry
));
409 if (inputElement
.parentElement
!= null) {
410 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
411 inputElementElement
.appendChild(inputDataElement
);
412 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.inputName
);
413 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputElement
.inputAction
));
414 if (inputElement
.inputFormat
!= null) {
415 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputElement
.inputFormat
);
419 if (inputElement
.attributes
!= null) {
420 for (InputAttribute attribute
: inputElement
.attributes
) {
421 Element inputAttributeElement
= doc
.createElement(ATTRIBUTE_ELEMENT
);
422 inputElementElement
.appendChild(inputAttributeElement
);
423 inputAttributeElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.attributeName
);
424 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
425 inputAttributeElement
.appendChild(inputDataElement
);
426 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.inputName
);
427 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(attribute
.inputAction
));
428 if (attribute
.inputFormat
!= null) {
429 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, attribute
.inputFormat
);
434 if (inputElement
.childElements
!= null) {
435 for (InputElement childInputElement
: inputElement
.childElements
) {
436 inputElementElement
.appendChild(createInputElementElement(childInputElement
, doc
));
440 return inputElementElement
;
444 * Load all the XML trace definitions in the default definitions file.
446 * @return The loaded trace definitions
448 public static CustomXmlTraceDefinition
[] loadAll() {
449 return loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
453 * Load all the XML trace definitions in the given definitions file.
456 * Path to the definitions file to load
457 * @return The loaded trace definitions
459 public static CustomXmlTraceDefinition
[] loadAll(String path
) {
461 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
462 DocumentBuilder db
= dbf
.newDocumentBuilder();
464 // The following allows xml parsing without access to the dtd
465 EntityResolver resolver
= new EntityResolver() {
467 public InputSource
resolveEntity(String publicId
, String systemId
) {
468 String empty
= ""; //$NON-NLS-1$
469 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
470 return new InputSource(bais
);
473 db
.setEntityResolver(resolver
);
475 // The following catches xml parsing exceptions
476 db
.setErrorHandler(new ErrorHandler() {
478 public void error(SAXParseException saxparseexception
) throws SAXException
{}
481 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
484 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
485 throw saxparseexception
;
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 e
) {
513 Activator
.getDefault().logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
514 } catch (SAXException e
) {
515 Activator
.getDefault().logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
516 } catch (IOException e
) {
517 Activator
.getDefault().logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
519 return new CustomXmlTraceDefinition
[0];
523 * Load the given trace definition.
525 * @param definitionName
526 * Name of the XML trace definition to load
527 * @return The loaded trace definition
529 public static CustomXmlTraceDefinition
load(String definitionName
) {
531 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
532 DocumentBuilder db
= dbf
.newDocumentBuilder();
534 // The following allows xml parsing without access to the dtd
535 EntityResolver resolver
= new EntityResolver() {
537 public InputSource
resolveEntity(String publicId
, String systemId
) {
538 String empty
= ""; //$NON-NLS-1$
539 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
540 return new InputSource(bais
);
543 db
.setEntityResolver(resolver
);
545 // The following catches xml parsing exceptions
546 db
.setErrorHandler(new ErrorHandler() {
548 public void error(SAXParseException saxparseexception
) throws SAXException
{}
551 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
554 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
555 throw saxparseexception
;
559 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
560 Document doc
= db
.parse(file
);
562 Element root
= doc
.getDocumentElement();
563 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
567 NodeList nodeList
= root
.getChildNodes();
568 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
569 Node node
= nodeList
.item(i
);
570 if (node
instanceof Element
&&
571 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
572 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
573 return extractDefinition((Element
) node
);
576 } catch (ParserConfigurationException e
) {
577 Activator
.getDefault().logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
578 } catch (SAXException e
) {
579 Activator
.getDefault().logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
580 } catch (IOException e
) {
581 Activator
.getDefault().logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
587 * Extract a trace definition from an XML element.
589 * @param definitionElement
591 * @return The extracted trace definition
593 public static CustomXmlTraceDefinition
extractDefinition(Element definitionElement
) {
594 CustomXmlTraceDefinition def
= new CustomXmlTraceDefinition();
596 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
597 if (def
.definitionName
== null) {
601 NodeList nodeList
= definitionElement
.getChildNodes();
602 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
603 Node node
= nodeList
.item(i
);
604 String nodeName
= node
.getNodeName();
605 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
606 Element formatElement
= (Element
) node
;
607 def
.timeStampOutputFormat
= formatElement
.getTextContent();
608 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
609 InputElement inputElement
= extractInputElement((Element
) node
);
610 if (inputElement
!= null) {
611 if (def
.rootInputElement
== null) {
612 def
.rootInputElement
= inputElement
;
617 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
618 Element outputColumnElement
= (Element
) node
;
619 OutputColumn outputColumn
= new OutputColumn();
620 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
621 def
.outputs
.add(outputColumn
);
627 private static InputElement
extractInputElement(Element inputElementElement
) {
628 InputElement inputElement
= new InputElement();
629 inputElement
.elementName
= inputElementElement
.getAttribute(NAME_ATTRIBUTE
);
630 inputElement
.logEntry
= (Boolean
.toString(true).equals(inputElementElement
.getAttribute(LOG_ENTRY_ATTRIBUTE
))) ?
true : false;
631 NodeList nodeList
= inputElementElement
.getChildNodes();
632 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
633 Node node
= nodeList
.item(i
);
634 String nodeName
= node
.getNodeName();
635 if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
636 Element inputDataElement
= (Element
) node
;
637 inputElement
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
638 inputElement
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
639 inputElement
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
640 } else if (nodeName
.equals(ATTRIBUTE_ELEMENT
)) {
641 Element attributeElement
= (Element
) node
;
642 InputAttribute attribute
= new InputAttribute();
643 attribute
.attributeName
= attributeElement
.getAttribute(NAME_ATTRIBUTE
);
644 NodeList attributeNodeList
= attributeElement
.getChildNodes();
645 for (int j
= 0; j
< attributeNodeList
.getLength(); j
++) {
646 Node attributeNode
= attributeNodeList
.item(j
);
647 String attributeNodeName
= attributeNode
.getNodeName();
648 if (attributeNodeName
.equals(INPUT_DATA_ELEMENT
)) {
649 Element inputDataElement
= (Element
) attributeNode
;
650 attribute
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
651 attribute
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
652 attribute
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
655 inputElement
.addAttribute(attribute
);
656 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
657 Element childInputElementElement
= (Element
) node
;
658 InputElement childInputElement
= extractInputElement(childInputElementElement
);
659 if (childInputElement
!= null) {
660 inputElement
.addChild(childInputElement
);
668 * Delete the given trace definition from the list of currently loaded ones.
670 * @param definitionName
671 * Name of the trace definition to delete
673 public static void delete(String definitionName
) {
675 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
676 DocumentBuilder db
= dbf
.newDocumentBuilder();
678 // The following allows xml parsing without access to the dtd
679 EntityResolver resolver
= new EntityResolver() {
681 public InputSource
resolveEntity(String publicId
, String systemId
) {
682 String empty
= ""; //$NON-NLS-1$
683 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
684 return new InputSource(bais
);
687 db
.setEntityResolver(resolver
);
689 // The following catches xml parsing exceptions
690 db
.setErrorHandler(new ErrorHandler() {
692 public void error(SAXParseException saxparseexception
) throws SAXException
{}
695 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
698 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
699 throw saxparseexception
;
703 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
704 Document doc
= db
.parse(file
);
706 Element root
= doc
.getDocumentElement();
707 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
711 NodeList nodeList
= root
.getChildNodes();
712 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
713 Node node
= nodeList
.item(i
);
714 if (node
instanceof Element
&&
715 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
716 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
717 root
.removeChild(node
);
721 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
722 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
724 //initialize StreamResult with File object to save to file
725 StreamResult result
= new StreamResult(new StringWriter());
726 DOMSource source
= new DOMSource(doc
);
727 transformer
.transform(source
, result
);
728 String xmlString
= result
.getWriter().toString();
730 try (FileWriter writer
= new FileWriter(file
);) {
731 writer
.write(xmlString
);
734 TmfTraceType
.getInstance().removeCustomTraceType(TmfTraceType
.CUSTOM_XML_CATEGORY
, definitionName
);
736 } catch (ParserConfigurationException e
) {
737 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
738 } catch (SAXException e
) {
739 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
740 } catch (IOException e
) {
741 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
742 } catch (TransformerConfigurationException e
) {
743 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
744 } catch (TransformerFactoryConfigurationError e
) {
745 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
746 } catch (TransformerException e
) {
747 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$