[1] | 1 | package de.ugoe.cs.eventbench;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.FileNotFoundException;
|
---|
| 5 | import java.io.FileReader;
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.security.InvalidParameterException;
|
---|
| 8 | import java.util.LinkedList;
|
---|
| 9 | import java.util.List;
|
---|
| 10 | import java.util.SortedMap;
|
---|
| 11 | import java.util.TreeMap;
|
---|
| 12 |
|
---|
| 13 | import javax.xml.parsers.ParserConfigurationException;
|
---|
| 14 | import javax.xml.parsers.SAXParser;
|
---|
| 15 | import javax.xml.parsers.SAXParserFactory;
|
---|
| 16 |
|
---|
| 17 | import org.xml.sax.Attributes;
|
---|
| 18 | import org.xml.sax.InputSource;
|
---|
| 19 | import org.xml.sax.SAXException;
|
---|
| 20 | import org.xml.sax.SAXParseException;
|
---|
| 21 | import org.xml.sax.helpers.DefaultHandler;
|
---|
| 22 |
|
---|
| 23 | import de.ugoe.cs.eventbench.data.Event;
|
---|
| 24 | import de.ugoe.cs.eventbench.data.WindowTree;
|
---|
| 25 | import de.ugoe.cs.eventbench.data.WindowsMessage;
|
---|
| 26 | import de.ugoe.cs.eventbench.messagehandler.HandlerCreate;
|
---|
| 27 | import de.ugoe.cs.eventbench.messagehandler.HandlerDestroy;
|
---|
| 28 | import de.ugoe.cs.eventbench.messagehandler.HandlerSetText;
|
---|
| 29 | import de.ugoe.cs.eventbench.messagehandler.MessageHandler;
|
---|
| 30 | import de.ugoe.cs.eventbench.windowsdefs.MessageDefs;
|
---|
| 31 | import de.ugoe.cs.util.StringTools;
|
---|
| 32 | import de.ugoe.cs.util.console.Console;
|
---|
| 33 |
|
---|
| 34 | public class LogParser extends DefaultHandler {
|
---|
| 35 |
|
---|
| 36 | private MessageHandler currentHandler;
|
---|
| 37 |
|
---|
| 38 | private WindowsMessage currentMessage;
|
---|
| 39 |
|
---|
| 40 | private SequenceSplitter sequenceSplitter;
|
---|
| 41 |
|
---|
| 42 | private List<List<Event<WindowsMessage>>> sequences;
|
---|
| 43 |
|
---|
| 44 | private SortedMap<Integer, Integer> typeCounter;
|
---|
| 45 |
|
---|
| 46 | private boolean countMessageOccurences;
|
---|
| 47 |
|
---|
| 48 | public LogParser() {
|
---|
| 49 | this(false);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public LogParser(boolean countMessageOccurences) {
|
---|
| 53 | sequenceSplitter = new SequenceSplitter();
|
---|
| 54 | sequences = new LinkedList<List<Event<WindowsMessage>>>();
|
---|
| 55 | currentHandler = null;
|
---|
| 56 | this.countMessageOccurences = countMessageOccurences;
|
---|
| 57 | if( countMessageOccurences) {
|
---|
| 58 | typeCounter = new TreeMap<Integer, Integer>();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public List<List<Event<WindowsMessage>>> getSequences() {
|
---|
| 64 | return sequences;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | @Override
|
---|
| 68 | public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
|
---|
| 69 | if( qName.equals("session") ) {
|
---|
| 70 | Console.traceln("start of session");
|
---|
| 71 | sequenceSplitter = new SequenceSplitter();
|
---|
| 72 | }
|
---|
| 73 | else if( qName.equals("msg") ) {
|
---|
| 74 | String msgType = atts.getValue("type");
|
---|
| 75 | int msgInt = -1;
|
---|
| 76 | try {
|
---|
| 77 | msgInt = Integer.parseInt(msgType);
|
---|
| 78 |
|
---|
| 79 | if( countMessageOccurences ) {
|
---|
| 80 | Integer currentCount = typeCounter.get(msgInt);
|
---|
| 81 | if( currentCount==null ) {
|
---|
| 82 | typeCounter.put(msgInt, 1);
|
---|
| 83 | } else {
|
---|
| 84 | typeCounter.put(msgInt, currentCount+1);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | if( msgInt==MessageDefs.WM_CREATE ) {
|
---|
| 89 | currentHandler = new HandlerCreate();
|
---|
| 90 | currentHandler.onStartElement();
|
---|
| 91 | }
|
---|
| 92 | else if( msgInt==MessageDefs.WM_DESTROY ) {
|
---|
| 93 | currentHandler = new HandlerDestroy();
|
---|
| 94 | currentHandler.onStartElement();
|
---|
| 95 | }
|
---|
| 96 | else if( msgInt==MessageDefs.WM_SETTEXT ) {
|
---|
| 97 | currentHandler = new HandlerSetText();
|
---|
| 98 | currentHandler.onStartElement();
|
---|
| 99 | } else {
|
---|
| 100 | currentMessage = new WindowsMessage(msgInt);
|
---|
| 101 | }
|
---|
| 102 | } catch(NumberFormatException e) {
|
---|
| 103 | System.err.println("Invalid message type: type not a number");
|
---|
| 104 | e.printStackTrace();
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | else if( qName.equals("param") ) {
|
---|
| 108 | if( currentHandler!=null ) {
|
---|
| 109 | currentHandler.onParameter(atts.getValue("name"), atts.getValue("value"));
|
---|
| 110 | } else {
|
---|
| 111 | currentMessage.addParameter(atts.getValue("name"), atts.getValue("value"));
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | @Override
|
---|
| 117 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
| 118 | if( qName.equals("msg") ) {
|
---|
| 119 | if( currentHandler!=null ) {
|
---|
| 120 | currentHandler.onEndElement();
|
---|
| 121 | currentHandler = null;
|
---|
| 122 | } else {
|
---|
| 123 | try {
|
---|
| 124 | currentMessage.setTarget(WindowTree.getInstance());
|
---|
| 125 | sequenceSplitter.addMessage(currentMessage);
|
---|
| 126 | } catch (InvalidParameterException e) {
|
---|
| 127 | Console.traceln(e.getMessage() + " WindowsMessage " + currentMessage + " ignored.");
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | else if(qName.equals("session")) {
|
---|
| 132 | sequenceSplitter.endSession();
|
---|
| 133 | sequences.add(sequenceSplitter.getSequence());
|
---|
| 134 | Console.traceln("end of session");
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | public void parseFile(String filename) {
|
---|
| 139 | if( filename==null ) {
|
---|
| 140 | throw new InvalidParameterException("filename must not be null");
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | SAXParserFactory spf = SAXParserFactory.newInstance();
|
---|
| 144 | spf.setValidating(true);
|
---|
| 145 |
|
---|
| 146 | SAXParser saxParser = null;
|
---|
| 147 | InputSource inputSource = null;
|
---|
| 148 | try {
|
---|
| 149 | saxParser = spf.newSAXParser();
|
---|
| 150 | inputSource = new InputSource(new FileReader(filename));
|
---|
| 151 | } catch (ParserConfigurationException e) {
|
---|
| 152 | e.printStackTrace();
|
---|
| 153 | } catch (SAXException e) {
|
---|
| 154 | e.printStackTrace();
|
---|
| 155 | } catch (FileNotFoundException e) {
|
---|
| 156 | e.printStackTrace();
|
---|
| 157 | }
|
---|
| 158 | if( inputSource!=null ) {
|
---|
| 159 | inputSource.setSystemId("file://" + new File(filename).getAbsolutePath());
|
---|
| 160 | try {
|
---|
| 161 | if( saxParser==null) {
|
---|
| 162 | throw new RuntimeException("SAXParser creation failed");
|
---|
| 163 | }
|
---|
| 164 | saxParser.parse(inputSource, this);
|
---|
| 165 | } catch (SAXParseException e) {
|
---|
| 166 | Console.printerrln("Failure parsing file in line " + e.getLineNumber() + ", column " + e.getColumnNumber() +".");
|
---|
| 167 | e.printStackTrace();
|
---|
| 168 | } catch (SAXException e) {
|
---|
| 169 | e.printStackTrace();
|
---|
| 170 | } catch (IOException e) {
|
---|
| 171 | e.printStackTrace();
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | if( countMessageOccurences ) {
|
---|
| 175 | Console.println("Message statistics:");
|
---|
| 176 | Console.println(typeCounter.toString().replace(" ", StringTools.ENDLINE).replaceAll("[\\{\\}]",""));
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|