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 located in a directory. 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 CMDpreprocessDirJFC 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 sourcePath;
|
---|
38 | String targetPath;
|
---|
39 | try {
|
---|
40 | sourcePath = (String) parameters.get(0);
|
---|
41 | targetPath = (String) parameters.get(1);
|
---|
42 | } catch (Exception e) {
|
---|
43 | throw new InvalidParameterException();
|
---|
44 | }
|
---|
45 |
|
---|
46 | File sourceFolder = new File(sourcePath);
|
---|
47 | if (!sourceFolder.isDirectory()) {
|
---|
48 | Console.printerrln(sourcePath + " is not a directory");
|
---|
49 | }
|
---|
50 | String absolutPathSource = sourceFolder.getAbsolutePath();
|
---|
51 | File targetFolder = new File(targetPath);
|
---|
52 | if( !targetFolder.isDirectory()) {
|
---|
53 | Console.printerrln(targetPath + " is not a directory");
|
---|
54 | }
|
---|
55 | String absolutPathTarget = targetFolder.getAbsolutePath();
|
---|
56 |
|
---|
57 | for(String filename : sourceFolder.list()) {
|
---|
58 | String source = absolutPathSource + "/" + filename;
|
---|
59 | File file = new File(source);
|
---|
60 | InputStreamReader reader;
|
---|
61 | try {
|
---|
62 | FileInputStream fis = new FileInputStream(file);
|
---|
63 | reader = new InputStreamReader(fis, "UTF-16");
|
---|
64 | } catch (FileNotFoundException e) {
|
---|
65 | Console.printerrln(e.getMessage());
|
---|
66 | return;
|
---|
67 | } catch (UnsupportedEncodingException e) {
|
---|
68 | Console.printerrln(e.getMessage());
|
---|
69 | return;
|
---|
70 | }
|
---|
71 | char[] buffer = new char[(int) file.length()];
|
---|
72 | try {
|
---|
73 | reader.read(buffer);
|
---|
74 | reader.close();
|
---|
75 | } catch (IOException e) {
|
---|
76 | Console.printerrln(e.getMessage());
|
---|
77 | return;
|
---|
78 | }
|
---|
79 |
|
---|
80 | String content = new String(buffer).trim();
|
---|
81 |
|
---|
82 | int index = filename.lastIndexOf('.');
|
---|
83 | String target = absolutPathTarget + "/" + filename.substring(0, index) + "xml";
|
---|
84 |
|
---|
85 | OutputStreamWriter writer;
|
---|
86 | try {
|
---|
87 | FileOutputStream fos = new FileOutputStream(target);
|
---|
88 | writer = new OutputStreamWriter(fos, "UTF-16");
|
---|
89 | } catch (IOException e) {
|
---|
90 | Console.printerrln(e.getMessage());
|
---|
91 | return;
|
---|
92 | }
|
---|
93 | try {
|
---|
94 | writer.write(content);
|
---|
95 | if (!content.endsWith("</sessions>")) {
|
---|
96 | writer.write("</sessions>");
|
---|
97 | }
|
---|
98 | writer.close();
|
---|
99 | } catch (IOException e) {
|
---|
100 | Console.printerrln(e.getMessage());
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * (non-Javadoc)
|
---|
107 | *
|
---|
108 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
109 | */
|
---|
110 | @Override
|
---|
111 | public void help() {
|
---|
112 | Console.println("Usage: preprocessDirJFC <sourcePath> <targetPath>");
|
---|
113 | }
|
---|
114 |
|
---|
115 | }
|
---|