source: trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/commands/CMDpreprocessJFC.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 3.1 KB
Line 
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
15package de.ugoe.cs.autoquest.plugin.jfc.commands;
16
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.FileNotFoundException;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.InputStreamReader;
23import java.io.OutputStreamWriter;
24import java.io.UnsupportedEncodingException;
25import java.util.List;
26
27import de.ugoe.cs.util.console.Command;
28import de.ugoe.cs.util.console.Console;
29
30/**
31 * <p>
32 * Command to pre-process files written by EventBench's JFCMonitor. The only
33 * task of the pre-processing is checking if the session was closed properly,
34 * i.e., if the XML file ends with a {@code </sessions>} tag. If this is not the
35 * case, the tag will be appended to the file.
36 * </p>
37 *
38 * @author Steffen Herbold
39 * @version 1.0
40 */
41public class CMDpreprocessJFC implements Command {
42
43        /*
44         * (non-Javadoc)
45         *
46         * @see de.ugoe.cs.util.console.Command#run(java.util.List)
47         */
48        @Override
49        public void run(List<Object> parameters) {
50                String source;
51                String target;
52                try {
53                        source = (String) parameters.get(0);
54                        target = (String) parameters.get(1);
55                } catch (Exception e) {
56                        throw new IllegalArgumentException();
57                }
58
59                File file = new File(source);
60                InputStreamReader reader = null;
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                OutputStreamWriter writer;
83                try {
84                        FileOutputStream fos = new FileOutputStream(target);
85                        writer = new OutputStreamWriter(fos, "UTF-8");
86                } catch (IOException e) {
87                        Console.printerrln(e.getMessage());
88                        return;
89                }
90                try {
91                        writer.write(content);
92                        if (!content.endsWith("</sessions>")) {
93                                writer.write("</sessions>");
94                        }
95                        writer.close();
96                } catch (IOException e) {
97                        Console.printerrln(e.getMessage());
98                }
99        }
100
101        /*
102         * (non-Javadoc)
103         *
104         * @see de.ugoe.cs.util.console.Command#help()
105         */
106        @Override
107        public String help() {
108                return "preprocessJFC <sourceFile> <targetFile>";
109        }
110
111}
Note: See TracBrowser for help on using the repository browser.