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