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 |
|
---|
15 | package de.ugoe.cs.autoquest.plugin.mfc;
|
---|
16 |
|
---|
17 | import java.io.File;
|
---|
18 | import java.io.FileNotFoundException;
|
---|
19 | import java.io.FileOutputStream;
|
---|
20 | import java.io.IOException;
|
---|
21 | import java.io.OutputStreamWriter;
|
---|
22 | import java.util.logging.Level;
|
---|
23 |
|
---|
24 | import org.apache.commons.codec.binary.Base64;
|
---|
25 |
|
---|
26 | import de.ugoe.cs.util.FileTools;
|
---|
27 | import de.ugoe.cs.util.StringTools;
|
---|
28 | import de.ugoe.cs.util.console.Console;
|
---|
29 |
|
---|
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 | */
|
---|
41 | public class LogPreprocessor {
|
---|
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 | */
|
---|
50 | private boolean sessionOpen = false;
|
---|
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 | */
|
---|
58 | private boolean msgIncomplete = false;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * <p>
|
---|
62 | * Flag that marks whether the log file is Base64 encoded.
|
---|
63 | * </p>
|
---|
64 | */
|
---|
65 | private boolean base64;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * <p>
|
---|
69 | * Constructor. Creates a new LogPreprocessor that does not decode Base64.
|
---|
70 | * </p>
|
---|
71 | */
|
---|
72 | public LogPreprocessor() {
|
---|
73 | this(false);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * <p>
|
---|
78 | * Constructor. Creates a new LogPreprocessor.
|
---|
79 | * </p>
|
---|
80 | *
|
---|
81 | * @param base64
|
---|
82 | * if true, Base64 will be decoded.
|
---|
83 | */
|
---|
84 | public LogPreprocessor(boolean base64) {
|
---|
85 | this.base64 = base64;
|
---|
86 | }
|
---|
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(
|
---|
106 | new FileOutputStream(target), "UTF-8");
|
---|
107 | targetFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
---|
108 | + StringTools.ENDLINE);
|
---|
109 | targetFile.write("<log>" + StringTools.ENDLINE);
|
---|
110 | processFile(source, targetFile);
|
---|
111 | if (sessionOpen) {
|
---|
112 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
---|
113 | }
|
---|
114 | targetFile.write("</log>");
|
---|
115 | targetFile.close();
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * <p>
|
---|
120 | * Pre-processes all files in a given source folder.
|
---|
121 | * </p>
|
---|
122 | *
|
---|
123 | * @param path
|
---|
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(
|
---|
136 | new FileOutputStream(target), "UTF-8");
|
---|
137 | targetFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
---|
138 | + StringTools.ENDLINE);
|
---|
139 | targetFile.write("<log>" + StringTools.ENDLINE);
|
---|
140 | File folder = new File(path);
|
---|
141 | if (!folder.isDirectory()) {
|
---|
142 | targetFile.close();
|
---|
143 | throw new IOException(path + " is not a directory");
|
---|
144 | }
|
---|
145 | String absolutPath = folder.getAbsolutePath();
|
---|
146 | for (String filename : folder.list()) {
|
---|
147 | String source = absolutPath + "/" + filename;
|
---|
148 | Console.traceln(Level.INFO, "Processing file: " + source);
|
---|
149 | processFile(source, targetFile);
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (sessionOpen) {
|
---|
153 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
---|
154 | }
|
---|
155 | targetFile.write("</log>");
|
---|
156 | targetFile.close();
|
---|
157 | }
|
---|
158 |
|
---|
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 | */
|
---|
174 | private void processFile(String source, OutputStreamWriter targetFile)
|
---|
175 | throws FileNotFoundException, IOException {
|
---|
176 | String[] lines = FileTools.getLinesFromFile(source, false);
|
---|
177 | String incompleteLine = "";
|
---|
178 | for (String currentLine : lines) {
|
---|
179 | if (currentLine.contains("UL: <session>")) {
|
---|
180 | if (sessionOpen) {
|
---|
181 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
---|
182 | targetFile.write(" <session>" + StringTools.ENDLINE);
|
---|
183 | } else {
|
---|
184 | targetFile.write(" <session>" + StringTools.ENDLINE);
|
---|
185 | sessionOpen = true;
|
---|
186 | }
|
---|
187 | } else if (currentLine.contains("UL: </session>")) {
|
---|
188 | if (sessionOpen) {
|
---|
189 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
---|
190 | sessionOpen = false;
|
---|
191 | }
|
---|
192 | } else if (msgIncomplete || currentLine.contains("UL: ")) {
|
---|
193 |
|
---|
194 | String currentContent;
|
---|
195 | String actualLine;
|
---|
196 | if (msgIncomplete) {
|
---|
197 | actualLine = currentLine;
|
---|
198 | } else {
|
---|
199 | String[] splitResult = currentLine.split("UL: ");
|
---|
200 | actualLine = splitResult[1];
|
---|
201 | }
|
---|
202 | if (base64) {
|
---|
203 | Base64 decoder = new Base64();
|
---|
204 | byte[] decoded = decoder.decode(actualLine);
|
---|
205 | currentContent = new String(decoded, "UTF-16LE");
|
---|
206 | if( currentContent.length()!=0 ) {
|
---|
207 | currentContent = currentContent.substring(0,
|
---|
208 | currentContent.length() - 1);
|
---|
209 | }
|
---|
210 | } else {
|
---|
211 | currentContent = actualLine;
|
---|
212 | }
|
---|
213 | if (msgIncomplete) {
|
---|
214 | incompleteLine += currentContent;
|
---|
215 | if (incompleteLine.contains("</msg>")) {
|
---|
216 | msgIncomplete = false;
|
---|
217 | targetFile.write(incompleteLine + StringTools.ENDLINE);
|
---|
218 | incompleteLine = "";
|
---|
219 | }
|
---|
220 | } else {
|
---|
221 | if (currentContent.contains("<msg") && sessionOpen) {
|
---|
222 | if (currentContent.contains("</msg>")) {
|
---|
223 | targetFile.write(" " + currentContent
|
---|
224 | + StringTools.ENDLINE);
|
---|
225 | } else {
|
---|
226 | msgIncomplete = true;
|
---|
227 | incompleteLine += currentContent;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | }
|
---|