Changeset 1663 for trunk/autoquest-ui-core/src/main/java/de/ugoe
- Timestamp:
- 08/12/14 15:44:16 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-ui-core/src/main/java/de/ugoe/cs/autoquest/commands/sequences/CMDgenerateReplayfile.java
r1270 r1663 15 15 package de.ugoe.cs.autoquest.commands.sequences; 16 16 17 import java.io.BufferedWriter; 17 18 import java.io.File; 18 19 import java.io.FileOutputStream; … … 20 21 import java.io.OutputStreamWriter; 21 22 import java.util.Collection; 23 import java.util.Iterator; 22 24 import java.util.List; 23 25 import java.util.logging.Level; … … 45 47 public class CMDgenerateReplayfile implements Command { 46 48 47 /* 48 * (non-Javadoc) 49 * 50 * @see de.ugoe.cs.util.console.Command#help() 51 */ 52 @Override 53 public String help() { 54 return "generateReplayfile <filename> <sequences>"; 55 } 56 57 /* 58 * (non-Javadoc) 59 * 60 * @see de.ugoe.cs.util.console.Command#run(java.util.List) 61 */ 62 @SuppressWarnings("unchecked") 63 @Override 64 public void run(List<Object> parameters) { 65 String filename; 66 String sequencesName; 67 try { 68 filename = (String) parameters.get(0); 69 sequencesName = (String) parameters.get(1); 70 } catch (Exception e) { 71 throw new IllegalArgumentException(); 72 } 73 74 Collection<List<Event>> sequences = null; 75 Object dataObject = GlobalDataContainer.getInstance().getData( 76 sequencesName); 77 if (dataObject == null) { 78 CommandHelpers.objectNotFoundMessage(sequencesName); 79 return; 80 } 81 if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) { 82 CommandHelpers.objectNotType(sequencesName, 83 "Collection<List<Event<?>>>"); 84 return; 85 } 86 87 sequences = (Collection<List<Event>>) dataObject; 88 createLogfileMultipleSessions(sequences, filename); 89 } 90 91 /** 92 * <p> 93 * {@link IReplayDecorator} to be used. If this field is {@code null}, no decorator is used. 94 * Default: {@code null} 95 * </p> 96 */ 97 private IReplayDecorator decorator = null; 98 99 /** 100 * <p> 101 * Id of the current session. The starting id is 1. 102 * </p> 103 */ 104 int sessionId = 1; 105 106 /** 107 * <p> 108 * Creates a replay file that contains multiple event sequences. 109 * </p> 110 * 111 * @param sequences 112 * collection of event sequences from which the sessions are generated 113 * @param filename 114 * name and path of the replay file 115 */ 116 private void createLogfileMultipleSessions(Collection<List<Event>> sequences, String filename) { 117 OutputStreamWriter writer = openReplayFile(filename); 118 if (writer != null) { 119 try { 120 try { 121 decorator = 122 sequences.iterator().next().get(0).getReplayables().get(0).getDecorator(); 123 } 124 catch (Exception e) { 125 // in the above line, many things can go wrong: emtpy sequences, null 126 // references, etc. However, all failures just indicate that no replay decorator 127 // should be used, hence, we ignore the exception 128 Console.traceln(Level.FINEST, "no decorator used for " + filename); 129 } 130 if (decorator != null) { 131 writer.write(decorator.getHeader()); 132 } 133 for (List<Event> actions : sequences) { 134 writeSession(actions, writer); 135 } 136 if (decorator != null) { 137 writer.write(decorator.getFooter()); 138 } 139 decorator = null; 140 writer.close(); 141 } 142 catch (IOException e) { 143 Console.printerrln("Unable to write replay file " + filename); 144 } 145 } 146 } 147 148 /** 149 * <p> 150 * Helper function that opens the replay file for writing. 151 * </p> 152 * 153 * @param filename 154 * name and path of the replay file 155 * @return {@link OutputStreamWriter} that writes to the replay file 156 */ 157 private OutputStreamWriter openReplayFile(String filename) { 158 File file = new File(filename); 159 boolean fileCreated; 160 try { 161 fileCreated = file.createNewFile(); 162 if (!fileCreated) { 163 Console.traceln(Level.INFO, "Created logfile " + filename); 164 } 165 else { 166 Console.traceln(Level.INFO, "Overwrote existing logfile " + filename); 167 } 168 } 169 catch (IOException e) { 170 Console.printerrln("Unable to create file " + filename); 171 Console.logException(e); 172 } 173 OutputStreamWriter writer = null; 174 try { 175 writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-16"); 176 } 177 catch (IOException e) { 178 Console.printerrln("Unable to open file for writing (read-only file):" + filename); 179 Console.logException(e); 180 } 181 return writer; 182 } 183 184 /** 185 * <p> 186 * Helper function that adds an event sequence to the replay. 187 * </p> 188 * 189 * @param actions 190 * event sequences to be added 191 * @param writer 192 * {@link OutputStreamWriter} to which the replay is added 193 * @throws IOException 194 * thrown if there is a problem writing to writer 195 */ 196 private void writeSession(List<Event> actions, OutputStreamWriter writer) throws IOException { 197 if (decorator != null) { 198 writer.write(decorator.getSessionHeader(sessionId)); 199 } 200 for (Event currentAction : actions) { 201 202 List<? extends IReplayable> replayables = currentAction.getReplayables(); 203 for (IReplayable replayble : replayables) { 204 writer.write(replayble.getReplay() + StringTools.ENDLINE); 205 writer.flush(); 206 } 207 } 208 if (decorator != null) { 209 writer.write(decorator.getSessionFooter(sessionId)); 210 } 211 sessionId++; 212 } 49 /* 50 * (non-Javadoc) 51 * 52 * @see de.ugoe.cs.util.console.Command#help() 53 */ 54 @Override 55 public String help() { 56 return "generateReplayfile <filename> <sequences>"; 57 } 58 59 /* 60 * (non-Javadoc) 61 * 62 * @see de.ugoe.cs.util.console.Command#run(java.util.List) 63 */ 64 @SuppressWarnings("unchecked") 65 @Override 66 public void run(List<Object> parameters) { 67 String filename; 68 String sequencesName; 69 try { 70 filename = (String) parameters.get(0); 71 sequencesName = (String) parameters.get(1); 72 } 73 catch (Exception e) { 74 throw new IllegalArgumentException(); 75 } 76 77 Collection<List<Event>> sequences = null; 78 Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName); 79 if (dataObject == null) { 80 CommandHelpers.objectNotFoundMessage(sequencesName); 81 return; 82 } 83 if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) { 84 CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>"); 85 return; 86 } 87 88 sequences = (Collection<List<Event>>) dataObject; 89 createLogfileMultipleSessions(sequences, filename); 90 91 writeJacaretoXML(sequences, filename); 92 } 93 94 private void writeLine(BufferedWriter writer, String line) throws IOException { 95 writer.write(line); 96 writer.newLine(); 97 } 98 99 private void writeJacaretoHead(BufferedWriter writer) throws IOException { 100 writeLine(writer, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"); 101 writeLine(writer, "<JacaretoStructure>"); 102 writeLine(writer, "<Record>"); 103 104 // TODO: This header content is basically copy+paste from a 105 // specific jacareto replay file right now. 106 // Some things such as screen resolution and especially the 107 // application starter details need to be changed for general cases. 108 writeLine(writer, 109 "<Calendar procTime=\"0\" duration=\"0\" year=\"2014\" month=\"8\" date=\"11\" hour=\"14\" min=\"43\" sec=\"41\" uuid=\"06831ba1-f28a-4e05-b46e-ce9d8f9ffa0f\" />"); 110 writeLine(writer, 111 "<SystemInfo procTime=\"0\" duration=\"0\" screenWidth=\"2646\" screenHeight=\"1024\" javaVersion=\"1.7.0_65\" lookAndFeel=\"javax.swing.plaf.metal.MetalLookAndFeel\" uuid=\"720f430f-52cf-4d8b-9fbe-58434f766efe\" />"); 112 writeLine(writer, 113 "<KeyboardState procTime=\"0\" duration=\"0\" isNumLockOn=\"false\" isScrollLockOn=\"false\" isCapsLockOn=\"false\" applyIsNumLockOn=\"true\" applyIsScrollLockOn=\"true\" applyIsCapsLockOn=\"true\" uuid=\"28146f79-9fc7-49f9-b4a8-5866a7625683\" />"); 114 writeLine(writer, "ComponentMode numberPopupMenues=\"true\" />"); 115 writeLine(writer, 116 "<ApplicationStarter procTime=\"5\" duration=\"5\" name=\"HelloWorldSwing\" class=\"HelloWorldSwing\" initclass=\"\" basepath=\"/home/daniel/project/autoquest-jfcmonitor\" classpathext=\"${basepath}/helloswing.jar;${basepath}/.;\" detectDuration=\"false\" captureparams=\"\" replayparams=\"\" uuid=\"a7b7d7b9-caa9-4d6d-b052-cf74d353275e\" />"); 117 } 118 119 private void writeJacaretoEvents(BufferedWriter writer, Collection<List<Event>> sequences) 120 throws IOException 121 { 122 for (List<Event> sequence : sequences) { 123 for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { 124 Event event = eventIter.next(); 125 126 // TODO 127 } 128 } 129 } 130 131 private void writeJacaretoTail(BufferedWriter writer) throws IOException { 132 writeLine(writer, "</Record>"); 133 134 // TODO: There is a really big <structure> part in jacareto's replay 135 // files but they make no sense to me right now - skip this until later 136 // or until jacareto complains. =) 137 writeLine(writer, "<Structure>"); 138 writeLine(writer, "</Structure>"); 139 140 writeLine(writer, "<JacaretoStructure>"); 141 } 142 143 private void writeJacaretoXML(Collection<List<Event>> sequences, String filename) { 144 BufferedWriter writer = new BufferedWriter(openReplayFile(filename + ".xml")); 145 146 try { 147 writeJacaretoHead(writer); 148 writeJacaretoEvents(writer, sequences); 149 writeJacaretoTail(writer); 150 151 writer.flush(); 152 writer.close(); 153 } 154 catch (IOException e) { 155 Console.printerrln("Unable to write Jacareto replay file " + filename); 156 } 157 } 158 159 /** 160 * <p> 161 * {@link IReplayDecorator} to be used. If this field is {@code null}, no decorator is used. 162 * Default: {@code null} 163 * </p> 164 */ 165 private IReplayDecorator decorator = null; 166 167 /** 168 * <p> 169 * Id of the current session. The starting id is 1. 170 * </p> 171 */ 172 int sessionId = 1; 173 174 /** 175 * <p> 176 * Creates a replay file that contains multiple event sequences. 177 * </p> 178 * 179 * @param sequences 180 * collection of event sequences from which the sessions are generated 181 * @param filename 182 * name and path of the replay file 183 */ 184 private void createLogfileMultipleSessions(Collection<List<Event>> sequences, String filename) { 185 OutputStreamWriter writer = openReplayFile(filename); 186 if (writer != null) { 187 try { 188 try { 189 decorator = 190 sequences.iterator().next().get(0).getReplayables().get(0).getDecorator(); 191 } 192 catch (Exception e) { 193 // in the above line, many things can go wrong: emtpy sequences, null 194 // references, etc. However, all failures just indicate that no replay decorator 195 // should be used, hence, we ignore the exception 196 Console.traceln(Level.FINEST, "no decorator used for " + filename); 197 } 198 if (decorator != null) { 199 writer.write(decorator.getHeader()); 200 } 201 for (List<Event> actions : sequences) { 202 writeSession(actions, writer); 203 } 204 if (decorator != null) { 205 writer.write(decorator.getFooter()); 206 } 207 decorator = null; 208 writer.close(); 209 } 210 catch (IOException e) { 211 Console.printerrln("Unable to write replay file " + filename); 212 } 213 } 214 } 215 216 /** 217 * <p> 218 * Helper function that opens the replay file for writing. 219 * </p> 220 * 221 * @param filename 222 * name and path of the replay file 223 * @param encoding 224 * file encoding, empty string for platform default 225 * @return {@link OutputStreamWriter} that writes to the replay file 226 */ 227 private OutputStreamWriter openReplayFile(String filename, String encoding) { 228 File file = new File(filename); 229 boolean fileCreated; 230 try { 231 fileCreated = file.createNewFile(); 232 if (!fileCreated) { 233 Console.traceln(Level.INFO, "Created logfile " + filename); 234 } 235 else { 236 Console.traceln(Level.INFO, "Overwrote existing logfile " + filename); 237 } 238 } 239 catch (IOException e) { 240 Console.printerrln("Unable to create file " + filename); 241 Console.logException(e); 242 } 243 OutputStreamWriter writer = null; 244 try { 245 if (encoding.isEmpty()) { 246 writer = new OutputStreamWriter(new FileOutputStream(file)); 247 } 248 else { 249 writer = new OutputStreamWriter(new FileOutputStream(file), encoding); 250 } 251 } 252 catch (IOException e) { 253 Console.printerrln("Unable to open file for writing (read-only file):" + filename); 254 Console.logException(e); 255 } 256 return writer; 257 } 258 259 private OutputStreamWriter openReplayFile(String filename) { 260 return openReplayFile(filename, ""); 261 } 262 263 /** 264 * <p> 265 * Helper function that adds an event sequence to the replay. 266 * </p> 267 * 268 * @param actions 269 * event sequences to be added 270 * @param writer 271 * {@link OutputStreamWriter} to which the replay is added 272 * @throws IOException 273 * thrown if there is a problem writing to writer 274 */ 275 private void writeSession(List<Event> actions, OutputStreamWriter writer) throws IOException { 276 if (decorator != null) { 277 writer.write(decorator.getSessionHeader(sessionId)); 278 } 279 for (Event currentAction : actions) { 280 281 List<? extends IReplayable> replayables = currentAction.getReplayables(); 282 for (IReplayable replayble : replayables) { 283 writer.write(replayble.getReplay() + StringTools.ENDLINE); 284 writer.flush(); 285 } 286 } 287 if (decorator != null) { 288 writer.write(decorator.getSessionFooter(sessionId)); 289 } 290 sessionId++; 291 } 213 292 }
Note: See TracChangeset
for help on using the changeset viewer.