- Timestamp:
- 09/09/11 06:23:36 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/windows/LogPreprocessor.java
r75 r171 13 13 import de.ugoe.cs.util.console.Console; 14 14 15 /** 16 * <p> 17 * Pre-processes log files generated by the EventBench's MFCUsageMonitor. It 18 * decodes Base64 encoding into UTF-16. It removes all lines of the log file, 19 * that do not start with the prefix "UL:", end everything before the prefix and 20 * the prefix itself. 21 * </p> 22 * 23 * @author Steffen Herbold 24 * @version 1.0 25 */ 15 26 public class LogPreprocessor { 16 27 28 /** 29 * <p> 30 * Internal flag that monitors whether there is an open session-node in the 31 * XML file to ensure that there is a closing session-node for each opening 32 * session node and, thereby, ensure that the XML file is well formed. 33 * </p> 34 */ 17 35 private boolean sessionOpen = false; 36 37 /** 38 * <p> 39 * Internal flag that monitors whether a message node is longer than one 40 * line, as the prefix handling is different in this case. 41 * </p> 42 */ 18 43 private boolean msgIncomplete = false; 19 44 45 /** 46 * <p> 47 * Flag that marks whether the log file is Base64 encoded. 48 * </p> 49 */ 20 50 private boolean base64; 21 51 52 /** 53 * <p> 54 * Constructor. Creates a new LogPreprocessor that does not decode Base64. 55 * </p> 56 */ 22 57 public LogPreprocessor() { 23 58 this(false); 24 59 } 25 60 61 /** 62 * <p> 63 * Constructor. Creates a new LogPreprocessor. 64 * </p> 65 * 66 * @param base64 67 * if true, Base64 will be decoded. 68 */ 26 69 public LogPreprocessor(boolean base64) { 27 70 this.base64 = base64; 28 71 } 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); 72 73 /** 74 * <p> 75 * Pre-processes a single log file. 76 * </p> 77 * 78 * @param source 79 * name and path of the source file 80 * @param target 81 * name and path of the target file 82 * @throws IOException 83 * thrown if there is a problem with reading from or writing to 84 * the source, respectively target file 85 * @throws FileNotFoundException 86 * thrown if the source file is not found 87 */ 88 public void convertToXml(String source, String target) throws IOException, 89 FileNotFoundException { 90 OutputStreamWriter targetFile = new OutputStreamWriter( 91 new FileOutputStream(target), "UTF-16"); 92 targetFile.write("<?xml version=\"1.0\" encoding=\"UTF-16\"?>" 93 + StringTools.ENDLINE); 33 94 targetFile.write("<log>" + StringTools.ENDLINE); 34 95 processFile(source, targetFile); 35 if ( sessionOpen) {96 if (sessionOpen) { 36 97 targetFile.write(" </session>" + StringTools.ENDLINE); 37 98 } … … 39 100 targetFile.close(); 40 101 } 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); 102 103 /** 104 * <p> 105 * Pre-processes all files in a given source folder. 106 * </p> 107 * 108 * @param source 109 * path of the source folder 110 * @param target 111 * name and path of the target file 112 * @throws IOException 113 * thrown if there is a problem with reading from or writing to 114 * the source, respectively target file 115 * @throws FileNotFoundException 116 * thrown if the source file is not found 117 */ 118 public void convertDirToXml(String path, String target) throws IOException, 119 FileNotFoundException { 120 OutputStreamWriter targetFile = new OutputStreamWriter( 121 new FileOutputStream(target), "UTF-16"); 122 targetFile.write("<?xml version=\"1.0\" encoding=\"UTF-16\"?>" 123 + StringTools.ENDLINE); 46 124 targetFile.write("<log>" + StringTools.ENDLINE); 47 125 File folder = new File(path); 48 if ( !folder.isDirectory()) {126 if (!folder.isDirectory()) { 49 127 throw new IOException(path + " is not a directory"); 50 128 } 51 129 String absolutPath = folder.getAbsolutePath(); 52 for ( String filename : folder.list()) {130 for (String filename : folder.list()) { 53 131 String source = absolutPath + "/" + filename; 54 132 Console.traceln("Processing file: " + source); 55 133 processFile(source, targetFile); 56 134 } 57 58 if ( sessionOpen) {135 136 if (sessionOpen) { 59 137 targetFile.write(" </session>" + StringTools.ENDLINE); 60 138 } … … 63 141 } 64 142 143 /** 144 * <p> 145 * Internal function that pre-processes a log file. 146 * </p> 147 * 148 * @param source 149 * name and path of the source file 150 * @param target 151 * name and path of the target file 152 * @throws IOException 153 * thrown if there is a problem with reading from or writing to 154 * the source, respectively target file 155 * @throws FileNotFoundException 156 * thrown if the source file is not found 157 */ 65 158 private void processFile(String source, OutputStreamWriter targetFile) 66 159 throws FileNotFoundException, IOException { … … 68 161 String incompleteLine = ""; 69 162 // Open source and read line by line 70 for ( String currentLine : lines) {71 if (currentLine.contains("UL: <session>")) {72 if (sessionOpen) {163 for (String currentLine : lines) { 164 if (currentLine.contains("UL: <session>")) { 165 if (sessionOpen) { 73 166 targetFile.write(" </session>" + StringTools.ENDLINE); 74 167 targetFile.write(" <session>" + StringTools.ENDLINE); … … 77 170 sessionOpen = true; 78 171 } 79 } else if (currentLine.contains("UL: </session>")) {80 if (sessionOpen) {172 } else if (currentLine.contains("UL: </session>")) { 173 if (sessionOpen) { 81 174 targetFile.write(" </session>" + StringTools.ENDLINE); 82 175 sessionOpen = false; 83 176 } 84 } else if (msgIncomplete || currentLine.contains("UL: ")) {85 177 } else if (msgIncomplete || currentLine.contains("UL: ")) { 178 86 179 String currentContent; 87 180 String actualLine; 88 if ( msgIncomplete) {181 if (msgIncomplete) { 89 182 actualLine = currentLine; 90 183 } else { … … 92 185 actualLine = splitResult[1]; 93 186 } 94 if ( base64) {187 if (base64) { 95 188 Base64 decoder = new Base64(); 96 189 byte[] decoded = decoder.decode(actualLine); 97 190 currentContent = new String(decoded, "UTF-16LE"); 98 currentContent = currentContent.substring(0, currentContent.length()-1); 191 currentContent = currentContent.substring(0, 192 currentContent.length() - 1); 99 193 } else { 100 194 currentContent = actualLine; 101 195 } 102 if ( msgIncomplete) {196 if (msgIncomplete) { 103 197 incompleteLine += currentContent; 104 if ( incompleteLine.contains("</msg>")) {198 if (incompleteLine.contains("</msg>")) { 105 199 msgIncomplete = false; 106 200 targetFile.write(incompleteLine + StringTools.ENDLINE); … … 108 202 } 109 203 } else { 110 if( currentContent.contains("<msg") && sessionOpen ) { 111 if( currentContent.contains("</msg>") ) { 112 targetFile.write(" " + currentContent + StringTools.ENDLINE); 204 if (currentContent.contains("<msg") && sessionOpen) { 205 if (currentContent.contains("</msg>")) { 206 targetFile.write(" " + currentContent 207 + StringTools.ENDLINE); 113 208 } else { 114 209 msgIncomplete = true;
Note: See TracChangeset
for help on using the changeset viewer.