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 path
|
---|
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 | for (String currentLine : lines) {
|
---|
163 | if (currentLine.contains("UL: <session>")) {
|
---|
164 | if (sessionOpen) {
|
---|
165 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
---|
166 | targetFile.write(" <session>" + StringTools.ENDLINE);
|
---|
167 | } else {
|
---|
168 | targetFile.write(" <session>" + StringTools.ENDLINE);
|
---|
169 | sessionOpen = true;
|
---|
170 | }
|
---|
171 | } else if (currentLine.contains("UL: </session>")) {
|
---|
172 | if (sessionOpen) {
|
---|
173 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
---|
174 | sessionOpen = false;
|
---|
175 | }
|
---|
176 | } else if (msgIncomplete || currentLine.contains("UL: ")) {
|
---|
177 |
|
---|
178 | String currentContent;
|
---|
179 | String actualLine;
|
---|
180 | if (msgIncomplete) {
|
---|
181 | actualLine = currentLine;
|
---|
182 | } else {
|
---|
183 | String[] splitResult = currentLine.split("UL: ");
|
---|
184 | actualLine = splitResult[1];
|
---|
185 | }
|
---|
186 | if (base64) {
|
---|
187 | Base64 decoder = new Base64();
|
---|
188 | byte[] decoded = decoder.decode(actualLine);
|
---|
189 | currentContent = new String(decoded, "UTF-16LE");
|
---|
190 | if( currentContent.length()!=0 ) {
|
---|
191 | currentContent = currentContent.substring(0,
|
---|
192 | currentContent.length() - 1);
|
---|
193 | }
|
---|
194 | } else {
|
---|
195 | currentContent = actualLine;
|
---|
196 | }
|
---|
197 | if (msgIncomplete) {
|
---|
198 | incompleteLine += currentContent;
|
---|
199 | if (incompleteLine.contains("</msg>")) {
|
---|
200 | msgIncomplete = false;
|
---|
201 | targetFile.write(incompleteLine + StringTools.ENDLINE);
|
---|
202 | incompleteLine = "";
|
---|
203 | }
|
---|
204 | } else {
|
---|
205 | if (currentContent.contains("<msg") && sessionOpen) {
|
---|
206 | if (currentContent.contains("</msg>")) {
|
---|
207 | targetFile.write(" " + currentContent
|
---|
208 | + StringTools.ENDLINE);
|
---|
209 | } else {
|
---|
210 | msgIncomplete = true;
|
---|
211 | incompleteLine += currentContent;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | }
|
---|