source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/jfc/commands/CMDpreprocessDirJFC.java @ 384

Last change on this file since 384 was 384, checked in by sherbold, 12 years ago
  • added tracing output command preprocessDirJFC
  • Property svn:mime-type set to text/plain
File size: 3.3 KB
Line 
1package de.ugoe.cs.eventbench.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;
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 directory. The only task
20 * of the pre-processing is checking if the session was closed properly, i.e.,
21 * if the XML file ends with a {@code </sessions>} tag. If this is not the case,
22 * 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 InvalidParameterException();
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("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 + "/" + filename.substring(0, index) + ".xml";
85                       
86                        Console.traceln("   Saving as: " + target);
87                       
88                        OutputStreamWriter writer;
89                        try {
90                                FileOutputStream fos = new FileOutputStream(target);
91                                writer = new OutputStreamWriter(fos, "UTF-16");
92                        } catch (IOException e) {
93                                Console.printerrln(e.getMessage());
94                                return;
95                        }
96                        try {
97                                writer.write(content);
98                                if (!content.endsWith("</sessions>")) {
99                                        writer.write("</sessions>");
100                                }
101                                writer.close();
102                        } catch (IOException e) {
103                                Console.printerrln(e.getMessage());
104                        }
105                }
106        }
107
108        /*
109         * (non-Javadoc)
110         *
111         * @see de.ugoe.cs.util.console.Command#help()
112         */
113        @Override
114        public void help() {
115                Console.println("Usage: preprocessDirJFC <sourcePath> <targetPath>");
116        }
117
118}
Note: See TracBrowser for help on using the repository browser.