Ignore:
Timestamp:
09/09/11 06:23:36 (13 years ago)
Author:
sherbold
Message:
  • code documentation and formatting
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/windows/LogPreprocessor.java

    r75 r171  
    1313import de.ugoe.cs.util.console.Console; 
    1414 
     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 */ 
    1526public 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         */ 
    1735        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         */ 
    1843        private boolean msgIncomplete = false; 
    19          
     44 
     45        /** 
     46         * <p> 
     47         * Flag that marks whether the log file is Base64 encoded. 
     48         * </p> 
     49         */ 
    2050        private boolean base64; 
    21          
     51 
     52        /** 
     53         * <p> 
     54         * Constructor. Creates a new LogPreprocessor that does not decode Base64. 
     55         * </p> 
     56         */ 
    2257        public LogPreprocessor() { 
    2358                this(false); 
    2459        } 
    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         */ 
    2669        public LogPreprocessor(boolean base64) { 
    2770                this.base64 = base64; 
    2871        } 
    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); 
    3394                targetFile.write("<log>" + StringTools.ENDLINE); 
    3495                processFile(source, targetFile); 
    35                 if( sessionOpen ) { 
     96                if (sessionOpen) { 
    3697                        targetFile.write(" </session>" + StringTools.ENDLINE); 
    3798                } 
     
    39100                targetFile.close(); 
    40101        } 
    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); 
    46124                targetFile.write("<log>" + StringTools.ENDLINE); 
    47125                File folder = new File(path); 
    48                 if( !folder.isDirectory() ) { 
     126                if (!folder.isDirectory()) { 
    49127                        throw new IOException(path + " is not a directory"); 
    50128                } 
    51129                String absolutPath = folder.getAbsolutePath(); 
    52                 for( String filename : folder.list() ) { 
     130                for (String filename : folder.list()) { 
    53131                        String source = absolutPath + "/" + filename; 
    54132                        Console.traceln("Processing file: " + source); 
    55133                        processFile(source, targetFile); 
    56134                } 
    57                  
    58                 if( sessionOpen ) { 
     135 
     136                if (sessionOpen) { 
    59137                        targetFile.write(" </session>" + StringTools.ENDLINE); 
    60138                } 
     
    63141        } 
    64142 
     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         */ 
    65158        private void processFile(String source, OutputStreamWriter targetFile) 
    66159                        throws FileNotFoundException, IOException { 
     
    68161                String incompleteLine = ""; 
    69162                // 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) { 
    73166                                        targetFile.write(" </session>" + StringTools.ENDLINE); 
    74167                                        targetFile.write(" <session>" + StringTools.ENDLINE); 
     
    77170                                        sessionOpen = true; 
    78171                                } 
    79                         } else if( currentLine.contains("UL: </session>")) { 
    80                                 if( sessionOpen) { 
     172                        } else if (currentLine.contains("UL: </session>")) { 
     173                                if (sessionOpen) { 
    81174                                        targetFile.write(" </session>" + StringTools.ENDLINE); 
    82175                                        sessionOpen = false; 
    83176                                } 
    84                         } else if( msgIncomplete || currentLine.contains("UL: ")) { 
    85                                  
     177                        } else if (msgIncomplete || currentLine.contains("UL: ")) { 
     178 
    86179                                String currentContent; 
    87180                                String actualLine; 
    88                                 if( msgIncomplete ) { 
     181                                if (msgIncomplete) { 
    89182                                        actualLine = currentLine; 
    90183                                } else { 
     
    92185                                        actualLine = splitResult[1]; 
    93186                                } 
    94                                 if( base64 ) { 
     187                                if (base64) { 
    95188                                        Base64 decoder = new Base64(); 
    96189                                        byte[] decoded = decoder.decode(actualLine); 
    97190                                        currentContent = new String(decoded, "UTF-16LE"); 
    98                                         currentContent = currentContent.substring(0, currentContent.length()-1); 
     191                                        currentContent = currentContent.substring(0, 
     192                                                        currentContent.length() - 1); 
    99193                                } else { 
    100194                                        currentContent = actualLine; 
    101195                                } 
    102                                 if( msgIncomplete ) { 
     196                                if (msgIncomplete) { 
    103197                                        incompleteLine += currentContent; 
    104                                         if( incompleteLine.contains("</msg>") ) { 
     198                                        if (incompleteLine.contains("</msg>")) { 
    105199                                                msgIncomplete = false; 
    106200                                                targetFile.write(incompleteLine + StringTools.ENDLINE); 
     
    108202                                        } 
    109203                                } 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); 
    113208                                                } else { 
    114209                                                        msgIncomplete = true; 
Note: See TracChangeset for help on using the changeset viewer.