Changeset 706 for trunk/quest-ui-core/src
- Timestamp:
- 08/30/12 13:29:50 (12 years ago)
- Location:
- trunk/quest-ui-core/src/main/java/de/ugoe/cs/quest
- Files:
-
- 2 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/quest-ui-core/src/main/java/de/ugoe/cs/quest/ui/commands/CMDgenerateReplayfile.java
r667 r706 1 1 package de.ugoe.cs.quest.ui.commands; 2 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.OutputStreamWriter; 3 7 import java.security.InvalidParameterException; 4 8 import java.util.Collection; 5 9 import java.util.List; 10 import java.util.logging.Level; 6 11 7 12 import de.ugoe.cs.quest.CommandHelpers; 8 import de.ugoe.cs.quest. ReplayGenerator;13 import de.ugoe.cs.quest.IReplayDecorator; 9 14 import de.ugoe.cs.quest.SequenceInstanceOf; 10 15 import de.ugoe.cs.quest.eventcore.Event; 16 import de.ugoe.cs.quest.eventcore.IReplayable; 17 import de.ugoe.cs.util.StringTools; 11 18 import de.ugoe.cs.util.console.Command; 19 import de.ugoe.cs.util.console.Console; 12 20 import de.ugoe.cs.util.console.GlobalDataContainer; 13 21 … … 16 24 * Command to create a replay file from stored sessions. 17 25 * </p> 26 * 27 * TODO: Add appropriate checks if Events are replayable 18 28 * 19 29 * @author Steffen Herbold … … 63 73 64 74 sequences = (Collection<List<Event>>) dataObject; 65 ReplayGenerator generator = new ReplayGenerator(); 66 generator.createLogfileMultipleSessions(sequences, filename); 75 createLogfileMultipleSessions(sequences, filename); 67 76 } 77 78 /** 79 * <p> 80 * {@link IReplayDecorator} to be used. If this field is {@code null}, no decorator is used. 81 * Default: {@code null} 82 * </p> 83 */ 84 private IReplayDecorator decorator = null; 68 85 86 /** 87 * <p> 88 * Id of the current session. The starting id is 1. 89 * </p> 90 */ 91 int sessionId = 1; 92 93 /** 94 * <p> 95 * Creates a replay file that contains multiple event sequences. 96 * </p> 97 * 98 * @param sequences 99 * collection of event sequences from which the sessions are generated 100 * @param filename 101 * name and path of the replay file 102 */ 103 private void createLogfileMultipleSessions(Collection<List<Event>> sequences, String filename) { 104 OutputStreamWriter writer = openReplayFile(filename); 105 if (writer != null) { 106 try { 107 try { 108 decorator = 109 sequences.iterator().next().get(0).getReplayables().get(0).getDecorator(); 110 } 111 catch (Exception e) { 112 // in the above line, many things can go wrong: emtpy sequences, null 113 // references, etc. However, all failures just indicate that no replay decorator 114 // should be used, hence, we ignore the exception 115 } 116 if (decorator != null) { 117 writer.write(decorator.getHeader()); 118 } 119 for (List<Event> actions : sequences) { 120 writeSession(actions, writer); 121 } 122 if (decorator != null) { 123 writer.write(decorator.getFooter()); 124 } 125 decorator = null; 126 writer.close(); 127 } 128 catch (IOException e) { 129 Console.printerrln("Unable to write replay file " + filename); 130 } 131 } 132 } 133 134 /** 135 * <p> 136 * Helper function that opens the replay file for writing. 137 * </p> 138 * 139 * @param filename 140 * name and path of the replay file 141 * @return {@link OutputStreamWriter} that writes to the replay file 142 */ 143 private OutputStreamWriter openReplayFile(String filename) { 144 File file = new File(filename); 145 boolean fileCreated; 146 try { 147 fileCreated = file.createNewFile(); 148 if (!fileCreated) { 149 Console.traceln(Level.INFO, "Created logfile " + filename); 150 } 151 else { 152 Console.traceln(Level.INFO, "Overwrote existing logfile " + filename); 153 } 154 } 155 catch (IOException e) { 156 Console.printerrln("Unable to create file " + filename); 157 Console.logException(e); 158 } 159 OutputStreamWriter writer = null; 160 try { 161 writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-16"); 162 } 163 catch (IOException e) { 164 Console.printerrln("Unable to open file for writing (read-only file):" + filename); 165 Console.logException(e); 166 } 167 return writer; 168 } 169 170 /** 171 * <p> 172 * Helper function that adds an event sequence to the replay. 173 * </p> 174 * 175 * @param actions 176 * event sequences to be added 177 * @param writer 178 * {@link OutputStreamWriter} to which the replay is added 179 * @throws IOException 180 * thrown if there is a problem writing to writer 181 */ 182 private void writeSession(List<Event> actions, OutputStreamWriter writer) throws IOException { 183 if (decorator != null) { 184 writer.write(decorator.getSessionHeader(sessionId)); 185 } 186 for (Event currentAction : actions) { 187 188 List<? extends IReplayable> replayables = currentAction.getReplayables(); 189 for (IReplayable replayble : replayables) { 190 writer.write(replayble.getReplay() + StringTools.ENDLINE); 191 writer.flush(); 192 } 193 } 194 if (decorator != null) { 195 writer.write(decorator.getSessionFooter(sessionId)); 196 } 197 sessionId++; 198 } 69 199 }
Note: See TracChangeset
for help on using the changeset viewer.