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