[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.plugin.jfc.commands;
|
---|
[573] | 16 |
|
---|
| 17 | import java.io.File;
|
---|
| 18 | import java.io.FileInputStream;
|
---|
| 19 | import java.io.FileNotFoundException;
|
---|
| 20 | import java.io.FileOutputStream;
|
---|
| 21 | import java.io.IOException;
|
---|
| 22 | import java.io.InputStreamReader;
|
---|
| 23 | import java.io.OutputStreamWriter;
|
---|
| 24 | import java.io.UnsupportedEncodingException;
|
---|
| 25 | import java.util.List;
|
---|
[639] | 26 | import java.util.logging.Level;
|
---|
[573] | 27 |
|
---|
| 28 | import de.ugoe.cs.util.console.Command;
|
---|
| 29 | import de.ugoe.cs.util.console.Console;
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * <p>
|
---|
| 33 | * Command to pre-process files written by EventBench's JFCMonitor located in a
|
---|
| 34 | * directory. The only task of the pre-processing is checking if the session was
|
---|
| 35 | * closed properly, i.e., if the XML file ends with a {@code </sessions>} tag.
|
---|
| 36 | * If this is not the case, the tag will be appended to the file.
|
---|
| 37 | * </p>
|
---|
| 38 | *
|
---|
| 39 | * @author Steffen Herbold
|
---|
| 40 | * @version 1.0
|
---|
| 41 | */
|
---|
| 42 | public class CMDpreprocessDirJFC implements Command {
|
---|
| 43 |
|
---|
| 44 | /*
|
---|
| 45 | * (non-Javadoc)
|
---|
| 46 | *
|
---|
| 47 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 48 | */
|
---|
| 49 | @Override
|
---|
| 50 | public void run(List<Object> parameters) {
|
---|
| 51 | String sourcePath;
|
---|
| 52 | String targetPath;
|
---|
| 53 | try {
|
---|
| 54 | sourcePath = (String) parameters.get(0);
|
---|
| 55 | targetPath = (String) parameters.get(1);
|
---|
| 56 | } catch (Exception e) {
|
---|
[766] | 57 | throw new IllegalArgumentException();
|
---|
[573] | 58 | }
|
---|
| 59 |
|
---|
| 60 | File sourceFolder = new File(sourcePath);
|
---|
| 61 | if (!sourceFolder.isDirectory()) {
|
---|
| 62 | Console.printerrln(sourcePath + " is not a directory");
|
---|
| 63 | }
|
---|
| 64 | String absolutPathSource = sourceFolder.getAbsolutePath();
|
---|
| 65 | File targetFolder = new File(targetPath);
|
---|
| 66 | if (!targetFolder.isDirectory()) {
|
---|
| 67 | Console.printerrln(targetPath + " is not a directory");
|
---|
| 68 | }
|
---|
| 69 | String absolutPathTarget = targetFolder.getAbsolutePath();
|
---|
| 70 |
|
---|
| 71 | for (String filename : sourceFolder.list()) {
|
---|
| 72 | String source = absolutPathSource + "/" + filename;
|
---|
[639] | 73 | Console.traceln(Level.INFO, "Preprocessing file: " + source);
|
---|
[573] | 74 | File file = new File(source);
|
---|
| 75 | InputStreamReader reader;
|
---|
| 76 | try {
|
---|
| 77 | FileInputStream fis = new FileInputStream(file);
|
---|
| 78 | reader = new InputStreamReader(fis, "UTF-16");
|
---|
| 79 | } catch (FileNotFoundException e) {
|
---|
| 80 | Console.printerrln(e.getMessage());
|
---|
| 81 | return;
|
---|
| 82 | } catch (UnsupportedEncodingException e) {
|
---|
| 83 | Console.printerrln(e.getMessage());
|
---|
| 84 | return;
|
---|
| 85 | }
|
---|
| 86 | char[] buffer = new char[(int) file.length()];
|
---|
| 87 | try {
|
---|
| 88 | reader.read(buffer);
|
---|
| 89 | reader.close();
|
---|
| 90 | } catch (IOException e) {
|
---|
| 91 | Console.printerrln(e.getMessage());
|
---|
| 92 | return;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | String content = new String(buffer).trim();
|
---|
| 96 |
|
---|
| 97 | int index = filename.lastIndexOf('.');
|
---|
| 98 | String target = absolutPathTarget + "/"
|
---|
| 99 | + filename.substring(0, index) + ".xml";
|
---|
| 100 |
|
---|
[639] | 101 | Console.traceln(Level.INFO, " Saving as: " + target);
|
---|
[573] | 102 |
|
---|
| 103 | OutputStreamWriter writer;
|
---|
| 104 | try {
|
---|
| 105 | FileOutputStream fos = new FileOutputStream(target);
|
---|
| 106 | writer = new OutputStreamWriter(fos, "UTF-8");
|
---|
| 107 | } catch (IOException e) {
|
---|
| 108 | Console.printerrln(e.getMessage());
|
---|
| 109 | return;
|
---|
| 110 | }
|
---|
| 111 | try {
|
---|
| 112 | writer.write(content);
|
---|
| 113 | if (!content.endsWith("</sessions>")) {
|
---|
| 114 | writer.write("</sessions>");
|
---|
| 115 | }
|
---|
| 116 | writer.close();
|
---|
| 117 | } catch (IOException e) {
|
---|
| 118 | Console.printerrln(e.getMessage());
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /*
|
---|
| 124 | * (non-Javadoc)
|
---|
| 125 | *
|
---|
| 126 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 127 | */
|
---|
| 128 | @Override
|
---|
[664] | 129 | public String help() {
|
---|
[728] | 130 | return "preprocessDirJFC <sourceDirectory> <targetDirectory>";
|
---|
[573] | 131 | }
|
---|
| 132 |
|
---|
| 133 | }
|
---|