source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/windows/MFCLogParser.java @ 297

Last change on this file since 297 was 297, checked in by sherbold, 12 years ago
  • renamed de.ugoe.cs.eventbench.windows.LogParser? to MFCLogParser
File size: 7.6 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.Collection;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.SortedMap;
14import java.util.TreeMap;
15
16import javax.xml.parsers.ParserConfigurationException;
17import javax.xml.parsers.SAXParser;
18import javax.xml.parsers.SAXParserFactory;
19
20import org.xml.sax.Attributes;
21import org.xml.sax.InputSource;
22import org.xml.sax.SAXException;
23import org.xml.sax.SAXParseException;
24import org.xml.sax.helpers.DefaultHandler;
25
26import de.ugoe.cs.eventbench.windows.data.WindowTree;
27import de.ugoe.cs.eventbench.windows.data.WindowsEvent;
28import de.ugoe.cs.eventbench.windows.data.WindowsMessage;
29import de.ugoe.cs.util.StringTools;
30import de.ugoe.cs.util.console.Console;
31
32/**
33 * <p>
34 * This class provides functionality to parse XML log files generated by the
35 * MFCUsageMonitor of EventBench. The result of parsing a file is a collection
36 * of event sequences. It uses the {@link SequenceSplitter} and the
37 * {@link EventGenerator} as well as custom defined {@link MessageHandler} for
38 * the parsing.
39 * </p>
40 *
41 * @author Steffen Herbold
42 * @version 1.0
43 */
44public class MFCLogParser extends DefaultHandler {
45
46        /**
47         * <p>
48         * If a custom message handler is used, this field contains its handle.
49         * Otherwise this field is {@code null}.
50         * </p>
51         */
52        private MessageHandler currentHandler;
53
54        /**
55         * <p>
56         * Handle to the message that is currently parsed.
57         * </p>
58         */
59        private WindowsMessage currentMessage;
60
61        /**
62         * <p>
63         * {@link SequenceSplitter} instance used by the {@link MFCLogParser}.
64         * </p>
65         */
66        private SequenceSplitter sequenceSplitter;
67
68        /**
69         * <p>
70         * Collection of event sequences that is contained in the log file, which is
71         * parsed.
72         * </p>
73         */
74        private Collection<List<WindowsEvent>> sequences;
75
76        /**
77         * <p>
78         * Debugging variable that allows the analysis which message type occurs how
79         * often in the log file. Can be used to enhance the message filter.
80         * </p>
81         */
82        private SortedMap<Integer, Integer> typeCounter;
83
84        /**
85         * <p>
86         * Debugging variable that enables the counting of the occurrences of each
87         * message. Used in combination with {@link #typeCounter}.
88         * </p>
89         */
90        private boolean countMessageOccurences;
91
92        /**
93         * <p>
94         * Constructor. Creates a new LogParser that does not count message
95         * occurrences.
96         * </p>
97         */
98        public MFCLogParser() {
99                this(false);
100        }
101
102        /**
103         * <p>
104         * Constructor. Creates a new LogParser.
105         * </p>
106         *
107         * @param countMessageOccurences
108         *            if true, the occurrences of each message type in the log is
109         *            counted.
110         */
111        public MFCLogParser(boolean countMessageOccurences) {
112                sequenceSplitter = new SequenceSplitter();
113                sequences = new LinkedList<List<WindowsEvent>>();
114                currentHandler = null;
115                this.countMessageOccurences = countMessageOccurences;
116                if (countMessageOccurences) {
117                        typeCounter = new TreeMap<Integer, Integer>();
118                }
119
120        }
121
122        /**
123         * <p>
124         * Returns the collection of event sequences that is obtained from parsing
125         * log files.
126         * </p>
127         *
128         * @return collection of event sequences
129         */
130        public Collection<List<WindowsEvent>> getSequences() {
131                return sequences;
132        }
133
134        /*
135         * (non-Javadoc)
136         *
137         * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
138         * java.lang.String, java.lang.String, org.xml.sax.Attributes)
139         */
140        @Override
141        public void startElement(String uri, String localName, String qName,
142                        Attributes atts) throws SAXException {
143                if (qName.equals("session")) {
144                        Console.traceln("start of session");
145                        sequenceSplitter = new SequenceSplitter();
146                } else if (qName.equals("msg")) {
147                        String msgType = atts.getValue("type");
148                        int msgInt = -1;
149                        try {
150                                msgInt = Integer.parseInt(msgType);
151
152                                if (countMessageOccurences) {
153                                        Integer currentCount = typeCounter.get(msgInt);
154                                        if (currentCount == null) {
155                                                typeCounter.put(msgInt, 1);
156                                        } else {
157                                                typeCounter.put(msgInt, currentCount + 1);
158                                        }
159                                }
160
161                                if (msgInt == MessageDefs.WM_CREATE) {
162                                        currentHandler = new HandlerCreate();
163                                        currentHandler.onStartElement();
164                                } else if (msgInt == MessageDefs.WM_DESTROY) {
165                                        currentHandler = new HandlerDestroy();
166                                        currentHandler.onStartElement();
167                                } else if (msgInt == MessageDefs.WM_SETTEXT) {
168                                        currentHandler = new HandlerSetText();
169                                        currentHandler.onStartElement();
170                                } else {
171                                        currentMessage = new WindowsMessage(msgInt);
172                                }
173                        } catch (NumberFormatException e) {
174                                Console.printerrln("Invalid message type: type not a number");
175                                e.printStackTrace();
176                        }
177                } else if (qName.equals("param")) {
178                        if (currentHandler != null) {
179                                currentHandler.onParameter(atts.getValue("name"),
180                                                atts.getValue("value"));
181                        } else {
182                                currentMessage.addParameter(atts.getValue("name"),
183                                                atts.getValue("value"));
184                        }
185                }
186        }
187
188        /*
189         * (non-Javadoc)
190         *
191         * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
192         * java.lang.String, java.lang.String)
193         */
194        @Override
195        public void endElement(String uri, String localName, String qName)
196                        throws SAXException {
197                if (qName.equals("msg")) {
198                        if (currentHandler != null) {
199                                currentHandler.onEndElement();
200                                currentHandler = null;
201                        } else {
202                                try {
203                                        currentMessage.setTarget(WindowTree.getInstance());
204                                        sequenceSplitter.addMessage(currentMessage);
205                                } catch (InvalidParameterException e) {
206                                        Console.traceln(e.getMessage() + " WindowsMessage "
207                                                        + currentMessage + " ignored.");
208                                }
209                        }
210                } else if (qName.equals("session")) {
211                        sequenceSplitter.endSession();
212                        sequences.add(sequenceSplitter.getSequence());
213                        Console.traceln("end of session");
214                }
215        }
216
217        /**
218         * <p>
219         * Parses a given log file created by the MFCMonitor and adds its contents
220         * to the collection of event sequences.
221         * </p>
222         *
223         * @param filename
224         *            name and path of the log file
225         */
226        public void parseFile(String filename) {
227                if (filename == null) {
228                        throw new InvalidParameterException("filename must not be null");
229                }
230
231                SAXParserFactory spf = SAXParserFactory.newInstance();
232                spf.setValidating(true);
233
234                SAXParser saxParser = null;
235                InputSource inputSource = null;
236                try {
237                        saxParser = spf.newSAXParser();
238                        inputSource = new InputSource(new InputStreamReader(
239                                        new FileInputStream(filename), "UTF-16"));
240                } catch (UnsupportedEncodingException e) {
241                        e.printStackTrace();
242                } catch (ParserConfigurationException e) {
243                        e.printStackTrace();
244                } catch (SAXException e) {
245                        e.printStackTrace();
246                } catch (FileNotFoundException e) {
247                        e.printStackTrace();
248                }
249                if (inputSource != null) {
250                        inputSource.setSystemId("file://"
251                                        + new File(filename).getAbsolutePath());
252                        try {
253                                if (saxParser == null) {
254                                        throw new RuntimeException("SAXParser creation failed");
255                                }
256                                saxParser.parse(inputSource, this);
257                        } catch (SAXParseException e) {
258                                Console.printerrln("Failure parsing file in line "
259                                                + e.getLineNumber() + ", column " + e.getColumnNumber()
260                                                + ".");
261                                e.printStackTrace();
262                        } catch (SAXException e) {
263                                e.printStackTrace();
264                        } catch (IOException e) {
265                                e.printStackTrace();
266                        }
267                }
268                if (countMessageOccurences) {
269                        Console.println("Message statistics:");
270                        Console.println(typeCounter.toString()
271                                        .replace(" ", StringTools.ENDLINE)
272                                        .replaceAll("[\\{\\}]", ""));
273                }
274        }
275}
Note: See TracBrowser for help on using the repository browser.