source: trunk/autoquest-ui-core/src/main/java/de/ugoe/cs/autoquest/commands/sequences/CMDgenerateJacaretoReplay.java @ 1672

Last change on this file since 1672 was 1671, checked in by dmay, 10 years ago

jacareto replay generation now has its own command - functionality
is still work in progress

  • Property svn:mime-type set to text/plain
File size: 6.5 KB
Line 
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
15package de.ugoe.cs.autoquest.commands.sequences;
16
17import java.io.BufferedWriter;
18import java.io.IOException;
19import java.util.ArrayList;
20import java.util.Collection;
21import java.util.Iterator;
22import java.util.List;
23
24import de.ugoe.cs.autoquest.CommandHelpers;
25import de.ugoe.cs.autoquest.SequenceInstanceOf;
26import de.ugoe.cs.autoquest.eventcore.Event;
27import de.ugoe.cs.util.console.Console;
28import de.ugoe.cs.util.console.GlobalDataContainer;
29
30/**
31 * <p>
32 * Command to create a Jacareto xml replay file from stored sessions.
33 * </p>
34 *
35 * @author Daniel May
36 * @version 1.0
37 */
38public class CMDgenerateJacaretoReplay extends CMDgenerateReplayfile {
39
40    /*
41     * (non-Javadoc)
42     *
43     * @see de.ugoe.cs.util.console.Command#help()
44     */
45    @Override
46    public String help() {
47        return "generateJacaretoReplay <filename> <sequences>";
48    }
49
50    /*
51     * (non-Javadoc)
52     *
53     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
54     */
55    @SuppressWarnings("unchecked")
56    @Override
57    public void run(List<Object> parameters) {
58        // TODO: except for the last lines, this function is identical to
59        // superclass CMDgenerateReplayfile, extract a function
60        String filename;
61        String sequencesName;
62        try {
63            filename = (String) parameters.get(0);
64            sequencesName = (String) parameters.get(1);
65        }
66        catch (Exception e) {
67            throw new IllegalArgumentException();
68        }
69
70        Collection<List<Event>> sequences = null;
71        Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
72        if (dataObject == null) {
73            CommandHelpers.objectNotFoundMessage(sequencesName);
74            return;
75        }
76        if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
77            CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>");
78            return;
79        }
80
81        sequences = (Collection<List<Event>>) dataObject;
82
83        writeJacaretoXML(sequences, filename);
84    }
85
86    private void writeLine(BufferedWriter writer, String line) throws IOException {
87        writer.write(line);
88        writer.newLine();
89    }
90
91    private void writeJacaretoHead(BufferedWriter writer) throws IOException {
92        writeLine(writer, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
93        writeLine(writer, "<JacaretoStructure>");
94        writeLine(writer, "<Record>");
95
96        // TODO: This header content is basically copy+paste from a
97        // specific jacareto replay file right now.
98        // Some things such as screen resolution and especially the
99        // application starter details need to be changed for general cases.
100        writeLine(writer,
101                  "<Calendar procTime=\"0\" duration=\"0\" year=\"2014\" month=\"8\" date=\"11\" hour=\"14\" min=\"43\" sec=\"41\" uuid=\"06831ba1-f28a-4e05-b46e-ce9d8f9ffa0f\" />");
102        writeLine(writer,
103                  "<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\" />");
104        writeLine(writer,
105                  "<KeyboardState procTime=\"0\" duration=\"0\" isNumLockOn=\"false\" isScrollLockOn=\"false\" isCapsLockOn=\"false\" applyIsNumLockOn=\"true\" applyIsScrollLockOn=\"true\" applyIsCapsLockOn=\"true\" uuid=\"28146f79-9fc7-49f9-b4a8-5866a7625683\" />");
106        writeLine(writer, "ComponentMode numberPopupMenues=\"true\" />");
107        writeLine(writer,
108                  "<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\" />");
109    }
110
111    private ArrayList<String> writeJacaretoEvents(BufferedWriter writer,
112                                                  Collection<List<Event>> sequences)
113        throws IOException
114    {
115        ArrayList<String> structure = new ArrayList<String>();
116        structure.add("<StructureElement class=\"jacareto.struct.RootElement\">");
117        // reference the elements that we included in the header
118        structure.add("<Recordable ref=\"1\" />"); // Calendar
119        structure.add("<Recordable ref=\"2\" />"); // SystemInfo
120        structure.add("<Recordable ref=\"3\" />"); // KeyboardState
121        structure.add("<Recordable ref=\"4\" />"); // ApplicationStarter
122
123        for (List<Event> sequence : sequences) {
124            for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
125                Event event = eventIter.next();
126
127                // TODO
128                System.out.println(event.getTarget());
129                System.out.println(event.getType());
130            }
131        }
132
133        return structure;
134    }
135
136    private void writeJacaretoTail(BufferedWriter writer, ArrayList<String> structure)
137        throws IOException
138    {
139        writeLine(writer, "</Record>");
140
141        // write the recording's structure
142        writeLine(writer, "<Structure>");
143        for (String element : structure) {
144            writeLine(writer, element);
145        }
146        writeLine(writer, "</Structure>");
147
148        writeLine(writer, "<JacaretoStructure>");
149    }
150
151    private void writeJacaretoXML(Collection<List<Event>> sequences, String filename) {
152        BufferedWriter writer = new BufferedWriter(openReplayFile(filename + ".xml"));
153
154        try {
155            writeJacaretoHead(writer);
156            ArrayList<String> structure = writeJacaretoEvents(writer, sequences);
157            writeJacaretoTail(writer, structure);
158
159            writer.flush();
160            writer.close();
161        }
162        catch (IOException e) {
163            Console.printerrln("Unable to write Jacareto replay file " + filename);
164        }
165    }
166
167}
Note: See TracBrowser for help on using the repository browser.