releng: Transition to jdt.annotation 2.0
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / tmf / analysis / xml / core / model / TmfXmlEventHandler.java
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
13 package org.eclipse.tracecompass.tmf.analysis.xml.core.model;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
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.module.XmlUtils;
25 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
26 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
27 import org.w3c.dom.Element;
28
29 /**
30 * This Class implements an EventHandler in the XML-defined state system
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;
51 private final IXmlStateSystemContainer fParent;
52
53 /**
54 * Constructor
55 *
56 * @param modelFactory
57 * The factory used to create XML model elements
58 * @param node
59 * XML event handler element
60 * @param parent
61 * The state system container this event handler belongs to
62 */
63 public TmfXmlEventHandler(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer parent) {
64 fParent = parent;
65 String name = node.getAttribute(TmfXmlStrings.HANDLER_EVENT_NAME);
66 if (name == null) {
67 throw new IllegalArgumentException();
68 }
69 fName = name;
70
71 List<@NonNull Element> childElements = XmlUtils.getChildElements(node, TmfXmlStrings.STATE_CHANGE);
72 /* load state changes */
73 for (Element childElem : childElements) {
74 TmfXmlStateChange stateChange = modelFactory.createStateChange(childElem, fParent);
75 fStateChangeList.add(stateChange);
76 }
77 }
78
79 private boolean appliesToEvent(ITmfEvent event) {
80 String eventName = event.getName();
81
82 /* test for full name */
83 if (eventName.equals(fName)) {
84 return true;
85 }
86
87 /* test for the wildcard at the end */
88 if ((fName.endsWith(TmfXmlStrings.WILDCARD) && eventName.startsWith(fName.replace(TmfXmlStrings.WILDCARD, TmfXmlStrings.NULL)))) {
89 return true;
90 }
91 return false;
92 }
93
94 /**
95 * If the event handler can handle the event, it applies all state changes
96 * to modify the state system accordingly
97 *
98 * @param event
99 * The trace event to handle
100 */
101 public void handleEvent(ITmfEvent event) {
102 if (!appliesToEvent(event)) {
103 return;
104 }
105
106 /* Process all state changes */
107 for (TmfXmlStateChange stateChange : fStateChangeList) {
108 try {
109 stateChange.handleEvent(event);
110 } catch (AttributeNotFoundException ae) {
111 /*
112 * This would indicate a problem with the logic of the manager
113 * here, so it shouldn't happen.
114 */
115 Activator.logError("Attribute not found", ae); //$NON-NLS-1$
116 } catch (TimeRangeException tre) {
117 /*
118 * This would happen if the events in the trace aren't ordered
119 * chronologically, which should never be the case ...
120 */
121 Activator.logError("TimeRangeException caught in the state system's event manager. Are the events in the trace correctly ordered?", tre); //$NON-NLS-1$
122 } catch (StateValueTypeException sve) {
123 /*
124 * This would happen if we were trying to push/pop attributes
125 * not of type integer. Which, once again, should never happen.
126 */
127 Activator.logError("State value type error", sve); //$NON-NLS-1$
128 }
129
130 }
131
132 }
133
134 @Override
135 public String toString() {
136 return "TmfXmlEventHandler: " + fName; //$NON-NLS-1$
137 }
138
139 }
This page took 0.037742 seconds and 6 git commands to generate.