1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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
10 * Florian Wininger - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
;
15 import java
.util
.Collections
;
16 import java
.util
.HashMap
;
17 import java
.util
.HashSet
;
18 import java
.util
.List
;
22 import org
.eclipse
.core
.runtime
.IPath
;
23 import org
.eclipse
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
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
;
34 import org
.w3c
.dom
.Element
;
35 import org
.w3c
.dom
.NodeList
;
38 * This is the state change input plug-in for TMF's state system which handles
41 * @author Florian Wininger
43 public class XmlStateProvider
extends AbstractTmfStateProvider
implements IXmlStateSystemContainer
{
45 private final IPath fFilePath
;
46 @NonNull private final String fStateId
;
48 /** List of all Event Handlers */
49 private final Set
<TmfXmlEventHandler
> fEventHandlers
= new HashSet
<>();
51 /** List of all Locations */
52 private final Set
<TmfXmlLocation
> fLocations
;
54 /** Map for defined values */
55 private final Map
<String
, String
> fDefinedValues
= new HashMap
<>();
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 * Instantiate a new state provider plug-in.
67 * The state system id, corresponding to the analysis_id
68 * attribute of the state provider element of the XML file
70 * Path to the XML file containing the state provider definition
72 public XmlStateProvider(@NonNull ITmfTrace trace
, @NonNull String stateid
, IPath file
) {
73 super(trace
, ITmfEvent
.class, stateid
);
76 Element doc
= XmlUtils
.getElementInFile(fFilePath
.makeAbsolute().toOSString(), TmfXmlStrings
.STATE_PROVIDER
, fStateId
);
78 fLocations
= new HashSet
<>();
82 ITmfXmlModelFactory modelFactory
= TmfXmlReadWriteModelFactory
.getInstance();
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
));
90 /* parser for the locations */
91 List
<Element
> childElements
= XmlUtils
.getChildElements(doc
, TmfXmlStrings
.LOCATION
);
92 Set
<TmfXmlLocation
> locations
= new HashSet
<>();
93 for (Element element
: childElements
) {
94 if (element
== null) {
97 TmfXmlLocation location
= modelFactory
.createLocation(element
, this);
98 locations
.add(location
);
100 fLocations
= Collections
.unmodifiableSet(locations
);
102 /* parser for the event handlers */
103 childElements
= XmlUtils
.getChildElements(doc
, TmfXmlStrings
.EVENT_HANDLER
);
104 for (Element element
: childElements
) {
105 if (element
== null) {
108 TmfXmlEventHandler handler
= modelFactory
.createEventHandler(element
, this);
109 fEventHandlers
.add(handler
);
114 * Get the state id of the state provider
116 * @return The state id of the state provider
119 public String
getStateId() {
123 // ------------------------------------------------------------------------
125 // ------------------------------------------------------------------------
128 public int getVersion() {
129 Element ssNode
= XmlUtils
.getElementInFile(fFilePath
.makeAbsolute().toOSString(), TmfXmlStrings
.STATE_PROVIDER
, fStateId
);
130 if (ssNode
!= null) {
131 return Integer
.parseInt(ssNode
.getAttribute(TmfXmlStrings
.VERSION
));
134 * The version attribute is mandatory and XML files that don't validate
135 * with the XSD are ignored, so this should never happen
137 throw new IllegalStateException("The state provider XML node should have a version attribute"); //$NON-NLS-1$
141 public XmlStateProvider
getNewInstance() {
142 return new XmlStateProvider(this.getTrace(), getStateId(), fFilePath
);
146 protected void eventHandle(ITmfEvent event
) {
147 for (TmfXmlEventHandler eventHandler
: fEventHandlers
) {
148 eventHandler
.handleEvent(event
);
153 public ITmfStateSystem
getStateSystem() {
154 return getStateSystemBuilder();
157 // ------------------------------------------------------------------------
159 // ------------------------------------------------------------------------
162 public Iterable
<TmfXmlLocation
> getLocations() {
167 * Get the defined value associated with a constant
170 * The constant defining this value
171 * @return The actual value corresponding to this constant
173 public String
getDefinedValue(String constant
) {
174 return fDefinedValues
.get(constant
);
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));