tmf: Move TmfTraceType and custom parsers to tmf.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / parsers / custom / CustomXmlTraceDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.parsers.custom;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.File;
17 import java.io.FileWriter;
18 import java.io.IOException;
19 import java.io.StringWriter;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
26 import javax.xml.transform.OutputKeys;
27 import javax.xml.transform.Transformer;
28 import javax.xml.transform.TransformerConfigurationException;
29 import javax.xml.transform.TransformerException;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.TransformerFactoryConfigurationError;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
34
35 import org.eclipse.linuxtools.internal.tmf.core.Activator;
36 import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.Node;
40 import org.w3c.dom.NodeList;
41 import org.xml.sax.EntityResolver;
42 import org.xml.sax.ErrorHandler;
43 import org.xml.sax.InputSource;
44 import org.xml.sax.SAXException;
45 import org.xml.sax.SAXParseException;
46
47 /**
48 * Trace definition for custom XML traces.
49 *
50 * @author Patrick Tassé
51 * @since 3.0
52 */
53 public class CustomXmlTraceDefinition extends CustomTraceDefinition {
54
55 /** "ignore" tag */
56 public static final String TAG_IGNORE = Messages.CustomXmlTraceDefinition_ignoreTag;
57
58 /** Name of the XML definitions file */
59 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME = "custom_xml_parsers.xml"; //$NON-NLS-1$
60
61 /** Path to the XML definitions file */
62 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME =
63 Activator.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();
64
65 private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT = Messages.CustomXmlTraceDefinition_definitionRootElement;
66 private static final String DEFINITION_ELEMENT = Messages.CustomXmlTraceDefinition_definition;
67 private static final String NAME_ATTRIBUTE = Messages.CustomXmlTraceDefinition_name;
68 private static final String LOG_ENTRY_ATTRIBUTE = Messages.CustomXmlTraceDefinition_logEntry;
69 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT = Messages.CustomXmlTraceDefinition_timestampOutputFormat;
70 private static final String INPUT_ELEMENT_ELEMENT = Messages.CustomXmlTraceDefinition_inputElement;
71 private static final String ATTRIBUTE_ELEMENT = Messages.CustomXmlTraceDefinition_attribute;
72 private static final String INPUT_DATA_ELEMENT = Messages.CustomXmlTraceDefinition_inputData;
73 private static final String ACTION_ATTRIBUTE = Messages.CustomXmlTraceDefinition_action;
74 private static final String FORMAT_ATTRIBUTE = Messages.CustomXmlTraceDefinition_format;
75 private static final String OUTPUT_COLUMN_ELEMENT = Messages.CustomXmlTraceDefinition_outputColumn;
76
77 /** Top-level input element */
78 public InputElement rootInputElement;
79
80 /**
81 * Default constructor
82 */
83 public CustomXmlTraceDefinition() {
84 this("", null, new ArrayList<OutputColumn>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
85 }
86
87 /**
88 * Full constructor
89 *
90 * @param logtype
91 * Type of trace type
92 * @param rootElement
93 * The top-level XML element
94 * @param outputs
95 * The list of output columns
96 * @param timeStampOutputFormat
97 * The timestamp format to use
98 */
99 public CustomXmlTraceDefinition(String logtype, InputElement rootElement,
100 List<OutputColumn> outputs, String timeStampOutputFormat) {
101 this.definitionName = logtype;
102 this.rootInputElement = rootElement;
103 this.outputs = outputs;
104 this.timeStampOutputFormat = timeStampOutputFormat;
105 }
106
107 /**
108 * Wrapper for input XML elements
109 */
110 public static class InputElement {
111
112 /** Name of the element */
113 public String elementName;
114
115 /** Indicates if this is a log entry */
116 public boolean logEntry;
117
118 /** Name of the input element */
119 public String inputName;
120
121 /** Input action */
122 public int inputAction;
123
124 /** Input format */
125 public String inputFormat;
126
127 /** XML attributes of this element */
128 public List<InputAttribute> attributes;
129
130 /** Parent element */
131 public InputElement parentElement;
132
133 /** Following element in the file */
134 public InputElement nextElement;
135
136 /** Child elements */
137 public List<InputElement> childElements;
138
139 /**
140 * Default (empty) constructor
141 */
142 public InputElement() {}
143
144 /**
145 * Constructor
146 *
147 * @param elementName
148 * Element name
149 * @param logEntry
150 * If this element is a log entry
151 * @param inputName
152 * Name of the the input
153 * @param inputAction
154 * Input action
155 * @param inputFormat
156 * Input format
157 * @param attributes
158 * XML attributes of this element
159 */
160 public InputElement(String elementName, boolean logEntry,
161 String inputName, int inputAction, String inputFormat,
162 List<InputAttribute> attributes) {
163 this.elementName = elementName;
164 this.logEntry = logEntry;
165 this.inputName = inputName;
166 this.inputAction = inputAction;
167 this.inputFormat = inputFormat;
168 this.attributes = attributes;
169 }
170
171 /**
172 * Add a XML attribute to the element
173 *
174 * @param attribute
175 * The attribute to add
176 */
177 public void addAttribute(InputAttribute attribute) {
178 if (attributes == null) {
179 attributes = new ArrayList<>(1);
180 }
181 attributes.add(attribute);
182 }
183
184 /**
185 * Add a child element to this one.
186 *
187 * @param input
188 * The input element to add as child
189 */
190 public void addChild(InputElement input) {
191 if (childElements == null) {
192 childElements = new ArrayList<>(1);
193 } else if (childElements.size() > 0) {
194 InputElement last = childElements.get(childElements.size() - 1);
195 last.nextElement = input;
196 }
197 childElements.add(input);
198 input.parentElement = this;
199 }
200
201 /**
202 * Set the following input element.
203 *
204 * @param input
205 * The input element to add as next element
206 */
207 public void addNext(InputElement input) {
208 if (parentElement != null) {
209 int index = parentElement.childElements.indexOf(this);
210 parentElement.childElements.add(index + 1, input);
211 InputElement next = nextElement;
212 nextElement = input;
213 input.nextElement = next;
214 }
215 input.parentElement = this.parentElement;
216 }
217
218 /**
219 * Move this element up in its parent's list of children.
220 */
221 public void moveUp() {
222 if (parentElement != null) {
223 int index = parentElement.childElements.indexOf(this);
224 if (index > 0) {
225 parentElement.childElements.add(index - 1 , parentElement.childElements.remove(index));
226 parentElement.childElements.get(index).nextElement = nextElement;
227 nextElement = parentElement.childElements.get(index);
228 }
229 }
230 }
231
232 /**
233 * Move this element down in its parent's list of children.
234 */
235 public void moveDown() {
236 if (parentElement != null) {
237 int index = parentElement.childElements.indexOf(this);
238 if (index < parentElement.childElements.size() - 1) {
239 parentElement.childElements.add(index + 1 , parentElement.childElements.remove(index));
240 nextElement = parentElement.childElements.get(index).nextElement;
241 parentElement.childElements.get(index).nextElement = this;
242 }
243 }
244 }
245
246 }
247
248 /**
249 * Wrapper for XML element attributes
250 */
251 public static class InputAttribute {
252
253 /** Name of the XML attribute */
254 public String attributeName;
255
256 /** Input name */
257 public String inputName;
258
259 /** Input action */
260 public int inputAction;
261
262 /** Input format */
263 public String inputFormat;
264
265 /**
266 * Default (empty) constructor
267 */
268 public InputAttribute() {}
269
270 /**
271 * Constructor
272 *
273 * @param attributeName
274 * Name of the XML attribute
275 * @param inputName
276 * Input name
277 * @param inputAction
278 * Input action
279 * @param inputFormat
280 * Input format
281 */
282 public InputAttribute(String attributeName, String inputName,
283 int inputAction, String inputFormat) {
284 this.attributeName = attributeName;
285 this.inputName = inputName;
286 this.inputAction = inputAction;
287 this.inputFormat = inputFormat;
288 }
289 }
290
291 @Override
292 public void save() {
293 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
294 }
295
296 @Override
297 public void save(String path) {
298 try {
299 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
300 DocumentBuilder db = dbf.newDocumentBuilder();
301
302 // The following allows xml parsing without access to the dtd
303 EntityResolver resolver = new EntityResolver() {
304 @Override
305 public InputSource resolveEntity(String publicId, String systemId) {
306 String empty = ""; //$NON-NLS-1$
307 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
308 return new InputSource(bais);
309 }
310 };
311 db.setEntityResolver(resolver);
312
313 // The following catches xml parsing exceptions
314 db.setErrorHandler(new ErrorHandler() {
315 @Override
316 public void error(SAXParseException saxparseexception) throws SAXException {}
317
318 @Override
319 public void warning(SAXParseException saxparseexception) throws SAXException {}
320
321 @Override
322 public void fatalError(SAXParseException saxparseexception) throws SAXException {
323 throw saxparseexception;
324 }
325 });
326
327 Document doc = null;
328 File file = new File(path);
329 if (file.canRead()) {
330 doc = db.parse(file);
331 if (! doc.getDocumentElement().getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
332 return;
333 }
334 } else {
335 doc = db.newDocument();
336 Node node = doc.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT);
337 doc.appendChild(node);
338 }
339
340 Element root = doc.getDocumentElement();
341
342 NodeList nodeList = root.getChildNodes();
343 for (int i = 0; i < nodeList.getLength(); i++) {
344 Node node = nodeList.item(i);
345 if (node instanceof Element &&
346 node.getNodeName().equals(DEFINITION_ELEMENT) &&
347 definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {
348 root.removeChild(node);
349 }
350 }
351 Element definitionElement = doc.createElement(DEFINITION_ELEMENT);
352 root.appendChild(definitionElement);
353 definitionElement.setAttribute(NAME_ATTRIBUTE, definitionName);
354
355 Element formatElement = doc.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT);
356 definitionElement.appendChild(formatElement);
357 formatElement.appendChild(doc.createTextNode(timeStampOutputFormat));
358
359 if (rootInputElement != null) {
360 definitionElement.appendChild(createInputElementElement(rootInputElement, doc));
361 }
362
363 if (outputs != null) {
364 for (OutputColumn output : outputs) {
365 Element outputColumnElement = doc.createElement(OUTPUT_COLUMN_ELEMENT);
366 definitionElement.appendChild(outputColumnElement);
367 outputColumnElement.setAttribute(NAME_ATTRIBUTE, output.name);
368 }
369 }
370
371 Transformer transformer = TransformerFactory.newInstance().newTransformer();
372 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
373
374 //initialize StreamResult with File object to save to file
375 StreamResult result = new StreamResult(new StringWriter());
376 DOMSource source = new DOMSource(doc);
377 transformer.transform(source, result);
378 String xmlString = result.getWriter().toString();
379
380 try (FileWriter writer = new FileWriter(file);) {
381 writer.write(xmlString);
382 }
383
384 TmfTraceType.getInstance().addCustomTraceType(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
385
386 } catch (ParserConfigurationException e) {
387 Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
388 } catch (TransformerConfigurationException e) {
389 Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
390 } catch (TransformerFactoryConfigurationError e) {
391 Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
392 } catch (TransformerException e) {
393 Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
394 } catch (IOException e) {
395 Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
396 } catch (SAXException e) {
397 Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
398 }
399 }
400
401 private Element createInputElementElement(InputElement inputElement, Document doc) {
402 Element inputElementElement = doc.createElement(INPUT_ELEMENT_ELEMENT);
403 inputElementElement.setAttribute(NAME_ATTRIBUTE, inputElement.elementName);
404
405 if (inputElement.logEntry) {
406 inputElementElement.setAttribute(LOG_ENTRY_ATTRIBUTE, Boolean.toString(inputElement.logEntry));
407 }
408
409 if (inputElement.parentElement != null) {
410 Element inputDataElement = doc.createElement(INPUT_DATA_ELEMENT);
411 inputElementElement.appendChild(inputDataElement);
412 inputDataElement.setAttribute(NAME_ATTRIBUTE, inputElement.inputName);
413 inputDataElement.setAttribute(ACTION_ATTRIBUTE, Integer.toString(inputElement.inputAction));
414 if (inputElement.inputFormat != null) {
415 inputDataElement.setAttribute(FORMAT_ATTRIBUTE, inputElement.inputFormat);
416 }
417 }
418
419 if (inputElement.attributes != null) {
420 for (InputAttribute attribute : inputElement.attributes) {
421 Element inputAttributeElement = doc.createElement(ATTRIBUTE_ELEMENT);
422 inputElementElement.appendChild(inputAttributeElement);
423 inputAttributeElement.setAttribute(NAME_ATTRIBUTE, attribute.attributeName);
424 Element inputDataElement = doc.createElement(INPUT_DATA_ELEMENT);
425 inputAttributeElement.appendChild(inputDataElement);
426 inputDataElement.setAttribute(NAME_ATTRIBUTE, attribute.inputName);
427 inputDataElement.setAttribute(ACTION_ATTRIBUTE, Integer.toString(attribute.inputAction));
428 if (attribute.inputFormat != null) {
429 inputDataElement.setAttribute(FORMAT_ATTRIBUTE, attribute.inputFormat);
430 }
431 }
432 }
433
434 if (inputElement.childElements != null) {
435 for (InputElement childInputElement : inputElement.childElements) {
436 inputElementElement.appendChild(createInputElementElement(childInputElement, doc));
437 }
438 }
439
440 return inputElementElement;
441 }
442
443 /**
444 * Load all the XML trace definitions in the default definitions file.
445 *
446 * @return The loaded trace definitions
447 */
448 public static CustomXmlTraceDefinition[] loadAll() {
449 return loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
450 }
451
452 /**
453 * Load all the XML trace definitions in the given definitions file.
454 *
455 * @param path
456 * Path to the definitions file to load
457 * @return The loaded trace definitions
458 */
459 public static CustomXmlTraceDefinition[] loadAll(String path) {
460 try {
461 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
462 DocumentBuilder db = dbf.newDocumentBuilder();
463
464 // The following allows xml parsing without access to the dtd
465 EntityResolver resolver = new EntityResolver() {
466 @Override
467 public InputSource resolveEntity(String publicId, String systemId) {
468 String empty = ""; //$NON-NLS-1$
469 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
470 return new InputSource(bais);
471 }
472 };
473 db.setEntityResolver(resolver);
474
475 // The following catches xml parsing exceptions
476 db.setErrorHandler(new ErrorHandler() {
477 @Override
478 public void error(SAXParseException saxparseexception) throws SAXException {}
479
480 @Override
481 public void warning(SAXParseException saxparseexception) throws SAXException {}
482
483 @Override
484 public void fatalError(SAXParseException saxparseexception) throws SAXException {
485 throw saxparseexception;
486 }
487 });
488
489 File file = new File(path);
490 if (!file.canRead()) {
491 return new CustomXmlTraceDefinition[0];
492 }
493 Document doc = db.parse(file);
494
495 Element root = doc.getDocumentElement();
496 if (! root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
497 return new CustomXmlTraceDefinition[0];
498 }
499
500 ArrayList<CustomXmlTraceDefinition> defList = new ArrayList<>();
501 NodeList nodeList = root.getChildNodes();
502 for (int i = 0; i < nodeList.getLength(); i++) {
503 Node node = nodeList.item(i);
504 if (node instanceof Element && node.getNodeName().equals(DEFINITION_ELEMENT)) {
505 CustomXmlTraceDefinition def = extractDefinition((Element) node);
506 if (def != null) {
507 defList.add(def);
508 }
509 }
510 }
511 return defList.toArray(new CustomXmlTraceDefinition[0]);
512 } catch (ParserConfigurationException e) {
513 Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
514 } catch (SAXException e) {
515 Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
516 } catch (IOException e) {
517 Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
518 }
519 return new CustomXmlTraceDefinition[0];
520 }
521
522 /**
523 * Load the given trace definition.
524 *
525 * @param definitionName
526 * Name of the XML trace definition to load
527 * @return The loaded trace definition
528 */
529 public static CustomXmlTraceDefinition load(String definitionName) {
530 try {
531 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
532 DocumentBuilder db = dbf.newDocumentBuilder();
533
534 // The following allows xml parsing without access to the dtd
535 EntityResolver resolver = new EntityResolver() {
536 @Override
537 public InputSource resolveEntity(String publicId, String systemId) {
538 String empty = ""; //$NON-NLS-1$
539 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
540 return new InputSource(bais);
541 }
542 };
543 db.setEntityResolver(resolver);
544
545 // The following catches xml parsing exceptions
546 db.setErrorHandler(new ErrorHandler() {
547 @Override
548 public void error(SAXParseException saxparseexception) throws SAXException {}
549
550 @Override
551 public void warning(SAXParseException saxparseexception) throws SAXException {}
552
553 @Override
554 public void fatalError(SAXParseException saxparseexception) throws SAXException {
555 throw saxparseexception;
556 }
557 });
558
559 File file = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
560 Document doc = db.parse(file);
561
562 Element root = doc.getDocumentElement();
563 if (! root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
564 return null;
565 }
566
567 NodeList nodeList = root.getChildNodes();
568 for (int i = 0; i < nodeList.getLength(); i++) {
569 Node node = nodeList.item(i);
570 if (node instanceof Element &&
571 node.getNodeName().equals(DEFINITION_ELEMENT) &&
572 definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {
573 return extractDefinition((Element) node);
574 }
575 }
576 } catch (ParserConfigurationException e) {
577 Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
578 } catch (SAXException e) {
579 Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
580 } catch (IOException e) {
581 Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
582 }
583 return null;
584 }
585
586 /**
587 * Extract a trace definition from an XML element.
588 *
589 * @param definitionElement
590 * Definition element
591 * @return The extracted trace definition
592 */
593 public static CustomXmlTraceDefinition extractDefinition(Element definitionElement) {
594 CustomXmlTraceDefinition def = new CustomXmlTraceDefinition();
595
596 def.definitionName = definitionElement.getAttribute(NAME_ATTRIBUTE);
597 if (def.definitionName == null) {
598 return null;
599 }
600
601 NodeList nodeList = definitionElement.getChildNodes();
602 for (int i = 0; i < nodeList.getLength(); i++) {
603 Node node = nodeList.item(i);
604 String nodeName = node.getNodeName();
605 if (nodeName.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT)) {
606 Element formatElement = (Element) node;
607 def.timeStampOutputFormat = formatElement.getTextContent();
608 } else if (nodeName.equals(INPUT_ELEMENT_ELEMENT)) {
609 InputElement inputElement = extractInputElement((Element) node);
610 if (inputElement != null) {
611 if (def.rootInputElement == null) {
612 def.rootInputElement = inputElement;
613 } else {
614 return null;
615 }
616 }
617 } else if (nodeName.equals(OUTPUT_COLUMN_ELEMENT)) {
618 Element outputColumnElement = (Element) node;
619 OutputColumn outputColumn = new OutputColumn();
620 outputColumn.name = outputColumnElement.getAttribute(NAME_ATTRIBUTE);
621 def.outputs.add(outputColumn);
622 }
623 }
624 return def;
625 }
626
627 private static InputElement extractInputElement(Element inputElementElement) {
628 InputElement inputElement = new InputElement();
629 inputElement.elementName = inputElementElement.getAttribute(NAME_ATTRIBUTE);
630 inputElement.logEntry = (Boolean.toString(true).equals(inputElementElement.getAttribute(LOG_ENTRY_ATTRIBUTE))) ? true : false;
631 NodeList nodeList = inputElementElement.getChildNodes();
632 for (int i = 0; i < nodeList.getLength(); i++) {
633 Node node = nodeList.item(i);
634 String nodeName = node.getNodeName();
635 if (nodeName.equals(INPUT_DATA_ELEMENT)) {
636 Element inputDataElement = (Element) node;
637 inputElement.inputName = inputDataElement.getAttribute(NAME_ATTRIBUTE);
638 inputElement.inputAction = Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE));
639 inputElement.inputFormat = inputDataElement.getAttribute(FORMAT_ATTRIBUTE);
640 } else if (nodeName.equals(ATTRIBUTE_ELEMENT)) {
641 Element attributeElement = (Element) node;
642 InputAttribute attribute = new InputAttribute();
643 attribute.attributeName = attributeElement.getAttribute(NAME_ATTRIBUTE);
644 NodeList attributeNodeList = attributeElement.getChildNodes();
645 for (int j = 0; j < attributeNodeList.getLength(); j++) {
646 Node attributeNode = attributeNodeList.item(j);
647 String attributeNodeName = attributeNode.getNodeName();
648 if (attributeNodeName.equals(INPUT_DATA_ELEMENT)) {
649 Element inputDataElement = (Element) attributeNode;
650 attribute.inputName = inputDataElement.getAttribute(NAME_ATTRIBUTE);
651 attribute.inputAction = Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE));
652 attribute.inputFormat = inputDataElement.getAttribute(FORMAT_ATTRIBUTE);
653 }
654 }
655 inputElement.addAttribute(attribute);
656 } else if (nodeName.equals(INPUT_ELEMENT_ELEMENT)) {
657 Element childInputElementElement = (Element) node;
658 InputElement childInputElement = extractInputElement(childInputElementElement);
659 if (childInputElement != null) {
660 inputElement.addChild(childInputElement);
661 }
662 }
663 }
664 return inputElement;
665 }
666
667 /**
668 * Delete the given trace definition from the list of currently loaded ones.
669 *
670 * @param definitionName
671 * Name of the trace definition to delete
672 */
673 public static void delete(String definitionName) {
674 try {
675 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
676 DocumentBuilder db = dbf.newDocumentBuilder();
677
678 // The following allows xml parsing without access to the dtd
679 EntityResolver resolver = new EntityResolver() {
680 @Override
681 public InputSource resolveEntity(String publicId, String systemId) {
682 String empty = ""; //$NON-NLS-1$
683 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
684 return new InputSource(bais);
685 }
686 };
687 db.setEntityResolver(resolver);
688
689 // The following catches xml parsing exceptions
690 db.setErrorHandler(new ErrorHandler() {
691 @Override
692 public void error(SAXParseException saxparseexception) throws SAXException {}
693
694 @Override
695 public void warning(SAXParseException saxparseexception) throws SAXException {}
696
697 @Override
698 public void fatalError(SAXParseException saxparseexception) throws SAXException {
699 throw saxparseexception;
700 }
701 });
702
703 File file = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
704 Document doc = db.parse(file);
705
706 Element root = doc.getDocumentElement();
707 if (! root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
708 return;
709 }
710
711 NodeList nodeList = root.getChildNodes();
712 for (int i = 0; i < nodeList.getLength(); i++) {
713 Node node = nodeList.item(i);
714 if (node instanceof Element &&
715 node.getNodeName().equals(DEFINITION_ELEMENT) &&
716 definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {
717 root.removeChild(node);
718 }
719 }
720
721 Transformer transformer = TransformerFactory.newInstance().newTransformer();
722 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
723
724 //initialize StreamResult with File object to save to file
725 StreamResult result = new StreamResult(new StringWriter());
726 DOMSource source = new DOMSource(doc);
727 transformer.transform(source, result);
728 String xmlString = result.getWriter().toString();
729
730 try (FileWriter writer = new FileWriter(file);) {
731 writer.write(xmlString);
732 }
733
734 TmfTraceType.getInstance().removeCustomTraceType(TmfTraceType.CUSTOM_XML_CATEGORY, definitionName);
735
736 } catch (ParserConfigurationException e) {
737 Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
738 } catch (SAXException e) {
739 Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
740 } catch (IOException e) {
741 Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
742 } catch (TransformerConfigurationException e) {
743 Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
744 } catch (TransformerFactoryConfigurationError e) {
745 Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
746 } catch (TransformerException e) {
747 Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
748 }
749 }
750 }
This page took 0.047738 seconds and 6 git commands to generate.