1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 É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
.ArrayList
;
16 import java
.util
.Collections
;
17 import java
.util
.HashMap
;
18 import java
.util
.HashSet
;
19 import java
.util
.List
;
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
;
39 * This is the state change input plug-in for TMF's state system which handles
42 * @author Florian Wininger
44 public class XmlStateProvider
extends AbstractTmfStateProvider
implements IXmlStateSystemContainer
{
46 private final IPath fFilePath
;
47 private final @NonNull String fStateId
;
49 /** List of all Event Handlers */
50 private final List
<TmfXmlEventHandler
> fEventHandlers
= new ArrayList
<>();
52 /** List of all Locations */
53 private final @NonNull Set
<@NonNull TmfXmlLocation
> fLocations
;
55 /** Map for defined values */
56 private final Map
<String
, String
> fDefinedValues
= new HashMap
<>();
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
63 * Instantiate a new state provider plug-in.
68 * The state system id, corresponding to the analysis_id
69 * attribute of the state provider element of the XML file
71 * Path to the XML file containing the state provider definition
73 public XmlStateProvider(@NonNull ITmfTrace trace
, @NonNull String stateid
, IPath file
) {
74 super(trace
, stateid
);
77 Element doc
= XmlUtils
.getElementInFile(fFilePath
.makeAbsolute().toOSString(), TmfXmlStrings
.STATE_PROVIDER
, fStateId
);
79 fLocations
= new HashSet
<>();
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
));
91 /* parser for the locations */
92 List
<Element
> childElements
= XmlUtils
.getChildElements(doc
, TmfXmlStrings
.LOCATION
);
93 Set
<@NonNull TmfXmlLocation
> locations
= new HashSet
<>();
94 for (Element element
: childElements
) {
95 if (element
== null) {
98 TmfXmlLocation location
= modelFactory
.createLocation(element
, this);
99 locations
.add(location
);
101 fLocations
= Collections
.unmodifiableSet(locations
);
103 /* parser for the event handlers */
104 childElements
= XmlUtils
.getChildElements(doc
, TmfXmlStrings
.EVENT_HANDLER
);
105 for (Element element
: childElements
) {
106 if (element
== null) {
109 TmfXmlEventHandler handler
= modelFactory
.createEventHandler(element
, this);
110 fEventHandlers
.add(handler
);
115 * Get the state id of the state provider
117 * @return The state id of the state provider
120 public String
getStateId() {
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
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
));
135 * The version attribute is mandatory and XML files that don't validate
136 * with the XSD are ignored, so this should never happen
138 throw new IllegalStateException("The state provider XML node should have a version attribute"); //$NON-NLS-1$
142 public XmlStateProvider
getNewInstance() {
143 return new XmlStateProvider(this.getTrace(), getStateId(), fFilePath
);
147 protected void eventHandle(ITmfEvent event
) {
148 for (TmfXmlEventHandler eventHandler
: fEventHandlers
) {
149 eventHandler
.handleEvent(event
);
154 public ITmfStateSystem
getStateSystem() {
155 return getStateSystemBuilder();
158 // ------------------------------------------------------------------------
160 // ------------------------------------------------------------------------
163 public Iterable
<TmfXmlLocation
> getLocations() {
168 * Get the defined value associated with a constant
171 * The constant defining this value
172 * @return The actual value corresponding to this constant
174 public String
getDefinedValue(String constant
) {
175 return fDefinedValues
.get(constant
);
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));