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
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.Activator
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
23 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
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
;
27 import org
.w3c
.dom
.NodeList
;
30 * This Class implements an EventHandler in the XML-defined state system
34 * <eventHandler eventName="eventName">
44 * @author Florian Wininger
46 public class TmfXmlEventHandler
{
48 /* list of states changes */
49 private final List
<TmfXmlStateChange
> fStateChangeList
= new ArrayList
<>();
50 private final String fName
;
51 private final IXmlStateSystemContainer fParent
;
57 * The factory used to create XML model elements
59 * XML event handler element
61 * The state system container this event handler belongs to
63 public TmfXmlEventHandler(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer parent
) {
65 fName
= node
.getAttribute(TmfXmlStrings
.HANDLER_EVENT_NAME
);
67 NodeList nodesChanges
= node
.getElementsByTagName(TmfXmlStrings
.STATE_CHANGE
);
68 /* load state changes */
69 for (int i
= 0; i
< nodesChanges
.getLength(); i
++) {
70 TmfXmlStateChange stateChange
= modelFactory
.createStateChange((Element
) nodesChanges
.item(i
), fParent
);
71 fStateChangeList
.add(stateChange
);
75 private boolean appliesToEvent(ITmfEvent event
) {
76 String eventName
= event
.getType().getName();
78 /* test for full name */
79 if (eventName
.equals(fName
)) {
83 /* test for the wildcard at the end */
84 if ((fName
.endsWith(TmfXmlStrings
.WILDCARD
) && eventName
.startsWith(fName
.replace(TmfXmlStrings
.WILDCARD
, TmfXmlStrings
.NULL
)))) {
91 * If the event handler can handle the event, it applies all state changes
92 * to modify the state system accordingly
95 * The trace event to handle
97 public void handleEvent(@NonNull ITmfEvent event
) {
98 if (!appliesToEvent(event
)) {
102 /* Process all state changes */
103 for (TmfXmlStateChange stateChange
: fStateChangeList
) {
105 stateChange
.handleEvent(event
);
106 } catch (AttributeNotFoundException ae
) {
108 * This would indicate a problem with the logic of the manager
109 * here, so it shouldn't happen.
111 Activator
.logError("Attribute not found", ae
); //$NON-NLS-1$
112 } catch (TimeRangeException tre
) {
114 * This would happen if the events in the trace aren't ordered
115 * chronologically, which should never be the case ...
117 Activator
.logError("TimeRangeException caught in the state system's event manager. Are the events in the trace correctly ordered?", tre
); //$NON-NLS-1$
118 } catch (StateValueTypeException sve
) {
120 * This would happen if we were trying to push/pop attributes
121 * not of type integer. Which, once again, should never happen.
123 Activator
.logError("State value type error", sve
); //$NON-NLS-1$
131 public String
toString() {
132 return "TmfXmlEventHandler: " + fName
; //$NON-NLS-1$