tmf: Null-annotate state system API classes
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / tmf / analysis / xml / core / stateprovider / XmlStateProvider.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 * Florian Wininger - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
25 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
26 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlEventHandler;
27 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlLocation;
28 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.readwrite.TmfXmlReadWriteModelFactory;
29 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
30 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
31 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
32 import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
33 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.NodeList;
36
37 /**
38 * This is the state change input plug-in for TMF's state system which handles
39 * the XML Format
40 *
41 * @author Florian Wininger
42 */
43 public class XmlStateProvider extends AbstractTmfStateProvider implements IXmlStateSystemContainer {
44
45 private final IPath fFilePath;
46 @NonNull private final String fStateId;
47
48 /** List of all Event Handlers */
49 private final Set<TmfXmlEventHandler> fEventHandlers = new HashSet<>();
50
51 /** List of all Locations */
52 private final Set<TmfXmlLocation> fLocations;
53
54 /** Map for defined values */
55 private final Map<String, String> fDefinedValues = new HashMap<>();
56
57 // ------------------------------------------------------------------------
58 // Constructor
59 // ------------------------------------------------------------------------
60
61 /**
62 * Instantiate a new state provider plug-in.
63 *
64 * @param trace
65 * The trace
66 * @param stateid
67 * The state system id, corresponding to the analysis_id
68 * attribute of the state provider element of the XML file
69 * @param file
70 * Path to the XML file containing the state provider definition
71 */
72 public XmlStateProvider(@NonNull ITmfTrace trace, @NonNull String stateid, IPath file) {
73 super(trace, ITmfEvent.class, stateid);
74 fStateId = stateid;
75 fFilePath = file;
76 Element doc = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId);
77 if (doc == null) {
78 fLocations = new HashSet<>();
79 return;
80 }
81
82 ITmfXmlModelFactory modelFactory = TmfXmlReadWriteModelFactory.getInstance();
83 /* parser for defined Values */
84 NodeList definedStateNodes = doc.getElementsByTagName(TmfXmlStrings.DEFINED_VALUE);
85 for (int i = 0; i < definedStateNodes.getLength(); i++) {
86 Element element = (Element) definedStateNodes.item(i);
87 fDefinedValues.put(element.getAttribute(TmfXmlStrings.NAME), element.getAttribute(TmfXmlStrings.VALUE));
88 }
89
90 /* parser for the locations */
91 List<Element> childElements = XmlUtils.getChildElements(doc, TmfXmlStrings.LOCATION);
92 Set<TmfXmlLocation> locations = new HashSet<>();
93 for (Element element : childElements) {
94 if (element == null) {
95 continue;
96 }
97 TmfXmlLocation location = modelFactory.createLocation(element, this);
98 locations.add(location);
99 }
100 fLocations = Collections.unmodifiableSet(locations);
101
102 /* parser for the event handlers */
103 childElements = XmlUtils.getChildElements(doc, TmfXmlStrings.EVENT_HANDLER);
104 for (Element element : childElements) {
105 if (element == null) {
106 continue;
107 }
108 TmfXmlEventHandler handler = modelFactory.createEventHandler(element, this);
109 fEventHandlers.add(handler);
110 }
111 }
112
113 /**
114 * Get the state id of the state provider
115 *
116 * @return The state id of the state provider
117 */
118 @NonNull
119 public String getStateId() {
120 return fStateId;
121 }
122
123 // ------------------------------------------------------------------------
124 // IStateChangeInput
125 // ------------------------------------------------------------------------
126
127 @Override
128 public int getVersion() {
129 Element ssNode = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId);
130 if (ssNode != null) {
131 return Integer.parseInt(ssNode.getAttribute(TmfXmlStrings.VERSION));
132 }
133 /*
134 * The version attribute is mandatory and XML files that don't validate
135 * with the XSD are ignored, so this should never happen
136 */
137 throw new IllegalStateException("The state provider XML node should have a version attribute"); //$NON-NLS-1$
138 }
139
140 @Override
141 public XmlStateProvider getNewInstance() {
142 return new XmlStateProvider(this.getTrace(), getStateId(), fFilePath);
143 }
144
145 @Override
146 protected void eventHandle(ITmfEvent event) {
147 for (TmfXmlEventHandler eventHandler : fEventHandlers) {
148 eventHandler.handleEvent(event);
149 }
150 }
151
152 @Override
153 public ITmfStateSystem getStateSystem() {
154 return getStateSystemBuilder();
155 }
156
157 // ------------------------------------------------------------------------
158 // Operations
159 // ------------------------------------------------------------------------
160
161 @Override
162 public Iterable<TmfXmlLocation> getLocations() {
163 return fLocations;
164 }
165
166 /**
167 * Get the defined value associated with a constant
168 *
169 * @param constant
170 * The constant defining this value
171 * @return The actual value corresponding to this constant
172 */
173 public String getDefinedValue(String constant) {
174 return fDefinedValues.get(constant);
175 }
176
177 @Override
178 public String getAttributeValue(String name) {
179 String attribute = name;
180 if (attribute.startsWith(TmfXmlStrings.VARIABLE_PREFIX)) {
181 /* search the attribute in the map without the fist character $ */
182 attribute = getDefinedValue(attribute.substring(1));
183 }
184 return attribute;
185 }
186
187 }
This page took 0.034509 seconds and 5 git commands to generate.