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

Last change on this file since 75 was 75, checked in by sherbold, 13 years ago
  • refactoring
File size: 5.5 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.data.Event;
26import de.ugoe.cs.eventbench.windows.data.WindowTree;
27import de.ugoe.cs.eventbench.windows.data.WindowsMessage;
28import de.ugoe.cs.eventbench.windowsdefs.MessageDefs;
29import de.ugoe.cs.util.StringTools;
30import de.ugoe.cs.util.console.Console;
31
32public class LogParser extends DefaultHandler {
33       
34        private MessageHandler currentHandler;
35       
36        private WindowsMessage currentMessage;
37       
38        private SequenceSplitter sequenceSplitter;
39       
40        private List<List<Event<WindowsMessage>>> sequences;
41       
42        private SortedMap<Integer, Integer> typeCounter;
43       
44        private boolean countMessageOccurences;
45       
46        public LogParser() {
47                this(false);
48        }
49       
50        public LogParser(boolean countMessageOccurences) {
51                sequenceSplitter = new SequenceSplitter();
52                sequences = new LinkedList<List<Event<WindowsMessage>>>();
53                currentHandler = null;
54                this.countMessageOccurences = countMessageOccurences;
55                if( countMessageOccurences) {
56                        typeCounter = new TreeMap<Integer, Integer>();
57                }
58               
59        }
60       
61        public List<List<Event<WindowsMessage>>> getSequences() {
62                return sequences;
63        }
64       
65        @Override
66        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
67                if( qName.equals("session") ) {
68                        Console.traceln("start of session");
69                        sequenceSplitter = new SequenceSplitter();
70                }
71                else if( qName.equals("msg") ) {
72                        String msgType = atts.getValue("type");
73                        int msgInt = -1;
74                        try {
75                                msgInt = Integer.parseInt(msgType);
76                               
77                                if( countMessageOccurences ) {
78                                        Integer currentCount = typeCounter.get(msgInt);
79                                        if( currentCount==null ) {
80                                                typeCounter.put(msgInt, 1);
81                                        } else {
82                                                typeCounter.put(msgInt, currentCount+1);
83                                        }
84                                }
85                               
86                                if( msgInt==MessageDefs.WM_CREATE ) {
87                                        currentHandler = new HandlerCreate();
88                                        currentHandler.onStartElement();
89                                }
90                                else if( msgInt==MessageDefs.WM_DESTROY ) {
91                                        currentHandler = new HandlerDestroy();
92                                        currentHandler.onStartElement();
93                                }
94                                else if( msgInt==MessageDefs.WM_SETTEXT ) {
95                                        currentHandler = new HandlerSetText();
96                                        currentHandler.onStartElement();
97                                } else {
98                                        currentMessage = new WindowsMessage(msgInt);
99                                }
100                        } catch(NumberFormatException e) {
101                                Console.printerrln("Invalid message type: type not a number");
102                                e.printStackTrace();
103                        }
104                }
105                else if( qName.equals("param") ) {
106                        if( currentHandler!=null ) {
107                                currentHandler.onParameter(atts.getValue("name"), atts.getValue("value"));
108                        } else {
109                                currentMessage.addParameter(atts.getValue("name"), atts.getValue("value"));
110                        }
111                }
112        }
113       
114        @Override
115        public void endElement(String uri, String localName, String qName) throws SAXException {
116                if( qName.equals("msg") ) {
117                        if( currentHandler!=null ) {
118                                currentHandler.onEndElement();
119                                currentHandler = null;
120                        } else {
121                                try {
122                                        currentMessage.setTarget(WindowTree.getInstance());
123                                        sequenceSplitter.addMessage(currentMessage);
124                                } catch (InvalidParameterException e) {
125                                        Console.traceln(e.getMessage() + " WindowsMessage " + currentMessage + " ignored.");
126                                }                               
127                        }
128                }
129                else if(qName.equals("session")) {
130                        sequenceSplitter.endSession();
131                        sequences.add(sequenceSplitter.getSequence());
132                        Console.traceln("end of session");
133                }
134        }
135       
136        public void parseFile(String filename) {
137                if( filename==null ) {
138                        throw new InvalidParameterException("filename must not be null");
139                }
140               
141                SAXParserFactory spf = SAXParserFactory.newInstance();
142                spf.setValidating(true);
143               
144                SAXParser saxParser = null;
145                InputSource inputSource = null;
146                try {
147                        saxParser = spf.newSAXParser();
148                        inputSource = new InputSource(new InputStreamReader(new FileInputStream(filename), "UTF-16"));
149                } catch (UnsupportedEncodingException e) {
150                        e.printStackTrace();
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}
Note: See TracBrowser for help on using the repository browser.