source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/windows/LogParser.java @ 77

Last change on this file since 77 was 77, checked in by sherbold, 13 years ago

+ introduced de.ugoe.cs.eventbench.windows.WindowsEvent?

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