1 /*******************************************************************************
2 * Copyright (c) 2014 Ecole Polytechnique de Montreal
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
.model
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
18 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.Activator
;
19 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
22 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
23 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
24 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
26 import org
.w3c
.dom
.Element
;
29 * This Class implements an EventHandler in the XML-defined state system
33 * <eventHandler eventName="eventName">
43 * @author Florian Wininger
45 public class TmfXmlEventHandler
{
47 /* list of states changes */
48 private final List
<TmfXmlStateChange
> fStateChangeList
= new ArrayList
<>();
49 private final String fName
;
50 private final IXmlStateSystemContainer fParent
;
56 * The factory used to create XML model elements
58 * XML event handler element
60 * The state system container this event handler belongs to
62 public TmfXmlEventHandler(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer parent
) {
64 String name
= node
.getAttribute(TmfXmlStrings
.HANDLER_EVENT_NAME
);
66 throw new IllegalArgumentException();
70 List
<Element
> childElements
= XmlUtils
.getChildElements(node
, TmfXmlStrings
.STATE_CHANGE
);
71 /* load state changes */
72 for (Element childElem
: childElements
) {
73 if (childElem
== null) {
76 TmfXmlStateChange stateChange
= modelFactory
.createStateChange(childElem
, fParent
);
77 fStateChangeList
.add(stateChange
);
81 private boolean appliesToEvent(ITmfEvent event
) {
82 String eventName
= event
.getName();
84 /* test for full name */
85 if (eventName
.equals(fName
)) {
89 /* test for the wildcard at the end */
90 if ((fName
.endsWith(TmfXmlStrings
.WILDCARD
) && eventName
.startsWith(fName
.replace(TmfXmlStrings
.WILDCARD
, TmfXmlStrings
.NULL
)))) {
97 * If the event handler can handle the event, it applies all state changes
98 * to modify the state system accordingly
101 * The trace event to handle
103 public void handleEvent(ITmfEvent event
) {
104 if (!appliesToEvent(event
)) {
108 /* Process all state changes */
109 for (TmfXmlStateChange stateChange
: fStateChangeList
) {
111 stateChange
.handleEvent(event
);
112 } catch (AttributeNotFoundException ae
) {
114 * This would indicate a problem with the logic of the manager
115 * here, so it shouldn't happen.
117 Activator
.logError("Attribute not found", ae
); //$NON-NLS-1$
118 } catch (TimeRangeException tre
) {
120 * This would happen if the events in the trace aren't ordered
121 * chronologically, which should never be the case ...
123 Activator
.logError("TimeRangeException caught in the state system's event manager. Are the events in the trace correctly ordered?", tre
); //$NON-NLS-1$
124 } catch (StateValueTypeException sve
) {
126 * This would happen if we were trying to push/pop attributes
127 * not of type integer. Which, once again, should never happen.
129 Activator
.logError("State value type error", sve
); //$NON-NLS-1$
137 public String
toString() {
138 return "TmfXmlEventHandler: " + fName
; //$NON-NLS-1$