source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/windows/LogPreprocessor.java @ 74

Last change on this file since 74 was 74, checked in by sherbold, 13 years ago
  • refactoring
File size: 3.9 KB
Line 
1package de.ugoe.cs.eventbench.windows;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.FileReader;
7import java.io.IOException;
8import java.io.OutputStreamWriter;
9
10import org.apache.commons.codec.binary.Base64;
11
12import de.ugoe.cs.util.FileTools;
13import de.ugoe.cs.util.StringTools;
14import de.ugoe.cs.util.console.Console;
15
16public class LogPreprocessor {
17       
18        private boolean sessionOpen = false;
19        private boolean msgIncomplete = false;
20       
21        private boolean base64;
22       
23        public LogPreprocessor() {
24                this(false);
25        }
26       
27        public LogPreprocessor(boolean base64) {
28                this.base64 = base64;
29        }
30       
31        public void convertToXml(String source, String target) throws IOException, FileNotFoundException {
32                OutputStreamWriter targetFile = new OutputStreamWriter(new FileOutputStream(target), "UTF-16");
33                targetFile.write("<?xml version=\"1.0\" encoding=\"UTF-16\"?>" + StringTools.ENDLINE);
34                targetFile.write("<log>" + StringTools.ENDLINE);
35                processFile(source, targetFile);
36                if( sessionOpen ) {
37                        targetFile.write(" </session>" + StringTools.ENDLINE);
38                }
39                targetFile.write("</log>");
40                targetFile.close();
41        }
42       
43       
44        public void convertDirToXml(String path, String target) throws IOException, FileNotFoundException {
45                OutputStreamWriter targetFile = new OutputStreamWriter(new FileOutputStream(target), "UTF-16");
46                targetFile.write("<?xml version=\"1.0\" encoding=\"UTF-16\"?>" + StringTools.ENDLINE);
47                targetFile.write("<log>" + StringTools.ENDLINE);
48                File folder = new File(path);
49                if( !folder.isDirectory() ) {
50                        throw new IOException(path + " is not a directory");
51                }
52                String absolutPath = folder.getAbsolutePath();
53                for( String filename : folder.list() ) {
54                        String source = absolutPath + "/" + filename;
55                        Console.traceln("Processing file: " + source);
56                        processFile(source, targetFile);
57                }
58               
59                if( sessionOpen ) {
60                        targetFile.write(" </session>" + StringTools.ENDLINE);
61                }
62                targetFile.write("</log>");
63                targetFile.close();
64        }
65
66        private void processFile(String source, OutputStreamWriter targetFile)
67                        throws FileNotFoundException, IOException {
68                String[] lines = FileTools.getLinesFromFile(source, false);
69                String incompleteLine = "";
70                // Open source and read line by line
71                for( String currentLine : lines ) {
72                        if( currentLine.contains("UL: <session>")) {
73                                if( sessionOpen) {
74                                        targetFile.write(" </session>" + StringTools.ENDLINE);
75                                        targetFile.write(" <session>" + StringTools.ENDLINE);
76                                } else {
77                                        targetFile.write(" <session>" + StringTools.ENDLINE);
78                                        sessionOpen = true;
79                                }
80                        } else if( currentLine.contains("UL: </session>")) {
81                                if( sessionOpen) {
82                                        targetFile.write(" </session>" + StringTools.ENDLINE);
83                                        sessionOpen = false;
84                                }
85                        } else if( msgIncomplete || currentLine.contains("UL: ")) {
86                               
87                                String currentContent;
88                                String actualLine;
89                                if( msgIncomplete ) {
90                                        actualLine = currentLine;
91                                } else {
92                                        String[] splitResult = currentLine.split("UL: ");
93                                        actualLine = splitResult[1];
94                                }
95                                if( base64 ) {
96                                        Base64 decoder = new Base64();
97                                        byte[] decoded = decoder.decode(actualLine);
98                                        currentContent = new String(decoded, "UTF-16LE");
99                                        currentContent = currentContent.substring(0, currentContent.length()-1);
100                                } else {
101                                        currentContent = actualLine;
102                                }
103                                if( msgIncomplete ) {
104                                        incompleteLine += currentContent;
105                                        if( incompleteLine.contains("</msg>") ) {
106                                                msgIncomplete = false;
107                                                targetFile.write(incompleteLine + StringTools.ENDLINE);
108                                                incompleteLine = "";
109                                        }
110                                } else {
111                                        if( currentContent.contains("<msg") && sessionOpen ) {
112                                                if( currentContent.contains("</msg>") ) {
113                                                        targetFile.write("  " + currentContent + StringTools.ENDLINE);
114                                                } else {
115                                                        msgIncomplete = true;
116                                                        incompleteLine += currentContent;
117                                                }
118                                        }
119                                }
120                        }
121                }
122        }
123
124}
Note: See TracBrowser for help on using the repository browser.