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
.w3c
.dom
.Document
;
38 import org
.w3c
.dom
.Element
;
39 import org
.w3c
.dom
.Node
;
40 import org
.w3c
.dom
.NodeList
;
41 import org
.xml
.sax
.EntityResolver
;
42 import org
.xml
.sax
.ErrorHandler
;
43 import org
.xml
.sax
.InputSource
;
44 import org
.xml
.sax
.SAXException
;
45 import org
.xml
.sax
.SAXParseException
;
48 * Trace definition for custom XML traces.
50 * @author Patrick Tassé
52 public class CustomXmlTraceDefinition
extends CustomTraceDefinition
{
55 public static final String TAG_IGNORE
= Messages
.CustomXmlTraceDefinition_ignoreTag
;
57 /** Name of the XML definitions file */
58 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
= "custom_xml_parsers.xml"; //$NON-NLS-1$
60 /** Path to the XML definitions file */
61 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
=
62 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
64 private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
= Messages
.CustomXmlTraceDefinition_definitionRootElement
;
65 private static final String DEFINITION_ELEMENT
= Messages
.CustomXmlTraceDefinition_definition
;
66 private static final String NAME_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_name
;
67 private static final String LOG_ENTRY_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_logEntry
;
68 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= Messages
.CustomXmlTraceDefinition_timestampOutputFormat
;
69 private static final String INPUT_ELEMENT_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputElement
;
70 private static final String ATTRIBUTE_ELEMENT
= Messages
.CustomXmlTraceDefinition_attribute
;
71 private static final String INPUT_DATA_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputData
;
72 private static final String ACTION_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_action
;
73 private static final String FORMAT_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_format
;
74 private static final String OUTPUT_COLUMN_ELEMENT
= Messages
.CustomXmlTraceDefinition_outputColumn
;
76 /** Top-level input element */
77 public InputElement rootInputElement
;
82 public CustomXmlTraceDefinition() {
83 this("", null, new ArrayList
<OutputColumn
>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
92 * The top-level XML element
94 * The list of output columns
95 * @param timeStampOutputFormat
96 * The timestamp format to use
98 public CustomXmlTraceDefinition(String logtype
, InputElement rootElement
,
99 List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
100 this.definitionName
= logtype
;
101 this.rootInputElement
= rootElement
;
102 this.outputs
= outputs
;
103 this.timeStampOutputFormat
= timeStampOutputFormat
;
107 * Wrapper for input XML elements
109 public static class InputElement
{
111 /** Name of the element */
112 public String elementName
;
114 /** Indicates if this is a log entry */
115 public boolean logEntry
;
117 /** Name of the input element */
118 public String inputName
;
121 public int inputAction
;
124 public String inputFormat
;
126 /** XML attributes of this element */
127 public List
<InputAttribute
> attributes
;
129 /** Parent element */
130 public InputElement parentElement
;
132 /** Following element in the file */
133 public InputElement nextElement
;
135 /** Child elements */
136 public List
<InputElement
> childElements
;
139 * Default (empty) constructor
141 public InputElement() {}
149 * If this element is a log entry
151 * Name of the the input
157 * XML attributes of this element
159 public InputElement(String elementName
, boolean logEntry
,
160 String inputName
, int inputAction
, String inputFormat
,
161 List
<InputAttribute
> attributes
) {
162 this.elementName
= elementName
;
163 this.logEntry
= logEntry
;
164 this.inputName
= inputName
;
165 this.inputAction
= inputAction
;
166 this.inputFormat
= inputFormat
;
167 this.attributes
= attributes
;
171 * Add a XML attribute to the element
174 * The attribute to add
176 public void addAttribute(InputAttribute attribute
) {
177 if (attributes
== null) {
178 attributes
= new ArrayList
<InputAttribute
>(1);
180 attributes
.add(attribute
);
184 * Add a child element to this one.
187 * The input element to add as child
189 public void addChild(InputElement input
) {
190 if (childElements
== null) {
191 childElements
= new ArrayList
<InputElement
>(1);
192 } else if (childElements
.size() > 0) {
193 InputElement last
= childElements
.get(childElements
.size() - 1);
194 last
.nextElement
= input
;
196 childElements
.add(input
);
197 input
.parentElement
= this;
201 * Set the following input element.
204 * The input element to add as next element
206 public void addNext(InputElement input
) {
207 if (parentElement
!= null) {
208 int index
= parentElement
.childElements
.indexOf(this);
209 parentElement
.childElements
.add(index
+ 1, input
);
210 InputElement next
= nextElement
;
212 input
.nextElement
= next
;
214 input
.parentElement
= this.parentElement
;
218 * Move this element up in its parent's list of children.
220 public void moveUp() {
221 if (parentElement
!= null) {
222 int index
= parentElement
.childElements
.indexOf(this);
224 parentElement
.childElements
.add(index
- 1 , parentElement
.childElements
.remove(index
));
225 parentElement
.childElements
.get(index
).nextElement
= nextElement
;
226 nextElement
= parentElement
.childElements
.get(index
);
232 * Move this element down in its parent's list of children.
234 public void moveDown() {
235 if (parentElement
!= null) {
236 int index
= parentElement
.childElements
.indexOf(this);
237 if (index
< parentElement
.childElements
.size() - 1) {
238 parentElement
.childElements
.add(index
+ 1 , parentElement
.childElements
.remove(index
));
239 nextElement
= parentElement
.childElements
.get(index
).nextElement
;
240 parentElement
.childElements
.get(index
).nextElement
= this;
248 * Wrapper for XML element attributes
250 public static class InputAttribute
{
252 /** Name of the XML attribute */
253 public String attributeName
;
256 public String inputName
;
259 public int inputAction
;
262 public String inputFormat
;
265 * Default (empty) constructor
267 public InputAttribute() {}
272 * @param attributeName
273 * Name of the XML attribute
281 public InputAttribute(String attributeName
, String inputName
,
282 int inputAction
, String inputFormat
) {
283 this.attributeName
= attributeName
;
284 this.inputName
= inputName
;
285 this.inputAction
= inputAction
;
286 this.inputFormat
= inputFormat
;
292 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
296 public void save(String path
) {
298 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
299 DocumentBuilder db
= dbf
.newDocumentBuilder();
301 // The following allows xml parsing without access to the dtd
302 EntityResolver resolver
= new EntityResolver() {
304 public InputSource
resolveEntity(String publicId
, String systemId
) {
305 String empty
= ""; //$NON-NLS-1$
306 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
307 return new InputSource(bais
);
310 db
.setEntityResolver(resolver
);
312 // The following catches xml parsing exceptions
313 db
.setErrorHandler(new ErrorHandler() {
315 public void error(SAXParseException saxparseexception
) throws SAXException
{}
318 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
321 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
322 throw saxparseexception
;
327 File file
= new File(path
);
328 if (file
.canRead()) {
329 doc
= db
.parse(file
);
330 if (! doc
.getDocumentElement().getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
334 doc
= db
.newDocument();
335 Node node
= doc
.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
);
336 doc
.appendChild(node
);
339 Element root
= doc
.getDocumentElement();
341 NodeList nodeList
= root
.getChildNodes();
342 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
343 Node node
= nodeList
.item(i
);
344 if (node
instanceof Element
&&
345 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
346 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
347 root
.removeChild(node
);
350 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
351 root
.appendChild(definitionElement
);
352 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
354 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
355 definitionElement
.appendChild(formatElement
);
356 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
358 if (rootInputElement
!= null) {
359 definitionElement
.appendChild(createInputElementElement(rootInputElement
, doc
));
362 if (outputs
!= null) {
363 for (OutputColumn output
: outputs
) {
364 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
365 definitionElement
.appendChild(outputColumnElement
);
366 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
370 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
371 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
373 //initialize StreamResult with File object to save to file
374 StreamResult result
= new StreamResult(new StringWriter());
375 DOMSource source
= new DOMSource(doc
);
376 transformer
.transform(source
, result
);
377 String xmlString
= result
.getWriter().toString();
379 FileWriter writer
= new FileWriter(file
);
380 writer
.write(xmlString
);
382 } catch (ParserConfigurationException e
) {
383 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
384 } catch (TransformerConfigurationException e
) {
385 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
386 } catch (TransformerFactoryConfigurationError e
) {
387 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
388 } catch (TransformerException e
) {
389 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
390 } catch (IOException e
) {
391 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
392 } catch (SAXException e
) {
393 Activator
.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
397 private Element
createInputElementElement(InputElement inputElement
, Document doc
) {
398 Element inputElementElement
= doc
.createElement(INPUT_ELEMENT_ELEMENT
);
399 inputElementElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.elementName
);
401 if (inputElement
.logEntry
) {
402 inputElementElement
.setAttribute(LOG_ENTRY_ATTRIBUTE
, Boolean
.toString(inputElement
.logEntry
));
405 if (inputElement
.parentElement
!= null) {
406 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
407 inputElementElement
.appendChild(inputDataElement
);
408 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.inputName
);
409 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputElement
.inputAction
));
410 if (inputElement
.inputFormat
!= null) {
411 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputElement
.inputFormat
);
415 if (inputElement
.attributes
!= null) {
416 for (InputAttribute attribute
: inputElement
.attributes
) {
417 Element inputAttributeElement
= doc
.createElement(ATTRIBUTE_ELEMENT
);
418 inputElementElement
.appendChild(inputAttributeElement
);
419 inputAttributeElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.attributeName
);
420 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
421 inputAttributeElement
.appendChild(inputDataElement
);
422 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.inputName
);
423 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(attribute
.inputAction
));
424 if (attribute
.inputFormat
!= null) {
425 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, attribute
.inputFormat
);
430 if (inputElement
.childElements
!= null) {
431 for (InputElement childInputElement
: inputElement
.childElements
) {
432 inputElementElement
.appendChild(createInputElementElement(childInputElement
, doc
));
436 return inputElementElement
;
440 * Load all the XML trace definitions in the default definitions file.
442 * @return The loaded trace definitions
444 public static CustomXmlTraceDefinition
[] loadAll() {
445 return loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
449 * Load all the XML trace definitions in the given definitions file.
452 * Path to the definitions file to load
453 * @return The loaded trace definitions
455 public static CustomXmlTraceDefinition
[] loadAll(String path
) {
457 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
458 DocumentBuilder db
= dbf
.newDocumentBuilder();
460 // The following allows xml parsing without access to the dtd
461 EntityResolver resolver
= new EntityResolver() {
463 public InputSource
resolveEntity(String publicId
, String systemId
) {
464 String empty
= ""; //$NON-NLS-1$
465 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
466 return new InputSource(bais
);
469 db
.setEntityResolver(resolver
);
471 // The following catches xml parsing exceptions
472 db
.setErrorHandler(new ErrorHandler() {
474 public void error(SAXParseException saxparseexception
) throws SAXException
{}
477 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
480 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
481 throw saxparseexception
;
485 File file
= new File(path
);
486 if (!file
.canRead()) {
487 return new CustomXmlTraceDefinition
[0];
489 Document doc
= db
.parse(file
);
491 Element root
= doc
.getDocumentElement();
492 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
493 return new CustomXmlTraceDefinition
[0];
496 ArrayList
<CustomXmlTraceDefinition
> defList
= new ArrayList
<CustomXmlTraceDefinition
>();
497 NodeList nodeList
= root
.getChildNodes();
498 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
499 Node node
= nodeList
.item(i
);
500 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
501 CustomXmlTraceDefinition def
= extractDefinition((Element
) node
);
507 return defList
.toArray(new CustomXmlTraceDefinition
[0]);
508 } catch (ParserConfigurationException e
) {
509 Activator
.getDefault().logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
510 } catch (SAXException e
) {
511 Activator
.getDefault().logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
512 } catch (IOException e
) {
513 Activator
.getDefault().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
{}
547 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
550 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
551 throw saxparseexception
;
555 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
556 Document doc
= db
.parse(file
);
558 Element root
= doc
.getDocumentElement();
559 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
563 NodeList nodeList
= root
.getChildNodes();
564 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
565 Node node
= nodeList
.item(i
);
566 if (node
instanceof Element
&&
567 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
568 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
569 return extractDefinition((Element
) node
);
572 } catch (ParserConfigurationException e
) {
573 Activator
.getDefault().logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
574 } catch (SAXException e
) {
575 Activator
.getDefault().logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
576 } catch (IOException e
) {
577 Activator
.getDefault().logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
583 * Extract a trace definition from an XML element.
585 * @param definitionElement
587 * @return The extracted trace definition
589 public static CustomXmlTraceDefinition
extractDefinition(Element definitionElement
) {
590 CustomXmlTraceDefinition def
= new CustomXmlTraceDefinition();
592 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
593 if (def
.definitionName
== null) {
597 NodeList nodeList
= definitionElement
.getChildNodes();
598 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
599 Node node
= nodeList
.item(i
);
600 String nodeName
= node
.getNodeName();
601 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
602 Element formatElement
= (Element
) node
;
603 def
.timeStampOutputFormat
= formatElement
.getTextContent();
604 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
605 InputElement inputElement
= extractInputElement((Element
) node
);
606 if (inputElement
!= null) {
607 if (def
.rootInputElement
== null) {
608 def
.rootInputElement
= inputElement
;
613 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
614 Element outputColumnElement
= (Element
) node
;
615 OutputColumn outputColumn
= new OutputColumn();
616 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
617 def
.outputs
.add(outputColumn
);
623 private static InputElement
extractInputElement(Element inputElementElement
) {
624 InputElement inputElement
= new InputElement();
625 inputElement
.elementName
= inputElementElement
.getAttribute(NAME_ATTRIBUTE
);
626 inputElement
.logEntry
= (Boolean
.toString(true).equals(inputElementElement
.getAttribute(LOG_ENTRY_ATTRIBUTE
))) ?
true : false;
627 NodeList nodeList
= inputElementElement
.getChildNodes();
628 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
629 Node node
= nodeList
.item(i
);
630 String nodeName
= node
.getNodeName();
631 if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
632 Element inputDataElement
= (Element
) node
;
633 inputElement
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
634 inputElement
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
635 inputElement
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
636 } else if (nodeName
.equals(ATTRIBUTE_ELEMENT
)) {
637 Element attributeElement
= (Element
) node
;
638 InputAttribute attribute
= new InputAttribute();
639 attribute
.attributeName
= attributeElement
.getAttribute(NAME_ATTRIBUTE
);
640 NodeList attributeNodeList
= attributeElement
.getChildNodes();
641 for (int j
= 0; j
< attributeNodeList
.getLength(); j
++) {
642 Node attributeNode
= attributeNodeList
.item(j
);
643 String attributeNodeName
= attributeNode
.getNodeName();
644 if (attributeNodeName
.equals(INPUT_DATA_ELEMENT
)) {
645 Element inputDataElement
= (Element
) attributeNode
;
646 attribute
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
647 attribute
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
648 attribute
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
651 inputElement
.addAttribute(attribute
);
652 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
653 Element childInputElementElement
= (Element
) node
;
654 InputElement childInputElement
= extractInputElement(childInputElementElement
);
655 if (childInputElement
!= null) {
656 inputElement
.addChild(childInputElement
);
664 * Delete the given trace definition from the list of currently loaded ones.
666 * @param definitionName
667 * Name of the trace definition to delete
669 public static void delete(String definitionName
) {
671 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
672 DocumentBuilder db
= dbf
.newDocumentBuilder();
674 // The following allows xml parsing without access to the dtd
675 EntityResolver resolver
= new EntityResolver() {
677 public InputSource
resolveEntity(String publicId
, String systemId
) {
678 String empty
= ""; //$NON-NLS-1$
679 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
680 return new InputSource(bais
);
683 db
.setEntityResolver(resolver
);
685 // The following catches xml parsing exceptions
686 db
.setErrorHandler(new ErrorHandler() {
688 public void error(SAXParseException saxparseexception
) throws SAXException
{}
691 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
694 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
695 throw saxparseexception
;
699 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
700 Document doc
= db
.parse(file
);
702 Element root
= doc
.getDocumentElement();
703 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
707 NodeList nodeList
= root
.getChildNodes();
708 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
709 Node node
= nodeList
.item(i
);
710 if (node
instanceof Element
&&
711 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
712 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
713 root
.removeChild(node
);
717 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
718 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
720 //initialize StreamResult with File object to save to file
721 StreamResult result
= new StreamResult(new StringWriter());
722 DOMSource source
= new DOMSource(doc
);
723 transformer
.transform(source
, result
);
724 String xmlString
= result
.getWriter().toString();
726 FileWriter writer
= new FileWriter(file
);
727 writer
.write(xmlString
);
729 } catch (ParserConfigurationException e
) {
730 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
731 } catch (SAXException e
) {
732 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
733 } catch (IOException e
) {
734 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
735 } catch (TransformerConfigurationException e
) {
736 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
737 } catch (TransformerFactoryConfigurationError e
) {
738 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
739 } catch (TransformerException e
) {
740 Activator
.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$