Remove deprecated and unused methods related to custom traces
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomXmlTraceDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 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 * @since 3.0
59 */
60 public class CustomXmlTraceDefinition extends CustomTraceDefinition {
61
62 /** "ignore" tag */
63 public static final String TAG_IGNORE = Messages.CustomXmlTraceDefinition_ignoreTag;
64
65 /**
66 * Custom XML label used internally and therefore should not be externalized
67 */
68 public static final String CUSTOM_XML_CATEGORY = "Custom XML"; //$NON-NLS-1$
69
70
71 /** Name of the default XML definitions file */
72 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME = "custom_xml_default_parsers.xml"; //$NON-NLS-1$
73
74 /** Name of the XML definitions file */
75 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME = "custom_xml_parsers.xml"; //$NON-NLS-1$
76
77 /** Path to the XML definitions file */
78 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME =
79 Platform.getInstallLocation().getURL().getPath() + "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
80 CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME;
81
82 /** Path to the XML definitions file */
83 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME =
84 Activator.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();
85
86 /**
87 * Legacy path to the XML definitions file (in the UI plug-in of linux tools) TODO Remove
88 * once we feel the transition phase is over.
89 */
90 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI =
91 Activator.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
92 .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
93 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();
94
95 /**
96 * Legacy path to the XML definitions file (in the core plug-in of linux tools) TODO Remove
97 * once we feel the transition phase is over.
98 */
99 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE =
100 Activator.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
101 .append("org.eclipse.linuxtools.tmf.core") //$NON-NLS-1$
102 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();
103
104 // TODO: These strings should not be externalized
105 private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT = Messages.CustomXmlTraceDefinition_definitionRootElement;
106 private static final String DEFINITION_ELEMENT = Messages.CustomXmlTraceDefinition_definition;
107 private static final String CATEGORY_ATTRIBUTE = Messages.CustomXmlTraceDefinition_category;
108 private static final String NAME_ATTRIBUTE = Messages.CustomXmlTraceDefinition_name;
109 private static final String LOG_ENTRY_ATTRIBUTE = Messages.CustomXmlTraceDefinition_logEntry;
110 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT = Messages.CustomXmlTraceDefinition_timestampOutputFormat;
111 private static final String INPUT_ELEMENT_ELEMENT = Messages.CustomXmlTraceDefinition_inputElement;
112 private static final String ATTRIBUTE_ELEMENT = Messages.CustomXmlTraceDefinition_attribute;
113 private static final String INPUT_DATA_ELEMENT = Messages.CustomXmlTraceDefinition_inputData;
114 private static final String ACTION_ATTRIBUTE = Messages.CustomXmlTraceDefinition_action;
115 private static final String FORMAT_ATTRIBUTE = Messages.CustomXmlTraceDefinition_format;
116 private static final String OUTPUT_COLUMN_ELEMENT = Messages.CustomXmlTraceDefinition_outputColumn;
117
118 /** Top-level input element */
119 public CustomXmlInputElement rootInputElement;
120
121 /**
122 * Default constructor
123 */
124 public CustomXmlTraceDefinition() {
125 this(CUSTOM_XML_CATEGORY, "", null, new ArrayList<OutputColumn>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
126 }
127
128 /**
129 * Full constructor
130 *
131 * @param category
132 * Category of the trace type
133 * @param traceType
134 * Name of the trace type
135 * @param rootElement
136 * The top-level XML element
137 * @param outputs
138 * The list of output columns
139 * @param timeStampOutputFormat
140 * The timestamp format to use
141 * @since 3.2
142 */
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;
150 }
151
152 @Override
153 public void save() {
154 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
155 }
156
157 @Override
158 public void save(String path) {
159 try {
160 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
161 DocumentBuilder db = dbf.newDocumentBuilder();
162
163 // The following allows xml parsing without access to the dtd
164 db.setEntityResolver(createEmptyEntityResolver());
165
166 // The following catches xml parsing exceptions
167 db.setErrorHandler(createErrorHandler());
168
169 Document doc = null;
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)) {
174 return;
175 }
176 } else {
177 doc = db.newDocument();
178 Node node = doc.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT);
179 doc.appendChild(node);
180 }
181
182 Element root = doc.getDocumentElement();
183
184 Element oldDefinitionElement = findDefinitionElement(root, categoryName, definitionName);
185 if (oldDefinitionElement != null) {
186 root.removeChild(oldDefinitionElement);
187 }
188 Element definitionElement = doc.createElement(DEFINITION_ELEMENT);
189 root.appendChild(definitionElement);
190 definitionElement.setAttribute(CATEGORY_ATTRIBUTE, categoryName);
191 definitionElement.setAttribute(NAME_ATTRIBUTE, definitionName);
192
193 Element formatElement = doc.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT);
194 definitionElement.appendChild(formatElement);
195 formatElement.appendChild(doc.createTextNode(timeStampOutputFormat));
196
197 if (rootInputElement != null) {
198 definitionElement.appendChild(createInputElementElement(rootInputElement, doc));
199 }
200
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);
206 }
207 }
208
209 Transformer transformer = TransformerFactory.newInstance().newTransformer();
210 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
211
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();
217
218 try (FileWriter writer = new FileWriter(file);) {
219 writer.write(xmlString);
220 }
221
222 TmfTraceType.addCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
223
224 } catch (ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException | IOException | SAXException e) {
225 Activator.logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
226 }
227 }
228
229 private Element createInputElementElement(CustomXmlInputElement inputElement, Document doc) {
230 Element inputElementElement = doc.createElement(INPUT_ELEMENT_ELEMENT);
231 inputElementElement.setAttribute(NAME_ATTRIBUTE, inputElement.getElementName());
232
233 if (inputElement.isLogEntry()) {
234 inputElementElement.setAttribute(LOG_ENTRY_ATTRIBUTE, Boolean.toString(inputElement.isLogEntry()));
235 }
236
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());
244 }
245 }
246
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());
258 }
259 }
260 }
261
262 if (inputElement.getChildElements() != null) {
263 for (CustomXmlInputElement childInputElement : inputElement.getChildElements()) {
264 inputElementElement.appendChild(createInputElementElement(childInputElement, doc));
265 }
266 }
267
268 return inputElementElement;
269 }
270
271 /**
272 * Load all custom XML trace definitions, including the user-defined and
273 * default (built-in) parsers.
274 *
275 * @return The loaded trace definitions
276 */
277 public static CustomXmlTraceDefinition[] loadAll() {
278 return loadAll(true);
279 }
280
281 /**
282 * Load all custom XML trace definitions, including the user-defined and,
283 * optionally, the default (built-in) parsers.
284 *
285 * @param includeDefaults
286 * if true, the default (built-in) parsers are included
287 *
288 * @return The loaded trace definitions
289 * @since 3.2
290 */
291 public static CustomXmlTraceDefinition[] loadAll(boolean includeDefaults) {
292 File defaultFile = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
293 File legacyFileUI = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI);
294 File legacyFileCore = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE);
295
296 /*
297 * If there is no file at the expected location, check the legacy
298 * locations instead.
299 */
300 if (!defaultFile.exists()) {
301 if (legacyFileCore.exists()) {
302 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE);
303 } else if (legacyFileUI.exists()) {
304 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI);
305 }
306 }
307
308 Set<CustomXmlTraceDefinition> defs = new TreeSet<>(new Comparator<CustomXmlTraceDefinition>() {
309 @Override
310 public int compare(CustomXmlTraceDefinition o1, CustomXmlTraceDefinition o2) {
311 int result = o1.categoryName.compareTo(o2.categoryName);
312 if (result != 0) {
313 return result;
314 }
315 return o1.definitionName.compareTo(o2.definitionName);
316 }
317 });
318 defs.addAll(Arrays.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME)));
319 if (includeDefaults) {
320 defs.addAll(Arrays.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME)));
321 }
322 return defs.toArray(new CustomXmlTraceDefinition[0]);
323 }
324
325 private static void transferDefinitions(String defFile) {
326 CustomXmlTraceDefinition[] oldDefs = loadAll(defFile);
327 for (CustomXmlTraceDefinition def : oldDefs) {
328 /* Save in the new location */
329 def.save();
330 }
331 }
332
333
334 /**
335 * Load all the XML trace definitions in the given definitions file.
336 *
337 * @param path
338 * Path to the definitions file to load
339 * @return The loaded trace definitions
340 */
341 public static CustomXmlTraceDefinition[] loadAll(String path) {
342 File file = new File(path);
343 if (!file.canRead()) {
344 return new CustomXmlTraceDefinition[0];
345 }
346 try (FileInputStream fis = new FileInputStream(file);) {
347 return loadAll(fis);
348 } catch (IOException e) {
349 Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
350 }
351 return new CustomXmlTraceDefinition[0];
352 }
353
354 /**
355 * Load all the XML trace definitions from the given stream
356 *
357 * @param stream
358 * An input stream from which to read the definitions
359 * @return The loaded trace definitions
360 * @since 3.2
361 */
362 public static CustomXmlTraceDefinition[] loadAll(InputStream stream) {
363 try {
364 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
365 DocumentBuilder db = dbf.newDocumentBuilder();
366
367 // The following allows xml parsing without access to the dtd
368 db.setEntityResolver(createEmptyEntityResolver());
369
370 // The following catches xml parsing exceptions
371 db.setErrorHandler(createErrorHandler());
372
373 Document doc = db.parse(stream);
374 Element root = doc.getDocumentElement();
375 if (!root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
376 return new CustomXmlTraceDefinition[0];
377 }
378
379 ArrayList<CustomXmlTraceDefinition> defList = new ArrayList<>();
380 NodeList nodeList = root.getChildNodes();
381 for (int i = 0; i < nodeList.getLength(); i++) {
382 Node node = nodeList.item(i);
383 if (node instanceof Element && node.getNodeName().equals(DEFINITION_ELEMENT)) {
384 CustomXmlTraceDefinition def = extractDefinition((Element) node);
385 if (def != null) {
386 defList.add(def);
387 }
388 }
389 }
390 return defList.toArray(new CustomXmlTraceDefinition[0]);
391 } catch (ParserConfigurationException | SAXException | IOException e) {
392 Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + stream, e); //$NON-NLS-1$
393 }
394 return new CustomXmlTraceDefinition[0];
395 }
396
397 /**
398 * Load the given trace definition.
399 *
400 * @param categoryName
401 * Category of the definition to load
402 * @param definitionName
403 * Name of the XML trace definition to load
404 * @return The loaded trace definition
405 * @since 3.2
406 */
407 public static CustomXmlTraceDefinition load(String categoryName, String definitionName) {
408 try {
409 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
410 DocumentBuilder db = dbf.newDocumentBuilder();
411
412 // The following allows xml parsing without access to the dtd
413 EntityResolver resolver = new EntityResolver() {
414 @Override
415 public InputSource resolveEntity(String publicId, String systemId) {
416 String empty = ""; //$NON-NLS-1$
417 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
418 return new InputSource(bais);
419 }
420 };
421 db.setEntityResolver(resolver);
422
423 // The following catches xml parsing exceptions
424 db.setErrorHandler(new ErrorHandler() {
425 @Override
426 public void error(SAXParseException saxparseexception) throws SAXException {
427 }
428
429 @Override
430 public void warning(SAXParseException saxparseexception) throws SAXException {
431 }
432
433 @Override
434 public void fatalError(SAXParseException saxparseexception) throws SAXException {
435 throw saxparseexception;
436 }
437 });
438
439 CustomXmlTraceDefinition value = lookupXmlDefinition(categoryName, definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
440 if (value == null) {
441 value = lookupXmlDefinition(categoryName, definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME);
442 }
443 return value;
444 } catch (ParserConfigurationException | SAXException | IOException e) {
445 Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
446 }
447 return null;
448 }
449
450 private static CustomXmlTraceDefinition lookupXmlDefinition(String categoryName, String definitionName, DocumentBuilder db, String source) throws SAXException, IOException {
451 File file = new File(source);
452 if (!file.exists()) {
453 return null;
454 }
455
456 Document doc = db.parse(file);
457
458 Element root = doc.getDocumentElement();
459 if (!root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
460 return null;
461 }
462
463 Element definitionElement = findDefinitionElement(root, categoryName, definitionName);
464 if (definitionElement != null) {
465 return extractDefinition(definitionElement);
466 }
467 return null;
468 }
469
470 private static Element findDefinitionElement(Element root, String categoryName, String definitionName) {
471 NodeList nodeList = root.getChildNodes();
472 for (int i = 0; i < nodeList.getLength(); i++) {
473 Node node = nodeList.item(i);
474 if (node instanceof Element && node.getNodeName().equals(DEFINITION_ELEMENT)) {
475 Element element = (Element) node;
476 String categoryAttribute = element.getAttribute(CATEGORY_ATTRIBUTE);
477 if (categoryAttribute.isEmpty()) {
478 categoryAttribute = CUSTOM_XML_CATEGORY;
479 }
480 String nameAttribute = element.getAttribute(NAME_ATTRIBUTE);
481 if (categoryName.equals(categoryAttribute) &&
482 definitionName.equals(nameAttribute)) {
483 return element;
484 }
485 }
486 }
487 return null;
488 }
489
490 /**
491 * Extract a trace definition from an XML element.
492 *
493 * @param definitionElement
494 * Definition element
495 * @return The extracted trace definition
496 */
497 public static CustomXmlTraceDefinition extractDefinition(Element definitionElement) {
498 CustomXmlTraceDefinition def = new CustomXmlTraceDefinition();
499
500 def.categoryName = definitionElement.getAttribute(CATEGORY_ATTRIBUTE);
501 if (def.categoryName.isEmpty()) {
502 def.categoryName = CUSTOM_XML_CATEGORY;
503 }
504 def.definitionName = definitionElement.getAttribute(NAME_ATTRIBUTE);
505 if (def.definitionName.isEmpty()) {
506 return null;
507 }
508
509 NodeList nodeList = definitionElement.getChildNodes();
510 for (int i = 0; i < nodeList.getLength(); i++) {
511 Node node = nodeList.item(i);
512 String nodeName = node.getNodeName();
513 if (nodeName.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT)) {
514 Element formatElement = (Element) node;
515 def.timeStampOutputFormat = formatElement.getTextContent();
516 } else if (nodeName.equals(INPUT_ELEMENT_ELEMENT)) {
517 CustomXmlInputElement inputElement = extractInputElement((Element) node);
518 if (inputElement != null) {
519 if (def.rootInputElement == null) {
520 def.rootInputElement = inputElement;
521 } else {
522 return null;
523 }
524 }
525 } else if (nodeName.equals(OUTPUT_COLUMN_ELEMENT)) {
526 Element outputColumnElement = (Element) node;
527 OutputColumn outputColumn = new OutputColumn();
528 outputColumn.name = outputColumnElement.getAttribute(NAME_ATTRIBUTE);
529 def.outputs.add(outputColumn);
530 }
531 }
532 return def;
533 }
534
535 private static CustomXmlInputElement extractInputElement(Element inputElementElement) {
536 CustomXmlInputElement inputElement = new CustomXmlInputElement();
537 inputElement.setElementName(inputElementElement.getAttribute(NAME_ATTRIBUTE));
538 inputElement.setLogEntry((Boolean.toString(true).equals(inputElementElement.getAttribute(LOG_ENTRY_ATTRIBUTE))) ? true : false);
539 NodeList nodeList = inputElementElement.getChildNodes();
540 for (int i = 0; i < nodeList.getLength(); i++) {
541 Node node = nodeList.item(i);
542 String nodeName = node.getNodeName();
543 if (nodeName.equals(INPUT_DATA_ELEMENT)) {
544 Element inputDataElement = (Element) node;
545 inputElement.setInputName(inputDataElement.getAttribute(NAME_ATTRIBUTE));
546 inputElement.setInputAction(Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE)));
547 inputElement.setInputFormat(inputDataElement.getAttribute(FORMAT_ATTRIBUTE));
548 } else if (nodeName.equals(ATTRIBUTE_ELEMENT)) {
549 Element attributeElement = (Element) node;
550
551 String attributeName = attributeElement.getAttribute(NAME_ATTRIBUTE);
552 String inputName = null;
553 int inputAction = 0;
554 String inputFormat = null;
555 NodeList attributeNodeList = attributeElement.getChildNodes();
556 for (int j = 0; j < attributeNodeList.getLength(); j++) {
557 Node attributeNode = attributeNodeList.item(j);
558 String attributeNodeName = attributeNode.getNodeName();
559 if (attributeNodeName.equals(INPUT_DATA_ELEMENT)) {
560 Element inputDataElement = (Element) attributeNode;
561 inputName = inputDataElement.getAttribute(NAME_ATTRIBUTE);
562 inputAction = Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE));
563 inputFormat = inputDataElement.getAttribute(FORMAT_ATTRIBUTE);
564 }
565 }
566 inputElement.addAttribute(new CustomXmlInputAttribute(attributeName, inputName, inputAction, inputFormat));
567 } else if (nodeName.equals(INPUT_ELEMENT_ELEMENT)) {
568 Element childInputElementElement = (Element) node;
569 CustomXmlInputElement childInputElement = extractInputElement(childInputElementElement);
570 if (childInputElement != null) {
571 inputElement.addChild(childInputElement);
572 }
573 }
574 }
575 return inputElement;
576 }
577
578 /**
579 * Delete a definition from the currently loaded ones.
580 *
581 * @param categoryName
582 * The category of the definition to delete
583 * @param definitionName
584 * The name of the definition to delete
585 * @since 3.2
586 */
587 public static void delete(String categoryName, String definitionName) {
588 try {
589 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
590 DocumentBuilder db = dbf.newDocumentBuilder();
591
592 // The following allows xml parsing without access to the dtd
593 EntityResolver resolver = new EntityResolver() {
594 @Override
595 public InputSource resolveEntity(String publicId, String systemId) {
596 String empty = ""; //$NON-NLS-1$
597 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
598 return new InputSource(bais);
599 }
600 };
601 db.setEntityResolver(resolver);
602
603 // The following catches xml parsing exceptions
604 db.setErrorHandler(new ErrorHandler() {
605 @Override
606 public void error(SAXParseException saxparseexception) throws SAXException {
607 }
608
609 @Override
610 public void warning(SAXParseException saxparseexception) throws SAXException {
611 }
612
613 @Override
614 public void fatalError(SAXParseException saxparseexception) throws SAXException {
615 throw saxparseexception;
616 }
617 });
618
619 File file = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
620 Document doc = db.parse(file);
621
622 Element root = doc.getDocumentElement();
623 if (!root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
624 return;
625 }
626
627 Element definitionElement = findDefinitionElement(root, categoryName, definitionName);
628 if (definitionElement != null) {
629 root.removeChild(definitionElement);
630 }
631
632 Transformer transformer = TransformerFactory.newInstance().newTransformer();
633 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
634
635 // initialize StreamResult with File object to save to file
636 StreamResult result = new StreamResult(new StringWriter());
637 DOMSource source = new DOMSource(doc);
638 transformer.transform(source, result);
639 String xmlString = result.getWriter().toString();
640
641 try (FileWriter writer = new FileWriter(file);) {
642 writer.write(xmlString);
643 }
644
645 TmfTraceType.removeCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
646 // Check if default definition needs to be reloaded
647 TmfTraceType.addCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
648
649 } catch (ParserConfigurationException | SAXException | IOException | TransformerFactoryConfigurationError | TransformerException e) {
650 Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
651 }
652 }
653 }
This page took 0.045679 seconds and 5 git commands to generate.