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