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