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 | |
---|
15 | package de.ugoe.cs.autoquest.plugin.jfc.commands; |
---|
16 | |
---|
17 | import java.io.BufferedWriter; |
---|
18 | import java.io.File; |
---|
19 | import java.io.FileOutputStream; |
---|
20 | import java.io.IOException; |
---|
21 | import java.io.OutputStreamWriter; |
---|
22 | import java.util.ArrayList; |
---|
23 | import java.util.Collection; |
---|
24 | import java.util.Iterator; |
---|
25 | import java.util.List; |
---|
26 | import java.util.UUID; |
---|
27 | import java.util.logging.Level; |
---|
28 | |
---|
29 | import de.ugoe.cs.autoquest.CommandHelpers; |
---|
30 | import de.ugoe.cs.autoquest.SequenceInstanceOf; |
---|
31 | import de.ugoe.cs.util.console.Command; |
---|
32 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
33 | import de.ugoe.cs.autoquest.eventcore.IEventTarget; |
---|
34 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElement; |
---|
35 | import de.ugoe.cs.util.console.Console; |
---|
36 | import de.ugoe.cs.util.console.GlobalDataContainer; |
---|
37 | |
---|
38 | /** |
---|
39 | * <p> |
---|
40 | * Command to create a Jacareto xml replay file from stored sessions. |
---|
41 | * </p> |
---|
42 | * |
---|
43 | * @author Daniel May |
---|
44 | * @version 1.0 |
---|
45 | */ |
---|
46 | public class CMDgenerateJacaretoReplay implements Command { |
---|
47 | |
---|
48 | private int nextRef; |
---|
49 | |
---|
50 | /* |
---|
51 | * (non-Javadoc) |
---|
52 | * |
---|
53 | * @see de.ugoe.cs.util.console.Command#help() |
---|
54 | */ |
---|
55 | @Override |
---|
56 | public String help() { |
---|
57 | return "generateJacaretoReplay <filename> <sequences>"; |
---|
58 | } |
---|
59 | |
---|
60 | /* |
---|
61 | * (non-Javadoc) |
---|
62 | * |
---|
63 | * @see de.ugoe.cs.util.console.Command#run(java.util.List) |
---|
64 | */ |
---|
65 | @SuppressWarnings("unchecked") |
---|
66 | @Override |
---|
67 | public void run(List<Object> parameters) { |
---|
68 | String filename; |
---|
69 | String sequencesName; |
---|
70 | try { |
---|
71 | filename = (String) parameters.get(0); |
---|
72 | sequencesName = (String) parameters.get(1); |
---|
73 | } |
---|
74 | catch (Exception e) { |
---|
75 | throw new IllegalArgumentException(); |
---|
76 | } |
---|
77 | |
---|
78 | Collection<List<Event>> sequences = null; |
---|
79 | Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName); |
---|
80 | if (dataObject == null) { |
---|
81 | CommandHelpers.objectNotFoundMessage(sequencesName); |
---|
82 | return; |
---|
83 | } |
---|
84 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) { |
---|
85 | CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>"); |
---|
86 | return; |
---|
87 | } |
---|
88 | |
---|
89 | sequences = (Collection<List<Event>>) dataObject; |
---|
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 ArrayList<String> writeJacaretoEvents(BufferedWriter writer, |
---|
120 | Collection<List<Event>> sequences) |
---|
121 | throws IOException |
---|
122 | { |
---|
123 | ArrayList<String> structure = new ArrayList<String>(); |
---|
124 | structure.add("<StructureElement class=\"jacareto.struct.RootElement\">"); |
---|
125 | // reference the elements that we included in the header |
---|
126 | structure.add("<Recordable ref=\"0\" />"); // Calendar |
---|
127 | structure.add("<Recordable ref=\"1\" />"); // SystemInfo |
---|
128 | structure.add("<Recordable ref=\"2\" />"); // KeyboardState |
---|
129 | structure.add("<Recordable ref=\"3\" />"); // ComponentMode |
---|
130 | structure.add("<Recordable ref=\"4\" />"); // ApplicationStarter |
---|
131 | nextRef = 5; |
---|
132 | |
---|
133 | for (List<Event> sequence : sequences) { |
---|
134 | for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { |
---|
135 | Event event = eventIter.next(); |
---|
136 | |
---|
137 | // TODO: do a mapping file or something to map |
---|
138 | // from autoquest events to jacareto events |
---|
139 | if (event.getType().getName().equals("LeftMouseClick")) { |
---|
140 | JFCGUIElement target = (JFCGUIElement) event.getTarget(); |
---|
141 | |
---|
142 | // FIXME: assume that the target is a checkbox for now =) |
---|
143 | // this is an item event and an action event in jacareto |
---|
144 | //@formatter:off |
---|
145 | writeLine(writer, |
---|
146 | "<ItemEvent " |
---|
147 | + "procTime=\"1\" " |
---|
148 | + "duration=\"8\" " |
---|
149 | + "source=\"" + target.getJacaretoHierarchy() + "\" " |
---|
150 | + "class=\"" + target.getSpecification().getType() + "\" " |
---|
151 | + "uuid=\"" + UUID.randomUUID() + "\" " |
---|
152 | + "ID=\"701\" " |
---|
153 | + "item=\"\" " |
---|
154 | + "stateChange=\"1\" />" |
---|
155 | ); |
---|
156 | writeLine(writer, |
---|
157 | "<ActionEvent " |
---|
158 | + "procTime=\"1\" " |
---|
159 | + "duration=\"0\" " |
---|
160 | + "source=\"" + target.getJacaretoHierarchy() + "\" " |
---|
161 | + "class=\"" + target.getSpecification().getType() + "\" " |
---|
162 | + "uuid=\"" + UUID.randomUUID() + "\" " |
---|
163 | + "ID=\"1001\" " |
---|
164 | + "command=" + target.getName() + " " |
---|
165 | + "modifiers=\"16\" />" |
---|
166 | ); |
---|
167 | //@formatter:on |
---|
168 | structure.add("<StructureElement class=\"jacareto.struct.ItemStateChange\">"); |
---|
169 | structure.add("<Recordable ref=\"" + (nextRef++) + "\" />"); |
---|
170 | structure.add("<Recordable ref=\"" + (nextRef++) + "\" />"); |
---|
171 | structure.add("</StructureElement>"); |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | return structure; |
---|
177 | } |
---|
178 | |
---|
179 | private void writeJacaretoTail(BufferedWriter writer, ArrayList<String> structure) |
---|
180 | throws IOException |
---|
181 | { |
---|
182 | writeLine(writer, "</Record>"); |
---|
183 | |
---|
184 | // write the recording's structure |
---|
185 | writeLine(writer, "<Structure>"); |
---|
186 | for (String element : structure) { |
---|
187 | writeLine(writer, element); |
---|
188 | } |
---|
189 | // close root element |
---|
190 | writeLine(writer, "</StructureElement>"); |
---|
191 | writeLine(writer, "</Structure>"); |
---|
192 | } |
---|
193 | |
---|
194 | private void writeJacaretoXML(Collection<List<Event>> sequences, String filename) { |
---|
195 | BufferedWriter writer = new BufferedWriter(openReplayFile(filename + ".xml")); |
---|
196 | |
---|
197 | try { |
---|
198 | writeJacaretoHead(writer); |
---|
199 | ArrayList<String> structure = writeJacaretoEvents(writer, sequences); |
---|
200 | writeJacaretoTail(writer, structure); |
---|
201 | writeLine(writer, "</JacaretoStructure>"); |
---|
202 | |
---|
203 | writer.flush(); |
---|
204 | writer.close(); |
---|
205 | } |
---|
206 | catch (IOException e) { |
---|
207 | Console.printerrln("Unable to write Jacareto replay file " + filename); |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * <p> |
---|
213 | * Helper function that opens the replay file for writing. |
---|
214 | * </p> |
---|
215 | * |
---|
216 | * @param filename |
---|
217 | * name and path of the replay file |
---|
218 | * @param encoding |
---|
219 | * file encoding, empty string for platform default |
---|
220 | * @return {@link OutputStreamWriter} that writes to the replay file |
---|
221 | */ |
---|
222 | private OutputStreamWriter openReplayFile(String filename) { |
---|
223 | File file = new File(filename); |
---|
224 | boolean fileCreated; |
---|
225 | try { |
---|
226 | fileCreated = file.createNewFile(); |
---|
227 | if (!fileCreated) { |
---|
228 | Console.traceln(Level.INFO, "Created logfile " + filename); |
---|
229 | } |
---|
230 | else { |
---|
231 | Console.traceln(Level.INFO, "Overwrote existing logfile " + filename); |
---|
232 | } |
---|
233 | } |
---|
234 | catch (IOException e) { |
---|
235 | Console.printerrln("Unable to create file " + filename); |
---|
236 | Console.logException(e); |
---|
237 | } |
---|
238 | OutputStreamWriter writer = null; |
---|
239 | try { |
---|
240 | writer = new OutputStreamWriter(new FileOutputStream(file)); |
---|
241 | } |
---|
242 | catch (IOException e) { |
---|
243 | Console.printerrln("Unable to open file for writing (read-only file):" + filename); |
---|
244 | Console.logException(e); |
---|
245 | } |
---|
246 | return writer; |
---|
247 | } |
---|
248 | } |
---|