tmf : Add parameters to XML core methods
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / tmf / analysis / xml / core / model / TmfXmlLocation.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ecole Polytechnique de Montreal
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 * Florian Wininger - Initial API and implementation
11 ******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.analysis.xml.core.model;
14
15 import java.util.LinkedList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
20 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
21 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
22 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
23 import org.w3c.dom.Element;
24
25 /**
26 * This Class implements a Location in the XML-defined state system, ie a named
27 * shortcut to reach a given attribute, used in state attributes
28 *
29 * <pre>
30 * example:
31 * <location id="CurrentCPU">
32 * <stateAttribute type="constant" value="CPUs" />
33 * <stateAttribute type="eventField" name="cpu" />
34 * ...
35 * </location>
36 * </pre>
37 *
38 * @author Florian Wininger
39 */
40 public class TmfXmlLocation {
41
42 /** Path in the State System */
43 private final List<ITmfXmlStateAttribute> fPath = new LinkedList<>();
44
45 /** ID : name of the location */
46 private final String fId;
47 private final IXmlStateSystemContainer fContainer;
48
49 /**
50 * Constructor
51 *
52 * @param modelFactory
53 * The factory used to create XML model elements
54 * @param location
55 * XML node element
56 * @param container
57 * The state system container this location belongs to
58 */
59 public TmfXmlLocation(ITmfXmlModelFactory modelFactory, Element location, IXmlStateSystemContainer container) {
60 String id = location.getAttribute(TmfXmlStrings.ID);
61 fId = id;
62 fContainer = container;
63
64 List<@Nullable Element> childElements = XmlUtils.getChildElements(location);
65 for (Element attribute : childElements) {
66 if (attribute == null) {
67 continue;
68 }
69 ITmfXmlStateAttribute xAttribute = modelFactory.createStateAttribute(attribute, fContainer);
70 fPath.add(xAttribute);
71 }
72 }
73
74 /**
75 * Get the id of the location
76 *
77 * @return The id of a location
78 */
79 public String getId() {
80 return fId;
81 }
82
83 /**
84 * Get the quark for the path represented by this location
85 *
86 * @param event
87 * The event being handled
88 *
89 * @param startQuark
90 * The starting quark for relative search, use
91 * {@link IXmlStateSystemContainer#ROOT_QUARK} for the root of
92 * the attribute tree
93 * @param scenarioInfo
94 * The active scenario details. Or <code>null</code> if there is
95 * no scenario.
96 * @return The quark at the leaf of the path
97 * @since 2.0
98 */
99 public int getLocationQuark(@Nullable ITmfEvent event, int startQuark, @Nullable TmfXmlScenarioInfo scenarioInfo) {
100 int quark = startQuark;
101 for (ITmfXmlStateAttribute attrib : fPath) {
102 quark = attrib.getAttributeQuark(event, quark, scenarioInfo);
103 if (quark == IXmlStateSystemContainer.ERROR_QUARK) {
104 break;
105 }
106 }
107 return quark;
108 }
109
110 /**
111 * Get the quark for the path represented by this location
112 *
113 * @param startQuark
114 * The starting quark for relative search, use
115 * {@link IXmlStateSystemContainer#ROOT_QUARK} for the root of
116 * the attribute tree
117 * @param scenarioInfo
118 * The active scenario details. Or <code>null</code> if there is
119 * no scenario.
120 * @return The quark at the leaf of the path
121 * @since 2.0
122 */
123 public int getLocationQuark(int startQuark, @Nullable TmfXmlScenarioInfo scenarioInfo) {
124 int quark = startQuark;
125 for (ITmfXmlStateAttribute attrib : fPath) {
126 quark = attrib.getAttributeQuark(quark, scenarioInfo);
127 if (quark == IXmlStateSystemContainer.ERROR_QUARK) {
128 break;
129 }
130 }
131 return quark;
132 }
133
134 @Override
135 public String toString() {
136 return "TmfXmlLocation " + fId + ": " + fPath; //$NON-NLS-1$ //$NON-NLS-2$
137 }
138
139 }
This page took 0.045019 seconds and 5 git commands to generate.