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

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