- 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/LogParser.java
r77 r171 28 28 import de.ugoe.cs.util.console.Console; 29 29 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 */ 30 42 public 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 */ 32 50 private MessageHandler currentHandler; 33 51 52 /** 53 * <p> 54 * Handle to the message that is currently parsed. 55 * </p> 56 */ 34 57 private WindowsMessage currentMessage; 35 58 59 /** 60 * <p> 61 * {@link SequenceSplitter} instance used by the {@link LogParser}. 62 * </p> 63 */ 36 64 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 */ 38 72 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 */ 40 80 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 */ 42 88 private boolean countMessageOccurences; 43 89 90 /** 91 * <p> 92 * Constructor. Creates a new LogParser that does not count message 93 * occurrences. 94 * </p> 95 */ 44 96 public LogParser() { 45 97 this(false); 46 98 } 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 */ 48 109 public LogParser(boolean countMessageOccurences) { 49 110 sequenceSplitter = new SequenceSplitter(); … … 51 112 currentHandler = null; 52 113 this.countMessageOccurences = countMessageOccurences; 53 if (countMessageOccurences) {114 if (countMessageOccurences) { 54 115 typeCounter = new TreeMap<Integer, Integer>(); 55 116 } 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 */ 59 128 public List<List<WindowsEvent>> getSequences() { 60 129 return sequences; 61 130 } 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 */ 63 138 @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")) { 66 142 Console.traceln("start of session"); 67 143 sequenceSplitter = new SequenceSplitter(); 68 } 69 else if( qName.equals("msg") ) { 144 } else if (qName.equals("msg")) { 70 145 String msgType = atts.getValue("type"); 71 146 int msgInt = -1; 72 147 try { 73 148 msgInt = Integer.parseInt(msgType); 74 75 if ( countMessageOccurences) {149 150 if (countMessageOccurences) { 76 151 Integer currentCount = typeCounter.get(msgInt); 77 if ( currentCount==null) {152 if (currentCount == null) { 78 153 typeCounter.put(msgInt, 1); 79 154 } else { 80 typeCounter.put(msgInt, currentCount +1);155 typeCounter.put(msgInt, currentCount + 1); 81 156 } 82 157 } 83 84 if ( msgInt==MessageDefs.WM_CREATE) {158 159 if (msgInt == MessageDefs.WM_CREATE) { 85 160 currentHandler = new HandlerCreate(); 86 161 currentHandler.onStartElement(); 87 } 88 else if( msgInt==MessageDefs.WM_DESTROY ) { 162 } else if (msgInt == MessageDefs.WM_DESTROY) { 89 163 currentHandler = new HandlerDestroy(); 90 164 currentHandler.onStartElement(); 91 } 92 else if( msgInt==MessageDefs.WM_SETTEXT ) { 165 } else if (msgInt == MessageDefs.WM_SETTEXT) { 93 166 currentHandler = new HandlerSetText(); 94 167 currentHandler.onStartElement(); … … 96 169 currentMessage = new WindowsMessage(msgInt); 97 170 } 98 } catch (NumberFormatException e) {171 } catch (NumberFormatException e) { 99 172 Console.printerrln("Invalid message type: type not a number"); 100 173 e.printStackTrace(); 101 174 } 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")); 106 179 } 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 */ 112 192 @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) { 116 197 currentHandler.onEndElement(); 117 198 currentHandler = null; … … 121 202 sequenceSplitter.addMessage(currentMessage); 122 203 } 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")) { 128 209 sequenceSplitter.endSession(); 129 210 sequences.add(sequenceSplitter.getSequence()); … … 131 212 } 132 213 } 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 */ 134 224 public void parseFile(String filename) { 135 if ( filename==null) {225 if (filename == null) { 136 226 throw new InvalidParameterException("filename must not be null"); 137 227 } 138 228 139 229 SAXParserFactory spf = SAXParserFactory.newInstance(); 140 230 spf.setValidating(true); 141 231 142 232 SAXParser saxParser = null; 143 233 InputSource inputSource = null; 144 234 try { 145 235 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")); 147 238 } catch (UnsupportedEncodingException e) { 148 239 e.printStackTrace(); … … 154 245 e.printStackTrace(); 155 246 } 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 } 162 254 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(); 166 260 } catch (SAXException e) { 167 261 e.printStackTrace(); … … 170 264 } 171 265 } 172 if ( countMessageOccurences) {266 if (countMessageOccurences) { 173 267 Console.println("Message statistics:"); 174 Console.println(typeCounter.toString().replace(" ", StringTools.ENDLINE).replaceAll("[\\{\\}]","")); 268 Console.println(typeCounter.toString() 269 .replace(" ", StringTools.ENDLINE) 270 .replaceAll("[\\{\\}]", "")); 175 271 } 176 272 }
Note: See TracChangeset
for help on using the changeset viewer.