Commit | Line | Data |
---|---|---|
e11e382c FW |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider; |
e11e382c | 14 | |
0f7276b6 GB |
15 | import java.util.Collections; |
16 | import java.util.HashMap; | |
17 | import java.util.HashSet; | |
12685851 | 18 | import java.util.List; |
0f7276b6 GB |
19 | import java.util.Map; |
20 | import java.util.Set; | |
e11e382c | 21 | |
e11e382c | 22 | import org.eclipse.core.runtime.IPath; |
1d7e62f9 | 23 | import org.eclipse.jdt.annotation.NonNull; |
e894a508 | 24 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
2bdf0193 AM |
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; | |
e11e382c | 34 | import org.w3c.dom.Element; |
e11e382c | 35 | import org.w3c.dom.NodeList; |
e11e382c FW |
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 | */ | |
1d7e62f9 | 43 | public class XmlStateProvider extends AbstractTmfStateProvider implements IXmlStateSystemContainer { |
0f7276b6 | 44 | |
e11e382c | 45 | private final IPath fFilePath; |
1d7e62f9 | 46 | @NonNull private final String fStateId; |
e11e382c | 47 | |
0f7276b6 GB |
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 | ||
e11e382c FW |
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 | */ | |
d0c7e4ba | 72 | public XmlStateProvider(@NonNull ITmfTrace trace, @NonNull String stateid, IPath file) { |
e11e382c FW |
73 | super(trace, ITmfEvent.class, stateid); |
74 | fStateId = stateid; | |
75 | fFilePath = file; | |
1d7e62f9 | 76 | Element doc = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId); |
0f7276b6 GB |
77 | if (doc == null) { |
78 | fLocations = new HashSet<>(); | |
79 | return; | |
80 | } | |
81 | ||
b1ebf44e | 82 | ITmfXmlModelFactory modelFactory = TmfXmlReadWriteModelFactory.getInstance(); |
0f7276b6 GB |
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 */ | |
12685851 | 91 | List<Element> childElements = XmlUtils.getChildElements(doc, TmfXmlStrings.LOCATION); |
0f7276b6 | 92 | Set<TmfXmlLocation> locations = new HashSet<>(); |
12685851 GB |
93 | for (Element element : childElements) { |
94 | if (element == null) { | |
95 | continue; | |
96 | } | |
1d7e62f9 | 97 | TmfXmlLocation location = modelFactory.createLocation(element, this); |
0f7276b6 GB |
98 | locations.add(location); |
99 | } | |
100 | fLocations = Collections.unmodifiableSet(locations); | |
101 | ||
102 | /* parser for the event handlers */ | |
12685851 GB |
103 | childElements = XmlUtils.getChildElements(doc, TmfXmlStrings.EVENT_HANDLER); |
104 | for (Element element : childElements) { | |
105 | if (element == null) { | |
106 | continue; | |
107 | } | |
1d7e62f9 | 108 | TmfXmlEventHandler handler = modelFactory.createEventHandler(element, this); |
0f7276b6 GB |
109 | fEventHandlers.add(handler); |
110 | } | |
e11e382c FW |
111 | } |
112 | ||
113 | /** | |
114 | * Get the state id of the state provider | |
115 | * | |
116 | * @return The state id of the state provider | |
117 | */ | |
1d7e62f9 | 118 | @NonNull |
e11e382c FW |
119 | public String getStateId() { |
120 | return fStateId; | |
121 | } | |
122 | ||
123 | // ------------------------------------------------------------------------ | |
124 | // IStateChangeInput | |
125 | // ------------------------------------------------------------------------ | |
126 | ||
127 | @Override | |
128 | public int getVersion() { | |
1d7e62f9 GB |
129 | Element ssNode = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId); |
130 | if (ssNode != null) { | |
131 | return Integer.parseInt(ssNode.getAttribute(TmfXmlStrings.VERSION)); | |
e11e382c FW |
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) { | |
0f7276b6 GB |
147 | for (TmfXmlEventHandler eventHandler : fEventHandlers) { |
148 | eventHandler.handleEvent(event); | |
149 | } | |
150 | } | |
151 | ||
152 | @Override | |
1d7e62f9 | 153 | public ITmfStateSystem getStateSystem() { |
d0c7e4ba | 154 | return getStateSystemBuilder(); |
e11e382c FW |
155 | } |
156 | ||
157 | // ------------------------------------------------------------------------ | |
158 | // Operations | |
159 | // ------------------------------------------------------------------------ | |
160 | ||
1d7e62f9 | 161 | @Override |
0f7276b6 GB |
162 | public Iterable<TmfXmlLocation> getLocations() { |
163 | return fLocations; | |
164 | } | |
e11e382c | 165 | |
0f7276b6 GB |
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 | ||
1d7e62f9 | 177 | @Override |
0f7276b6 GB |
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; | |
e11e382c FW |
185 | } |
186 | ||
187 | } |