[922] | 1 | package de.ugoe.cs.autoquest.plugin.jfc.commands;
|
---|
[573] | 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.FileInputStream;
|
---|
| 5 | import java.io.FileNotFoundException;
|
---|
| 6 | import java.io.FileOutputStream;
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 | import java.io.InputStreamReader;
|
---|
| 9 | import java.io.OutputStreamWriter;
|
---|
| 10 | import java.io.UnsupportedEncodingException;
|
---|
| 11 | import java.util.List;
|
---|
| 12 |
|
---|
| 13 | import de.ugoe.cs.util.console.Command;
|
---|
| 14 | import de.ugoe.cs.util.console.Console;
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * <p>
|
---|
| 18 | * Command to pre-process files written by EventBench's JFCMonitor. The only
|
---|
| 19 | * task of the pre-processing is checking if the session was closed properly,
|
---|
| 20 | * i.e., if the XML file ends with a {@code </sessions>} tag. If this is not the
|
---|
| 21 | * case, the tag will be appended to the file.
|
---|
| 22 | * </p>
|
---|
| 23 | *
|
---|
| 24 | * @author Steffen Herbold
|
---|
| 25 | * @version 1.0
|
---|
| 26 | */
|
---|
| 27 | public class CMDpreprocessJFC implements Command {
|
---|
| 28 |
|
---|
| 29 | /*
|
---|
| 30 | * (non-Javadoc)
|
---|
| 31 | *
|
---|
| 32 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 33 | */
|
---|
| 34 | @Override
|
---|
| 35 | public void run(List<Object> parameters) {
|
---|
| 36 | String source;
|
---|
| 37 | String target;
|
---|
| 38 | try {
|
---|
| 39 | source = (String) parameters.get(0);
|
---|
| 40 | target = (String) parameters.get(1);
|
---|
| 41 | } catch (Exception e) {
|
---|
[766] | 42 | throw new IllegalArgumentException();
|
---|
[573] | 43 | }
|
---|
| 44 |
|
---|
| 45 | File file = new File(source);
|
---|
[641] | 46 | InputStreamReader reader = null;
|
---|
[573] | 47 | try {
|
---|
| 48 | FileInputStream fis = new FileInputStream(file);
|
---|
| 49 | reader = new InputStreamReader(fis, "UTF-16");
|
---|
| 50 | } catch (FileNotFoundException e) {
|
---|
| 51 | Console.printerrln(e.getMessage());
|
---|
| 52 | return;
|
---|
| 53 | } catch (UnsupportedEncodingException e) {
|
---|
| 54 | Console.printerrln(e.getMessage());
|
---|
| 55 | return;
|
---|
| 56 | }
|
---|
| 57 | char[] buffer = new char[(int) file.length()];
|
---|
| 58 | try {
|
---|
| 59 | reader.read(buffer);
|
---|
| 60 | reader.close();
|
---|
| 61 | } catch (IOException e) {
|
---|
| 62 | Console.printerrln(e.getMessage());
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | String content = new String(buffer).trim();
|
---|
| 67 |
|
---|
| 68 | OutputStreamWriter writer;
|
---|
| 69 | try {
|
---|
| 70 | FileOutputStream fos = new FileOutputStream(target);
|
---|
| 71 | writer = new OutputStreamWriter(fos, "UTF-8");
|
---|
| 72 | } catch (IOException e) {
|
---|
| 73 | Console.printerrln(e.getMessage());
|
---|
| 74 | return;
|
---|
| 75 | }
|
---|
| 76 | try {
|
---|
| 77 | writer.write(content);
|
---|
| 78 | if (!content.endsWith("</sessions>")) {
|
---|
| 79 | writer.write("</sessions>");
|
---|
| 80 | }
|
---|
| 81 | writer.close();
|
---|
| 82 | } catch (IOException e) {
|
---|
| 83 | Console.printerrln(e.getMessage());
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /*
|
---|
| 88 | * (non-Javadoc)
|
---|
| 89 | *
|
---|
| 90 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 91 | */
|
---|
| 92 | @Override
|
---|
[664] | 93 | public String help() {
|
---|
| 94 | return "preprocessJFC <sourceFile> <targetFile>";
|
---|
[573] | 95 | }
|
---|
| 96 |
|
---|
| 97 | }
|
---|