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