1 | package de.ugoe.cs.eventbench;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileWriter;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.List;
|
---|
7 |
|
---|
8 | import de.ugoe.cs.eventbench.data.IReplayable;
|
---|
9 | import de.ugoe.cs.eventbench.data.ReplayableEvent;
|
---|
10 | import de.ugoe.cs.util.StringTools;
|
---|
11 | import de.ugoe.cs.util.console.Console;
|
---|
12 |
|
---|
13 | public class ReplayGenerator {
|
---|
14 |
|
---|
15 | public void createLogfileMultipleSessions(List<List<ReplayableEvent<?>>> sequences, String filename) {
|
---|
16 | FileWriter writer = openReplayFile(filename);
|
---|
17 | if( writer!=null ) {
|
---|
18 | try {
|
---|
19 | writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + StringTools.ENDLINE);
|
---|
20 | writer.write("<log>" + StringTools.ENDLINE);
|
---|
21 |
|
---|
22 | for( List<ReplayableEvent<?>> actions : sequences ) {
|
---|
23 | writeSession(actions, writer);
|
---|
24 | }
|
---|
25 |
|
---|
26 | writer.write("</log>" + StringTools.ENDLINE);
|
---|
27 | writer.close();
|
---|
28 | } catch (IOException e) {
|
---|
29 | Console.printerrln("Unable to write replay file " + filename);
|
---|
30 | }
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public void createLogfileSingleSession(List<ReplayableEvent<?>> actions, String filename) {
|
---|
35 | FileWriter writer = openReplayFile(filename);
|
---|
36 | if( writer!=null ) {
|
---|
37 | try {
|
---|
38 | writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + StringTools.ENDLINE);
|
---|
39 | writer.write("<log>" + StringTools.ENDLINE);
|
---|
40 |
|
---|
41 | writeSession(actions, writer);
|
---|
42 |
|
---|
43 | writer.write("</log>" + StringTools.ENDLINE);
|
---|
44 | writer.close();
|
---|
45 | } catch (IOException e) {
|
---|
46 | Console.printerrln("Unable to write replay file " + filename);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | private FileWriter openReplayFile(String filename) {
|
---|
52 | File file = new File(filename);
|
---|
53 | boolean fileCreated;
|
---|
54 | try {
|
---|
55 | fileCreated = file.createNewFile();
|
---|
56 | if( !fileCreated ) {
|
---|
57 | Console.traceln("Created logfile " + filename);
|
---|
58 | } else {
|
---|
59 | Console.traceln("Overwrote existing logfile " + filename);
|
---|
60 | }
|
---|
61 | } catch (IOException e) {
|
---|
62 | Console.printerrln("Unable to create file " + filename);
|
---|
63 | Console.printStacktrace(e);
|
---|
64 | }
|
---|
65 | FileWriter writer = null;
|
---|
66 | try {
|
---|
67 | writer = new FileWriter(file);
|
---|
68 | } catch (IOException e) {
|
---|
69 | Console.printerrln("Unable to open file for writing (read-only file):" + filename);
|
---|
70 | Console.printStacktrace(e);
|
---|
71 | }
|
---|
72 | return writer;
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void writeSession(List<ReplayableEvent<?>> actions, FileWriter writer)
|
---|
76 | throws IOException {
|
---|
77 | writer.write(" <session>" + StringTools.ENDLINE);
|
---|
78 | for( ReplayableEvent<?> currentAction : actions ) {
|
---|
79 |
|
---|
80 | List<? extends IReplayable> replayables = currentAction.getReplayMessages();
|
---|
81 | for( IReplayable replayble : replayables ) {
|
---|
82 | writer.write(replayble.getReplayXml()+StringTools.ENDLINE);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | writer.write(" </session>" + StringTools.ENDLINE);
|
---|
86 | }
|
---|
87 |
|
---|
88 | }
|
---|