1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 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é
59 public class CustomXmlTraceDefinition
extends CustomTraceDefinition
{
62 public static final String TAG_IGNORE
= Messages
.CustomXmlTraceDefinition_ignoreTag
;
65 * Custom XML label used internally and therefore should not be externalized
67 public static final String CUSTOM_XML_CATEGORY
= "Custom XML"; //$NON-NLS-1$
70 /** Name of the default XML definitions file */
71 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME
= "custom_xml_default_parsers.xml"; //$NON-NLS-1$
73 /** Name of the XML definitions file */
74 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
= "custom_xml_parsers.xml"; //$NON-NLS-1$
76 /** Path to the XML definitions file */
77 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
=
78 Platform
.getInstallLocation().getURL().getPath() + "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
79 CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME
;
81 /** Path to the XML definitions file */
82 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
=
83 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
86 * Legacy path to the XML definitions file (in the UI plug-in of linux tools) TODO Remove
87 * once we feel the transition phase is over.
89 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
=
90 Activator
.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
91 .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
92 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
95 * Legacy path to the XML definitions file (in the core plug-in of linux tools) TODO Remove
96 * once we feel the transition phase is over.
98 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
=
99 Activator
.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
100 .append("org.eclipse.linuxtools.tmf.core") //$NON-NLS-1$
101 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
103 // TODO: These strings should not be externalized
104 private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
= Messages
.CustomXmlTraceDefinition_definitionRootElement
;
105 private static final String DEFINITION_ELEMENT
= Messages
.CustomXmlTraceDefinition_definition
;
106 private static final String CATEGORY_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_category
;
107 private static final String NAME_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_name
;
108 private static final String LOG_ENTRY_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_logEntry
;
109 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= Messages
.CustomXmlTraceDefinition_timestampOutputFormat
;
110 private static final String INPUT_ELEMENT_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputElement
;
111 private static final String ATTRIBUTE_ELEMENT
= Messages
.CustomXmlTraceDefinition_attribute
;
112 private static final String INPUT_DATA_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputData
;
113 private static final String ACTION_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_action
;
114 private static final String FORMAT_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_format
;
115 private static final String OUTPUT_COLUMN_ELEMENT
= Messages
.CustomXmlTraceDefinition_outputColumn
;
117 /** Top-level input element */
118 public CustomXmlInputElement rootInputElement
;
121 * Default constructor
123 public CustomXmlTraceDefinition() {
124 this(CUSTOM_XML_CATEGORY
, "", null, new ArrayList
<OutputColumn
>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
131 * Category of the trace type
133 * Name of the trace type
135 * The top-level XML element
137 * The list of output columns
138 * @param timeStampOutputFormat
139 * The timestamp format to use
141 public CustomXmlTraceDefinition(String category
, String traceType
, CustomXmlInputElement rootElement
,
142 List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
143 this.categoryName
= category
;
144 this.definitionName
= traceType
;
145 this.rootInputElement
= rootElement
;
146 this.outputs
= outputs
;
147 this.timeStampOutputFormat
= timeStampOutputFormat
;
152 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
156 public void save(String path
) {
158 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
159 DocumentBuilder db
= dbf
.newDocumentBuilder();
161 // The following allows xml parsing without access to the dtd
162 db
.setEntityResolver(createEmptyEntityResolver());
164 // The following catches xml parsing exceptions
165 db
.setErrorHandler(createErrorHandler());
168 File file
= new File(path
);
169 if (file
.canRead()) {
170 doc
= db
.parse(file
);
171 if (!doc
.getDocumentElement().getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
172 Activator
.logError(String
.format("Error saving CustomXmlTraceDefinition: path=%s is not a valid custom parser file", path
)); //$NON-NLS-1$
176 doc
= db
.newDocument();
177 Node node
= doc
.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
);
178 doc
.appendChild(node
);
181 Element root
= doc
.getDocumentElement();
183 Element oldDefinitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
184 if (oldDefinitionElement
!= null) {
185 root
.removeChild(oldDefinitionElement
);
187 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
188 root
.appendChild(definitionElement
);
189 definitionElement
.setAttribute(CATEGORY_ATTRIBUTE
, categoryName
);
190 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
192 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
193 definitionElement
.appendChild(formatElement
);
194 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
196 if (rootInputElement
!= null) {
197 definitionElement
.appendChild(createInputElementElement(rootInputElement
, doc
));
200 if (outputs
!= null) {
201 for (OutputColumn output
: outputs
) {
202 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
203 definitionElement
.appendChild(outputColumnElement
);
204 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
208 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
209 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
211 // initialize StreamResult with File object to save to file
212 StreamResult result
= new StreamResult(new StringWriter());
213 DOMSource source
= new DOMSource(doc
);
214 transformer
.transform(source
, result
);
215 String xmlString
= result
.getWriter().toString();
217 try (FileWriter writer
= new FileWriter(file
);) {
218 writer
.write(xmlString
);
221 TmfTraceType
.addCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
223 } catch (ParserConfigurationException
| TransformerFactoryConfigurationError
| TransformerException
| IOException
| SAXException e
) {
224 Activator
.logError("Error saving CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
228 private Element
createInputElementElement(CustomXmlInputElement inputElement
, Document doc
) {
229 Element inputElementElement
= doc
.createElement(INPUT_ELEMENT_ELEMENT
);
230 inputElementElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.getElementName());
232 if (inputElement
.isLogEntry()) {
233 inputElementElement
.setAttribute(LOG_ENTRY_ATTRIBUTE
, Boolean
.toString(inputElement
.isLogEntry()));
236 if (inputElement
.getParentElement() != null) {
237 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
238 inputElementElement
.appendChild(inputDataElement
);
239 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.getInputName());
240 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputElement
.getInputAction()));
241 if (inputElement
.getInputFormat() != null) {
242 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputElement
.getInputFormat());
246 if (inputElement
.getAttributes() != null) {
247 for (CustomXmlInputAttribute attribute
: inputElement
.getAttributes()) {
248 Element inputAttributeElement
= doc
.createElement(ATTRIBUTE_ELEMENT
);
249 inputElementElement
.appendChild(inputAttributeElement
);
250 inputAttributeElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.getAttributeName());
251 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
252 inputAttributeElement
.appendChild(inputDataElement
);
253 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.getInputName());
254 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(attribute
.getInputAction()));
255 if (attribute
.getInputFormat() != null) {
256 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, attribute
.getInputFormat());
261 if (inputElement
.getChildElements() != null) {
262 for (CustomXmlInputElement childInputElement
: inputElement
.getChildElements()) {
263 inputElementElement
.appendChild(createInputElementElement(childInputElement
, doc
));
267 return inputElementElement
;
271 * Load all custom XML trace definitions, including the user-defined and
272 * default (built-in) parsers.
274 * @return The loaded trace definitions
276 public static CustomXmlTraceDefinition
[] loadAll() {
277 return loadAll(true);
281 * Load all custom XML trace definitions, including the user-defined and,
282 * optionally, the default (built-in) parsers.
284 * @param includeDefaults
285 * if true, the default (built-in) parsers are included
287 * @return The loaded trace definitions
289 public static CustomXmlTraceDefinition
[] loadAll(boolean includeDefaults
) {
290 File defaultFile
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
291 File legacyFileUI
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
);
292 File legacyFileCore
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
);
295 * If there is no file at the expected location, check the legacy
298 if (!defaultFile
.exists()) {
299 if (legacyFileCore
.exists()) {
300 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
);
301 } else if (legacyFileUI
.exists()) {
302 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
);
306 Set
<CustomXmlTraceDefinition
> defs
= new TreeSet
<>(new Comparator
<CustomXmlTraceDefinition
>() {
308 public int compare(CustomXmlTraceDefinition o1
, CustomXmlTraceDefinition o2
) {
309 int result
= o1
.categoryName
.compareTo(o2
.categoryName
);
313 return o1
.definitionName
.compareTo(o2
.definitionName
);
316 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
)));
317 if (includeDefaults
) {
318 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
)));
320 return defs
.toArray(new CustomXmlTraceDefinition
[0]);
323 private static void transferDefinitions(String defFile
) {
324 CustomXmlTraceDefinition
[] oldDefs
= loadAll(defFile
);
325 for (CustomXmlTraceDefinition def
: oldDefs
) {
326 /* Save in the new location */
333 * Load all the XML trace definitions in the given definitions file.
336 * Path to the definitions file to load
337 * @return The loaded trace definitions
339 public static CustomXmlTraceDefinition
[] loadAll(String path
) {
340 File file
= new File(path
);
341 if (!file
.canRead()) {
342 return new CustomXmlTraceDefinition
[0];
344 try (FileInputStream fis
= new FileInputStream(file
);) {
346 } catch (IOException e
) {
347 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
349 return new CustomXmlTraceDefinition
[0];
353 * Load all the XML trace definitions from the given stream
356 * An input stream from which to read the definitions
357 * @return The loaded trace definitions
359 public static CustomXmlTraceDefinition
[] loadAll(InputStream stream
) {
361 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
362 DocumentBuilder db
= dbf
.newDocumentBuilder();
364 // The following allows xml parsing without access to the dtd
365 db
.setEntityResolver(createEmptyEntityResolver());
367 // The following catches xml parsing exceptions
368 db
.setErrorHandler(createErrorHandler());
370 Document doc
= db
.parse(stream
);
371 Element root
= doc
.getDocumentElement();
372 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
373 return new CustomXmlTraceDefinition
[0];
376 ArrayList
<CustomXmlTraceDefinition
> defList
= new ArrayList
<>();
377 NodeList nodeList
= root
.getChildNodes();
378 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
379 Node node
= nodeList
.item(i
);
380 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
381 CustomXmlTraceDefinition def
= extractDefinition((Element
) node
);
387 return defList
.toArray(new CustomXmlTraceDefinition
[0]);
388 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
389 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + stream
, e
); //$NON-NLS-1$
391 return new CustomXmlTraceDefinition
[0];
395 * Load the given trace definition.
397 * @param categoryName
398 * Category of the definition to load
399 * @param definitionName
400 * Name of the XML trace definition to load
401 * @return The loaded trace definition
403 public static CustomXmlTraceDefinition
load(String categoryName
, String definitionName
) {
405 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
406 DocumentBuilder db
= dbf
.newDocumentBuilder();
408 // The following allows xml parsing without access to the dtd
409 EntityResolver resolver
= new EntityResolver() {
411 public InputSource
resolveEntity(String publicId
, String systemId
) {
412 String empty
= ""; //$NON-NLS-1$
413 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
414 return new InputSource(bais
);
417 db
.setEntityResolver(resolver
);
419 // The following catches xml parsing exceptions
420 db
.setErrorHandler(new ErrorHandler() {
422 public void error(SAXParseException saxparseexception
) throws SAXException
{
426 public void warning(SAXParseException saxparseexception
) throws SAXException
{
430 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
431 throw saxparseexception
;
435 CustomXmlTraceDefinition value
= lookupXmlDefinition(categoryName
, definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
437 value
= lookupXmlDefinition(categoryName
, definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
);
440 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
441 Activator
.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
446 private static CustomXmlTraceDefinition
lookupXmlDefinition(String categoryName
, String definitionName
, DocumentBuilder db
, String source
) throws SAXException
, IOException
{
447 File file
= new File(source
);
448 if (!file
.exists()) {
452 Document doc
= db
.parse(file
);
454 Element root
= doc
.getDocumentElement();
455 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
459 Element definitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
460 if (definitionElement
!= null) {
461 return extractDefinition(definitionElement
);
466 private static Element
findDefinitionElement(Element root
, String categoryName
, String definitionName
) {
467 NodeList nodeList
= root
.getChildNodes();
468 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
469 Node node
= nodeList
.item(i
);
470 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
471 Element element
= (Element
) node
;
472 String categoryAttribute
= element
.getAttribute(CATEGORY_ATTRIBUTE
);
473 if (categoryAttribute
.isEmpty()) {
474 categoryAttribute
= CUSTOM_XML_CATEGORY
;
476 String nameAttribute
= element
.getAttribute(NAME_ATTRIBUTE
);
477 if (categoryName
.equals(categoryAttribute
) &&
478 definitionName
.equals(nameAttribute
)) {
487 * Extract a trace definition from an XML element.
489 * @param definitionElement
491 * @return The extracted trace definition
493 public static CustomXmlTraceDefinition
extractDefinition(Element definitionElement
) {
494 CustomXmlTraceDefinition def
= new CustomXmlTraceDefinition();
496 def
.categoryName
= definitionElement
.getAttribute(CATEGORY_ATTRIBUTE
);
497 if (def
.categoryName
.isEmpty()) {
498 def
.categoryName
= CUSTOM_XML_CATEGORY
;
500 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
501 if (def
.definitionName
.isEmpty()) {
505 NodeList nodeList
= definitionElement
.getChildNodes();
506 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
507 Node node
= nodeList
.item(i
);
508 String nodeName
= node
.getNodeName();
509 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
510 Element formatElement
= (Element
) node
;
511 def
.timeStampOutputFormat
= formatElement
.getTextContent();
512 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
513 CustomXmlInputElement inputElement
= extractInputElement((Element
) node
);
514 if (inputElement
!= null) {
515 if (def
.rootInputElement
== null) {
516 def
.rootInputElement
= inputElement
;
521 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
522 Element outputColumnElement
= (Element
) node
;
523 OutputColumn outputColumn
= new OutputColumn();
524 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
525 def
.outputs
.add(outputColumn
);
531 private static CustomXmlInputElement
extractInputElement(Element inputElementElement
) {
532 CustomXmlInputElement inputElement
= new CustomXmlInputElement();
533 inputElement
.setElementName(inputElementElement
.getAttribute(NAME_ATTRIBUTE
));
534 inputElement
.setLogEntry((Boolean
.toString(true).equals(inputElementElement
.getAttribute(LOG_ENTRY_ATTRIBUTE
))) ?
true : false);
535 NodeList nodeList
= inputElementElement
.getChildNodes();
536 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
537 Node node
= nodeList
.item(i
);
538 String nodeName
= node
.getNodeName();
539 if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
540 Element inputDataElement
= (Element
) node
;
541 inputElement
.setInputName(inputDataElement
.getAttribute(NAME_ATTRIBUTE
));
542 inputElement
.setInputAction(Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
)));
543 inputElement
.setInputFormat(inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
));
544 } else if (nodeName
.equals(ATTRIBUTE_ELEMENT
)) {
545 Element attributeElement
= (Element
) node
;
547 String attributeName
= attributeElement
.getAttribute(NAME_ATTRIBUTE
);
548 String inputName
= null;
550 String inputFormat
= null;
551 NodeList attributeNodeList
= attributeElement
.getChildNodes();
552 for (int j
= 0; j
< attributeNodeList
.getLength(); j
++) {
553 Node attributeNode
= attributeNodeList
.item(j
);
554 String attributeNodeName
= attributeNode
.getNodeName();
555 if (attributeNodeName
.equals(INPUT_DATA_ELEMENT
)) {
556 Element inputDataElement
= (Element
) attributeNode
;
557 inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
558 inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
559 inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
562 inputElement
.addAttribute(new CustomXmlInputAttribute(attributeName
, inputName
, inputAction
, inputFormat
));
563 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
564 Element childInputElementElement
= (Element
) node
;
565 CustomXmlInputElement childInputElement
= extractInputElement(childInputElementElement
);
566 if (childInputElement
!= null) {
567 inputElement
.addChild(childInputElement
);
575 * Delete a definition from the currently loaded ones.
577 * @param categoryName
578 * The category of the definition to delete
579 * @param definitionName
580 * The name of the definition to delete
582 public static void delete(String categoryName
, String definitionName
) {
584 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
585 DocumentBuilder db
= dbf
.newDocumentBuilder();
587 // The following allows xml parsing without access to the dtd
588 EntityResolver resolver
= new EntityResolver() {
590 public InputSource
resolveEntity(String publicId
, String systemId
) {
591 String empty
= ""; //$NON-NLS-1$
592 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
593 return new InputSource(bais
);
596 db
.setEntityResolver(resolver
);
598 // The following catches xml parsing exceptions
599 db
.setErrorHandler(new ErrorHandler() {
601 public void error(SAXParseException saxparseexception
) throws SAXException
{
605 public void warning(SAXParseException saxparseexception
) throws SAXException
{
609 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
610 throw saxparseexception
;
614 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
615 Document doc
= db
.parse(file
);
617 Element root
= doc
.getDocumentElement();
618 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
622 Element definitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
623 if (definitionElement
!= null) {
624 root
.removeChild(definitionElement
);
627 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
628 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
630 // initialize StreamResult with File object to save to file
631 StreamResult result
= new StreamResult(new StringWriter());
632 DOMSource source
= new DOMSource(doc
);
633 transformer
.transform(source
, result
);
634 String xmlString
= result
.getWriter().toString();
636 try (FileWriter writer
= new FileWriter(file
);) {
637 writer
.write(xmlString
);
640 TmfTraceType
.removeCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
641 // Check if default definition needs to be reloaded
642 TmfTraceType
.addCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
644 } catch (ParserConfigurationException
| SAXException
| IOException
| TransformerFactoryConfigurationError
| TransformerException e
) {
645 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$