source: trunk/quest-plugin-mfc/src/main/java/de/ugoe/cs/quest/plugin/mfc/LogPreprocessor.java @ 832

Last change on this file since 832 was 641, checked in by sherbold, 12 years ago
  • fixed some warnings
File size: 6.4 KB
RevLine 
[434]1package de.ugoe.cs.quest.plugin.mfc;
[1]2
3import java.io.File;
4import java.io.FileNotFoundException;
[44]5import java.io.FileOutputStream;
[1]6import java.io.IOException;
[44]7import java.io.OutputStreamWriter;
[639]8import java.util.logging.Level;
[1]9
10import org.apache.commons.codec.binary.Base64;
11
[74]12import de.ugoe.cs.util.FileTools;
[1]13import de.ugoe.cs.util.StringTools;
14import de.ugoe.cs.util.console.Console;
15
[171]16/**
17 * <p>
18 * Pre-processes log files generated by the EventBench's MFCUsageMonitor. It
19 * decodes Base64 encoding into UTF-16. It removes all lines of the log file,
20 * that do not start with the prefix "UL:", end everything before the prefix and
21 * the prefix itself.
22 * </p>
23 *
24 * @author Steffen Herbold
25 * @version 1.0
26 */
[1]27public class LogPreprocessor {
[171]28
29        /**
30         * <p>
31         * Internal flag that monitors whether there is an open session-node in the
32         * XML file to ensure that there is a closing session-node for each opening
33         * session node and, thereby, ensure that the XML file is well formed.
34         * </p>
35         */
[1]36        private boolean sessionOpen = false;
[171]37
38        /**
39         * <p>
40         * Internal flag that monitors whether a message node is longer than one
41         * line, as the prefix handling is different in this case.
42         * </p>
43         */
[1]44        private boolean msgIncomplete = false;
[171]45
46        /**
47         * <p>
48         * Flag that marks whether the log file is Base64 encoded.
49         * </p>
50         */
[1]51        private boolean base64;
[171]52
53        /**
54         * <p>
55         * Constructor. Creates a new LogPreprocessor that does not decode Base64.
56         * </p>
57         */
[1]58        public LogPreprocessor() {
59                this(false);
60        }
[171]61
62        /**
63         * <p>
64         * Constructor. Creates a new LogPreprocessor.
65         * </p>
66         *
67         * @param base64
68         *            if true, Base64 will be decoded.
69         */
[1]70        public LogPreprocessor(boolean base64) {
71                this.base64 = base64;
72        }
[171]73
74        /**
75         * <p>
76         * Pre-processes a single log file.
77         * </p>
78         *
79         * @param source
80         *            name and path of the source file
81         * @param target
82         *            name and path of the target file
83         * @throws IOException
84         *             thrown if there is a problem with reading from or writing to
85         *             the source, respectively target file
86         * @throws FileNotFoundException
87         *             thrown if the source file is not found
88         */
89        public void convertToXml(String source, String target) throws IOException,
90                        FileNotFoundException {
91                OutputStreamWriter targetFile = new OutputStreamWriter(
[418]92                                new FileOutputStream(target), "UTF-8");
93                targetFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
[171]94                                + StringTools.ENDLINE);
[1]95                targetFile.write("<log>" + StringTools.ENDLINE);
96                processFile(source, targetFile);
[171]97                if (sessionOpen) {
[1]98                        targetFile.write(" </session>" + StringTools.ENDLINE);
99                }
100                targetFile.write("</log>");
101                targetFile.close();
102        }
[171]103
104        /**
105         * <p>
106         * Pre-processes all files in a given source folder.
107         * </p>
108         *
[223]109         * @param path
[171]110         *            path of the source folder
111         * @param target
112         *            name and path of the target file
113         * @throws IOException
114         *             thrown if there is a problem with reading from or writing to
115         *             the source, respectively target file
116         * @throws FileNotFoundException
117         *             thrown if the source file is not found
118         */
119        public void convertDirToXml(String path, String target) throws IOException,
120                        FileNotFoundException {
121                OutputStreamWriter targetFile = new OutputStreamWriter(
[418]122                                new FileOutputStream(target), "UTF-8");
123                targetFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
[171]124                                + StringTools.ENDLINE);
[1]125                targetFile.write("<log>" + StringTools.ENDLINE);
126                File folder = new File(path);
[171]127                if (!folder.isDirectory()) {
[641]128                    targetFile.close();
[1]129                        throw new IOException(path + " is not a directory");
130                }
131                String absolutPath = folder.getAbsolutePath();
[171]132                for (String filename : folder.list()) {
[1]133                        String source = absolutPath + "/" + filename;
[639]134                        Console.traceln(Level.INFO, "Processing file: " + source);
[1]135                        processFile(source, targetFile);
136                }
[171]137
138                if (sessionOpen) {
[1]139                        targetFile.write(" </session>" + StringTools.ENDLINE);
140                }
141                targetFile.write("</log>");
142                targetFile.close();
143        }
144
[171]145        /**
146         * <p>
147         * Internal function that pre-processes a log file.
148         * </p>
149         *
150         * @param source
151         *            name and path of the source file
152         * @param target
153         *            name and path of the target file
154         * @throws IOException
155         *             thrown if there is a problem with reading from or writing to
156         *             the source, respectively target file
157         * @throws FileNotFoundException
158         *             thrown if the source file is not found
159         */
[44]160        private void processFile(String source, OutputStreamWriter targetFile)
[1]161                        throws FileNotFoundException, IOException {
[74]162                String[] lines = FileTools.getLinesFromFile(source, false);
[1]163                String incompleteLine = "";
[171]164                for (String currentLine : lines) {
165                        if (currentLine.contains("UL: <session>")) {
166                                if (sessionOpen) {
[1]167                                        targetFile.write(" </session>" + StringTools.ENDLINE);
168                                        targetFile.write(" <session>" + StringTools.ENDLINE);
169                                } else {
170                                        targetFile.write(" <session>" + StringTools.ENDLINE);
171                                        sessionOpen = true;
172                                }
[171]173                        } else if (currentLine.contains("UL: </session>")) {
174                                if (sessionOpen) {
[1]175                                        targetFile.write(" </session>" + StringTools.ENDLINE);
176                                        sessionOpen = false;
177                                }
[171]178                        } else if (msgIncomplete || currentLine.contains("UL: ")) {
179
[1]180                                String currentContent;
181                                String actualLine;
[171]182                                if (msgIncomplete) {
[1]183                                        actualLine = currentLine;
184                                } else {
185                                        String[] splitResult = currentLine.split("UL: ");
186                                        actualLine = splitResult[1];
187                                }
[171]188                                if (base64) {
[1]189                                        Base64 decoder = new Base64();
190                                        byte[] decoded = decoder.decode(actualLine);
191                                        currentContent = new String(decoded, "UTF-16LE");
[403]192                                        if( currentContent.length()!=0 ) {
193                                                currentContent = currentContent.substring(0,
194                                                                currentContent.length() - 1);
195                                        }
[1]196                                } else {
197                                        currentContent = actualLine;
198                                }
[171]199                                if (msgIncomplete) {
[1]200                                        incompleteLine += currentContent;
[171]201                                        if (incompleteLine.contains("</msg>")) {
[1]202                                                msgIncomplete = false;
203                                                targetFile.write(incompleteLine + StringTools.ENDLINE);
204                                                incompleteLine = "";
205                                        }
206                                } else {
[171]207                                        if (currentContent.contains("<msg") && sessionOpen) {
208                                                if (currentContent.contains("</msg>")) {
209                                                        targetFile.write("  " + currentContent
210                                                                        + StringTools.ENDLINE);
[1]211                                                } else {
212                                                        msgIncomplete = true;
213                                                        incompleteLine += currentContent;
214                                                }
215                                        }
216                                }
217                        }
218                }
219        }
220
221}
Note: See TracBrowser for help on using the repository browser.