[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 |
|
---|
[2233] | 71 | String[] children = sourceFolder.list();
|
---|
| 72 |
|
---|
| 73 | if (children != null) {
|
---|
| 74 | for (String filename : children) {
|
---|
| 75 | String source = absolutPathSource + "/" + filename;
|
---|
| 76 | Console.traceln(Level.INFO, "Preprocessing file: " + source);
|
---|
| 77 | File file = new File(source);
|
---|
| 78 | InputStreamReader reader;
|
---|
| 79 | try {
|
---|
| 80 | FileInputStream fis = new FileInputStream(file);
|
---|
| 81 | reader = new InputStreamReader(fis, "UTF-16");
|
---|
| 82 | } catch (FileNotFoundException e) {
|
---|
| 83 | Console.printerrln(e.getMessage());
|
---|
| 84 | return;
|
---|
| 85 | } catch (UnsupportedEncodingException e) {
|
---|
| 86 | Console.printerrln(e.getMessage());
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 | char[] buffer = new char[(int) file.length()];
|
---|
| 90 | try {
|
---|
| 91 | reader.read(buffer);
|
---|
| 92 | reader.close();
|
---|
| 93 | } catch (IOException e) {
|
---|
| 94 | Console.printerrln(e.getMessage());
|
---|
| 95 | return;
|
---|
| 96 | }
|
---|
[573] | 97 |
|
---|
[2233] | 98 | String content = new String(buffer).trim();
|
---|
[573] | 99 |
|
---|
[2233] | 100 | int index = filename.lastIndexOf('.');
|
---|
| 101 | String target = absolutPathTarget + "/"
|
---|
| 102 | + filename.substring(0, index) + ".xml";
|
---|
[573] | 103 |
|
---|
[2233] | 104 | Console.traceln(Level.INFO, " Saving as: " + target);
|
---|
[573] | 105 |
|
---|
[2233] | 106 | OutputStreamWriter writer;
|
---|
| 107 | try {
|
---|
| 108 | FileOutputStream fos = new FileOutputStream(target);
|
---|
| 109 | writer = new OutputStreamWriter(fos, "UTF-8");
|
---|
| 110 | } catch (IOException e) {
|
---|
| 111 | Console.printerrln(e.getMessage());
|
---|
| 112 | return;
|
---|
| 113 | }
|
---|
| 114 | try {
|
---|
| 115 | writer.write(content);
|
---|
| 116 | if (!content.endsWith("</sessions>")) {
|
---|
| 117 | writer.write("</sessions>");
|
---|
| 118 | }
|
---|
| 119 | writer.close();
|
---|
| 120 | } catch (IOException e) {
|
---|
| 121 | Console.printerrln(e.getMessage());
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[573] | 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /*
|
---|
| 128 | * (non-Javadoc)
|
---|
| 129 | *
|
---|
| 130 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 131 | */
|
---|
| 132 | @Override
|
---|
[664] | 133 | public String help() {
|
---|
[728] | 134 | return "preprocessDirJFC <sourceDirectory> <targetDirectory>";
|
---|
[573] | 135 | }
|
---|
| 136 |
|
---|
| 137 | }
|
---|