[353] | 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>
|
---|
[392] | 19 | * Command to pre-process files written by EventBench's JFCMonitor located in a
|
---|
| 20 | * directory. The only task of the pre-processing is checking if the session was
|
---|
| 21 | * closed properly, i.e., if the XML file ends with a {@code </sessions>} tag.
|
---|
| 22 | * If this is not the case, the tag will be appended to the file.
|
---|
[353] | 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);
|
---|
[392] | 52 | if (!targetFolder.isDirectory()) {
|
---|
[353] | 53 | Console.printerrln(targetPath + " is not a directory");
|
---|
| 54 | }
|
---|
| 55 | String absolutPathTarget = targetFolder.getAbsolutePath();
|
---|
[392] | 56 |
|
---|
| 57 | for (String filename : sourceFolder.list()) {
|
---|
[353] | 58 | String source = absolutPathSource + "/" + filename;
|
---|
[384] | 59 | Console.traceln("Preprocessing file: " + source);
|
---|
[353] | 60 | File file = new File(source);
|
---|
| 61 | InputStreamReader reader;
|
---|
| 62 | try {
|
---|
| 63 | FileInputStream fis = new FileInputStream(file);
|
---|
| 64 | reader = new InputStreamReader(fis, "UTF-16");
|
---|
| 65 | } catch (FileNotFoundException e) {
|
---|
| 66 | Console.printerrln(e.getMessage());
|
---|
| 67 | return;
|
---|
| 68 | } catch (UnsupportedEncodingException e) {
|
---|
| 69 | Console.printerrln(e.getMessage());
|
---|
| 70 | return;
|
---|
| 71 | }
|
---|
| 72 | char[] buffer = new char[(int) file.length()];
|
---|
| 73 | try {
|
---|
| 74 | reader.read(buffer);
|
---|
| 75 | reader.close();
|
---|
| 76 | } catch (IOException e) {
|
---|
| 77 | Console.printerrln(e.getMessage());
|
---|
| 78 | return;
|
---|
| 79 | }
|
---|
[392] | 80 |
|
---|
[353] | 81 | String content = new String(buffer).trim();
|
---|
[392] | 82 |
|
---|
[353] | 83 | int index = filename.lastIndexOf('.');
|
---|
[392] | 84 | String target = absolutPathTarget + "/"
|
---|
| 85 | + filename.substring(0, index) + ".xml";
|
---|
| 86 |
|
---|
[384] | 87 | Console.traceln(" Saving as: " + target);
|
---|
[392] | 88 |
|
---|
[353] | 89 | OutputStreamWriter writer;
|
---|
| 90 | try {
|
---|
| 91 | FileOutputStream fos = new FileOutputStream(target);
|
---|
[392] | 92 | writer = new OutputStreamWriter(fos, "UTF-8");
|
---|
[353] | 93 | } catch (IOException e) {
|
---|
| 94 | Console.printerrln(e.getMessage());
|
---|
| 95 | return;
|
---|
| 96 | }
|
---|
| 97 | try {
|
---|
| 98 | writer.write(content);
|
---|
| 99 | if (!content.endsWith("</sessions>")) {
|
---|
| 100 | writer.write("</sessions>");
|
---|
| 101 | }
|
---|
| 102 | writer.close();
|
---|
| 103 | } catch (IOException e) {
|
---|
| 104 | Console.printerrln(e.getMessage());
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /*
|
---|
| 110 | * (non-Javadoc)
|
---|
| 111 | *
|
---|
| 112 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 113 | */
|
---|
| 114 | @Override
|
---|
| 115 | public void help() {
|
---|
| 116 | Console.println("Usage: preprocessDirJFC <sourcePath> <targetPath>");
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | }
|
---|