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

Last change on this file since 2249 was 2233, checked in by pharms, 7 years ago
  • solved some findbugs issues
File size: 4.2 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;
26import java.util.logging.Level;
27
28import de.ugoe.cs.util.console.Command;
29import 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 */
42public 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) {
57                        throw new IllegalArgumentException();
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        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                        }
97
98                        String content = new String(buffer).trim();
99
100                        int index = filename.lastIndexOf('.');
101                        String target = absolutPathTarget + "/"
102                                        + filename.substring(0, index) + ".xml";
103
104                        Console.traceln(Level.INFO, "   Saving as: " + target);
105
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                }
124                }
125        }
126
127        /*
128         * (non-Javadoc)
129         *
130         * @see de.ugoe.cs.util.console.Command#help()
131         */
132        @Override
133        public String help() {
134                return "preprocessDirJFC <sourceDirectory> <targetDirectory>";
135        }
136
137}
Note: See TracBrowser for help on using the repository browser.