Commit | Line | Data |
---|---|---|
0f7276b6 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ecole Polytechnique de Montreal | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Florian Wininger - Initial API and implementation | |
11 | ******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.analysis.xml.core.model; |
0f7276b6 GB |
14 | |
15 | import java.util.ArrayList; | |
16 | import java.util.List; | |
17 | ||
2bdf0193 | 18 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator; |
e894a508 AM |
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; | |
2bdf0193 | 22 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer; |
12685851 | 23 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; |
2bdf0193 AM |
24 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
25 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
0f7276b6 | 26 | import org.w3c.dom.Element; |
0f7276b6 GB |
27 | |
28 | /** | |
1d7e62f9 | 29 | * This Class implements an EventHandler in the XML-defined state system |
0f7276b6 GB |
30 | * |
31 | * <pre> | |
32 | * example: | |
33 | * <eventHandler eventName="eventName"> | |
34 | * <stateChange> | |
35 | * ... | |
36 | * </stateChange> | |
37 | * <stateChange> | |
38 | * ... | |
39 | * </stateChange> | |
40 | * </eventHandler> | |
41 | * </pre> | |
42 | * | |
43 | * @author Florian Wininger | |
44 | */ | |
45 | public class TmfXmlEventHandler { | |
46 | ||
47 | /* list of states changes */ | |
48 | private final List<TmfXmlStateChange> fStateChangeList = new ArrayList<>(); | |
49 | private final String fName; | |
1d7e62f9 | 50 | private final IXmlStateSystemContainer fParent; |
0f7276b6 GB |
51 | |
52 | /** | |
53 | * Constructor | |
54 | * | |
1d7e62f9 GB |
55 | * @param modelFactory |
56 | * The factory used to create XML model elements | |
0f7276b6 GB |
57 | * @param node |
58 | * XML event handler element | |
59 | * @param parent | |
1d7e62f9 | 60 | * The state system container this event handler belongs to |
0f7276b6 | 61 | */ |
1d7e62f9 | 62 | public TmfXmlEventHandler(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer parent) { |
0f7276b6 | 63 | fParent = parent; |
12685851 GB |
64 | String name = node.getAttribute(TmfXmlStrings.HANDLER_EVENT_NAME); |
65 | if (name == null) { | |
66 | throw new IllegalArgumentException(); | |
67 | } | |
68 | fName = name; | |
0f7276b6 | 69 | |
12685851 | 70 | List<Element> childElements = XmlUtils.getChildElements(node, TmfXmlStrings.STATE_CHANGE); |
0f7276b6 | 71 | /* load state changes */ |
12685851 GB |
72 | for (Element childElem : childElements) { |
73 | if (childElem == null) { | |
74 | continue; | |
75 | } | |
76 | TmfXmlStateChange stateChange = modelFactory.createStateChange(childElem, fParent); | |
0f7276b6 GB |
77 | fStateChangeList.add(stateChange); |
78 | } | |
79 | } | |
80 | ||
81 | private boolean appliesToEvent(ITmfEvent event) { | |
82 | String eventName = event.getType().getName(); | |
83 | ||
84 | /* test for full name */ | |
85 | if (eventName.equals(fName)) { | |
86 | return true; | |
87 | } | |
88 | ||
89 | /* test for the wildcard at the end */ | |
90 | if ((fName.endsWith(TmfXmlStrings.WILDCARD) && eventName.startsWith(fName.replace(TmfXmlStrings.WILDCARD, TmfXmlStrings.NULL)))) { | |
91 | return true; | |
92 | } | |
93 | return false; | |
94 | } | |
95 | ||
96 | /** | |
97 | * If the event handler can handle the event, it applies all state changes | |
98 | * to modify the state system accordingly | |
99 | * | |
100 | * @param event | |
101 | * The trace event to handle | |
102 | */ | |
12685851 | 103 | public void handleEvent(ITmfEvent event) { |
0f7276b6 GB |
104 | if (!appliesToEvent(event)) { |
105 | return; | |
106 | } | |
107 | ||
108 | /* Process all state changes */ | |
109 | for (TmfXmlStateChange stateChange : fStateChangeList) { | |
110 | try { | |
111 | stateChange.handleEvent(event); | |
112 | } catch (AttributeNotFoundException ae) { | |
113 | /* | |
114 | * This would indicate a problem with the logic of the manager | |
115 | * here, so it shouldn't happen. | |
116 | */ | |
117 | Activator.logError("Attribute not found", ae); //$NON-NLS-1$ | |
118 | } catch (TimeRangeException tre) { | |
119 | /* | |
120 | * This would happen if the events in the trace aren't ordered | |
121 | * chronologically, which should never be the case ... | |
122 | */ | |
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) { | |
125 | /* | |
126 | * This would happen if we were trying to push/pop attributes | |
127 | * not of type integer. Which, once again, should never happen. | |
128 | */ | |
129 | Activator.logError("State value type error", sve); //$NON-NLS-1$ | |
130 | } | |
131 | ||
132 | } | |
133 | ||
134 | } | |
135 | ||
446598f9 GB |
136 | @Override |
137 | public String toString() { | |
138 | return "TmfXmlEventHandler: " + fName; //$NON-NLS-1$ | |
139 | } | |
140 | ||
0f7276b6 | 141 | } |