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