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

Last change on this file since 664 was 664, checked in by sherbold, 12 years ago
  • modified Command.help(): the help now returns a string with its usage instead of writing directly to the console
  • modified listCommands: now writes the help string of each command instead of just the name; this way, the parameters are displayed directly
  • moved listCommands from quest-ui-core to java-utils
File size: 3.3 KB
Line 
1package de.ugoe.cs.quest.plugin.jfc.commands;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.io.OutputStreamWriter;
10import java.io.UnsupportedEncodingException;
11import java.security.InvalidParameterException;
12import java.util.List;
13import java.util.logging.Level;
14
15import de.ugoe.cs.util.console.Command;
16import de.ugoe.cs.util.console.Console;
17
18/**
19 * <p>
20 * Command to pre-process files written by EventBench's JFCMonitor located in a
21 * directory. The only task of the pre-processing is checking if the session was
22 * closed properly, i.e., if the XML file ends with a {@code </sessions>} tag.
23 * If this is not the case, the tag will be appended to the file.
24 * </p>
25 *
26 * @author Steffen Herbold
27 * @version 1.0
28 */
29public class CMDpreprocessDirJFC implements Command {
30
31        /*
32         * (non-Javadoc)
33         *
34         * @see de.ugoe.cs.util.console.Command#run(java.util.List)
35         */
36        @Override
37        public void run(List<Object> parameters) {
38                String sourcePath;
39                String targetPath;
40                try {
41                        sourcePath = (String) parameters.get(0);
42                        targetPath = (String) parameters.get(1);
43                } catch (Exception e) {
44                        throw new InvalidParameterException();
45                }
46
47                File sourceFolder = new File(sourcePath);
48                if (!sourceFolder.isDirectory()) {
49                        Console.printerrln(sourcePath + " is not a directory");
50                }
51                String absolutPathSource = sourceFolder.getAbsolutePath();
52                File targetFolder = new File(targetPath);
53                if (!targetFolder.isDirectory()) {
54                        Console.printerrln(targetPath + " is not a directory");
55                }
56                String absolutPathTarget = targetFolder.getAbsolutePath();
57
58                for (String filename : sourceFolder.list()) {
59                        String source = absolutPathSource + "/" + filename;
60                        Console.traceln(Level.INFO, "Preprocessing file: " + source);
61                        File file = new File(source);
62                        InputStreamReader reader;
63                        try {
64                                FileInputStream fis = new FileInputStream(file);
65                                reader = new InputStreamReader(fis, "UTF-16");
66                        } catch (FileNotFoundException e) {
67                                Console.printerrln(e.getMessage());
68                                return;
69                        } catch (UnsupportedEncodingException e) {
70                                Console.printerrln(e.getMessage());
71                                return;
72                        }
73                        char[] buffer = new char[(int) file.length()];
74                        try {
75                                reader.read(buffer);
76                                reader.close();
77                        } catch (IOException e) {
78                                Console.printerrln(e.getMessage());
79                                return;
80                        }
81
82                        String content = new String(buffer).trim();
83
84                        int index = filename.lastIndexOf('.');
85                        String target = absolutPathTarget + "/"
86                                        + filename.substring(0, index) + ".xml";
87
88                        Console.traceln(Level.INFO, "   Saving as: " + target);
89
90                        OutputStreamWriter writer;
91                        try {
92                                FileOutputStream fos = new FileOutputStream(target);
93                                writer = new OutputStreamWriter(fos, "UTF-8");
94                        } catch (IOException e) {
95                                Console.printerrln(e.getMessage());
96                                return;
97                        }
98                        try {
99                                writer.write(content);
100                                if (!content.endsWith("</sessions>")) {
101                                        writer.write("</sessions>");
102                                }
103                                writer.close();
104                        } catch (IOException e) {
105                                Console.printerrln(e.getMessage());
106                        }
107                }
108        }
109
110        /*
111         * (non-Javadoc)
112         *
113         * @see de.ugoe.cs.util.console.Command#help()
114         */
115        @Override
116        public String help() {
117                return "preprocessDirJFC <sourcePath> <targetPath>";
118        }
119
120}
Note: See TracBrowser for help on using the repository browser.