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 * Category of the trace type
134 * Name of the trace type
136 * The top-level XML element
138 * The list of output columns
139 * @param timeStampOutputFormat
140 * The timestamp format to use
143 public CustomXmlTraceDefinition(String category
, String traceType
, CustomXmlInputElement rootElement
,
144 List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
145 this.categoryName
= category
;
146 this.definitionName
= traceType
;
147 this.rootInputElement
= rootElement
;
148 this.outputs
= outputs
;
149 this.timeStampOutputFormat
= timeStampOutputFormat
;
154 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
158 public void save(String path
) {
160 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
161 DocumentBuilder db
= dbf
.newDocumentBuilder();
163 // The following allows xml parsing without access to the dtd
164 db
.setEntityResolver(createEmptyEntityResolver());
166 // The following catches xml parsing exceptions
167 db
.setErrorHandler(createErrorHandler());
170 File file
= new File(path
);
171 if (file
.canRead()) {
172 doc
= db
.parse(file
);
173 if (!doc
.getDocumentElement().getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
177 doc
= db
.newDocument();
178 Node node
= doc
.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
);
179 doc
.appendChild(node
);
182 Element root
= doc
.getDocumentElement();
184 Element oldDefinitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
185 if (oldDefinitionElement
!= null) {
186 root
.removeChild(oldDefinitionElement
);
188 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
189 root
.appendChild(definitionElement
);
190 definitionElement
.setAttribute(CATEGORY_ATTRIBUTE
, categoryName
);
191 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
193 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
194 definitionElement
.appendChild(formatElement
);
195 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
197 if (rootInputElement
!= null) {
198 definitionElement
.appendChild(createInputElementElement(rootInputElement
, doc
));
201 if (outputs
!= null) {
202 for (OutputColumn output
: outputs
) {
203 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
204 definitionElement
.appendChild(outputColumnElement
);
205 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
209 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
210 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
212 // initialize StreamResult with File object to save to file
213 StreamResult result
= new StreamResult(new StringWriter());
214 DOMSource source
= new DOMSource(doc
);
215 transformer
.transform(source
, result
);
216 String xmlString
= result
.getWriter().toString();
218 try (FileWriter writer
= new FileWriter(file
);) {
219 writer
.write(xmlString
);
222 TmfTraceType
.addCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
224 } catch (ParserConfigurationException
| TransformerFactoryConfigurationError
| TransformerException
| IOException
| SAXException e
) {
225 Activator
.logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
229 private Element
createInputElementElement(CustomXmlInputElement inputElement
, Document doc
) {
230 Element inputElementElement
= doc
.createElement(INPUT_ELEMENT_ELEMENT
);
231 inputElementElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.getElementName());
233 if (inputElement
.isLogEntry()) {
234 inputElementElement
.setAttribute(LOG_ENTRY_ATTRIBUTE
, Boolean
.toString(inputElement
.isLogEntry()));
237 if (inputElement
.getParentElement() != null) {
238 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
239 inputElementElement
.appendChild(inputDataElement
);
240 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.getInputName());
241 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputElement
.getInputAction()));
242 if (inputElement
.getInputFormat() != null) {
243 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputElement
.getInputFormat());
247 if (inputElement
.getAttributes() != null) {
248 for (CustomXmlInputAttribute attribute
: inputElement
.getAttributes()) {
249 Element inputAttributeElement
= doc
.createElement(ATTRIBUTE_ELEMENT
);
250 inputElementElement
.appendChild(inputAttributeElement
);
251 inputAttributeElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.getAttributeName());
252 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
253 inputAttributeElement
.appendChild(inputDataElement
);
254 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.getInputName());
255 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(attribute
.getInputAction()));
256 if (attribute
.getInputFormat() != null) {
257 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, attribute
.getInputFormat());
262 if (inputElement
.getChildElements() != null) {
263 for (CustomXmlInputElement childInputElement
: inputElement
.getChildElements()) {
264 inputElementElement
.appendChild(createInputElementElement(childInputElement
, doc
));
268 return inputElementElement
;
272 * Load all custom XML trace definitions, including the user-defined and
273 * default (built-in) parsers.
275 * @return The loaded trace definitions
277 public static CustomXmlTraceDefinition
[] loadAll() {
278 return loadAll(true);
282 * Load all custom XML trace definitions, including the user-defined and,
283 * optionally, the default (built-in) parsers.
285 * @param includeDefaults
286 * if true, the default (built-in) parsers are included
288 * @return The loaded trace definitions
291 public static CustomXmlTraceDefinition
[] loadAll(boolean includeDefaults
) {
292 File defaultFile
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
293 File legacyFileUI
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
);
294 File legacyFileCore
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
);
297 * If there is no file at the expected location, check the legacy
300 if (!defaultFile
.exists()) {
301 if (legacyFileCore
.exists()) {
302 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
);
303 } else if (legacyFileUI
.exists()) {
304 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
);
308 Set
<CustomXmlTraceDefinition
> defs
= new TreeSet
<>(new Comparator
<CustomXmlTraceDefinition
>() {
310 public int compare(CustomXmlTraceDefinition o1
, CustomXmlTraceDefinition o2
) {
311 int result
= o1
.categoryName
.compareTo(o2
.categoryName
);
315 return o1
.definitionName
.compareTo(o2
.definitionName
);
318 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
)));
319 if (includeDefaults
) {
320 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
)));
322 return defs
.toArray(new CustomXmlTraceDefinition
[0]);
325 private static void transferDefinitions(String defFile
) {
326 CustomXmlTraceDefinition
[] oldDefs
= loadAll(defFile
);
327 for (CustomXmlTraceDefinition def
: oldDefs
) {
328 /* Save in the new location */
335 * Load all the XML trace definitions in the given definitions file.
338 * Path to the definitions file to load
339 * @return The loaded trace definitions
341 public static CustomXmlTraceDefinition
[] loadAll(String path
) {
342 File file
= new File(path
);
343 if (!file
.canRead()) {
344 return new CustomXmlTraceDefinition
[0];
346 try (FileInputStream fis
= new FileInputStream(file
);) {
348 } catch (IOException e
) {
349 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
351 return new CustomXmlTraceDefinition
[0];
355 * Load all the XML trace definitions from the given stream
358 * An input stream from which to read the definitions
359 * @return The loaded trace definitions
362 public static CustomXmlTraceDefinition
[] loadAll(InputStream stream
) {
364 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
365 DocumentBuilder db
= dbf
.newDocumentBuilder();
367 // The following allows xml parsing without access to the dtd
368 db
.setEntityResolver(createEmptyEntityResolver());
370 // The following catches xml parsing exceptions
371 db
.setErrorHandler(createErrorHandler());
373 Document doc
= db
.parse(stream
);
374 Element root
= doc
.getDocumentElement();
375 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
376 return new CustomXmlTraceDefinition
[0];
379 ArrayList
<CustomXmlTraceDefinition
> defList
= new ArrayList
<>();
380 NodeList nodeList
= root
.getChildNodes();
381 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
382 Node node
= nodeList
.item(i
);
383 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
384 CustomXmlTraceDefinition def
= extractDefinition((Element
) node
);
390 return defList
.toArray(new CustomXmlTraceDefinition
[0]);
391 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
392 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + stream
, e
); //$NON-NLS-1$
394 return new CustomXmlTraceDefinition
[0];
398 * Load the given trace definition.
400 * @param categoryName
401 * Category of the definition to load
402 * @param definitionName
403 * Name of the XML trace definition to load
404 * @return The loaded trace definition
407 public static CustomXmlTraceDefinition
load(String categoryName
, String definitionName
) {
409 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
410 DocumentBuilder db
= dbf
.newDocumentBuilder();
412 // The following allows xml parsing without access to the dtd
413 EntityResolver resolver
= new EntityResolver() {
415 public InputSource
resolveEntity(String publicId
, String systemId
) {
416 String empty
= ""; //$NON-NLS-1$
417 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
418 return new InputSource(bais
);
421 db
.setEntityResolver(resolver
);
423 // The following catches xml parsing exceptions
424 db
.setErrorHandler(new ErrorHandler() {
426 public void error(SAXParseException saxparseexception
) throws SAXException
{
430 public void warning(SAXParseException saxparseexception
) throws SAXException
{
434 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
435 throw saxparseexception
;
439 CustomXmlTraceDefinition value
= lookupXmlDefinition(categoryName
, definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
441 value
= lookupXmlDefinition(categoryName
, definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
);
444 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
445 Activator
.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
450 private static CustomXmlTraceDefinition
lookupXmlDefinition(String categoryName
, String definitionName
, DocumentBuilder db
, String source
) throws SAXException
, IOException
{
451 File file
= new File(source
);
452 if (!file
.exists()) {
456 Document doc
= db
.parse(file
);
458 Element root
= doc
.getDocumentElement();
459 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
463 Element definitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
464 if (definitionElement
!= null) {
465 return extractDefinition(definitionElement
);
470 private static Element
findDefinitionElement(Element root
, String categoryName
, String definitionName
) {
471 NodeList nodeList
= root
.getChildNodes();
472 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
473 Node node
= nodeList
.item(i
);
474 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
475 Element element
= (Element
) node
;
476 String categoryAttribute
= element
.getAttribute(CATEGORY_ATTRIBUTE
);
477 if (categoryAttribute
.isEmpty()) {
478 categoryAttribute
= CUSTOM_XML_CATEGORY
;
480 String nameAttribute
= element
.getAttribute(NAME_ATTRIBUTE
);
481 if (categoryName
.equals(categoryAttribute
) &&
482 definitionName
.equals(nameAttribute
)) {
491 * Extract a trace definition from an XML element.
493 * @param definitionElement
495 * @return The extracted trace definition
497 public static CustomXmlTraceDefinition
extractDefinition(Element definitionElement
) {
498 CustomXmlTraceDefinition def
= new CustomXmlTraceDefinition();
500 def
.categoryName
= definitionElement
.getAttribute(CATEGORY_ATTRIBUTE
);
501 if (def
.categoryName
.isEmpty()) {
502 def
.categoryName
= CUSTOM_XML_CATEGORY
;
504 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
505 if (def
.definitionName
.isEmpty()) {
509 NodeList nodeList
= definitionElement
.getChildNodes();
510 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
511 Node node
= nodeList
.item(i
);
512 String nodeName
= node
.getNodeName();
513 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
514 Element formatElement
= (Element
) node
;
515 def
.timeStampOutputFormat
= formatElement
.getTextContent();
516 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
517 CustomXmlInputElement inputElement
= extractInputElement((Element
) node
);
518 if (inputElement
!= null) {
519 if (def
.rootInputElement
== null) {
520 def
.rootInputElement
= inputElement
;
525 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
526 Element outputColumnElement
= (Element
) node
;
527 OutputColumn outputColumn
= new OutputColumn();
528 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
529 def
.outputs
.add(outputColumn
);
535 private static CustomXmlInputElement
extractInputElement(Element inputElementElement
) {
536 CustomXmlInputElement inputElement
= new CustomXmlInputElement();
537 inputElement
.setElementName(inputElementElement
.getAttribute(NAME_ATTRIBUTE
));
538 inputElement
.setLogEntry((Boolean
.toString(true).equals(inputElementElement
.getAttribute(LOG_ENTRY_ATTRIBUTE
))) ?
true : false);
539 NodeList nodeList
= inputElementElement
.getChildNodes();
540 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
541 Node node
= nodeList
.item(i
);
542 String nodeName
= node
.getNodeName();
543 if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
544 Element inputDataElement
= (Element
) node
;
545 inputElement
.setInputName(inputDataElement
.getAttribute(NAME_ATTRIBUTE
));
546 inputElement
.setInputAction(Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
)));
547 inputElement
.setInputFormat(inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
));
548 } else if (nodeName
.equals(ATTRIBUTE_ELEMENT
)) {
549 Element attributeElement
= (Element
) node
;
551 String attributeName
= attributeElement
.getAttribute(NAME_ATTRIBUTE
);
552 String inputName
= null;
554 String inputFormat
= null;
555 NodeList attributeNodeList
= attributeElement
.getChildNodes();
556 for (int j
= 0; j
< attributeNodeList
.getLength(); j
++) {
557 Node attributeNode
= attributeNodeList
.item(j
);
558 String attributeNodeName
= attributeNode
.getNodeName();
559 if (attributeNodeName
.equals(INPUT_DATA_ELEMENT
)) {
560 Element inputDataElement
= (Element
) attributeNode
;
561 inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
562 inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
563 inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
566 inputElement
.addAttribute(new CustomXmlInputAttribute(attributeName
, inputName
, inputAction
, inputFormat
));
567 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
568 Element childInputElementElement
= (Element
) node
;
569 CustomXmlInputElement childInputElement
= extractInputElement(childInputElementElement
);
570 if (childInputElement
!= null) {
571 inputElement
.addChild(childInputElement
);
579 * Delete a definition from the currently loaded ones.
581 * @param categoryName
582 * The category of the definition to delete
583 * @param definitionName
584 * The name of the definition to delete
587 public static void delete(String categoryName
, String definitionName
) {
589 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
590 DocumentBuilder db
= dbf
.newDocumentBuilder();
592 // The following allows xml parsing without access to the dtd
593 EntityResolver resolver
= new EntityResolver() {
595 public InputSource
resolveEntity(String publicId
, String systemId
) {
596 String empty
= ""; //$NON-NLS-1$
597 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
598 return new InputSource(bais
);
601 db
.setEntityResolver(resolver
);
603 // The following catches xml parsing exceptions
604 db
.setErrorHandler(new ErrorHandler() {
606 public void error(SAXParseException saxparseexception
) throws SAXException
{
610 public void warning(SAXParseException saxparseexception
) throws SAXException
{
614 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
615 throw saxparseexception
;
619 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
620 Document doc
= db
.parse(file
);
622 Element root
= doc
.getDocumentElement();
623 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
627 Element definitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
628 if (definitionElement
!= null) {
629 root
.removeChild(definitionElement
);
632 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
633 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
635 // initialize StreamResult with File object to save to file
636 StreamResult result
= new StreamResult(new StringWriter());
637 DOMSource source
= new DOMSource(doc
);
638 transformer
.transform(source
, result
);
639 String xmlString
= result
.getWriter().toString();
641 try (FileWriter writer
= new FileWriter(file
);) {
642 writer
.write(xmlString
);
645 TmfTraceType
.removeCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
646 // Check if default definition needs to be reloaded
647 TmfTraceType
.addCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
649 } catch (ParserConfigurationException
| SAXException
| IOException
| TransformerFactoryConfigurationError
| TransformerException e
) {
650 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$