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
.linuxtools
.tmf
.analysis
.xml
.core
.stateprovider
;
15 import java
.util
.Collections
;
16 import java
.util
.HashMap
;
17 import java
.util
.HashSet
;
21 import org
.eclipse
.core
.runtime
.IPath
;
22 import org
.eclipse
.jdt
.annotation
.NonNull
;
23 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
24 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.model
.TmfXmlEventHandler
;
25 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.model
.TmfXmlLocation
;
26 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.model
.readwrite
.TmfXmlReadWriteModelFactory
;
27 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
28 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.AbstractTmfStateProvider
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
33 import org
.w3c
.dom
.Element
;
34 import org
.w3c
.dom
.NodeList
;
37 * This is the state change input plug-in for TMF's state system which handles
40 * @author Florian Wininger
42 public class XmlStateProvider
extends AbstractTmfStateProvider
implements IXmlStateSystemContainer
{
44 private final IPath fFilePath
;
45 @NonNull private final String fStateId
;
47 /** List of all Event Handlers */
48 private final Set
<TmfXmlEventHandler
> fEventHandlers
= new HashSet
<>();
50 /** List of all Locations */
51 private final Set
<TmfXmlLocation
> fLocations
;
53 /** Map for defined values */
54 private final Map
<String
, String
> fDefinedValues
= new HashMap
<>();
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 * Instantiate a new state provider plug-in.
66 * The state system id, corresponding to the analysis_id
67 * attribute of the state provider element of the XML file
69 * Path to the XML file containing the state provider definition
71 public XmlStateProvider(ITmfTrace trace
, @NonNull String stateid
, IPath file
) {
72 super(trace
, ITmfEvent
.class, stateid
);
75 Element doc
= XmlUtils
.getElementInFile(fFilePath
.makeAbsolute().toOSString(), TmfXmlStrings
.STATE_PROVIDER
, fStateId
);
77 fLocations
= new HashSet
<>();
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
));
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
);
97 fLocations
= Collections
.unmodifiableSet(locations
);
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
);
109 * Get the state id of the state provider
111 * @return The state id of the state provider
114 public String
getStateId() {
118 // ------------------------------------------------------------------------
120 // ------------------------------------------------------------------------
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
));
129 * The version attribute is mandatory and XML files that don't validate
130 * with the XSD are ignored, so this should never happen
132 throw new IllegalStateException("The state provider XML node should have a version attribute"); //$NON-NLS-1$
136 public XmlStateProvider
getNewInstance() {
137 return new XmlStateProvider(this.getTrace(), getStateId(), fFilePath
);
141 protected void eventHandle(ITmfEvent event
) {
145 for (TmfXmlEventHandler eventHandler
: fEventHandlers
) {
146 eventHandler
.handleEvent(event
);
151 public ITmfStateSystem
getStateSystem() {
155 // ------------------------------------------------------------------------
157 // ------------------------------------------------------------------------
160 public Iterable
<TmfXmlLocation
> getLocations() {
165 * Get the defined value associated with a constant
168 * The constant defining this value
169 * @return The actual value corresponding to this constant
171 public String
getDefinedValue(String constant
) {
172 return fDefinedValues
.get(constant
);
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));