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 static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
18 import java
.io
.ByteArrayInputStream
;
20 import java
.io
.FileInputStream
;
21 import java
.io
.FileWriter
;
22 import java
.io
.IOException
;
23 import java
.io
.InputStream
;
24 import java
.io
.StringWriter
;
25 import java
.util
.ArrayList
;
26 import java
.util
.Arrays
;
27 import java
.util
.Comparator
;
28 import java
.util
.List
;
30 import java
.util
.TreeSet
;
32 import javax
.xml
.parsers
.DocumentBuilder
;
33 import javax
.xml
.parsers
.DocumentBuilderFactory
;
34 import javax
.xml
.parsers
.ParserConfigurationException
;
35 import javax
.xml
.transform
.OutputKeys
;
36 import javax
.xml
.transform
.Transformer
;
37 import javax
.xml
.transform
.TransformerException
;
38 import javax
.xml
.transform
.TransformerFactory
;
39 import javax
.xml
.transform
.TransformerFactoryConfigurationError
;
40 import javax
.xml
.transform
.dom
.DOMSource
;
41 import javax
.xml
.transform
.stream
.StreamResult
;
43 import org
.eclipse
.core
.runtime
.Platform
;
44 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
46 import org
.w3c
.dom
.Document
;
47 import org
.w3c
.dom
.Element
;
48 import org
.w3c
.dom
.Node
;
49 import org
.w3c
.dom
.NodeList
;
50 import org
.xml
.sax
.EntityResolver
;
51 import org
.xml
.sax
.ErrorHandler
;
52 import org
.xml
.sax
.InputSource
;
53 import org
.xml
.sax
.SAXException
;
54 import org
.xml
.sax
.SAXParseException
;
57 * Trace definition for custom XML traces.
59 * @author Patrick Tassé
61 public class CustomXmlTraceDefinition
extends CustomTraceDefinition
{
64 public static final String TAG_IGNORE
= Messages
.CustomXmlTraceDefinition_ignoreTag
;
67 * Custom XML label used internally and therefore should not be externalized
69 public static final String CUSTOM_XML_CATEGORY
= "Custom XML"; //$NON-NLS-1$
72 /** Name of the default XML definitions file */
73 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME
= "custom_xml_default_parsers.xml"; //$NON-NLS-1$
75 /** Name of the XML definitions file */
76 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
= "custom_xml_parsers.xml"; //$NON-NLS-1$
78 /** Path to the XML definitions file */
79 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
=
80 Platform
.getInstallLocation().getURL().getPath() + "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
81 CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME
;
83 /** Path to the XML definitions file */
84 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
=
85 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
88 * Legacy path to the XML definitions file (in the UI plug-in of linux tools) TODO Remove
89 * once we feel the transition phase is over.
91 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
=
92 Activator
.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
93 .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
94 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
97 * Legacy path to the XML definitions file (in the core plug-in of linux tools) TODO Remove
98 * once we feel the transition phase is over.
100 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
=
101 Activator
.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
102 .append("org.eclipse.linuxtools.tmf.core") //$NON-NLS-1$
103 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
105 // TODO: These strings should not be externalized
106 private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
= Messages
.CustomXmlTraceDefinition_definitionRootElement
;
107 private static final String DEFINITION_ELEMENT
= Messages
.CustomXmlTraceDefinition_definition
;
108 private static final String CATEGORY_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_category
;
109 private static final String NAME_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_name
;
110 private static final String LOG_ENTRY_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_logEntry
;
111 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= Messages
.CustomXmlTraceDefinition_timestampOutputFormat
;
112 private static final String INPUT_ELEMENT_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputElement
;
113 private static final String ATTRIBUTE_ELEMENT
= Messages
.CustomXmlTraceDefinition_attribute
;
114 private static final String INPUT_DATA_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputData
;
115 private static final String ACTION_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_action
;
116 private static final String FORMAT_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_format
;
117 private static final String OUTPUT_COLUMN_ELEMENT
= Messages
.CustomXmlTraceDefinition_outputColumn
;
119 /** Top-level input element */
120 public CustomXmlInputElement rootInputElement
;
123 * Default constructor
125 public CustomXmlTraceDefinition() {
126 this(CUSTOM_XML_CATEGORY
, "", null, new ArrayList
<OutputColumn
>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
133 * Category of the trace type
135 * Name of the trace type
137 * The top-level XML element
139 * The list of output columns
140 * @param timeStampOutputFormat
141 * 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
290 public static CustomXmlTraceDefinition
[] loadAll(boolean includeDefaults
) {
291 File defaultFile
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
292 File legacyFileUI
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
);
293 File legacyFileCore
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
);
296 * If there is no file at the expected location, check the legacy
299 if (!defaultFile
.exists()) {
300 if (legacyFileCore
.exists()) {
301 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE
);
302 } else if (legacyFileUI
.exists()) {
303 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI
);
307 Set
<CustomXmlTraceDefinition
> defs
= new TreeSet
<>(new Comparator
<CustomXmlTraceDefinition
>() {
309 public int compare(CustomXmlTraceDefinition o1
, CustomXmlTraceDefinition o2
) {
310 int result
= o1
.categoryName
.compareTo(o2
.categoryName
);
314 return o1
.definitionName
.compareTo(o2
.definitionName
);
317 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
)));
318 if (includeDefaults
) {
319 defs
.addAll(Arrays
.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
)));
321 return defs
.toArray(new CustomXmlTraceDefinition
[0]);
324 private static void transferDefinitions(String defFile
) {
325 CustomXmlTraceDefinition
[] oldDefs
= loadAll(defFile
);
326 for (CustomXmlTraceDefinition def
: oldDefs
) {
327 /* Save in the new location */
334 * Load all the XML trace definitions in the given definitions file.
337 * Path to the definitions file to load
338 * @return The loaded trace definitions
340 public static CustomXmlTraceDefinition
[] loadAll(String path
) {
341 File file
= new File(path
);
342 if (!file
.canRead()) {
343 return new CustomXmlTraceDefinition
[0];
345 try (FileInputStream fis
= new FileInputStream(file
);) {
347 } catch (IOException e
) {
348 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
350 return new CustomXmlTraceDefinition
[0];
354 * Load all the XML trace definitions from the given stream
357 * An input stream from which to read the definitions
358 * @return The loaded trace definitions
360 public static CustomXmlTraceDefinition
[] loadAll(InputStream stream
) {
362 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
363 DocumentBuilder db
= dbf
.newDocumentBuilder();
365 // The following allows xml parsing without access to the dtd
366 db
.setEntityResolver(createEmptyEntityResolver());
368 // The following catches xml parsing exceptions
369 db
.setErrorHandler(createErrorHandler());
371 Document doc
= db
.parse(stream
);
372 Element root
= doc
.getDocumentElement();
373 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
374 return new CustomXmlTraceDefinition
[0];
377 ArrayList
<CustomXmlTraceDefinition
> defList
= new ArrayList
<>();
378 NodeList nodeList
= root
.getChildNodes();
379 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
380 Node node
= nodeList
.item(i
);
381 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
382 CustomXmlTraceDefinition def
= extractDefinition((Element
) node
);
388 return defList
.toArray(new CustomXmlTraceDefinition
[0]);
389 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
390 Activator
.logError("Error loading all in CustomXmlTraceDefinition: path=" + stream
, e
); //$NON-NLS-1$
392 return new CustomXmlTraceDefinition
[0];
396 * Load the given trace definition.
398 * @param categoryName
399 * Category of the definition to load
400 * @param definitionName
401 * Name of the XML trace definition to load
402 * @return The loaded trace definition
404 public static CustomXmlTraceDefinition
load(String categoryName
, String definitionName
) {
406 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
407 DocumentBuilder db
= dbf
.newDocumentBuilder();
409 // The following allows xml parsing without access to the dtd
410 EntityResolver resolver
= new EntityResolver() {
412 public InputSource
resolveEntity(String publicId
, String systemId
) {
413 String empty
= ""; //$NON-NLS-1$
414 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
415 return new InputSource(bais
);
418 db
.setEntityResolver(resolver
);
420 // The following catches xml parsing exceptions
421 db
.setErrorHandler(new ErrorHandler() {
423 public void error(SAXParseException saxparseexception
) throws SAXException
{
427 public void warning(SAXParseException saxparseexception
) throws SAXException
{
431 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
432 throw saxparseexception
;
436 CustomXmlTraceDefinition value
= lookupXmlDefinition(categoryName
, definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
438 value
= lookupXmlDefinition(categoryName
, definitionName
, db
, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME
);
441 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
442 Activator
.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
447 private static CustomXmlTraceDefinition
lookupXmlDefinition(String categoryName
, String definitionName
, DocumentBuilder db
, String source
) throws SAXException
, IOException
{
448 File file
= new File(source
);
449 if (!file
.exists()) {
453 Document doc
= db
.parse(file
);
455 Element root
= doc
.getDocumentElement();
456 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
460 Element definitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
461 if (definitionElement
!= null) {
462 return extractDefinition(definitionElement
);
467 private static Element
findDefinitionElement(Element root
, String categoryName
, String definitionName
) {
468 NodeList nodeList
= root
.getChildNodes();
469 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
470 Node node
= nodeList
.item(i
);
471 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
472 Element element
= (Element
) node
;
473 String categoryAttribute
= element
.getAttribute(CATEGORY_ATTRIBUTE
);
474 if (categoryAttribute
.isEmpty()) {
475 categoryAttribute
= CUSTOM_XML_CATEGORY
;
477 String nameAttribute
= element
.getAttribute(NAME_ATTRIBUTE
);
478 if (categoryName
.equals(categoryAttribute
) &&
479 definitionName
.equals(nameAttribute
)) {
488 * Extract a trace definition from an XML element.
490 * @param definitionElement
492 * @return The extracted trace definition
494 public static CustomXmlTraceDefinition
extractDefinition(Element definitionElement
) {
495 CustomXmlTraceDefinition def
= new CustomXmlTraceDefinition();
497 def
.categoryName
= definitionElement
.getAttribute(CATEGORY_ATTRIBUTE
);
498 if (def
.categoryName
.isEmpty()) {
499 def
.categoryName
= CUSTOM_XML_CATEGORY
;
501 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
502 if (def
.definitionName
.isEmpty()) {
506 NodeList nodeList
= definitionElement
.getChildNodes();
507 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
508 Node node
= nodeList
.item(i
);
509 String nodeName
= node
.getNodeName();
510 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
511 Element formatElement
= (Element
) node
;
512 def
.timeStampOutputFormat
= formatElement
.getTextContent();
513 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
514 CustomXmlInputElement inputElement
= extractInputElement((Element
) node
);
515 if (inputElement
!= null) {
516 if (def
.rootInputElement
== null) {
517 def
.rootInputElement
= inputElement
;
522 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
523 Element outputColumnElement
= (Element
) node
;
524 OutputColumn outputColumn
= new OutputColumn();
525 outputColumn
.name
= checkNotNull(outputColumnElement
.getAttribute(NAME_ATTRIBUTE
));
526 def
.outputs
.add(outputColumn
);
532 private static CustomXmlInputElement
extractInputElement(Element inputElementElement
) {
533 CustomXmlInputElement inputElement
= new CustomXmlInputElement();
534 inputElement
.setElementName(inputElementElement
.getAttribute(NAME_ATTRIBUTE
));
535 inputElement
.setLogEntry((Boolean
.toString(true).equals(inputElementElement
.getAttribute(LOG_ENTRY_ATTRIBUTE
))) ?
true : false);
536 NodeList nodeList
= inputElementElement
.getChildNodes();
537 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
538 Node node
= nodeList
.item(i
);
539 String nodeName
= node
.getNodeName();
540 if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
541 Element inputDataElement
= (Element
) node
;
542 inputElement
.setInputName(inputDataElement
.getAttribute(NAME_ATTRIBUTE
));
543 inputElement
.setInputAction(Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
)));
544 inputElement
.setInputFormat(inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
));
545 } else if (nodeName
.equals(ATTRIBUTE_ELEMENT
)) {
546 Element attributeElement
= (Element
) node
;
548 String attributeName
= attributeElement
.getAttribute(NAME_ATTRIBUTE
);
549 String inputName
= null;
551 String inputFormat
= null;
552 NodeList attributeNodeList
= attributeElement
.getChildNodes();
553 for (int j
= 0; j
< attributeNodeList
.getLength(); j
++) {
554 Node attributeNode
= attributeNodeList
.item(j
);
555 String attributeNodeName
= attributeNode
.getNodeName();
556 if (attributeNodeName
.equals(INPUT_DATA_ELEMENT
)) {
557 Element inputDataElement
= (Element
) attributeNode
;
558 inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
559 inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
560 inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
563 inputElement
.addAttribute(new CustomXmlInputAttribute(attributeName
, inputName
, inputAction
, inputFormat
));
564 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
565 Element childInputElementElement
= (Element
) node
;
566 CustomXmlInputElement childInputElement
= extractInputElement(childInputElementElement
);
567 if (childInputElement
!= null) {
568 inputElement
.addChild(childInputElement
);
576 * Delete a definition from the currently loaded ones.
578 * @param categoryName
579 * The category of the definition to delete
580 * @param definitionName
581 * The name of the definition to delete
583 public static void delete(String categoryName
, String definitionName
) {
585 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
586 DocumentBuilder db
= dbf
.newDocumentBuilder();
588 // The following allows xml parsing without access to the dtd
589 EntityResolver resolver
= new EntityResolver() {
591 public InputSource
resolveEntity(String publicId
, String systemId
) {
592 String empty
= ""; //$NON-NLS-1$
593 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
594 return new InputSource(bais
);
597 db
.setEntityResolver(resolver
);
599 // The following catches xml parsing exceptions
600 db
.setErrorHandler(new ErrorHandler() {
602 public void error(SAXParseException saxparseexception
) throws SAXException
{
606 public void warning(SAXParseException saxparseexception
) throws SAXException
{
610 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
611 throw saxparseexception
;
615 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
616 Document doc
= db
.parse(file
);
618 Element root
= doc
.getDocumentElement();
619 if (!root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
623 Element definitionElement
= findDefinitionElement(root
, categoryName
, definitionName
);
624 if (definitionElement
!= null) {
625 root
.removeChild(definitionElement
);
628 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
629 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
631 // initialize StreamResult with File object to save to file
632 StreamResult result
= new StreamResult(new StringWriter());
633 DOMSource source
= new DOMSource(doc
);
634 transformer
.transform(source
, result
);
635 String xmlString
= result
.getWriter().toString();
637 try (FileWriter writer
= new FileWriter(file
);) {
638 writer
.write(xmlString
);
641 TmfTraceType
.removeCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
642 // Check if default definition needs to be reloaded
643 TmfTraceType
.addCustomTraceType(CustomXmlTrace
.class, categoryName
, definitionName
);
645 } catch (ParserConfigurationException
| SAXException
| IOException
| TransformerFactoryConfigurationError
| TransformerException e
) {
646 Activator
.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$