source: trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/commands/CMDpreprocessJFC.java @ 922

Last change on this file since 922 was 922, checked in by sherbold, 12 years ago
  • renaming of packages from de.ugoe.cs.quest to de.ugoe.cs.autoquest
File size: 2.4 KB
Line 
1package de.ugoe.cs.autoquest.plugin.jfc.commands;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.io.OutputStreamWriter;
10import java.io.UnsupportedEncodingException;
11import java.util.List;
12
13import de.ugoe.cs.util.console.Command;
14import 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 */
27public 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) {
42                        throw new IllegalArgumentException();
43                }
44
45                File file = new File(source);
46                InputStreamReader reader = null;
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
93        public String help() {
94                return "preprocessJFC <sourceFile> <targetFile>";
95        }
96
97}
Note: See TracBrowser for help on using the repository browser.