ad73371c8523c9edc60121bf2253462d94ccf48b
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / tmf / analysis / xml / core / stateprovider / XmlStateProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 É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.ArrayList;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
26 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
27 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlEventHandler;
28 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlLocation;
29 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.readwrite.TmfXmlReadWriteModelFactory;
30 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
31 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
32 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
33 import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
34 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.NodeList;
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 */
44 public class XmlStateProvider extends AbstractTmfStateProvider implements IXmlStateSystemContainer {
45
46 private final IPath fFilePath;
47 @NonNull private final String fStateId;
48
49 /** List of all Event Handlers */
50 private final List<TmfXmlEventHandler> fEventHandlers = new ArrayList<>();
51
52 /** List of all Locations */
53 private final Set<TmfXmlLocation> fLocations;
54
55 /** Map for defined values */
56 private final Map<String, String> fDefinedValues = new HashMap<>();
57
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 */
73 public XmlStateProvider(@NonNull ITmfTrace trace, @NonNull String stateid, IPath file) {
74 super(trace, stateid);
75 fStateId = stateid;
76 fFilePath = file;
77 Element doc = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId);
78 if (doc == null) {
79 fLocations = new HashSet<>();
80 return;
81 }
82
83 ITmfXmlModelFactory modelFactory = TmfXmlReadWriteModelFactory.getInstance();
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 */
92 List<Element> childElements = XmlUtils.getChildElements(doc, TmfXmlStrings.LOCATION);
93 Set<TmfXmlLocation> locations = new HashSet<>();
94 for (Element element : childElements) {
95 if (element == null) {
96 continue;
97 }
98 TmfXmlLocation location = modelFactory.createLocation(element, this);
99 locations.add(location);
100 }
101 fLocations = Collections.unmodifiableSet(locations);
102
103 /* parser for the event handlers */
104 childElements = XmlUtils.getChildElements(doc, TmfXmlStrings.EVENT_HANDLER);
105 for (Element element : childElements) {
106 if (element == null) {
107 continue;
108 }
109 TmfXmlEventHandler handler = modelFactory.createEventHandler(element, this);
110 fEventHandlers.add(handler);
111 }
112 }
113
114 /**
115 * Get the state id of the state provider
116 *
117 * @return The state id of the state provider
118 */
119 @NonNull
120 public String getStateId() {
121 return fStateId;
122 }
123
124 // ------------------------------------------------------------------------
125 // IStateChangeInput
126 // ------------------------------------------------------------------------
127
128 @Override
129 public int getVersion() {
130 Element ssNode = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId);
131 if (ssNode != null) {
132 return Integer.parseInt(ssNode.getAttribute(TmfXmlStrings.VERSION));
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) {
148 for (TmfXmlEventHandler eventHandler : fEventHandlers) {
149 eventHandler.handleEvent(event);
150 }
151 }
152
153 @Override
154 public ITmfStateSystem getStateSystem() {
155 return getStateSystemBuilder();
156 }
157
158 // ------------------------------------------------------------------------
159 // Operations
160 // ------------------------------------------------------------------------
161
162 @Override
163 public Iterable<TmfXmlLocation> getLocations() {
164 return fLocations;
165 }
166
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
178 @Override
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;
186 }
187
188 }
This page took 0.034539 seconds and 4 git commands to generate.