tmf: Split the state system in a separate plugin
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.core / src / org / eclipse / linuxtools / 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.linuxtools.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.Map;
19 import java.util.Set;
20
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
24 import org.eclipse.linuxtools.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
25 import org.eclipse.linuxtools.tmf.analysis.xml.core.model.TmfXmlEventHandler;
26 import org.eclipse.linuxtools.tmf.analysis.xml.core.model.TmfXmlLocation;
27 import org.eclipse.linuxtools.tmf.analysis.xml.core.model.readwrite.TmfXmlReadWriteModelFactory;
28 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
29 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils;
30 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
31 import org.eclipse.linuxtools.tmf.core.statesystem.AbstractTmfStateProvider;
32 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.NodeList;
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 */
42 public class XmlStateProvider extends AbstractTmfStateProvider implements IXmlStateSystemContainer {
43
44 private final IPath fFilePath;
45 @NonNull private final String fStateId;
46
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
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 */
71 public XmlStateProvider(ITmfTrace trace, @NonNull String stateid, IPath file) {
72 super(trace, ITmfEvent.class, stateid);
73 fStateId = stateid;
74 fFilePath = file;
75 Element doc = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId);
76 if (doc == null) {
77 fLocations = new HashSet<>();
78 return;
79 }
80
81 ITmfXmlModelFactory modelFactory = TmfXmlReadWriteModelFactory.getInstance();
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);
94 TmfXmlLocation location = modelFactory.createLocation(element, this);
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);
103 TmfXmlEventHandler handler = modelFactory.createEventHandler(element, this);
104 fEventHandlers.add(handler);
105 }
106 }
107
108 /**
109 * Get the state id of the state provider
110 *
111 * @return The state id of the state provider
112 */
113 @NonNull
114 public String getStateId() {
115 return fStateId;
116 }
117
118 // ------------------------------------------------------------------------
119 // IStateChangeInput
120 // ------------------------------------------------------------------------
121
122 @Override
123 public int getVersion() {
124 Element ssNode = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId);
125 if (ssNode != null) {
126 return Integer.parseInt(ssNode.getAttribute(TmfXmlStrings.VERSION));
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) {
142 if (event == null) {
143 return;
144 }
145 for (TmfXmlEventHandler eventHandler : fEventHandlers) {
146 eventHandler.handleEvent(event);
147 }
148 }
149
150 @Override
151 public ITmfStateSystem getStateSystem() {
152 return ss;
153 }
154
155 // ------------------------------------------------------------------------
156 // Operations
157 // ------------------------------------------------------------------------
158
159 @Override
160 public Iterable<TmfXmlLocation> getLocations() {
161 return fLocations;
162 }
163
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
175 @Override
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;
183 }
184
185 }
This page took 0.035307 seconds and 5 git commands to generate.