51ebde5f50b5e2ddb19704cb10ff34759cf07b66
[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 * @param startQuark
89 * The starting quark for relative search, use
90 * {@link IXmlStateSystemContainer#ROOT_QUARK} for the root of
91 * the attribute tree
92 * @return The quark at the leaf of the path
93 */
94 public int getLocationQuark(@Nullable ITmfEvent event, int startQuark) {
95 int quark = startQuark;
96 for (ITmfXmlStateAttribute attrib : fPath) {
97 quark = attrib.getAttributeQuark(event, quark);
98 if (quark == IXmlStateSystemContainer.ERROR_QUARK) {
99 break;
100 }
101 }
102 return quark;
103 }
104
105 /**
106 * Get the quark for the path represented by this location
107 *
108 * @param startQuark
109 * The starting quark for relative search, use
110 * {@link IXmlStateSystemContainer#ROOT_QUARK} for the root of
111 * the attribute tree
112 * @return The quark at the leaf of the path
113 */
114 public int getLocationQuark(int startQuark) {
115 int quark = startQuark;
116 for (ITmfXmlStateAttribute attrib : fPath) {
117 quark = attrib.getAttributeQuark(quark);
118 if (quark == IXmlStateSystemContainer.ERROR_QUARK) {
119 break;
120 }
121 }
122 return quark;
123 }
124
125 @Override
126 public String toString() {
127 return "TmfXmlLocation " + fId + ": " + fPath; //$NON-NLS-1$ //$NON-NLS-2$
128 }
129
130 }
This page took 0.045726 seconds and 4 git commands to generate.