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