tmf: Make custom trace output column name @NonNull
[deliverable/tracecompass.git] / tmf / 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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.StringWriter;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Comparator;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.TreeSet;
31
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
34 import javax.xml.parsers.ParserConfigurationException;
35 import javax.xml.transform.OutputKeys;
36 import javax.xml.transform.Transformer;
37 import javax.xml.transform.TransformerException;
38 import javax.xml.transform.TransformerFactory;
39 import javax.xml.transform.TransformerFactoryConfigurationError;
40 import javax.xml.transform.dom.DOMSource;
41 import javax.xml.transform.stream.StreamResult;
42
43 import org.eclipse.core.runtime.Platform;
44 import org.eclipse.tracecompass.internal.tmf.core.Activator;
45 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
46 import org.w3c.dom.Document;
47 import org.w3c.dom.Element;
48 import org.w3c.dom.Node;
49 import org.w3c.dom.NodeList;
50 import org.xml.sax.EntityResolver;
51 import org.xml.sax.ErrorHandler;
52 import org.xml.sax.InputSource;
53 import org.xml.sax.SAXException;
54 import org.xml.sax.SAXParseException;
55
56 /**
57 * Trace definition for custom XML traces.
58 *
59 * @author Patrick Tassé
60 */
61 public class CustomXmlTraceDefinition extends CustomTraceDefinition {
62
63 /** "ignore" tag */
64 public static final String TAG_IGNORE = Messages.CustomXmlTraceDefinition_ignoreTag;
65
66 /**
67 * Custom XML label used internally and therefore should not be externalized
68 */
69 public static final String CUSTOM_XML_CATEGORY = "Custom XML"; //$NON-NLS-1$
70
71
72 /** Name of the default XML definitions file */
73 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME = "custom_xml_default_parsers.xml"; //$NON-NLS-1$
74
75 /** Name of the XML definitions file */
76 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME = "custom_xml_parsers.xml"; //$NON-NLS-1$
77
78 /** Path to the XML definitions file */
79 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME =
80 Platform.getInstallLocation().getURL().getPath() + "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$
81 CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_FILE_NAME;
82
83 /** Path to the XML definitions file */
84 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME =
85 Activator.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();
86
87 /**
88 * Legacy path to the XML definitions file (in the UI plug-in of linux tools) TODO Remove
89 * once we feel the transition phase is over.
90 */
91 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI =
92 Activator.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
93 .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
94 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();
95
96 /**
97 * Legacy path to the XML definitions file (in the core plug-in of linux tools) TODO Remove
98 * once we feel the transition phase is over.
99 */
100 private static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE =
101 Activator.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator()
102 .append("org.eclipse.linuxtools.tmf.core") //$NON-NLS-1$
103 .append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();
104
105 // TODO: These strings should not be externalized
106 private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT = Messages.CustomXmlTraceDefinition_definitionRootElement;
107 private static final String DEFINITION_ELEMENT = Messages.CustomXmlTraceDefinition_definition;
108 private static final String CATEGORY_ATTRIBUTE = Messages.CustomXmlTraceDefinition_category;
109 private static final String NAME_ATTRIBUTE = Messages.CustomXmlTraceDefinition_name;
110 private static final String LOG_ENTRY_ATTRIBUTE = Messages.CustomXmlTraceDefinition_logEntry;
111 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT = Messages.CustomXmlTraceDefinition_timestampOutputFormat;
112 private static final String INPUT_ELEMENT_ELEMENT = Messages.CustomXmlTraceDefinition_inputElement;
113 private static final String ATTRIBUTE_ELEMENT = Messages.CustomXmlTraceDefinition_attribute;
114 private static final String INPUT_DATA_ELEMENT = Messages.CustomXmlTraceDefinition_inputData;
115 private static final String ACTION_ATTRIBUTE = Messages.CustomXmlTraceDefinition_action;
116 private static final String FORMAT_ATTRIBUTE = Messages.CustomXmlTraceDefinition_format;
117 private static final String OUTPUT_COLUMN_ELEMENT = Messages.CustomXmlTraceDefinition_outputColumn;
118
119 /** Top-level input element */
120 public CustomXmlInputElement rootInputElement;
121
122 /**
123 * Default constructor
124 */
125 public CustomXmlTraceDefinition() {
126 this(CUSTOM_XML_CATEGORY, "", null, new ArrayList<OutputColumn>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
127 }
128
129 /**
130 * Full constructor
131 *
132 * @param category
133 * Category of the trace type
134 * @param traceType
135 * Name of the trace type
136 * @param rootElement
137 * The top-level XML element
138 * @param outputs
139 * The list of output columns
140 * @param timeStampOutputFormat
141 * The timestamp format to use
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 */
290 public static CustomXmlTraceDefinition[] loadAll(boolean includeDefaults) {
291 File defaultFile = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
292 File legacyFileUI = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI);
293 File legacyFileCore = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE);
294
295 /*
296 * If there is no file at the expected location, check the legacy
297 * locations instead.
298 */
299 if (!defaultFile.exists()) {
300 if (legacyFileCore.exists()) {
301 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_CORE);
302 } else if (legacyFileUI.exists()) {
303 transferDefinitions(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME_LEGACY_UI);
304 }
305 }
306
307 Set<CustomXmlTraceDefinition> defs = new TreeSet<>(new Comparator<CustomXmlTraceDefinition>() {
308 @Override
309 public int compare(CustomXmlTraceDefinition o1, CustomXmlTraceDefinition o2) {
310 int result = o1.categoryName.compareTo(o2.categoryName);
311 if (result != 0) {
312 return result;
313 }
314 return o1.definitionName.compareTo(o2.definitionName);
315 }
316 });
317 defs.addAll(Arrays.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME)));
318 if (includeDefaults) {
319 defs.addAll(Arrays.asList(loadAll(CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME)));
320 }
321 return defs.toArray(new CustomXmlTraceDefinition[0]);
322 }
323
324 private static void transferDefinitions(String defFile) {
325 CustomXmlTraceDefinition[] oldDefs = loadAll(defFile);
326 for (CustomXmlTraceDefinition def : oldDefs) {
327 /* Save in the new location */
328 def.save();
329 }
330 }
331
332
333 /**
334 * Load all the XML trace definitions in the given definitions file.
335 *
336 * @param path
337 * Path to the definitions file to load
338 * @return The loaded trace definitions
339 */
340 public static CustomXmlTraceDefinition[] loadAll(String path) {
341 File file = new File(path);
342 if (!file.canRead()) {
343 return new CustomXmlTraceDefinition[0];
344 }
345 try (FileInputStream fis = new FileInputStream(file);) {
346 return loadAll(fis);
347 } catch (IOException e) {
348 Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$
349 }
350 return new CustomXmlTraceDefinition[0];
351 }
352
353 /**
354 * Load all the XML trace definitions from the given stream
355 *
356 * @param stream
357 * An input stream from which to read the definitions
358 * @return The loaded trace definitions
359 */
360 public static CustomXmlTraceDefinition[] loadAll(InputStream stream) {
361 try {
362 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
363 DocumentBuilder db = dbf.newDocumentBuilder();
364
365 // The following allows xml parsing without access to the dtd
366 db.setEntityResolver(createEmptyEntityResolver());
367
368 // The following catches xml parsing exceptions
369 db.setErrorHandler(createErrorHandler());
370
371 Document doc = db.parse(stream);
372 Element root = doc.getDocumentElement();
373 if (!root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
374 return new CustomXmlTraceDefinition[0];
375 }
376
377 ArrayList<CustomXmlTraceDefinition> defList = new ArrayList<>();
378 NodeList nodeList = root.getChildNodes();
379 for (int i = 0; i < nodeList.getLength(); i++) {
380 Node node = nodeList.item(i);
381 if (node instanceof Element && node.getNodeName().equals(DEFINITION_ELEMENT)) {
382 CustomXmlTraceDefinition def = extractDefinition((Element) node);
383 if (def != null) {
384 defList.add(def);
385 }
386 }
387 }
388 return defList.toArray(new CustomXmlTraceDefinition[0]);
389 } catch (ParserConfigurationException | SAXException | IOException e) {
390 Activator.logError("Error loading all in CustomXmlTraceDefinition: path=" + stream, e); //$NON-NLS-1$
391 }
392 return new CustomXmlTraceDefinition[0];
393 }
394
395 /**
396 * Load the given trace definition.
397 *
398 * @param categoryName
399 * Category of the definition to load
400 * @param definitionName
401 * Name of the XML trace definition to load
402 * @return The loaded trace definition
403 */
404 public static CustomXmlTraceDefinition load(String categoryName, String definitionName) {
405 try {
406 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
407 DocumentBuilder db = dbf.newDocumentBuilder();
408
409 // The following allows xml parsing without access to the dtd
410 EntityResolver resolver = new EntityResolver() {
411 @Override
412 public InputSource resolveEntity(String publicId, String systemId) {
413 String empty = ""; //$NON-NLS-1$
414 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
415 return new InputSource(bais);
416 }
417 };
418 db.setEntityResolver(resolver);
419
420 // The following catches xml parsing exceptions
421 db.setErrorHandler(new ErrorHandler() {
422 @Override
423 public void error(SAXParseException saxparseexception) throws SAXException {
424 }
425
426 @Override
427 public void warning(SAXParseException saxparseexception) throws SAXException {
428 }
429
430 @Override
431 public void fatalError(SAXParseException saxparseexception) throws SAXException {
432 throw saxparseexception;
433 }
434 });
435
436 CustomXmlTraceDefinition value = lookupXmlDefinition(categoryName, definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
437 if (value == null) {
438 value = lookupXmlDefinition(categoryName, definitionName, db, CUSTOM_XML_TRACE_DEFINITIONS_DEFAULT_PATH_NAME);
439 }
440 return value;
441 } catch (ParserConfigurationException | SAXException | IOException e) {
442 Activator.logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
443 }
444 return null;
445 }
446
447 private static CustomXmlTraceDefinition lookupXmlDefinition(String categoryName, String definitionName, DocumentBuilder db, String source) throws SAXException, IOException {
448 File file = new File(source);
449 if (!file.exists()) {
450 return null;
451 }
452
453 Document doc = db.parse(file);
454
455 Element root = doc.getDocumentElement();
456 if (!root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
457 return null;
458 }
459
460 Element definitionElement = findDefinitionElement(root, categoryName, definitionName);
461 if (definitionElement != null) {
462 return extractDefinition(definitionElement);
463 }
464 return null;
465 }
466
467 private static Element findDefinitionElement(Element root, String categoryName, String definitionName) {
468 NodeList nodeList = root.getChildNodes();
469 for (int i = 0; i < nodeList.getLength(); i++) {
470 Node node = nodeList.item(i);
471 if (node instanceof Element && node.getNodeName().equals(DEFINITION_ELEMENT)) {
472 Element element = (Element) node;
473 String categoryAttribute = element.getAttribute(CATEGORY_ATTRIBUTE);
474 if (categoryAttribute.isEmpty()) {
475 categoryAttribute = CUSTOM_XML_CATEGORY;
476 }
477 String nameAttribute = element.getAttribute(NAME_ATTRIBUTE);
478 if (categoryName.equals(categoryAttribute) &&
479 definitionName.equals(nameAttribute)) {
480 return element;
481 }
482 }
483 }
484 return null;
485 }
486
487 /**
488 * Extract a trace definition from an XML element.
489 *
490 * @param definitionElement
491 * Definition element
492 * @return The extracted trace definition
493 */
494 public static CustomXmlTraceDefinition extractDefinition(Element definitionElement) {
495 CustomXmlTraceDefinition def = new CustomXmlTraceDefinition();
496
497 def.categoryName = definitionElement.getAttribute(CATEGORY_ATTRIBUTE);
498 if (def.categoryName.isEmpty()) {
499 def.categoryName = CUSTOM_XML_CATEGORY;
500 }
501 def.definitionName = definitionElement.getAttribute(NAME_ATTRIBUTE);
502 if (def.definitionName.isEmpty()) {
503 return null;
504 }
505
506 NodeList nodeList = definitionElement.getChildNodes();
507 for (int i = 0; i < nodeList.getLength(); i++) {
508 Node node = nodeList.item(i);
509 String nodeName = node.getNodeName();
510 if (nodeName.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT)) {
511 Element formatElement = (Element) node;
512 def.timeStampOutputFormat = formatElement.getTextContent();
513 } else if (nodeName.equals(INPUT_ELEMENT_ELEMENT)) {
514 CustomXmlInputElement inputElement = extractInputElement((Element) node);
515 if (inputElement != null) {
516 if (def.rootInputElement == null) {
517 def.rootInputElement = inputElement;
518 } else {
519 return null;
520 }
521 }
522 } else if (nodeName.equals(OUTPUT_COLUMN_ELEMENT)) {
523 Element outputColumnElement = (Element) node;
524 OutputColumn outputColumn = new OutputColumn();
525 outputColumn.name = checkNotNull(outputColumnElement.getAttribute(NAME_ATTRIBUTE));
526 def.outputs.add(outputColumn);
527 }
528 }
529 return def;
530 }
531
532 private static CustomXmlInputElement extractInputElement(Element inputElementElement) {
533 CustomXmlInputElement inputElement = new CustomXmlInputElement();
534 inputElement.setElementName(inputElementElement.getAttribute(NAME_ATTRIBUTE));
535 inputElement.setLogEntry((Boolean.toString(true).equals(inputElementElement.getAttribute(LOG_ENTRY_ATTRIBUTE))) ? true : false);
536 NodeList nodeList = inputElementElement.getChildNodes();
537 for (int i = 0; i < nodeList.getLength(); i++) {
538 Node node = nodeList.item(i);
539 String nodeName = node.getNodeName();
540 if (nodeName.equals(INPUT_DATA_ELEMENT)) {
541 Element inputDataElement = (Element) node;
542 inputElement.setInputName(inputDataElement.getAttribute(NAME_ATTRIBUTE));
543 inputElement.setInputAction(Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE)));
544 inputElement.setInputFormat(inputDataElement.getAttribute(FORMAT_ATTRIBUTE));
545 } else if (nodeName.equals(ATTRIBUTE_ELEMENT)) {
546 Element attributeElement = (Element) node;
547
548 String attributeName = attributeElement.getAttribute(NAME_ATTRIBUTE);
549 String inputName = null;
550 int inputAction = 0;
551 String inputFormat = null;
552 NodeList attributeNodeList = attributeElement.getChildNodes();
553 for (int j = 0; j < attributeNodeList.getLength(); j++) {
554 Node attributeNode = attributeNodeList.item(j);
555 String attributeNodeName = attributeNode.getNodeName();
556 if (attributeNodeName.equals(INPUT_DATA_ELEMENT)) {
557 Element inputDataElement = (Element) attributeNode;
558 inputName = inputDataElement.getAttribute(NAME_ATTRIBUTE);
559 inputAction = Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE));
560 inputFormat = inputDataElement.getAttribute(FORMAT_ATTRIBUTE);
561 }
562 }
563 inputElement.addAttribute(new CustomXmlInputAttribute(attributeName, inputName, inputAction, inputFormat));
564 } else if (nodeName.equals(INPUT_ELEMENT_ELEMENT)) {
565 Element childInputElementElement = (Element) node;
566 CustomXmlInputElement childInputElement = extractInputElement(childInputElementElement);
567 if (childInputElement != null) {
568 inputElement.addChild(childInputElement);
569 }
570 }
571 }
572 return inputElement;
573 }
574
575 /**
576 * Delete a definition from the currently loaded ones.
577 *
578 * @param categoryName
579 * The category of the definition to delete
580 * @param definitionName
581 * The name of the definition to delete
582 */
583 public static void delete(String categoryName, String definitionName) {
584 try {
585 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
586 DocumentBuilder db = dbf.newDocumentBuilder();
587
588 // The following allows xml parsing without access to the dtd
589 EntityResolver resolver = new EntityResolver() {
590 @Override
591 public InputSource resolveEntity(String publicId, String systemId) {
592 String empty = ""; //$NON-NLS-1$
593 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
594 return new InputSource(bais);
595 }
596 };
597 db.setEntityResolver(resolver);
598
599 // The following catches xml parsing exceptions
600 db.setErrorHandler(new ErrorHandler() {
601 @Override
602 public void error(SAXParseException saxparseexception) throws SAXException {
603 }
604
605 @Override
606 public void warning(SAXParseException saxparseexception) throws SAXException {
607 }
608
609 @Override
610 public void fatalError(SAXParseException saxparseexception) throws SAXException {
611 throw saxparseexception;
612 }
613 });
614
615 File file = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);
616 Document doc = db.parse(file);
617
618 Element root = doc.getDocumentElement();
619 if (!root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {
620 return;
621 }
622
623 Element definitionElement = findDefinitionElement(root, categoryName, definitionName);
624 if (definitionElement != null) {
625 root.removeChild(definitionElement);
626 }
627
628 Transformer transformer = TransformerFactory.newInstance().newTransformer();
629 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
630
631 // initialize StreamResult with File object to save to file
632 StreamResult result = new StreamResult(new StringWriter());
633 DOMSource source = new DOMSource(doc);
634 transformer.transform(source, result);
635 String xmlString = result.getWriter().toString();
636
637 try (FileWriter writer = new FileWriter(file);) {
638 writer.write(xmlString);
639 }
640
641 TmfTraceType.removeCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
642 // Check if default definition needs to be reloaded
643 TmfTraceType.addCustomTraceType(CustomXmlTrace.class, categoryName, definitionName);
644
645 } catch (ParserConfigurationException | SAXException | IOException | TransformerFactoryConfigurationError | TransformerException e) {
646 Activator.logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$
647 }
648 }
649 }
This page took 0.06082 seconds and 6 git commands to generate.