1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.plugin.genericevents; |
---|
16 | |
---|
17 | import java.time.Duration; |
---|
18 | import java.util.GregorianCalendar; |
---|
19 | import java.util.Map; |
---|
20 | import java.util.Set; |
---|
21 | |
---|
22 | import org.xml.sax.SAXException; |
---|
23 | |
---|
24 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
25 | import de.ugoe.cs.autoquest.eventcore.EventTargetModelException; |
---|
26 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
27 | import de.ugoe.cs.autoquest.eventcore.StringEventType; |
---|
28 | import de.ugoe.cs.autoquest.plugin.genericevents.eventCore.GenericEventTarget; |
---|
29 | import de.ugoe.cs.autoquest.plugin.genericevents.eventCore.GenericEventTargetSpec; |
---|
30 | |
---|
31 | /** |
---|
32 | * <p> |
---|
33 | * This class provides the functionality to parse XML log files generated by the generic event |
---|
34 | * monitor of AutoQUEST. The result of parsing a file is a collection of event sequences and a |
---|
35 | * target model. |
---|
36 | * </p> |
---|
37 | * |
---|
38 | * @author Patrick Harms |
---|
39 | * @version 1.0 |
---|
40 | * |
---|
41 | */ |
---|
42 | public class GenericEventLogParser extends AbstractDefaultLogParser { |
---|
43 | |
---|
44 | /** types of events to be ignored */ |
---|
45 | private Set<String> ignoredEvents; |
---|
46 | |
---|
47 | |
---|
48 | /** |
---|
49 | * <p> |
---|
50 | * used to provide the parser with a set of ignored event types. |
---|
51 | * </p> |
---|
52 | * |
---|
53 | * @param ignoredEvents |
---|
54 | */ |
---|
55 | public GenericEventLogParser(Set<String> ignoredEvents) { |
---|
56 | super(); |
---|
57 | this.ignoredEvents = ignoredEvents; |
---|
58 | } |
---|
59 | |
---|
60 | /* (non-Javadoc) |
---|
61 | * @see de.ugoe.cs.autoquest.plugin.html.AbstractDefaultLogParser#handleGUIElement(String, Map) |
---|
62 | */ |
---|
63 | @Override |
---|
64 | protected boolean handleTarget(String id, Map<String, String> parameters) |
---|
65 | throws SAXException |
---|
66 | { |
---|
67 | String parentId = parameters.get("parent"); |
---|
68 | |
---|
69 | if (parentId != null) { |
---|
70 | // check, if the parent is already in the tree |
---|
71 | if (super.getTargetTree().find(parentId) == null) { |
---|
72 | // element cannot yet be processed as parent is not existing yet |
---|
73 | return false; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | GenericEventTargetSpec specification = new GenericEventTargetSpec(id, parameters); |
---|
78 | |
---|
79 | try { |
---|
80 | super.getTargetTree().add(id, parentId, specification); |
---|
81 | } |
---|
82 | catch (EventTargetModelException e) { |
---|
83 | throw new SAXException("could not handle generic event target with id " + |
---|
84 | id + ": " + e.getMessage(), e); |
---|
85 | } |
---|
86 | |
---|
87 | return true; |
---|
88 | } |
---|
89 | |
---|
90 | /* (non-Javadoc) |
---|
91 | * @see de.ugoe.cs.autoquest.plugin.html.AbstractDefaultLogParser#handleEvent(String, Map) |
---|
92 | */ |
---|
93 | @Override |
---|
94 | protected boolean handleEvent(String type, Map<String, String> parameters) throws SAXException { |
---|
95 | // ignore the event based on its type if requested |
---|
96 | if (ignoredEvents.contains(type)) { |
---|
97 | return true; |
---|
98 | } |
---|
99 | |
---|
100 | String targetId = parameters.get("targetId"); |
---|
101 | |
---|
102 | // ignore the event based on type and target id if requested |
---|
103 | if (ignoredEvents.contains(type + "." + targetId)) { |
---|
104 | return true; |
---|
105 | } |
---|
106 | |
---|
107 | if (targetId == null) { |
---|
108 | throw new SAXException("event does not have a target id"); |
---|
109 | } |
---|
110 | |
---|
111 | GenericEventTarget target = super.getTargetTree().find(targetId); |
---|
112 | |
---|
113 | IEventType eventType = new StringEventType(type); |
---|
114 | |
---|
115 | if (eventType != null) { |
---|
116 | Event event = new Event(eventType, target); |
---|
117 | |
---|
118 | String timestampStr = parameters.get("timestamp"); |
---|
119 | |
---|
120 | if (timestampStr != null) { |
---|
121 | event.setTimestamp(determineTimestamp(timestampStr)); |
---|
122 | } |
---|
123 | |
---|
124 | for (Map.Entry<String, String> parameter : parameters.entrySet()) { |
---|
125 | if (!"targetId".equals(parameter.getKey()) && |
---|
126 | !"timestamp".equals(parameter.getKey())) |
---|
127 | { |
---|
128 | event.setParameter(parameter.getKey(), parameter.getValue()); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | super.addToSequence(event); |
---|
133 | } |
---|
134 | // else ignore unknown event type |
---|
135 | |
---|
136 | return true; |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * convenience method to align different timestamp formats |
---|
141 | */ |
---|
142 | private long determineTimestamp(String timestampStr) { |
---|
143 | long val = Long.parseLong(timestampStr); |
---|
144 | |
---|
145 | // We expect any recording to have taken place in years with at most 4 digits. Hence, |
---|
146 | // divide the val until we reach such a year |
---|
147 | long max = new GregorianCalendar(10000, 1, 1).getTimeInMillis(); |
---|
148 | |
---|
149 | while (max < val) { |
---|
150 | val /= 10; |
---|
151 | } |
---|
152 | |
---|
153 | // now, it can still be the case, that the base line is wrong. I.e., the remaining value |
---|
154 | // may be milliseconds, but not starting from 1970, as we expect it, but starting from |
---|
155 | // year 1 of the gregorian calendar. If this is the case, the date is still in the future. |
---|
156 | // Hence, we substract the milliseconds between year 1 of the gregorian calendar and 1970. |
---|
157 | // (We took these magic number, as they provide correct results. It's not fully clear, |
---|
158 | // why the January 1st 1970 does not work correctly but provides an offset of two days) |
---|
159 | if (val > System.currentTimeMillis()) { |
---|
160 | Duration duration = Duration.between(new GregorianCalendar(1, 1, 1).toInstant(), |
---|
161 | new GregorianCalendar(1969, 12, 30).toInstant()); |
---|
162 | |
---|
163 | val -= duration.toMillis(); |
---|
164 | } |
---|
165 | |
---|
166 | return val; |
---|
167 | } |
---|
168 | |
---|
169 | } |
---|