| 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 | 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 | String[] lines = FileTools.getLinesFromFile(source, false);
|
|---|
| 68 | String incompleteLine = "";
|
|---|
| 69 | // Open source and read line by line
|
|---|
| 70 | for( String currentLine : lines ) {
|
|---|
| 71 | if( currentLine.contains("UL: <session>")) {
|
|---|
| 72 | if( sessionOpen) {
|
|---|
| 73 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
|---|
| 74 | targetFile.write(" <session>" + StringTools.ENDLINE);
|
|---|
| 75 | } else {
|
|---|
| 76 | targetFile.write(" <session>" + StringTools.ENDLINE);
|
|---|
| 77 | sessionOpen = true;
|
|---|
| 78 | }
|
|---|
| 79 | } else if( currentLine.contains("UL: </session>")) {
|
|---|
| 80 | if( sessionOpen) {
|
|---|
| 81 | targetFile.write(" </session>" + StringTools.ENDLINE);
|
|---|
| 82 | sessionOpen = false;
|
|---|
| 83 | }
|
|---|
| 84 | } else if( msgIncomplete || currentLine.contains("UL: ")) {
|
|---|
| 85 |
|
|---|
| 86 | String currentContent;
|
|---|
| 87 | String actualLine;
|
|---|
| 88 | if( msgIncomplete ) {
|
|---|
| 89 | actualLine = currentLine;
|
|---|
| 90 | } else {
|
|---|
| 91 | String[] splitResult = currentLine.split("UL: ");
|
|---|
| 92 | actualLine = splitResult[1];
|
|---|
| 93 | }
|
|---|
| 94 | if( base64 ) {
|
|---|
| 95 | Base64 decoder = new Base64();
|
|---|
| 96 | byte[] decoded = decoder.decode(actualLine);
|
|---|
| 97 | currentContent = new String(decoded, "UTF-16LE");
|
|---|
| 98 | currentContent = currentContent.substring(0, currentContent.length()-1);
|
|---|
| 99 | } else {
|
|---|
| 100 | currentContent = actualLine;
|
|---|
| 101 | }
|
|---|
| 102 | if( msgIncomplete ) {
|
|---|
| 103 | incompleteLine += currentContent;
|
|---|
| 104 | if( incompleteLine.contains("</msg>") ) {
|
|---|
| 105 | msgIncomplete = false;
|
|---|
| 106 | targetFile.write(incompleteLine + StringTools.ENDLINE);
|
|---|
| 107 | incompleteLine = "";
|
|---|
| 108 | }
|
|---|
| 109 | } else {
|
|---|
| 110 | if( currentContent.contains("<msg") && sessionOpen ) {
|
|---|
| 111 | if( currentContent.contains("</msg>") ) {
|
|---|
| 112 | targetFile.write(" " + currentContent + StringTools.ENDLINE);
|
|---|
| 113 | } else {
|
|---|
| 114 | msgIncomplete = true;
|
|---|
| 115 | incompleteLine += currentContent;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | }
|
|---|