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