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