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
.TransformerConfigurationException
;
34 import javax
.xml
.transform
.TransformerException
;
35 import javax
.xml
.transform
.TransformerFactory
;
36 import javax
.xml
.transform
.TransformerFactoryConfigurationError
;
37 import javax
.xml
.transform
.dom
.DOMSource
;
38 import javax
.xml
.transform
.stream
.StreamResult
;
40 import org
.eclipse
.core
.runtime
.Platform
;
41 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
43 import org
.w3c
.dom
.Document
;
44 import org
.w3c
.dom
.Element
;
45 import org
.w3c
.dom
.Node
;
46 import org
.w3c
.dom
.NodeList
;
47 import org
.xml
.sax
.EntityResolver
;
48 import org
.xml
.sax
.ErrorHandler
;
49 import org
.xml
.sax
.InputSource
;
50 import org
.xml
.sax
.SAXException
;
51 import org
.xml
.sax
.SAXParseException
;
54 * Trace definition for custom XML traces.
56 * @author Patrick Tassé
59 public class CustomXmlTraceDefinition
extends CustomTraceDefinition
{
62 public static final String TAG_IGNORE
= Messages
.CustomXmlTraceDefinition_ignoreTag
;
64 /** Name of the default XML definitions file */
65 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME
= "custom_xml_default_parsers.xml"; //$NON-NLS-1$
67 /** Name of the XML definitions file */
68 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
= "custom_xml_parsers.xml"; //$NON-NLS-1$
70 /** Path to the XML definitions file */
71 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
=
72 Platform
.getInstallLocation().getURL().getPath() +
73 "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
74 CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME
;
76 /** Path to the XML definitions file */
77 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
=
78 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
80 /** Legacy path to the XML definitions file (in the UI plug-in)
81 * TODO Remove 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 EntityResolver resolver
= new EntityResolver() {
329 public InputSource
resolveEntity(String publicId
, String systemId
) {
330 String empty
= ""; //$NON-NLS-1$
331 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
332 return new InputSource(bais
);
335 db
.setEntityResolver(resolver
);
337 // The following catches xml parsing exceptions
338 db
.setErrorHandler(new ErrorHandler() {
340 public void error(SAXParseException saxparseexception
) throws SAXException
{
344 public void warning(SAXParseException saxparseexception
) throws SAXException
{
348 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
349 throw saxparseexception
;
354 File file
= new File(path
);
355 if (file
.canRead()) {
356 doc
= db
.parse(file
);
357 if (!doc
.getDocumentElement().getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
361 doc
= db
.newDocument();
362 Node node
= doc
.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
);
363 doc
.appendChild(node
);
366 Element root
= doc
.getDocumentElement();
368 NodeList nodeList
= root
.getChildNodes();
369 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
370 Node node
= nodeList
.item(i
);
371 if (node
instanceof Element
&&
372 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
373 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
374 root
.removeChild(node
);
377 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
378 root
.appendChild(definitionElement
);
379 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
381 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
382 definitionElement
.appendChild(formatElement
);
383 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
385 if (rootInputElement
!= null) {
386 definitionElement
.appendChild(createInputElementElement(rootInputElement
, doc
));
389 if (outputs
!= null) {
390 for (OutputColumn output
: outputs
) {
391 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
392 definitionElement
.appendChild(outputColumnElement
);
393 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
397 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
398 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
400 // initialize StreamResult with File object to save to file
401 StreamResult result
= new StreamResult(new StringWriter());
402 DOMSource source
= new DOMSource(doc
);
403 transformer
.transform(source
, result
);
404 String xmlString
= result
.getWriter().toString();
406 try (FileWriter writer
= new FileWriter(file
);) {
407 writer
.write(xmlString
);
410 TmfTraceType
.addCustomTraceType(TmfTraceType
.CUSTOM_XML_CATEGORY
, definitionName
);
412 } catch (ParserConfigurationException e
) {
413 Activator
.logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
414 } catch (TransformerConfigurationException e
) {
415 Activator
.logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
416 } catch (TransformerFactoryConfigurationError e
) {
417 Activator
.logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
418 } catch (TransformerException e
) {
419 Activator
.logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
420 } catch (IOException e
) {
421 Activator
.logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
422 } catch (SAXException e
) {
423 Activator
.logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
427 private Element
createInputElementElement(InputElement inputElement
, Document doc
) {
428 Element inputElementElement
= doc
.createElement(INPUT_ELEMENT_ELEMENT
);
429 inputElementElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.elementName
);
431 if (inputElement
.logEntry
) {
432 inputElementElement
.setAttribute(LOG_ENTRY_ATTRIBUTE
, Boolean
.toString(inputElement
.logEntry
));
435 if (inputElement
.parentElement
!= null) {
436 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
437 inputElementElement
.appendChild(inputDataElement
);
438 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.inputName
);
439 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputElement
.inputAction
));
440 if (inputElement
.inputFormat
!= null) {
441 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputElement
.inputFormat
);
445 if (inputElement
.attributes
!= null) {
446 for (InputAttribute attribute
: inputElement
.attributes
) {
447 Element inputAttributeElement
= doc
.createElement(ATTRIBUTE_ELEMENT
);
448 inputElementElement
.appendChild(inputAttributeElement
);
449 inputAttributeElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.attributeName
);
450 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
451 inputAttributeElement
.appendChild(inputDataElement
);
452 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.inputName
);
453 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(attribute
.inputAction
));
454 if (attribute
.inputFormat
!= null) {
455 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, attribute
.inputFormat
);
460 if (inputElement
.childElements
!= null) {
461 for (InputElement childInputElement
: inputElement
.childElements
) {
462 inputElementElement
.appendChild(createInputElementElement(childInputElement
, doc
));
466 return inputElementElement
;
470 * Load all the XML trace definitions in the default definitions file.
472 * @return The loaded trace definitions
474 public static CustomXmlTraceDefinition
[] loadAll() {
475 File defaultFile
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
476 File legacyFile
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY
);
479 * If there is no file at the expected location, check the legacy
482 if (!defaultFile
.exists() && legacyFile
.exists()) {
483 CustomXmlTraceDefinition
[] oldDefs
= loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY
);
484 for (CustomXmlTraceDefinition def
: oldDefs
) {
485 /* Save in the new location */
490 Set
<CustomXmlTraceDefinition
> defs
= new TreeSet
<>(new Comparator
<CustomXmlTraceDefinition
>() {
493 public int compare(CustomXmlTraceDefinition o1
, CustomXmlTraceDefinition o2
) {
494 return o1
.definitionName
.compareTo(o2
.definitionName
);
497 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
)));
498 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
)));
499 return defs
.toArray(new CustomXmlTraceDefinition
[0]);
503 * Load all the XML trace definitions in the given definitions file.
506 * Path to the definitions file to load
507 * @return The loaded trace definitions
509 public static CustomXmlTraceDefinition
[] loadAll(String path
) {
511 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
512 DocumentBuilder db
= dbf
.newDocumentBuilder();
514 // The following allows xml parsing without access to the dtd
515 EntityResolver resolver
= new EntityResolver() {
517 public InputSource
resolveEntity(String publicId
, String systemId
) {
518 String empty
= ""; //$NON-NLS-1$
519 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
520 return new InputSource(bais
);
523 db
.setEntityResolver(resolver
);
525 // The following catches xml parsing exceptions
526 db
.setErrorHandler(new ErrorHandler() {
528 public void error(SAXParseException saxparseexception
) throws SAXException
{
532 public void warning(SAXParseException saxparseexception
) throws SAXException
{
536 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
537 throw saxparseexception
;
541 File file
= new File(path
);
542 if (!file
.canRead()) {
543 return new CustomXmlTraceDefinition
[0];
545 Document doc
= db
.parse(file
);
547 Element root
= doc
.getDocumentElement();
548 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
549 return new CustomXmlTraceDefinition
[0];
552 ArrayList
<CustomXmlTraceDefinition
> defList
= new ArrayList
<>();
553 NodeList nodeList
= root
.getChildNodes();
554 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
555 Node node
= nodeList
.item(i
);
556 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
557 CustomXmlTraceDefinition def
= extractDefinition((Element
) node
);
563 return defList
.toArray(new CustomXmlTraceDefinition
[0]);
564 } catch (ParserConfigurationException e
) {
565 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
566 } catch (SAXException e
) {
567 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
568 } catch (IOException e
) {
569 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
571 return new CustomXmlTraceDefinition
[0];
575 * Load the given trace definition.
577 * @param definitionName
578 * Name of the XML trace definition to load
579 * @return The loaded trace definition
581 public static CustomXmlTraceDefinition
load(String definitionName
) {
583 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
584 DocumentBuilder db
= dbf
.newDocumentBuilder();
586 // The following allows xml parsing without access to the dtd
587 EntityResolver resolver
= new EntityResolver() {
589 public InputSource
resolveEntity(String publicId
, String systemId
) {
590 String empty
= ""; //$NON-NLS-1$
591 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
592 return new InputSource(bais
);
595 db
.setEntityResolver(resolver
);
597 // The following catches xml parsing exceptions
598 db
.setErrorHandler(new ErrorHandler() {
600 public void error(SAXParseException saxparseexception
) throws SAXException
{
604 public void warning(SAXParseException saxparseexception
) throws SAXException
{
608 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
609 throw saxparseexception
;
613 CustomXmlTraceDefinition value
= lookupXmlDefinition(definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
615 value
= lookupXmlDefinition(definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
);
618 } catch (ParserConfigurationException e
) {
619 Activator
.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
620 } catch (SAXException e
) {
621 Activator
.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
622 } catch (IOException e
) {
623 Activator
.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
628 private static CustomXmlTraceDefinition
lookupXmlDefinition(String definitionName
, DocumentBuilder db
, String source
) throws SAXException
, IOException
{
629 File file
= new File(source
);
630 if (!file
.exists()) {
634 Document doc
= db
.parse(file
);
636 Element root
= doc
.getDocumentElement();
637 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
641 NodeList nodeList
= root
.getChildNodes();
642 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
643 Node node
= nodeList
.item(i
);
644 if (node
instanceof Element
&&
645 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
646 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
647 return extractDefinition((Element
) node
);
654 * Extract a trace definition from an XML element.
656 * @param definitionElement
658 * @return The extracted trace definition
660 public static CustomXmlTraceDefinition
extractDefinition(Element definitionElement
) {
661 CustomXmlTraceDefinition def
= new CustomXmlTraceDefinition();
663 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
664 if (def
.definitionName
== null) {
668 NodeList nodeList
= definitionElement
.getChildNodes();
669 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
670 Node node
= nodeList
.item(i
);
671 String nodeName
= node
.getNodeName();
672 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
673 Element formatElement
= (Element
) node
;
674 def
.timeStampOutputFormat
= formatElement
.getTextContent();
675 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
676 InputElement inputElement
= extractInputElement((Element
) node
);
677 if (inputElement
!= null) {
678 if (def
.rootInputElement
== null) {
679 def
.rootInputElement
= inputElement
;
684 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
685 Element outputColumnElement
= (Element
) node
;
686 OutputColumn outputColumn
= new OutputColumn();
687 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
688 def
.outputs
.add(outputColumn
);
694 private static InputElement
extractInputElement(Element inputElementElement
) {
695 InputElement inputElement
= new InputElement();
696 inputElement
.elementName
= inputElementElement
.getAttribute(NAME_ATTRIBUTE
);
697 inputElement
.logEntry
= (Boolean
.toString(true).equals(inputElementElement
.getAttribute(LOG_ENTRY_ATTRIBUTE
))) ?
true : false;
698 NodeList nodeList
= inputElementElement
.getChildNodes();
699 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
700 Node node
= nodeList
.item(i
);
701 String nodeName
= node
.getNodeName();
702 if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
703 Element inputDataElement
= (Element
) node
;
704 inputElement
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
705 inputElement
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
706 inputElement
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
707 } else if (nodeName
.equals(ATTRIBUTE_ELEMENT
)) {
708 Element attributeElement
= (Element
) node
;
709 InputAttribute attribute
= new InputAttribute();
710 attribute
.attributeName
= attributeElement
.getAttribute(NAME_ATTRIBUTE
);
711 NodeList attributeNodeList
= attributeElement
.getChildNodes();
712 for (int j
= 0; j
< attributeNodeList
.getLength(); j
++) {
713 Node attributeNode
= attributeNodeList
.item(j
);
714 String attributeNodeName
= attributeNode
.getNodeName();
715 if (attributeNodeName
.equals(INPUT_DATA_ELEMENT
)) {
716 Element inputDataElement
= (Element
) attributeNode
;
717 attribute
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
718 attribute
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
719 attribute
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
722 inputElement
.addAttribute(attribute
);
723 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
724 Element childInputElementElement
= (Element
) node
;
725 InputElement childInputElement
= extractInputElement(childInputElementElement
);
726 if (childInputElement
!= null) {
727 inputElement
.addChild(childInputElement
);
735 * Delete the given trace definition from the list of currently loaded ones.
737 * @param definitionName
738 * Name of the trace definition to delete
740 public static void delete(String definitionName
) {
742 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
743 DocumentBuilder db
= dbf
.newDocumentBuilder();
745 // The following allows xml parsing without access to the dtd
746 EntityResolver resolver
= new EntityResolver() {
748 public InputSource
resolveEntity(String publicId
, String systemId
) {
749 String empty
= ""; //$NON-NLS-1$
750 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
751 return new InputSource(bais
);
754 db
.setEntityResolver(resolver
);
756 // The following catches xml parsing exceptions
757 db
.setErrorHandler(new ErrorHandler() {
759 public void error(SAXParseException saxparseexception
) throws SAXException
{
763 public void warning(SAXParseException saxparseexception
) throws SAXException
{
767 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
768 throw saxparseexception
;
772 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
773 Document doc
= db
.parse(file
);
775 Element root
= doc
.getDocumentElement();
776 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
780 NodeList nodeList
= root
.getChildNodes();
781 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
782 Node node
= nodeList
.item(i
);
783 if (node
instanceof Element
&&
784 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
785 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
786 root
.removeChild(node
);
790 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
791 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
793 // initialize StreamResult with File object to save to file
794 StreamResult result
= new StreamResult(new StringWriter());
795 DOMSource source
= new DOMSource(doc
);
796 transformer
.transform(source
, result
);
797 String xmlString
= result
.getWriter().toString();
799 try (FileWriter writer
= new FileWriter(file
);) {
800 writer
.write(xmlString
);
803 TmfTraceType
.removeCustomTraceType(TmfTraceType
.CUSTOM_XML_CATEGORY
, definitionName
);
804 // Check if default definition needs to be reloaded
805 TmfTraceType
.addCustomTraceType(TmfTraceType
.CUSTOM_XML_CATEGORY
, definitionName
);
807 } catch (ParserConfigurationException e
) {
808 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
809 } catch (SAXException e
) {
810 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
811 } catch (IOException e
) {
812 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
813 } catch (TransformerConfigurationException e
) {
814 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
815 } catch (TransformerFactoryConfigurationError e
) {
816 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
817 } catch (TransformerException e
) {
818 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$