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

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