tmf: Bug 495054: Importing or exporting custom parsers fails silently
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomXmlTraceDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 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.tracecompass.tmf.core.parsers.custom;
15
16 import java.io.ByteArrayInputStream;
17 import java.io.File;
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;
27 import java.util.Set;
28 import java.util.TreeSet;
29
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;
40
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;
53
54 /**
55 * Trace definition for custom XML traces.
56 *
57 * @author Patrick Tassé
58 */
59 public class CustomXmlTraceDefinition extends CustomTraceDefinition {
60
61 /** "ignore" tag */
62 public static final String TAG_IGNORE = Messages.CustomXmlTraceDefinition_ignoreTag;
63
64 /**
65 * Custom XML label used internally and therefore should not be externalized
66 */
67 public static final String CUSTOM_XML_CATEGORY = "Custom XML"; //$NON-NLS-1$
68
69
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$
72
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$
75
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;
80
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();
84
85 /**
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.
88 */
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();
93
94 /**
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.
97 */
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();
102
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;
116
117 /** Top-level input element */
118 public CustomXmlInputElement rootInputElement;
119
120 /**
121 * Default constructor
122 */
123 public CustomXmlTraceDefinition() {
124 this(CUSTOM_XML_CATEGORY, "", null, new ArrayList<OutputColumn>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
125 }
126
127 /**
128 * Full constructor
129 *
130 * @param category
131 * Category of the trace type
132 * @param traceType
133 * Name of the trace type
134 * @param rootElement
135 * The top-level XML element
136 * @param outputs
137 * The list of output columns
138 * @param timeStampOutputFormat
139 * The timestamp format to use
140 */
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;
148 }
149
150 @Override
151 public void save() {
152 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
153 }
154
155 @Override
156 public void save(String path) {
157 try {
158 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
159 DocumentBuilder db = dbf.newDocumentBuilder();
160
161 // The following allows xml parsing without access to the dtd
162 db.setEntityResolver(createEmptyEntityResolver());
163
164 // The following catches xml parsing exceptions
165 db.setErrorHandler(createErrorHandler());
166
167 Document doc = null;
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$
173 return;
174 }
175 } else {
176 doc = db.newDocument();
177 Node node = doc.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT);
178 doc.appendChild(node);
179 }
180
181 Element root = doc.getDocumentElement();
182
183 Element oldDefinitionElement = findDefinitionElement(root, categoryName, definitionName);
184 if (oldDefinitionElement != null) {
185 root.removeChild(oldDefinitionElement);
186 }
187 Element definitionElement = doc.createElement(DEFINITION_ELEMENT);
188 root.appendChild(definitionElement);
189 definitionElement.setAttribute(CATEGORY_ATTRIBUTE, categoryName);
190 definitionElement.setAttribute(NAME_ATTRIBUTE, definitionName);
191
192 Element formatElement = doc.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT);
193 definitionElement.appendChild(formatElement);
194 formatElement.appendChild(doc.createTextNode(timeStampOutputFormat));
195
196 if (rootInputElement != null) {
197 definitionElement.appendChild(createInputElementElement(rootInputElement, doc));
198 }
199
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);
205 }
206 }
207
208 Transformer transformer = TransformerFactory.newInstance().newTransformer();
209 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
210
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();
216
217 try (FileWriter writer = new FileWriter(file);) {
218 writer.write(xmlString);
219 }
220
221 TmfTraceType.addCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
222
223 } catch (ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException | IOException | SAXException e) {
224 Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
225 }
226 }
227
228 private Element createInputElementElement(CustomXmlInputElement inputElement, Document doc) {
229 Element inputElementElement = doc.createElement(INPUT_ELEMENT_ELEMENT);
230 inputElementElement.setAttribute(NAME_ATTRIBUTE, inputElement.getElementName());
231
232 if (inputElement.isLogEntry()) {
233 inputElementElement.setAttribute(LOG_ENTRY_ATTRIBUTE, Boolean.toString(inputElement.isLogEntry()));
234 }
235
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());
243 }
244 }
245
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());
257 }
258 }
259 }
260
261 if (inputElement.getChildElements() != null) {
262 for (CustomXmlInputElement childInputElement : inputElement.getChildElements()) {
263 inputElementElement.appendChild(createInputElementElement(childInputElement, doc));
264 }
265 }
266
267 return inputElementElement;
268 }
269
270 /**
271 * Load all custom XML trace definitions, including the user-defined and
272 * default (built-in) parsers.
273 *
274 * @return The loaded trace definitions
275 */
276 public static CustomXmlTraceDefinition[] loadAll() {
277 return loadAll(true);
278 }
279
280 /**
281 * Load all custom XML trace definitions, including the user-defined and,
282 * optionally, the default (built-in) parsers.
283 *
284 * @param includeDefaults
285 * if true, the default (built-in) parsers are included
286 *
287 * @return The loaded trace definitions
288 */
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);
293
294 /*
295 * If there is no file at the expected location, check the legacy
296 * locations instead.
297 */
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);
303 }
304 }
305
306 Set<CustomXmlTraceDefinition> defs = new TreeSet<>(new Comparator<CustomXmlTraceDefinition>() {
307 @Override
308 public int compare(CustomXmlTraceDefinition o1, CustomXmlTraceDefinition o2) {
309 int result = o1.categoryName.compareTo(o2.categoryName);
310 if (result != 0) {
311 return result;
312 }
313 return o1.definitionName.compareTo(o2.definitionName);
314 }
315 });
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)));
319 }
320 return defs.toArray(new CustomXmlTraceDefinition[0]);
321 }
322
323 private static void transferDefinitions(String defFile) {
324 CustomXmlTraceDefinition[] oldDefs = loadAll(defFile);
325 for (CustomXmlTraceDefinition def : oldDefs) {
326 /* Save in the new location */
327 def.save();
328 }
329 }
330
331
332 /**
333 * Load all the XML trace definitions in the given definitions file.
334 *
335 * @param path
336 * Path to the definitions file to load
337 * @return The loaded trace definitions
338 */
339 public static CustomXmlTraceDefinition[] loadAll(String path) {
340 File file = new File(path);
341 if (!file.canRead()) {
342 return new CustomXmlTraceDefinition[0];
343 }
344 try (FileInputStream fis = new FileInputStream(file);) {
345 return loadAll(fis);
346 } catch (IOException e) {
347 Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
348 }
349 return new CustomXmlTraceDefinition[0];
350 }
351
352 /**
353 * Load all the XML trace definitions from the given stream
354 *
355 * @param stream
356 * An input stream from which to read the definitions
357 * @return The loaded trace definitions
358 */
359 public static CustomXmlTraceDefinition[] loadAll(InputStream stream) {
360 try {
361 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
362 DocumentBuilder db = dbf.newDocumentBuilder();
363
364 // The following allows xml parsing without access to the dtd
365 db.setEntityResolver(createEmptyEntityResolver());
366
367 // The following catches xml parsing exceptions
368 db.setErrorHandler(createErrorHandler());
369
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];
374 }
375
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);
382 if (def != null) {
383 defList.add(def);
384 }
385 }
386 }
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$
390 }
391 return new CustomXmlTraceDefinition[0];
392 }
393
394 /**
395 * Load the given trace definition.
396 *
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
402 */
403 public static CustomXmlTraceDefinition load(String categoryName, String definitionName) {
404 try {
405 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
406 DocumentBuilder db = dbf.newDocumentBuilder();
407
408 // The following allows xml parsing without access to the dtd
409 EntityResolver resolver = new EntityResolver() {
410 @Override
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);
415 }
416 };
417 db.setEntityResolver(resolver);
418
419 // The following catches xml parsing exceptions
420 db.setErrorHandler(new ErrorHandler() {
421 @Override
422 public void error(SAXParseException saxparseexception) throws SAXException {
423 }
424
425 @Override
426 public void warning(SAXParseException saxparseexception) throws SAXException {
427 }
428
429 @Override
430 public void fatalError(SAXParseException saxparseexception) throws SAXException {
431 throw saxparseexception;
432 }
433 });
434
435 CustomXmlTraceDefinition value = lookupXmlDefinition(categoryName, definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
436 if (value == null) {
437 value = lookupXmlDefinition(categoryName, definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME);
438 }
439 return value;
440 } catch (ParserConfigurationException | SAXException | IOException e) {
441 Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
442 }
443 return null;
444 }
445
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()) {
449 return null;
450 }
451
452 Document doc = db.parse(file);
453
454 Element root = doc.getDocumentElement();
455 if (!root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
456 return null;
457 }
458
459 Element definitionElement = findDefinitionElement(root, categoryName, definitionName);
460 if (definitionElement != null) {
461 return extractDefinition(definitionElement);
462 }
463 return null;
464 }
465
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;
475 }
476 String nameAttribute = element.getAttribute(NAME_ATTRIBUTE);
477 if (categoryName.equals(categoryAttribute) &&
478 definitionName.equals(nameAttribute)) {
479 return element;
480 }
481 }
482 }
483 return null;
484 }
485
486 /**
487 * Extract a trace definition from an XML element.
488 *
489 * @param definitionElement
490 * Definition element
491 * @return The extracted trace definition
492 */
493 public static CustomXmlTraceDefinition extractDefinition(Element definitionElement) {
494 CustomXmlTraceDefinition def = new CustomXmlTraceDefinition();
495
496 def.categoryName = definitionElement.getAttribute(CATEGORY_ATTRIBUTE);
497 if (def.categoryName.isEmpty()) {
498 def.categoryName = CUSTOM_XML_CATEGORY;
499 }
500 def.definitionName = definitionElement.getAttribute(NAME_ATTRIBUTE);
501 if (def.definitionName.isEmpty()) {
502 return null;
503 }
504
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;
517 } else {
518 return null;
519 }
520 }
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);
526 }
527 }
528 return def;
529 }
530
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;
546
547 String attributeName = attributeElement.getAttribute(NAME_ATTRIBUTE);
548 String inputName = null;
549 int inputAction = 0;
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);
560 }
561 }
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);
568 }
569 }
570 }
571 return inputElement;
572 }
573
574 /**
575 * Delete a definition from the currently loaded ones.
576 *
577 * @param categoryName
578 * The category of the definition to delete
579 * @param definitionName
580 * The name of the definition to delete
581 */
582 public static void delete(String categoryName, String definitionName) {
583 try {
584 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
585 DocumentBuilder db = dbf.newDocumentBuilder();
586
587 // The following allows xml parsing without access to the dtd
588 EntityResolver resolver = new EntityResolver() {
589 @Override
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);
594 }
595 };
596 db.setEntityResolver(resolver);
597
598 // The following catches xml parsing exceptions
599 db.setErrorHandler(new ErrorHandler() {
600 @Override
601 public void error(SAXParseException saxparseexception) throws SAXException {
602 }
603
604 @Override
605 public void warning(SAXParseException saxparseexception) throws SAXException {
606 }
607
608 @Override
609 public void fatalError(SAXParseException saxparseexception) throws SAXException {
610 throw saxparseexception;
611 }
612 });
613
614 File file = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
615 Document doc = db.parse(file);
616
617 Element root = doc.getDocumentElement();
618 if (!root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
619 return;
620 }
621
622 Element definitionElement = findDefinitionElement(root, categoryName, definitionName);
623 if (definitionElement != null) {
624 root.removeChild(definitionElement);
625 }
626
627 Transformer transformer = TransformerFactory.newInstance().newTransformer();
628 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
629
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();
635
636 try (FileWriter writer = new FileWriter(file);) {
637 writer.write(xmlString);
638 }
639
640 TmfTraceType.removeCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
641 // Check if default definition needs to be reloaded
642 TmfTraceType.addCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
643
644 } catch (ParserConfigurationException | SAXException | IOException | TransformerFactoryConfigurationError | TransformerException e) {
645 Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
646 }
647 }
648 }
This page took 0.047685 seconds and 5 git commands to generate.