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
.linuxtools
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.model
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.linuxtools
.internal
.tmf
.analysis
.xml
.core
.Activator
;
20 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
21 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.stateprovider
.XmlStateProvider
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
26 import org
.w3c
.dom
.Element
;
27 import org
.w3c
.dom
.NodeList
;
30 * This Class implements an EventHandler in the XML state provider
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 XmlStateProvider fParent
;
57 * XML event handler element
59 * The state provider this event handler belongs to
61 public TmfXmlEventHandler(Element node
, XmlStateProvider parent
) {
63 fName
= node
.getAttribute(TmfXmlStrings
.HANDLER_EVENT_NAME
);
65 NodeList nodesChanges
= node
.getElementsByTagName(TmfXmlStrings
.STATE_CHANGE
);
66 /* load state changes */
67 for (int i
= 0; i
< nodesChanges
.getLength(); i
++) {
68 TmfXmlStateChange stateChange
= new TmfXmlStateChange((Element
) nodesChanges
.item(i
), fParent
);
69 fStateChangeList
.add(stateChange
);
73 private boolean appliesToEvent(ITmfEvent event
) {
74 String eventName
= event
.getType().getName();
76 /* test for full name */
77 if (eventName
.equals(fName
)) {
81 /* test for the wildcard at the end */
82 if ((fName
.endsWith(TmfXmlStrings
.WILDCARD
) && eventName
.startsWith(fName
.replace(TmfXmlStrings
.WILDCARD
, TmfXmlStrings
.NULL
)))) {
89 * If the event handler can handle the event, it applies all state changes
90 * to modify the state system accordingly
93 * The trace event to handle
95 public void handleEvent(@NonNull ITmfEvent event
) {
96 if (!appliesToEvent(event
)) {
100 /* Process all state changes */
101 for (TmfXmlStateChange stateChange
: fStateChangeList
) {
103 stateChange
.handleEvent(event
);
104 } catch (AttributeNotFoundException ae
) {
106 * This would indicate a problem with the logic of the manager
107 * here, so it shouldn't happen.
109 Activator
.logError("Attribute not found", ae
); //$NON-NLS-1$
110 } catch (TimeRangeException tre
) {
112 * This would happen if the events in the trace aren't ordered
113 * chronologically, which should never be the case ...
115 Activator
.logError("TimeRangeException caught in the state system's event manager. Are the events in the trace correctly ordered?", tre
); //$NON-NLS-1$
116 } catch (StateValueTypeException sve
) {
118 * This would happen if we were trying to push/pop attributes
119 * not of type integer. Which, once again, should never happen.
121 Activator
.logError("State value type error", sve
); //$NON-NLS-1$