1 | package de.ugoe.cs.quest.plugin.mfc;
|
---|
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 | import java.util.logging.Level;
|
---|
9 |
|
---|
10 | import org.apache.commons.codec.binary.Base64;
|
---|
11 |
|
---|
12 | import de.ugoe.cs.util.FileTools;
|
---|
13 | import de.ugoe.cs.util.StringTools;
|
---|
14 | import de.ugoe.cs.util.console.Console;
|
---|
15 |
|
---|
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 | */
|
---|
27 | public class LogPreprocessor {
|
---|
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 | */
|
---|
36 | private boolean sessionOpen = false;
|
---|
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 | */
|
---|
44 | private boolean msgIncomplete = false;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * <p>
|
---|
48 | * Flag that marks whether the log file is Base64 encoded.
|
---|
49 | * </p>
|
---|
50 | */
|
---|
51 | private boolean base64;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * <p>
|
---|
55 | * Constructor. Creates a new LogPreprocessor that does not decode Base64.
|
---|
56 | * </p>
|
---|
57 | */
|
---|
58 | public LogPreprocessor() {
|
---|
59 | this(false);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * <p>
|
---|
64 | * Constructor. Creates a new LogPreprocessor.
|
---|
65 | * </p>
|
---|
66 | *
|
---|
67 | * @param base64
|
---|
68 | * if true, Base64 will be decoded.
|
---|
69 | */
|
---|
70 | public LogPreprocessor(boolean base64) {
|
---|
71 | this.base64 = base64;
|
---|
72 | }
|
---|
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(
|
---|
92 | new FileOutputStream(target), "UTF-8");
|
---|
93 | targetFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
---|
94 | + StringTools.ENDLINE);
|
---|
95 | targetFile.write("<log>" + StringTools.ENDLINE);
|
---|
96 | processFile(source, targetFile);
|
---|
97 | if (sessionOpen) {
|
---|
98 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
---|
99 | }
|
---|
100 | targetFile.write("</log>");
|
---|
101 | targetFile.close();
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * <p>
|
---|
106 | * Pre-processes all files in a given source folder.
|
---|
107 | * </p>
|
---|
108 | *
|
---|
109 | * @param path
|
---|
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(
|
---|
122 | new FileOutputStream(target), "UTF-8");
|
---|
123 | targetFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
---|
124 | + StringTools.ENDLINE);
|
---|
125 | targetFile.write("<log>" + StringTools.ENDLINE);
|
---|
126 | File folder = new File(path);
|
---|
127 | if (!folder.isDirectory()) {
|
---|
128 | targetFile.close();
|
---|
129 | throw new IOException(path + " is not a directory");
|
---|
130 | }
|
---|
131 | String absolutPath = folder.getAbsolutePath();
|
---|
132 | for (String filename : folder.list()) {
|
---|
133 | String source = absolutPath + "/" + filename;
|
---|
134 | Console.traceln(Level.INFO, "Processing file: " + source);
|
---|
135 | processFile(source, targetFile);
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (sessionOpen) {
|
---|
139 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
---|
140 | }
|
---|
141 | targetFile.write("</log>");
|
---|
142 | targetFile.close();
|
---|
143 | }
|
---|
144 |
|
---|
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 | */
|
---|
160 | private void processFile(String source, OutputStreamWriter targetFile)
|
---|
161 | throws FileNotFoundException, IOException {
|
---|
162 | String[] lines = FileTools.getLinesFromFile(source, false);
|
---|
163 | String incompleteLine = "";
|
---|
164 | for (String currentLine : lines) {
|
---|
165 | if (currentLine.contains("UL: <session>")) {
|
---|
166 | if (sessionOpen) {
|
---|
167 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
---|
168 | targetFile.write(" <session>" + StringTools.ENDLINE);
|
---|
169 | } else {
|
---|
170 | targetFile.write(" <session>" + StringTools.ENDLINE);
|
---|
171 | sessionOpen = true;
|
---|
172 | }
|
---|
173 | } else if (currentLine.contains("UL: </session>")) {
|
---|
174 | if (sessionOpen) {
|
---|
175 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
---|
176 | sessionOpen = false;
|
---|
177 | }
|
---|
178 | } else if (msgIncomplete || currentLine.contains("UL: ")) {
|
---|
179 |
|
---|
180 | String currentContent;
|
---|
181 | String actualLine;
|
---|
182 | if (msgIncomplete) {
|
---|
183 | actualLine = currentLine;
|
---|
184 | } else {
|
---|
185 | String[] splitResult = currentLine.split("UL: ");
|
---|
186 | actualLine = splitResult[1];
|
---|
187 | }
|
---|
188 | if (base64) {
|
---|
189 | Base64 decoder = new Base64();
|
---|
190 | byte[] decoded = decoder.decode(actualLine);
|
---|
191 | currentContent = new String(decoded, "UTF-16LE");
|
---|
192 | if( currentContent.length()!=0 ) {
|
---|
193 | currentContent = currentContent.substring(0,
|
---|
194 | currentContent.length() - 1);
|
---|
195 | }
|
---|
196 | } else {
|
---|
197 | currentContent = actualLine;
|
---|
198 | }
|
---|
199 | if (msgIncomplete) {
|
---|
200 | incompleteLine += currentContent;
|
---|
201 | if (incompleteLine.contains("</msg>")) {
|
---|
202 | msgIncomplete = false;
|
---|
203 | targetFile.write(incompleteLine + StringTools.ENDLINE);
|
---|
204 | incompleteLine = "";
|
---|
205 | }
|
---|
206 | } else {
|
---|
207 | if (currentContent.contains("<msg") && sessionOpen) {
|
---|
208 | if (currentContent.contains("</msg>")) {
|
---|
209 | targetFile.write(" " + currentContent
|
---|
210 | + StringTools.ENDLINE);
|
---|
211 | } else {
|
---|
212 | msgIncomplete = true;
|
---|
213 | incompleteLine += currentContent;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | }
|
---|