f02a48c86e20a4ccf226b37dac99d59c5a6fd7a1
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.core / src / org / eclipse / linuxtools / 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.linuxtools.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.linuxtools.internal.tmf.analysis.xml.core.Activator;
20 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
21 import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
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;
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 fName = node.getAttribute(TmfXmlStrings.HANDLER_EVENT_NAME);
66
67 NodeList nodesChanges = node.getElementsByTagName(TmfXmlStrings.STATE_CHANGE);
68 /* load state changes */
69 for (int i = 0; i < nodesChanges.getLength(); i++) {
70 TmfXmlStateChange stateChange = modelFactory.createStateChange((Element) nodesChanges.item(i), fParent);
71 fStateChangeList.add(stateChange);
72 }
73 }
74
75 private boolean appliesToEvent(ITmfEvent event) {
76 String eventName = event.getType().getName();
77
78 /* test for full name */
79 if (eventName.equals(fName)) {
80 return true;
81 }
82
83 /* test for the wildcard at the end */
84 if ((fName.endsWith(TmfXmlStrings.WILDCARD) && eventName.startsWith(fName.replace(TmfXmlStrings.WILDCARD, TmfXmlStrings.NULL)))) {
85 return true;
86 }
87 return false;
88 }
89
90 /**
91 * If the event handler can handle the event, it applies all state changes
92 * to modify the state system accordingly
93 *
94 * @param event
95 * The trace event to handle
96 */
97 public void handleEvent(@NonNull ITmfEvent event) {
98 if (!appliesToEvent(event)) {
99 return;
100 }
101
102 /* Process all state changes */
103 for (TmfXmlStateChange stateChange : fStateChangeList) {
104 try {
105 stateChange.handleEvent(event);
106 } catch (AttributeNotFoundException ae) {
107 /*
108 * This would indicate a problem with the logic of the manager
109 * here, so it shouldn't happen.
110 */
111 Activator.logError("Attribute not found", ae); //$NON-NLS-1$
112 } catch (TimeRangeException tre) {
113 /*
114 * This would happen if the events in the trace aren't ordered
115 * chronologically, which should never be the case ...
116 */
117 Activator.logError("TimeRangeException caught in the state system's event manager. Are the events in the trace correctly ordered?", tre); //$NON-NLS-1$
118 } catch (StateValueTypeException sve) {
119 /*
120 * This would happen if we were trying to push/pop attributes
121 * not of type integer. Which, once again, should never happen.
122 */
123 Activator.logError("State value type error", sve); //$NON-NLS-1$
124 }
125
126 }
127
128 }
129
130 }
This page took 0.035946 seconds and 4 git commands to generate.