1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 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
.tracecompass
.tmf
.core
.parsers
.custom
;
16 import java
.io
.ByteArrayInputStream
;
18 import java
.io
.FileInputStream
;
19 import java
.io
.FileWriter
;
20 import java
.io
.IOException
;
21 import java
.io
.InputStream
;
22 import java
.io
.StringWriter
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Arrays
;
25 import java
.util
.Comparator
;
26 import java
.util
.List
;
28 import java
.util
.TreeSet
;
30 import javax
.xml
.parsers
.DocumentBuilder
;
31 import javax
.xml
.parsers
.DocumentBuilderFactory
;
32 import javax
.xml
.parsers
.ParserConfigurationException
;
33 import javax
.xml
.transform
.OutputKeys
;
34 import javax
.xml
.transform
.Transformer
;
35 import javax
.xml
.transform
.TransformerException
;
36 import javax
.xml
.transform
.TransformerFactory
;
37 import javax
.xml
.transform
.TransformerFactoryConfigurationError
;
38 import javax
.xml
.transform
.dom
.DOMSource
;
39 import javax
.xml
.transform
.stream
.StreamResult
;
41 import org
.eclipse
.core
.runtime
.Platform
;
42 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
44 import org
.w3c
.dom
.Document
;
45 import org
.w3c
.dom
.Element
;
46 import org
.w3c
.dom
.Node
;
47 import org
.w3c
.dom
.NodeList
;
48 import org
.xml
.sax
.EntityResolver
;
49 import org
.xml
.sax
.ErrorHandler
;
50 import org
.xml
.sax
.InputSource
;
51 import org
.xml
.sax
.SAXException
;
52 import org
.xml
.sax
.SAXParseException
;
55 * Trace definition for custom XML traces.
57 * @author Patrick Tassé
60 public class CustomXmlTraceDefinition
extends CustomTraceDefinition
{
63 public static final String TAG_IGNORE
= Messages
.CustomXmlTraceDefinition_ignoreTag
;
66 * Custom XML label used internally and therefore should not be externalized
68 public static final String CUSTOM_XML_CATEGORY
= "Custom XML"; //$NON-NLS-1$
71 /** Name of the default XML definitions file */
72 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME
= "custom_xml_default_parsers.xml"; //$NON-NLS-1$
74 /** Name of the XML definitions file */
75 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
= "custom_xml_parsers.xml"; //$NON-NLS-1$
77 /** Path to the XML definitions file */
78 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
=
79 Platform
.getInstallLocation().getURL().getPath() + "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
80 CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME
;
82 /** Path to the XML definitions file */
83 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
=
84 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
87 * Legacy path to the XML definitions file (in the UI plug-in of linux tools) TODO Remove
88 * once we feel the transition phase is over.
90 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
=
91 Activator
.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
92 .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
93 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
96 * Legacy path to the XML definitions file (in the core plug-in of linux tools) TODO Remove
97 * once we feel the transition phase is over.
99 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
=
100 Activator
.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
101 .append("org.eclipse.linuxtools.tmf.core") //$NON-NLS-1$
102 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
104 // TODO: These strings should not be externalized
105 private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
= Messages
.CustomXmlTraceDefinition_definitionRootElement
;
106 private static final String DEFINITION_ELEMENT
= Messages
.CustomXmlTraceDefinition_definition
;
107 private static final String CATEGORY_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_category
;
108 private static final String NAME_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_name
;
109 private static final String LOG_ENTRY_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_logEntry
;
110 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= Messages
.CustomXmlTraceDefinition_timestampOutputFormat
;
111 private static final String INPUT_ELEMENT_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputElement
;
112 private static final String ATTRIBUTE_ELEMENT
= Messages
.CustomXmlTraceDefinition_attribute
;
113 private static final String INPUT_DATA_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputData
;
114 private static final String ACTION_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_action
;
115 private static final String FORMAT_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_format
;
116 private static final String OUTPUT_COLUMN_ELEMENT
= Messages
.CustomXmlTraceDefinition_outputColumn
;
118 /** Top-level input element */
119 public CustomXmlInputElement rootInputElement
;
122 * Default constructor
124 public CustomXmlTraceDefinition() {
125 this(CUSTOM_XML_CATEGORY
, "", null, new ArrayList
<OutputColumn
>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
132 * Name of the trace type
134 * The top-level XML element
136 * The list of output columns
137 * @param timeStampOutputFormat
138 * The timestamp format to use
140 * {@link #CustomXmlTraceDefinition(String, String, CustomXmlInputElement, List, String)}
143 public CustomXmlTraceDefinition(String traceType
, CustomXmlInputElement rootElement
,
144 List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
145 this.definitionName
= traceType
;
146 this.rootInputElement
= rootElement
;
147 this.outputs
= outputs
;
148 this.timeStampOutputFormat
= timeStampOutputFormat
;
155 * Category of the trace type
157 * Name of the trace type
159 * The top-level XML element
161 * The list of output columns
162 * @param timeStampOutputFormat
163 * The timestamp format to use
166 public CustomXmlTraceDefinition(String category
, String traceType
, CustomXmlInputElement rootElement
,
167 List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
168 this.categoryName
= category
;
169 this.definitionName
= traceType
;
170 this.rootInputElement
= rootElement
;
171 this.outputs
= outputs
;
172 this.timeStampOutputFormat
= timeStampOutputFormat
;
177 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
181 public void save(String path
) {
183 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
184 DocumentBuilder db
= dbf
.newDocumentBuilder();
186 // The following allows xml parsing without access to the dtd
187 db
.setEntityResolver(createEmptyEntityResolver());
189 // The following catches xml parsing exceptions
190 db
.setErrorHandler(createErrorHandler());
193 File file
= new File(path
);
194 if (file
.canRead()) {
195 doc
= db
.parse(file
);
196 if (!doc
.getDocumentElement().getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
200 doc
= db
.newDocument();
201 Node node
= doc
.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
);
202 doc
.appendChild(node
);
205 Element root
= doc
.getDocumentElement();
207 Element oldDefinitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
208 if (oldDefinitionElement
!= null) {
209 root
.removeChild(oldDefinitionElement
);
211 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
212 root
.appendChild(definitionElement
);
213 definitionElement
.setAttribute(CATEGORY_ATTRIBUTE
, categoryName
);
214 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
216 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
217 definitionElement
.appendChild(formatElement
);
218 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
220 if (rootInputElement
!= null) {
221 definitionElement
.appendChild(createInputElementElement(rootInputElement
, doc
));
224 if (outputs
!= null) {
225 for (OutputColumn output
: outputs
) {
226 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
227 definitionElement
.appendChild(outputColumnElement
);
228 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
232 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
233 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
235 // initialize StreamResult with File object to save to file
236 StreamResult result
= new StreamResult(new StringWriter());
237 DOMSource source
= new DOMSource(doc
);
238 transformer
.transform(source
, result
);
239 String xmlString
= result
.getWriter().toString();
241 try (FileWriter writer
= new FileWriter(file
);) {
242 writer
.write(xmlString
);
245 TmfTraceType
.addCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
247 } catch (ParserConfigurationException
| TransformerFactoryConfigurationError
| TransformerException
| IOException
| SAXException e
) {
248 Activator
.logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
252 private Element
createInputElementElement(CustomXmlInputElement inputElement
, Document doc
) {
253 Element inputElementElement
= doc
.createElement(INPUT_ELEMENT_ELEMENT
);
254 inputElementElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.getElementName());
256 if (inputElement
.isLogEntry()) {
257 inputElementElement
.setAttribute(LOG_ENTRY_ATTRIBUTE
, Boolean
.toString(inputElement
.isLogEntry()));
260 if (inputElement
.getParentElement() != null) {
261 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
262 inputElementElement
.appendChild(inputDataElement
);
263 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.getInputName());
264 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputElement
.getInputAction()));
265 if (inputElement
.getInputFormat() != null) {
266 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputElement
.getInputFormat());
270 if (inputElement
.getAttributes() != null) {
271 for (CustomXmlInputAttribute attribute
: inputElement
.getAttributes()) {
272 Element inputAttributeElement
= doc
.createElement(ATTRIBUTE_ELEMENT
);
273 inputElementElement
.appendChild(inputAttributeElement
);
274 inputAttributeElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.getAttributeName());
275 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
276 inputAttributeElement
.appendChild(inputDataElement
);
277 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.getInputName());
278 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(attribute
.getInputAction()));
279 if (attribute
.getInputFormat() != null) {
280 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, attribute
.getInputFormat());
285 if (inputElement
.getChildElements() != null) {
286 for (CustomXmlInputElement childInputElement
: inputElement
.getChildElements()) {
287 inputElementElement
.appendChild(createInputElementElement(childInputElement
, doc
));
291 return inputElementElement
;
295 * Load all custom XML trace definitions, including the user-defined and
296 * default (built-in) parsers.
298 * @return The loaded trace definitions
300 public static CustomXmlTraceDefinition
[] loadAll() {
301 return loadAll(true);
305 * Load all custom XML trace definitions, including the user-defined and,
306 * optionally, the default (built-in) parsers.
308 * @param includeDefaults
309 * if true, the default (built-in) parsers are included
311 * @return The loaded trace definitions
314 public static CustomXmlTraceDefinition
[] loadAll(boolean includeDefaults
) {
315 File defaultFile
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
316 File legacyFileUI
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
);
317 File legacyFileCore
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
);
320 * If there is no file at the expected location, check the legacy
323 if (!defaultFile
.exists()) {
324 if (legacyFileCore
.exists()) {
325 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
);
326 } else if (legacyFileUI
.exists()) {
327 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
);
331 Set
<CustomXmlTraceDefinition
> defs
= new TreeSet
<>(new Comparator
<CustomXmlTraceDefinition
>() {
333 public int compare(CustomXmlTraceDefinition o1
, CustomXmlTraceDefinition o2
) {
334 int result
= o1
.categoryName
.compareTo(o2
.categoryName
);
338 return o1
.definitionName
.compareTo(o2
.definitionName
);
341 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
)));
342 if (includeDefaults
) {
343 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
)));
345 return defs
.toArray(new CustomXmlTraceDefinition
[0]);
348 private static void transferDefinitions(String defFile
) {
349 CustomXmlTraceDefinition
[] oldDefs
= loadAll(defFile
);
350 for (CustomXmlTraceDefinition def
: oldDefs
) {
351 /* Save in the new location */
358 * Load all the XML trace definitions in the given definitions file.
361 * Path to the definitions file to load
362 * @return The loaded trace definitions
364 public static CustomXmlTraceDefinition
[] loadAll(String path
) {
365 File file
= new File(path
);
366 if (!file
.canRead()) {
367 return new CustomXmlTraceDefinition
[0];
369 try (FileInputStream fis
= new FileInputStream(file
);) {
371 } catch (IOException e
) {
372 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
374 return new CustomXmlTraceDefinition
[0];
378 * Load all the XML trace definitions from the given stream
381 * An input stream from which to read the definitions
382 * @return The loaded trace definitions
385 public static CustomXmlTraceDefinition
[] loadAll(InputStream stream
) {
387 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
388 DocumentBuilder db
= dbf
.newDocumentBuilder();
390 // The following allows xml parsing without access to the dtd
391 db
.setEntityResolver(createEmptyEntityResolver());
393 // The following catches xml parsing exceptions
394 db
.setErrorHandler(createErrorHandler());
396 Document doc
= db
.parse(stream
);
397 Element root
= doc
.getDocumentElement();
398 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
399 return new CustomXmlTraceDefinition
[0];
402 ArrayList
<CustomXmlTraceDefinition
> defList
= new ArrayList
<>();
403 NodeList nodeList
= root
.getChildNodes();
404 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
405 Node node
= nodeList
.item(i
);
406 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
407 CustomXmlTraceDefinition def
= extractDefinition((Element
) node
);
413 return defList
.toArray(new CustomXmlTraceDefinition
[0]);
414 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
415 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + stream
, e
); //$NON-NLS-1$
417 return new CustomXmlTraceDefinition
[0];
421 * Load the given trace definition.
423 * @param definitionName
424 * Name of the XML trace definition to load
425 * @return The loaded trace definition
426 * @deprecated Use {@link #load(String, String)}
429 public static CustomXmlTraceDefinition
load(String definitionName
) {
430 return load(CUSTOM_XML_CATEGORY
, definitionName
);
434 * Load the given trace definition.
436 * @param categoryName
437 * Category of the definition to load
438 * @param definitionName
439 * Name of the XML trace definition to load
440 * @return The loaded trace definition
443 public static CustomXmlTraceDefinition
load(String categoryName
, String definitionName
) {
445 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
446 DocumentBuilder db
= dbf
.newDocumentBuilder();
448 // The following allows xml parsing without access to the dtd
449 EntityResolver resolver
= new EntityResolver() {
451 public InputSource
resolveEntity(String publicId
, String systemId
) {
452 String empty
= ""; //$NON-NLS-1$
453 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
454 return new InputSource(bais
);
457 db
.setEntityResolver(resolver
);
459 // The following catches xml parsing exceptions
460 db
.setErrorHandler(new ErrorHandler() {
462 public void error(SAXParseException saxparseexception
) throws SAXException
{
466 public void warning(SAXParseException saxparseexception
) throws SAXException
{
470 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
471 throw saxparseexception
;
475 CustomXmlTraceDefinition value
= lookupXmlDefinition(categoryName
, definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
477 value
= lookupXmlDefinition(categoryName
, definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
);
480 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
481 Activator
.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
486 private static CustomXmlTraceDefinition
lookupXmlDefinition(String categoryName
, String definitionName
, DocumentBuilder db
, String source
) throws SAXException
, IOException
{
487 File file
= new File(source
);
488 if (!file
.exists()) {
492 Document doc
= db
.parse(file
);
494 Element root
= doc
.getDocumentElement();
495 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
499 Element definitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
500 if (definitionElement
!= null) {
501 return extractDefinition(definitionElement
);
506 private static Element
findDefinitionElement(Element root
, String categoryName
, String definitionName
) {
507 NodeList nodeList
= root
.getChildNodes();
508 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
509 Node node
= nodeList
.item(i
);
510 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
511 Element element
= (Element
) node
;
512 String categoryAttribute
= element
.getAttribute(CATEGORY_ATTRIBUTE
);
513 if (categoryAttribute
.isEmpty()) {
514 categoryAttribute
= CUSTOM_XML_CATEGORY
;
516 String nameAttribute
= element
.getAttribute(NAME_ATTRIBUTE
);
517 if (categoryName
.equals(categoryAttribute
) &&
518 definitionName
.equals(nameAttribute
)) {
527 * Extract a trace definition from an XML element.
529 * @param definitionElement
531 * @return The extracted trace definition
533 public static CustomXmlTraceDefinition
extractDefinition(Element definitionElement
) {
534 CustomXmlTraceDefinition def
= new CustomXmlTraceDefinition();
536 def
.categoryName
= definitionElement
.getAttribute(CATEGORY_ATTRIBUTE
);
537 if (def
.categoryName
.isEmpty()) {
538 def
.categoryName
= CUSTOM_XML_CATEGORY
;
540 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
541 if (def
.definitionName
.isEmpty()) {
545 NodeList nodeList
= definitionElement
.getChildNodes();
546 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
547 Node node
= nodeList
.item(i
);
548 String nodeName
= node
.getNodeName();
549 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
550 Element formatElement
= (Element
) node
;
551 def
.timeStampOutputFormat
= formatElement
.getTextContent();
552 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
553 CustomXmlInputElement inputElement
= extractInputElement((Element
) node
);
554 if (inputElement
!= null) {
555 if (def
.rootInputElement
== null) {
556 def
.rootInputElement
= inputElement
;
561 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
562 Element outputColumnElement
= (Element
) node
;
563 OutputColumn outputColumn
= new OutputColumn();
564 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
565 def
.outputs
.add(outputColumn
);
571 private static CustomXmlInputElement
extractInputElement(Element inputElementElement
) {
572 CustomXmlInputElement inputElement
= new CustomXmlInputElement();
573 inputElement
.setElementName(inputElementElement
.getAttribute(NAME_ATTRIBUTE
));
574 inputElement
.setLogEntry((Boolean
.toString(true).equals(inputElementElement
.getAttribute(LOG_ENTRY_ATTRIBUTE
))) ?
true : false);
575 NodeList nodeList
= inputElementElement
.getChildNodes();
576 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
577 Node node
= nodeList
.item(i
);
578 String nodeName
= node
.getNodeName();
579 if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
580 Element inputDataElement
= (Element
) node
;
581 inputElement
.setInputName(inputDataElement
.getAttribute(NAME_ATTRIBUTE
));
582 inputElement
.setInputAction(Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
)));
583 inputElement
.setInputFormat(inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
));
584 } else if (nodeName
.equals(ATTRIBUTE_ELEMENT
)) {
585 Element attributeElement
= (Element
) node
;
587 String attributeName
= attributeElement
.getAttribute(NAME_ATTRIBUTE
);
588 String inputName
= null;
590 String inputFormat
= null;
591 NodeList attributeNodeList
= attributeElement
.getChildNodes();
592 for (int j
= 0; j
< attributeNodeList
.getLength(); j
++) {
593 Node attributeNode
= attributeNodeList
.item(j
);
594 String attributeNodeName
= attributeNode
.getNodeName();
595 if (attributeNodeName
.equals(INPUT_DATA_ELEMENT
)) {
596 Element inputDataElement
= (Element
) attributeNode
;
597 inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
598 inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
599 inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
602 inputElement
.addAttribute(new CustomXmlInputAttribute(attributeName
, inputName
, inputAction
, inputFormat
));
603 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
604 Element childInputElementElement
= (Element
) node
;
605 CustomXmlInputElement childInputElement
= extractInputElement(childInputElementElement
);
606 if (childInputElement
!= null) {
607 inputElement
.addChild(childInputElement
);
615 * Delete a definition from the currently loaded ones.
617 * @param definitionName
618 * The name of the definition to delete
619 * @deprecated Use {@link #delete(String, String)}
622 public static void delete(String definitionName
) {
623 delete(CUSTOM_XML_CATEGORY
, definitionName
);
627 * Delete a definition from the currently loaded ones.
629 * @param categoryName
630 * The category of the definition to delete
631 * @param definitionName
632 * The name of the definition to delete
635 public static void delete(String categoryName
, String definitionName
) {
637 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
638 DocumentBuilder db
= dbf
.newDocumentBuilder();
640 // The following allows xml parsing without access to the dtd
641 EntityResolver resolver
= new EntityResolver() {
643 public InputSource
resolveEntity(String publicId
, String systemId
) {
644 String empty
= ""; //$NON-NLS-1$
645 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
646 return new InputSource(bais
);
649 db
.setEntityResolver(resolver
);
651 // The following catches xml parsing exceptions
652 db
.setErrorHandler(new ErrorHandler() {
654 public void error(SAXParseException saxparseexception
) throws SAXException
{
658 public void warning(SAXParseException saxparseexception
) throws SAXException
{
662 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
663 throw saxparseexception
;
667 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
668 Document doc
= db
.parse(file
);
670 Element root
= doc
.getDocumentElement();
671 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
675 Element definitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
676 if (definitionElement
!= null) {
677 root
.removeChild(definitionElement
);
680 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
681 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
683 // initialize StreamResult with File object to save to file
684 StreamResult result
= new StreamResult(new StringWriter());
685 DOMSource source
= new DOMSource(doc
);
686 transformer
.transform(source
, result
);
687 String xmlString
= result
.getWriter().toString();
689 try (FileWriter writer
= new FileWriter(file
);) {
690 writer
.write(xmlString
);
693 TmfTraceType
.removeCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
694 // Check if default definition needs to be reloaded
695 TmfTraceType
.addCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
697 } catch (ParserConfigurationException
| SAXException
| IOException
| TransformerFactoryConfigurationError
| TransformerException e
) {
698 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$