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