TMF: Add XML state provider analysis module and XML utilities
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.core / src / org / eclipse / linuxtools / tmf / analysis / xml / core / module / XmlHeadInfo.java
1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.analysis.xml.core.module;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
19 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
20 import org.w3c.dom.Element;
21 import org.w3c.dom.Node;
22 import org.w3c.dom.NodeList;
23
24 /**
25 * Class to store and interpret the header information of XML-defined components
26 *
27 * @author Geneviève Bastien
28 */
29 public class XmlHeadInfo {
30
31 private final Node fHeadInfo;
32
33 /**
34 * Constructor
35 *
36 * @param item
37 * The XML node corresponding to this header
38 */
39 public XmlHeadInfo(Node item) {
40 fHeadInfo = item;
41 }
42
43 /**
44 * Get a list of child elements with the requested name
45 *
46 * @param nodeName
47 * The name of the nodes to get
48 * @return List of child elements
49 */
50 public List<Element> getElements(String nodeName) {
51 List<Element> list = new ArrayList<>();
52 NodeList nodes = fHeadInfo.getChildNodes();
53 for (int i = 0; i < nodes.getLength(); i++) {
54 Node node = nodes.item(i);
55 if (node instanceof Element) {
56 Element element = (Element) node;
57 if (element.getNodeName().equals(nodeName)) {
58 list.add(element);
59 }
60 }
61 }
62 return list;
63 }
64
65 /**
66 * Check whether, if this header information has trace types defined, this
67 * component applies to a given trace type.
68 *
69 * @param traceClass
70 * The trace type to check for
71 * @return True if there is no trace type information in header or if the
72 * trace type applies
73 */
74 public boolean checkTraceType(Class<? extends ITmfTrace> traceClass) {
75 /*
76 * By default this returns true, child implementation who have access to
77 * trace types will override this
78 *
79 * TODO: actually check the trace type here when trace types are moved
80 * to o.e.l.tmf.core instead of o.e.l.tmf.ui
81 */
82 return true;
83 }
84
85 /**
86 * Get the name of this component from the header information
87 *
88 * @return The name of the component
89 */
90 public String getName() {
91 List<Element> elements = getElements(TmfXmlStrings.LABEL);
92 if (elements.isEmpty()) {
93 return null;
94 }
95
96 Element element = elements.get(0);
97 return element.getAttribute(TmfXmlStrings.VALUE);
98 }
99
100 }
This page took 0.032769 seconds and 5 git commands to generate.