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

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