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

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