1 | // Copyright 2015 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.io.File; |
---|
18 | import java.io.FileOutputStream; |
---|
19 | import java.io.IOException; |
---|
20 | import java.io.OutputStreamWriter; |
---|
21 | import java.io.PrintWriter; |
---|
22 | import java.util.HashMap; |
---|
23 | import java.util.HashSet; |
---|
24 | import java.util.LinkedList; |
---|
25 | import java.util.List; |
---|
26 | import java.util.ListIterator; |
---|
27 | import java.util.Map; |
---|
28 | import java.util.Set; |
---|
29 | |
---|
30 | import org.apache.commons.io.FileUtils; |
---|
31 | import org.xml.sax.Attributes; |
---|
32 | import org.xml.sax.SAXException; |
---|
33 | |
---|
34 | import de.ugoe.cs.util.StringTools; |
---|
35 | import de.ugoe.cs.util.console.Console; |
---|
36 | |
---|
37 | /** |
---|
38 | * <p> |
---|
39 | * TODO comment |
---|
40 | * </p> |
---|
41 | * |
---|
42 | * @author Patrick Harms |
---|
43 | */ |
---|
44 | public class GenericEventLogSplitter extends AbstractDefaultLogParser { |
---|
45 | |
---|
46 | /** */ |
---|
47 | private Attributes sessionAttributes = null; |
---|
48 | |
---|
49 | /** */ |
---|
50 | private Map<String, Map<String, String>> targets = new HashMap<>(); |
---|
51 | |
---|
52 | /** */ |
---|
53 | private List<Event> events = new LinkedList<>(); |
---|
54 | |
---|
55 | /** |
---|
56 | * |
---|
57 | */ |
---|
58 | public void splitLogFile(File file, File destinationFolder, long timediff) |
---|
59 | throws SAXException, IOException |
---|
60 | { |
---|
61 | super.parseFile(file); |
---|
62 | |
---|
63 | List<List<Event>> splittedEvents = new LinkedList<>(); |
---|
64 | LinkedList<Event> currentList = new LinkedList<>(); |
---|
65 | splittedEvents.add(currentList); |
---|
66 | |
---|
67 | for (Event event : events) { |
---|
68 | if ((currentList.size() > 0) && |
---|
69 | ((event.getTimestamp() - currentList.getLast().getTimestamp()) > timediff)) |
---|
70 | { |
---|
71 | currentList = new LinkedList<>(); |
---|
72 | splittedEvents.add(currentList); |
---|
73 | } |
---|
74 | |
---|
75 | currentList.add(event); |
---|
76 | } |
---|
77 | |
---|
78 | if (splittedEvents.size() == 1) { |
---|
79 | // simply copy the file |
---|
80 | FileUtils.copyFileToDirectory(file, destinationFolder); |
---|
81 | } |
---|
82 | else { |
---|
83 | Console.println("splitting " + file.getAbsolutePath()); |
---|
84 | |
---|
85 | int index = 0; |
---|
86 | for (List<Event> session : splittedEvents) { |
---|
87 | PrintWriter out = null; |
---|
88 | try { |
---|
89 | String fileName = file.getName().substring(0, file.getName().lastIndexOf('.')) + |
---|
90 | "_" + index++ + file.getName().substring(file.getName().lastIndexOf('.')); |
---|
91 | File destFile = new File(destinationFolder, fileName); |
---|
92 | Console.println("writing " + destFile.getAbsolutePath()); |
---|
93 | |
---|
94 | FileOutputStream fis = new FileOutputStream(destFile); |
---|
95 | out = new PrintWriter(new OutputStreamWriter(fis, "UTF-8")); |
---|
96 | out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
---|
97 | out.print("<session"); |
---|
98 | |
---|
99 | for (int i = 0; i < sessionAttributes.getLength(); i++) { |
---|
100 | out.print(' '); |
---|
101 | out.print(sessionAttributes.getLocalName(i)); |
---|
102 | out.print("=\""); |
---|
103 | out.print(StringTools.xmlEntityReplacement(sessionAttributes.getValue(i))); |
---|
104 | out.print("\""); |
---|
105 | } |
---|
106 | |
---|
107 | out.println(">"); |
---|
108 | |
---|
109 | Set<String> loggedTargets = new HashSet<>(); |
---|
110 | for (Event event : session) { |
---|
111 | String targetId = event.getParameter("targetId"); |
---|
112 | if (!loggedTargets.contains(targetId)) { |
---|
113 | logTarget(targetId, out); |
---|
114 | loggedTargets.add(targetId); |
---|
115 | } |
---|
116 | logEvent(event, out); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | out.println("</session>"); |
---|
121 | } |
---|
122 | finally { |
---|
123 | if (out != null) { |
---|
124 | out.close(); |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * <p> |
---|
133 | * TODO: comment |
---|
134 | * </p> |
---|
135 | * |
---|
136 | * @param event |
---|
137 | * @param out |
---|
138 | */ |
---|
139 | private void logEvent(Event event, PrintWriter out) { |
---|
140 | out.print("<event type=\""); |
---|
141 | out.print(event.getType()); |
---|
142 | out.println("\">"); |
---|
143 | |
---|
144 | for (Map.Entry<String, String> parameter : event.getParameters().entrySet()) { |
---|
145 | logParam(parameter.getKey(), parameter.getValue(), out); |
---|
146 | } |
---|
147 | |
---|
148 | out.println("</event>"); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * <p> |
---|
153 | * TODO: comment |
---|
154 | * </p> |
---|
155 | * |
---|
156 | * @param targetId |
---|
157 | * @param out |
---|
158 | */ |
---|
159 | private void logTarget(String targetId, PrintWriter out) { |
---|
160 | Map<String, String> parameters = targets.get(targetId); |
---|
161 | |
---|
162 | if (parameters.containsKey("parent")) { |
---|
163 | logTarget(parameters.get("parent"), out); |
---|
164 | } |
---|
165 | |
---|
166 | out.print("<target id=\""); |
---|
167 | out.print(targetId); |
---|
168 | out.println("\">"); |
---|
169 | |
---|
170 | for (Map.Entry<String, String> param : targets.get(targetId).entrySet()) { |
---|
171 | logParam(param.getKey(), param.getValue(), out); |
---|
172 | } |
---|
173 | |
---|
174 | out.println("</target>"); |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * <p> |
---|
179 | * TODO: comment |
---|
180 | * </p> |
---|
181 | * |
---|
182 | * @param key |
---|
183 | * @param value |
---|
184 | * @param out |
---|
185 | */ |
---|
186 | private void logParam(String key, String value, PrintWriter out) { |
---|
187 | out.print(" <param name=\""); |
---|
188 | out.print(key); |
---|
189 | out.print("\" value=\""); |
---|
190 | out.print(StringTools.xmlEntityReplacement(value)); |
---|
191 | out.println("\"/>"); |
---|
192 | } |
---|
193 | |
---|
194 | /* (non-Javadoc) |
---|
195 | * @see de.ugoe.cs.autoquest.plugin.genericevents.AbstractDefaultLogParser#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) |
---|
196 | */ |
---|
197 | @Override |
---|
198 | public void startElement(String uri, String localName, String qName, Attributes atts) |
---|
199 | throws SAXException |
---|
200 | { |
---|
201 | super.startElement(uri, localName, qName, atts); |
---|
202 | |
---|
203 | if (qName.equals("session")) { |
---|
204 | sessionAttributes = atts; |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | /* (non-Javadoc) |
---|
209 | * @see de.ugoe.cs.autoquest.plugin.genericevents.AbstractDefaultLogParser#handleTarget(java.lang.String, java.util.Map) |
---|
210 | */ |
---|
211 | @Override |
---|
212 | protected boolean handleTarget(String id, Map<String, String> parameters) throws SAXException { |
---|
213 | targets.put(id, parameters); |
---|
214 | return true; |
---|
215 | } |
---|
216 | |
---|
217 | /* (non-Javadoc) |
---|
218 | * @see de.ugoe.cs.autoquest.plugin.genericevents.AbstractDefaultLogParser#handleEvent(java.lang.String, java.util.Map) |
---|
219 | */ |
---|
220 | @Override |
---|
221 | protected boolean handleEvent(String type, Map<String, String> parameters) throws SAXException { |
---|
222 | Event event = new Event(type, parameters); |
---|
223 | |
---|
224 | ListIterator<Event> iterator = events.listIterator(); |
---|
225 | boolean added = false; |
---|
226 | |
---|
227 | while (iterator.hasNext()) { |
---|
228 | Event candidate = iterator.next(); |
---|
229 | if (candidate.getTimestamp() > event.getTimestamp()) { |
---|
230 | iterator.previous(); |
---|
231 | iterator.add(event); |
---|
232 | added = true; |
---|
233 | break; |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | if (!added) { |
---|
238 | events.add(event); |
---|
239 | } |
---|
240 | |
---|
241 | return true; |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | /** |
---|
246 | * <p> |
---|
247 | * TODO comment |
---|
248 | * </p> |
---|
249 | * |
---|
250 | * @author Patrick Harms |
---|
251 | */ |
---|
252 | public class Event { |
---|
253 | |
---|
254 | private String type; |
---|
255 | private Map<String, String> parameters; |
---|
256 | |
---|
257 | /** |
---|
258 | * <p> |
---|
259 | * TODO: comment |
---|
260 | * </p> |
---|
261 | * |
---|
262 | */ |
---|
263 | public Event(String type, Map<String, String> parameters) { |
---|
264 | this.type = type; |
---|
265 | this.parameters = parameters; |
---|
266 | } |
---|
267 | |
---|
268 | /** |
---|
269 | * <p> |
---|
270 | * TODO: comment |
---|
271 | * </p> |
---|
272 | * |
---|
273 | */ |
---|
274 | public long getTimestamp() { |
---|
275 | return Long.parseLong(parameters.get("timestamp")); |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * <p> |
---|
280 | * TODO: comment |
---|
281 | * </p> |
---|
282 | * |
---|
283 | */ |
---|
284 | public Map<String, String> getParameters() { |
---|
285 | return parameters; |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * <p> |
---|
290 | * TODO: comment |
---|
291 | * </p> |
---|
292 | * |
---|
293 | */ |
---|
294 | public String getType() { |
---|
295 | return type; |
---|
296 | } |
---|
297 | |
---|
298 | /** |
---|
299 | * <p> |
---|
300 | * TODO: comment |
---|
301 | * </p> |
---|
302 | * |
---|
303 | */ |
---|
304 | public String getParameter(String key) { |
---|
305 | return parameters.get(key); |
---|
306 | } |
---|
307 | |
---|
308 | } |
---|
309 | |
---|
310 | } |
---|