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