[1671] | 1 | // Copyright 2012 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 | |
---|
[1673] | 15 | package de.ugoe.cs.autoquest.plugin.jfc.commands; |
---|
[1671] | 16 | |
---|
| 17 | import java.io.BufferedWriter; |
---|
[1673] | 18 | import java.io.File; |
---|
| 19 | import java.io.FileOutputStream; |
---|
[1671] | 20 | import java.io.IOException; |
---|
[1673] | 21 | import java.io.OutputStreamWriter; |
---|
[1671] | 22 | import java.util.ArrayList; |
---|
| 23 | import java.util.Collection; |
---|
| 24 | import java.util.Iterator; |
---|
| 25 | import java.util.List; |
---|
[1673] | 26 | import java.util.logging.Level; |
---|
[1671] | 27 | |
---|
| 28 | import de.ugoe.cs.autoquest.CommandHelpers; |
---|
| 29 | import de.ugoe.cs.autoquest.SequenceInstanceOf; |
---|
[1673] | 30 | import de.ugoe.cs.util.console.Command; |
---|
[1671] | 31 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
[1673] | 32 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElement; |
---|
[1671] | 33 | import de.ugoe.cs.util.console.Console; |
---|
| 34 | import de.ugoe.cs.util.console.GlobalDataContainer; |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * <p> |
---|
| 38 | * Command to create a Jacareto xml replay file from stored sessions. |
---|
| 39 | * </p> |
---|
| 40 | * |
---|
| 41 | * @author Daniel May |
---|
| 42 | * @version 1.0 |
---|
| 43 | */ |
---|
[1673] | 44 | public class CMDgenerateJacaretoReplay implements Command { |
---|
[1671] | 45 | |
---|
| 46 | /* |
---|
| 47 | * (non-Javadoc) |
---|
| 48 | * |
---|
| 49 | * @see de.ugoe.cs.util.console.Command#help() |
---|
| 50 | */ |
---|
| 51 | @Override |
---|
| 52 | public String help() { |
---|
| 53 | return "generateJacaretoReplay <filename> <sequences>"; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | /* |
---|
| 57 | * (non-Javadoc) |
---|
| 58 | * |
---|
| 59 | * @see de.ugoe.cs.util.console.Command#run(java.util.List) |
---|
| 60 | */ |
---|
| 61 | @SuppressWarnings("unchecked") |
---|
| 62 | @Override |
---|
| 63 | public void run(List<Object> parameters) { |
---|
| 64 | String filename; |
---|
| 65 | String sequencesName; |
---|
| 66 | try { |
---|
| 67 | filename = (String) parameters.get(0); |
---|
| 68 | sequencesName = (String) parameters.get(1); |
---|
| 69 | } |
---|
| 70 | catch (Exception e) { |
---|
| 71 | throw new IllegalArgumentException(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | Collection<List<Event>> sequences = null; |
---|
| 75 | Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName); |
---|
| 76 | if (dataObject == null) { |
---|
| 77 | CommandHelpers.objectNotFoundMessage(sequencesName); |
---|
| 78 | return; |
---|
| 79 | } |
---|
| 80 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) { |
---|
| 81 | CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>"); |
---|
| 82 | return; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | sequences = (Collection<List<Event>>) dataObject; |
---|
| 86 | |
---|
| 87 | writeJacaretoXML(sequences, filename); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | private void writeLine(BufferedWriter writer, String line) throws IOException { |
---|
| 91 | writer.write(line); |
---|
| 92 | writer.newLine(); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | private void writeJacaretoHead(BufferedWriter writer) throws IOException { |
---|
| 96 | writeLine(writer, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"); |
---|
| 97 | writeLine(writer, "<JacaretoStructure>"); |
---|
| 98 | writeLine(writer, "<Record>"); |
---|
| 99 | |
---|
| 100 | // TODO: This header content is basically copy+paste from a |
---|
| 101 | // specific jacareto replay file right now. |
---|
| 102 | // Some things such as screen resolution and especially the |
---|
| 103 | // application starter details need to be changed for general cases. |
---|
| 104 | writeLine(writer, |
---|
| 105 | "<Calendar procTime=\"0\" duration=\"0\" year=\"2014\" month=\"8\" date=\"11\" hour=\"14\" min=\"43\" sec=\"41\" uuid=\"06831ba1-f28a-4e05-b46e-ce9d8f9ffa0f\" />"); |
---|
| 106 | writeLine(writer, |
---|
| 107 | "<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\" />"); |
---|
| 108 | writeLine(writer, |
---|
| 109 | "<KeyboardState procTime=\"0\" duration=\"0\" isNumLockOn=\"false\" isScrollLockOn=\"false\" isCapsLockOn=\"false\" applyIsNumLockOn=\"true\" applyIsScrollLockOn=\"true\" applyIsCapsLockOn=\"true\" uuid=\"28146f79-9fc7-49f9-b4a8-5866a7625683\" />"); |
---|
| 110 | writeLine(writer, "ComponentMode numberPopupMenues=\"true\" />"); |
---|
| 111 | writeLine(writer, |
---|
| 112 | "<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\" />"); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | private ArrayList<String> writeJacaretoEvents(BufferedWriter writer, |
---|
| 116 | Collection<List<Event>> sequences) |
---|
| 117 | throws IOException |
---|
| 118 | { |
---|
| 119 | ArrayList<String> structure = new ArrayList<String>(); |
---|
| 120 | structure.add("<StructureElement class=\"jacareto.struct.RootElement\">"); |
---|
| 121 | // reference the elements that we included in the header |
---|
| 122 | structure.add("<Recordable ref=\"1\" />"); // Calendar |
---|
| 123 | structure.add("<Recordable ref=\"2\" />"); // SystemInfo |
---|
| 124 | structure.add("<Recordable ref=\"3\" />"); // KeyboardState |
---|
| 125 | structure.add("<Recordable ref=\"4\" />"); // ApplicationStarter |
---|
| 126 | |
---|
| 127 | for (List<Event> sequence : sequences) { |
---|
| 128 | for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { |
---|
| 129 | Event event = eventIter.next(); |
---|
| 130 | |
---|
[1673] | 131 | // TODO: do a mapping file or something to map |
---|
| 132 | // from autoquest events to jacareto events |
---|
| 133 | if (event.getType().getName().equals("KeyboardFocusChange")) { |
---|
| 134 | /* |
---|
| 135 | writeLine( |
---|
| 136 | "<FocusEvent procTime=\"0\" duration=\"0\" source="JFrame_(1).JRootPane_(1).JLayeredPane_(1).JPanel_(1).JPanel_(1).JCheckBox_(1)" +" + |
---|
| 137 | " class="javax.swing.JCheckBox" uuid="061bee8f-d8a3-477f-ab8d-bab02f614916" ID="1004" component="null" root="JFrame_(1)" xPos="0" yPos="0" width="0" height="0" isTemporary="false" />"); |
---|
| 138 | */ |
---|
| 139 | System.out.println(((JFCGUIElement)event.getTarget()).getJacaretoHierarchy()); |
---|
| 140 | } |
---|
| 141 | else { |
---|
| 142 | System.out.println(event.getType()); |
---|
| 143 | System.out.println(event.getTarget()); |
---|
| 144 | } |
---|
[1671] | 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | return structure; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | private void writeJacaretoTail(BufferedWriter writer, ArrayList<String> structure) |
---|
| 152 | throws IOException |
---|
| 153 | { |
---|
| 154 | writeLine(writer, "</Record>"); |
---|
| 155 | |
---|
| 156 | // write the recording's structure |
---|
| 157 | writeLine(writer, "<Structure>"); |
---|
| 158 | for (String element : structure) { |
---|
| 159 | writeLine(writer, element); |
---|
| 160 | } |
---|
| 161 | writeLine(writer, "</Structure>"); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | private void writeJacaretoXML(Collection<List<Event>> sequences, String filename) { |
---|
| 165 | BufferedWriter writer = new BufferedWriter(openReplayFile(filename + ".xml")); |
---|
| 166 | |
---|
| 167 | try { |
---|
| 168 | writeJacaretoHead(writer); |
---|
| 169 | ArrayList<String> structure = writeJacaretoEvents(writer, sequences); |
---|
| 170 | writeJacaretoTail(writer, structure); |
---|
[1673] | 171 | writeLine(writer, "<JacaretoStructure>"); |
---|
[1671] | 172 | |
---|
| 173 | writer.flush(); |
---|
| 174 | writer.close(); |
---|
| 175 | } |
---|
| 176 | catch (IOException e) { |
---|
| 177 | Console.printerrln("Unable to write Jacareto replay file " + filename); |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
[1673] | 181 | /** |
---|
| 182 | * <p> |
---|
| 183 | * Helper function that opens the replay file for writing. |
---|
| 184 | * </p> |
---|
| 185 | * |
---|
| 186 | * @param filename |
---|
| 187 | * name and path of the replay file |
---|
| 188 | * @param encoding |
---|
| 189 | * file encoding, empty string for platform default |
---|
| 190 | * @return {@link OutputStreamWriter} that writes to the replay file |
---|
| 191 | */ |
---|
| 192 | private OutputStreamWriter openReplayFile(String filename) { |
---|
| 193 | File file = new File(filename); |
---|
| 194 | boolean fileCreated; |
---|
| 195 | try { |
---|
| 196 | fileCreated = file.createNewFile(); |
---|
| 197 | if (!fileCreated) { |
---|
| 198 | Console.traceln(Level.INFO, "Created logfile " + filename); |
---|
| 199 | } |
---|
| 200 | else { |
---|
| 201 | Console.traceln(Level.INFO, "Overwrote existing logfile " + filename); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | catch (IOException e) { |
---|
| 205 | Console.printerrln("Unable to create file " + filename); |
---|
| 206 | Console.logException(e); |
---|
| 207 | } |
---|
| 208 | OutputStreamWriter writer = null; |
---|
| 209 | try { |
---|
| 210 | writer = new OutputStreamWriter(new FileOutputStream(file)); |
---|
| 211 | } |
---|
| 212 | catch (IOException e) { |
---|
| 213 | Console.printerrln("Unable to open file for writing (read-only file):" + filename); |
---|
| 214 | Console.logException(e); |
---|
| 215 | } |
---|
| 216 | return writer; |
---|
| 217 | } |
---|
[1671] | 218 | } |
---|