tmf: Move plugins to their own sub-directory
[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.tracecompass.internal.tmf.analysis.xml.core.Activator;
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;
22 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
23 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
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
28 /**
29 * This Class implements an EventHandler in the XML-defined state system
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;
50 private final IXmlStateSystemContainer fParent;
51
52 /**
53 * Constructor
54 *
55 * @param modelFactory
56 * The factory used to create XML model elements
57 * @param node
58 * XML event handler element
59 * @param parent
60 * The state system container this event handler belongs to
61 */
62 public TmfXmlEventHandler(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer parent) {
63 fParent = parent;
64 String name = node.getAttribute(TmfXmlStrings.HANDLER_EVENT_NAME);
65 if (name == null) {
66 throw new IllegalArgumentException();
67 }
68 fName = name;
69
70 List<Element> childElements = XmlUtils.getChildElements(node, TmfXmlStrings.STATE_CHANGE);
71 /* load state changes */
72 for (Element childElem : childElements) {
73 if (childElem == null) {
74 continue;
75 }
76 TmfXmlStateChange stateChange = modelFactory.createStateChange(childElem, fParent);
77 fStateChangeList.add(stateChange);
78 }
79 }
80
81 private boolean appliesToEvent(ITmfEvent event) {
82 String eventName = event.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 */
103 public void handleEvent(ITmfEvent event) {
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
136 @Override
137 public String toString() {
138 return "TmfXmlEventHandler: " + fName; //$NON-NLS-1$
139 }
140
141 }
This page took 0.050986 seconds and 6 git commands to generate.