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/LogParser.java

    r77 r171  
    2828import de.ugoe.cs.util.console.Console; 
    2929 
     30/** 
     31 * <p> 
     32 * This class provides functionality to parse XML log files generated by the 
     33 * MFCUsageMonitor of EventBench. The result of parsing a file is a collection 
     34 * of event sequences. It uses the {@link SequenceSplitter} and the 
     35 * {@link EventGenerator} as well as custom defined {@link MessageHandler} for 
     36 * the parsing. 
     37 * </p> 
     38 *  
     39 * @author Steffen Herbold 
     40 * @version 1.0 
     41 */ 
    3042public class LogParser extends DefaultHandler { 
    31          
     43 
     44        /** 
     45         * <p> 
     46         * If a custom message handler is used, this field contains its handle. 
     47         * Otherwise this field is {@code null}. 
     48         * </p> 
     49         */ 
    3250        private MessageHandler currentHandler; 
    33          
     51 
     52        /** 
     53         * <p> 
     54         * Handle to the message that is currently parsed. 
     55         * </p> 
     56         */ 
    3457        private WindowsMessage currentMessage; 
    35          
     58 
     59        /** 
     60         * <p> 
     61         * {@link SequenceSplitter} instance used by the {@link LogParser}. 
     62         * </p> 
     63         */ 
    3664        private SequenceSplitter sequenceSplitter; 
    37          
     65 
     66        /** 
     67         * <p> 
     68         * Collection of event sequences that is contained in the log file, which is 
     69         * parsed. 
     70         * </p> 
     71         */ 
    3872        private List<List<WindowsEvent>> sequences; 
    39          
     73 
     74        /** 
     75         * <p> 
     76         * Debugging variable that allows the analysis which message type occurs how 
     77         * often in the log file. Can be used to enhance the message filter. 
     78         * </p> 
     79         */ 
    4080        private SortedMap<Integer, Integer> typeCounter; 
    41          
     81 
     82        /** 
     83         * <p> 
     84         * Debugging variable that enables the counting of the occurrences of each 
     85         * message. Used in combination with {@link #typeCounter}. 
     86         * </p> 
     87         */ 
    4288        private boolean countMessageOccurences; 
    43          
     89 
     90        /** 
     91         * <p> 
     92         * Constructor. Creates a new LogParser that does not count message 
     93         * occurrences. 
     94         * </p> 
     95         */ 
    4496        public LogParser() { 
    4597                this(false); 
    4698        } 
    47          
     99 
     100        /** 
     101         * <p> 
     102         * Constructor. Creates a new LogParser. 
     103         * </p> 
     104         *  
     105         * @param countMessageOccurences 
     106         *            if true, the occurrences of each message type in the log is 
     107         *            counted. 
     108         */ 
    48109        public LogParser(boolean countMessageOccurences) { 
    49110                sequenceSplitter = new SequenceSplitter(); 
     
    51112                currentHandler = null; 
    52113                this.countMessageOccurences = countMessageOccurences; 
    53                 if( countMessageOccurences) { 
     114                if (countMessageOccurences) { 
    54115                        typeCounter = new TreeMap<Integer, Integer>(); 
    55116                } 
    56                  
    57         } 
    58          
     117 
     118        } 
     119 
     120        /** 
     121         * <p> 
     122         * Returns the collection of event sequences that is obtained from parsing 
     123         * log files. 
     124         * </p> 
     125         *  
     126         * @return collection of event sequences 
     127         */ 
    59128        public List<List<WindowsEvent>> getSequences() { 
    60129                return sequences; 
    61130        } 
    62          
     131 
     132        /* 
     133         * (non-Javadoc) 
     134         *  
     135         * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, 
     136         * java.lang.String, java.lang.String, org.xml.sax.Attributes) 
     137         */ 
    63138        @Override 
    64         public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 
    65                 if( qName.equals("session") ) { 
     139        public void startElement(String uri, String localName, String qName, 
     140                        Attributes atts) throws SAXException { 
     141                if (qName.equals("session")) { 
    66142                        Console.traceln("start of session"); 
    67143                        sequenceSplitter = new SequenceSplitter(); 
    68                 } 
    69                 else if( qName.equals("msg") ) { 
     144                } else if (qName.equals("msg")) { 
    70145                        String msgType = atts.getValue("type"); 
    71146                        int msgInt = -1; 
    72147                        try { 
    73148                                msgInt = Integer.parseInt(msgType); 
    74                                  
    75                                 if( countMessageOccurences ) { 
     149 
     150                                if (countMessageOccurences) { 
    76151                                        Integer currentCount = typeCounter.get(msgInt); 
    77                                         if( currentCount==null ) { 
     152                                        if (currentCount == null) { 
    78153                                                typeCounter.put(msgInt, 1); 
    79154                                        } else { 
    80                                                 typeCounter.put(msgInt, currentCount+1); 
     155                                                typeCounter.put(msgInt, currentCount + 1); 
    81156                                        } 
    82157                                } 
    83                                  
    84                                 if( msgInt==MessageDefs.WM_CREATE ) { 
     158 
     159                                if (msgInt == MessageDefs.WM_CREATE) { 
    85160                                        currentHandler = new HandlerCreate(); 
    86161                                        currentHandler.onStartElement(); 
    87                                 } 
    88                                 else if( msgInt==MessageDefs.WM_DESTROY ) { 
     162                                } else if (msgInt == MessageDefs.WM_DESTROY) { 
    89163                                        currentHandler = new HandlerDestroy(); 
    90164                                        currentHandler.onStartElement(); 
    91                                 } 
    92                                 else if( msgInt==MessageDefs.WM_SETTEXT ) { 
     165                                } else if (msgInt == MessageDefs.WM_SETTEXT) { 
    93166                                        currentHandler = new HandlerSetText(); 
    94167                                        currentHandler.onStartElement(); 
     
    96169                                        currentMessage = new WindowsMessage(msgInt); 
    97170                                } 
    98                         } catch(NumberFormatException e) { 
     171                        } catch (NumberFormatException e) { 
    99172                                Console.printerrln("Invalid message type: type not a number"); 
    100173                                e.printStackTrace(); 
    101174                        } 
    102                 } 
    103                 else if( qName.equals("param") ) { 
    104                         if( currentHandler!=null ) { 
    105                                 currentHandler.onParameter(atts.getValue("name"), atts.getValue("value")); 
     175                } else if (qName.equals("param")) { 
     176                        if (currentHandler != null) { 
     177                                currentHandler.onParameter(atts.getValue("name"), 
     178                                                atts.getValue("value")); 
    106179                        } else { 
    107                                 currentMessage.addParameter(atts.getValue("name"), atts.getValue("value")); 
    108                         } 
    109                 } 
    110         } 
    111          
     180                                currentMessage.addParameter(atts.getValue("name"), 
     181                                                atts.getValue("value")); 
     182                        } 
     183                } 
     184        } 
     185 
     186        /* 
     187         * (non-Javadoc) 
     188         *  
     189         * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, 
     190         * java.lang.String, java.lang.String) 
     191         */ 
    112192        @Override 
    113         public void endElement(String uri, String localName, String qName) throws SAXException { 
    114                 if( qName.equals("msg") ) { 
    115                         if( currentHandler!=null ) { 
     193        public void endElement(String uri, String localName, String qName) 
     194                        throws SAXException { 
     195                if (qName.equals("msg")) { 
     196                        if (currentHandler != null) { 
    116197                                currentHandler.onEndElement(); 
    117198                                currentHandler = null; 
     
    121202                                        sequenceSplitter.addMessage(currentMessage); 
    122203                                } catch (InvalidParameterException e) { 
    123                                         Console.traceln(e.getMessage() + " WindowsMessage " + currentMessage + " ignored."); 
    124                                 }                                
    125                         } 
    126                 } 
    127                 else if(qName.equals("session")) { 
     204                                        Console.traceln(e.getMessage() + " WindowsMessage " 
     205                                                        + currentMessage + " ignored."); 
     206                                } 
     207                        } 
     208                } else if (qName.equals("session")) { 
    128209                        sequenceSplitter.endSession(); 
    129210                        sequences.add(sequenceSplitter.getSequence()); 
     
    131212                } 
    132213        } 
    133          
     214 
     215        /** 
     216         * <p> 
     217         * Parses a given log file and adds its contents to the collection of event 
     218         * sequences. 
     219         * </p> 
     220         *  
     221         * @param filename 
     222         *            name and path of the log file 
     223         */ 
    134224        public void parseFile(String filename) { 
    135                 if( filename==null ) { 
     225                if (filename == null) { 
    136226                        throw new InvalidParameterException("filename must not be null"); 
    137227                } 
    138                  
     228 
    139229                SAXParserFactory spf = SAXParserFactory.newInstance(); 
    140230                spf.setValidating(true); 
    141                  
     231 
    142232                SAXParser saxParser = null; 
    143233                InputSource inputSource = null; 
    144234                try { 
    145235                        saxParser = spf.newSAXParser(); 
    146                         inputSource = new InputSource(new InputStreamReader(new FileInputStream(filename), "UTF-16")); 
     236                        inputSource = new InputSource(new InputStreamReader( 
     237                                        new FileInputStream(filename), "UTF-16")); 
    147238                } catch (UnsupportedEncodingException e) { 
    148239                        e.printStackTrace(); 
     
    154245                        e.printStackTrace(); 
    155246                } 
    156                 if( inputSource!=null ) { 
    157                 inputSource.setSystemId("file://" + new File(filename).getAbsolutePath()); 
    158                 try { 
    159                         if( saxParser==null) { 
    160                                 throw new RuntimeException("SAXParser creation failed"); 
    161                         } 
     247                if (inputSource != null) { 
     248                        inputSource.setSystemId("file://" 
     249                                        + new File(filename).getAbsolutePath()); 
     250                        try { 
     251                                if (saxParser == null) { 
     252                                        throw new RuntimeException("SAXParser creation failed"); 
     253                                } 
    162254                                saxParser.parse(inputSource, this); 
    163                 } catch (SAXParseException e) { 
    164                         Console.printerrln("Failure parsing file in line " + e.getLineNumber() + ", column " + e.getColumnNumber() +"."); 
    165                         e.printStackTrace(); 
     255                        } catch (SAXParseException e) { 
     256                                Console.printerrln("Failure parsing file in line " 
     257                                                + e.getLineNumber() + ", column " + e.getColumnNumber() 
     258                                                + "."); 
     259                                e.printStackTrace(); 
    166260                        } catch (SAXException e) { 
    167261                                e.printStackTrace(); 
     
    170264                        } 
    171265                } 
    172                 if( countMessageOccurences ) { 
     266                if (countMessageOccurences) { 
    173267                        Console.println("Message statistics:"); 
    174                         Console.println(typeCounter.toString().replace(" ", StringTools.ENDLINE).replaceAll("[\\{\\}]","")); 
     268                        Console.println(typeCounter.toString() 
     269                                        .replace(" ", StringTools.ENDLINE) 
     270                                        .replaceAll("[\\{\\}]", "")); 
    175271                } 
    176272        } 
Note: See TracChangeset for help on using the changeset viewer.