[305] | 1 | package de.ugoe.cs.eventbench.jfc.commands;
|
---|
[302] | 2 |
|
---|
| 3 | import java.io.File;
|
---|
[309] | 4 | import java.io.FileInputStream;
|
---|
[302] | 5 | import java.io.FileNotFoundException;
|
---|
[309] | 6 | import java.io.FileOutputStream;
|
---|
[302] | 7 | import java.io.IOException;
|
---|
[309] | 8 | import java.io.InputStreamReader;
|
---|
| 9 | import java.io.OutputStreamWriter;
|
---|
| 10 | import java.io.UnsupportedEncodingException;
|
---|
[302] | 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);
|
---|
[309] | 47 | InputStreamReader reader;
|
---|
[302] | 48 | try {
|
---|
[309] | 49 | FileInputStream fis = new FileInputStream(file);
|
---|
| 50 | reader = new InputStreamReader(fis, "UTF-16");
|
---|
[302] | 51 | } catch (FileNotFoundException e) {
|
---|
| 52 | Console.printerrln(e.getMessage());
|
---|
| 53 | return;
|
---|
[309] | 54 | } catch (UnsupportedEncodingException e) {
|
---|
| 55 | Console.printerrln(e.getMessage());
|
---|
| 56 | return;
|
---|
[302] | 57 | }
|
---|
| 58 | char[] buffer = new char[(int) file.length()];
|
---|
| 59 | try {
|
---|
[309] | 60 | reader.read(buffer);
|
---|
| 61 | reader.close();
|
---|
[302] | 62 | } catch (IOException e) {
|
---|
| 63 | Console.printerrln(e.getMessage());
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | String content = new String(buffer).trim();
|
---|
| 68 |
|
---|
[309] | 69 | OutputStreamWriter writer;
|
---|
[302] | 70 | try {
|
---|
[309] | 71 | FileOutputStream fos = new FileOutputStream(target);
|
---|
| 72 | writer = new OutputStreamWriter(fos, "UTF-16");
|
---|
[302] | 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 | }
|
---|
[309] | 82 | writer.close();
|
---|
[302] | 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 | }
|
---|