source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/jfc/JFCLogParser.java @ 422

Last change on this file since 422 was 422, checked in by pharms, 12 years ago

Added support for external specification of event filters

  • Property svn:mime-type set to text/plain
File size: 8.1 KB
Line 
1package de.ugoe.cs.eventbench.jfc;
2
3import java.awt.event.FocusEvent;
4import java.awt.event.MouseEvent;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.FileNotFoundException;
8import java.io.IOException;
9import java.io.InputStreamReader;
10import java.io.UnsupportedEncodingException;
11import java.security.InvalidParameterException;
12import java.util.Collection;
13import java.util.HashSet;
14import java.util.LinkedList;
15import java.util.List;
16
17import javax.xml.parsers.ParserConfigurationException;
18import javax.xml.parsers.SAXParser;
19import javax.xml.parsers.SAXParserFactory;
20
21import org.xml.sax.Attributes;
22import org.xml.sax.InputSource;
23import org.xml.sax.SAXException;
24import org.xml.sax.SAXParseException;
25import org.xml.sax.helpers.DefaultHandler;
26
27import de.ugoe.cs.eventbench.jfc.data.JFCEvent;
28import de.ugoe.cs.util.console.Console;
29
30/**
31 * <p>
32 * This class provides functionality to parse XML log files generated by the
33 * JFCMonitor of EventBench. The result of parsing a file is a collection of
34 * event sequences.
35 * </p>
36 *
37 * @author Steffen Herbold
38 * @version 1.0
39 */
40public class JFCLogParser extends DefaultHandler {
41
42        /**
43         * <p>
44         * Collection of event sequences that is contained in the log file, which is
45         * parsed.
46         * </p>
47         */
48        private Collection<List<JFCEvent>> sequences;
49
50        /**
51         * <p>
52         * Internal handle to the event that is currently being parsed.
53         * </p>
54         */
55        private JFCEvent currentEvent;
56
57        /**
58         * <p>
59         * Internal handle to the event sequence that is currently being parsed.
60         * </p>
61         */
62        private List<JFCEvent> currentSequence = null;
63
64        /**
65         * <p>
66         * Internally used string to build a string representing a component-node.
67         * </p>
68         */
69        private String componentString;
70
71        /**
72         * <p>
73         * Enumeration to differentiate if a parameter belongs to an event, a source
74         * or the parent of a source.
75         * </p>
76         *
77         * @author Steffen Herbold
78         * @version 1.0
79         */
80        private enum ParamSource {
81                EVENT, SOURCE, PARENT, COMPONENT
82        };
83
84        /**
85         * <p>
86         * Specifies whether the parameters that are currently being read belong the
87         * the event, the source or the parent.
88         * </p>
89         */
90        ParamSource paramSource = null;
91
92        private Collection<Integer> eventFilter;
93
94        /**
95         * <p>
96         * Constructor. Creates a new JFCLogParser with a default event filter.
97         * This ignores focus events, mouse pressed, and mouse released events.
98         * </p>
99         */
100        public JFCLogParser() {
101                sequences = new LinkedList<List<JFCEvent>>();
102                currentSequence = new LinkedList<JFCEvent>();
103                setupDefaultEventFilter();
104        }
105
106        /**
107         * <p>
108         * Constructor. Creates a new JFCLogParser with a specific event filter.
109         * The events in the provided collection are ignored by the parser.
110         * As events, the constants of the different event classes must be used.
111         * E.g. creating a collection and putting
112         * <code>MouseEvent.MOUSE_PRESSED</code> will cause the parser to ignore
113         * all mouse pressed events. If the provided collection is null, no event
114         * is ignored.
115         * </p>
116         *
117         * @param ignoredEvents the events to be ignored by the parser, can be null
118         */
119        public JFCLogParser(Collection<Integer> ignoredEvents) {
120                sequences = new LinkedList<List<JFCEvent>>();
121                currentSequence = new LinkedList<JFCEvent>();
122                eventFilter = ignoredEvents;
123        }
124
125        /**
126         * <p>
127         * creates a default event filter that ignores focus changes, mouse pressed
128         * and mouse released events.
129         * </p>
130         */
131        private void setupDefaultEventFilter() {
132                eventFilter = new HashSet<Integer>();
133                eventFilter.add(MouseEvent.MOUSE_PRESSED);
134                eventFilter.add(MouseEvent.MOUSE_RELEASED);
135                eventFilter.add(FocusEvent.FOCUS_GAINED);
136        }
137
138        /**
139         * <p>
140         * Returns the collection of event sequences that is obtained from parsing
141         * log files.
142         * </p>
143         *
144         * @return collection of event sequences
145         */
146        public Collection<List<JFCEvent>> getSequences() {
147                return sequences;
148        }
149
150        /*
151         * (non-Javadoc)
152         *
153         * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
154         * java.lang.String, java.lang.String, org.xml.sax.Attributes)
155         */
156        public void startElement(String uri, String localName, String qName,
157                        Attributes atts) throws SAXException {
158                if (qName.equals("sessions")) {
159                        currentSequence = new LinkedList<JFCEvent>();
160                }
161                if (qName.equals("newsession")) {
162                        Console.traceln("start of session");
163                        if (currentSequence != null && !currentSequence.isEmpty()) {
164                                sequences.add(currentSequence);
165                        }
166                        currentSequence = new LinkedList<JFCEvent>();
167                } else if (qName.equals("event")) {
168                        int eventId = Integer.parseInt(atts.getValue("id"));
169                        if ((eventFilter == null) || (!eventFilter.contains(eventId))) {
170                                currentEvent = new JFCEvent(atts.getValue("id"));
171                                paramSource = ParamSource.EVENT;
172                        }
173                } else if (currentEvent != null) {
174                        if (qName.equals("param")) {
175                                if (paramSource == ParamSource.EVENT) {
176                                        currentEvent.addParameter(atts.getValue("name"),
177                                                        atts.getValue("value"));
178                                } else if (paramSource == ParamSource.SOURCE) {
179                                        currentEvent.addSourceInformation(atts.getValue("name"),
180                                                        atts.getValue("value"));
181                                } else if (paramSource == ParamSource.PARENT) {
182                                        currentEvent.addParentInformation(atts.getValue("name"),
183                                                        atts.getValue("value"));
184                                } else if (paramSource == ParamSource.COMPONENT) {
185                                        componentString += "'" + atts.getValue("value") + "',";
186                                }
187                        } else if (qName.equals("source")) {
188                                paramSource = ParamSource.SOURCE;
189                        } else if (qName.equals("parent")) {
190                                paramSource = ParamSource.PARENT;
191                        } else if (qName.equals("component")) {
192                                paramSource = ParamSource.COMPONENT;
193                                componentString = "[";
194                        }
195                }
196        }
197
198        /*
199         * (non-Javadoc)
200         *
201         * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
202         * java.lang.String, java.lang.String)
203         */
204        @Override
205        public void endElement(String uri, String localName, String qName)
206                        throws SAXException {
207                if (qName.equals("sessions")) {
208                        if (currentSequence != null && !currentSequence.isEmpty()) {
209                                sequences.add(currentSequence);
210                        }
211                        currentSequence = null;
212                } else if (currentEvent != null) {
213                        if (qName.equals("event")) {
214                                currentSequence.add(currentEvent);
215                                currentEvent = null;
216                        } else if (qName.equals("source")) {
217                                paramSource = ParamSource.EVENT;
218                        } else if (qName.equals("parent")) {
219                                paramSource = ParamSource.SOURCE;
220                        } else if (qName.equals("component")) {
221                                paramSource.equals(ParamSource.SOURCE);
222                                currentEvent.extendTarget(componentString.substring(0,
223                                                componentString.length() - 1) + "]");
224                        }
225                }
226        }
227
228        /**
229         * <p>
230         * Parses a log file written by the JFCMonitor and creates a collection of
231         * event sequences.
232         * </p>
233         *
234         * @param filename
235         *            name and path of the log file
236         */
237        public void parseFile(String filename) {
238                if (filename == null) {
239                        throw new InvalidParameterException("filename must not be null");
240                }
241
242                SAXParserFactory spf = SAXParserFactory.newInstance();
243                spf.setValidating(true);
244
245                SAXParser saxParser = null;
246                InputSource inputSource = null;
247                try {
248                        saxParser = spf.newSAXParser();
249                        inputSource = new InputSource(new InputStreamReader(
250                                        new FileInputStream(filename), "UTF-8"));
251                } catch (UnsupportedEncodingException e) {
252                        e.printStackTrace();
253                } catch (ParserConfigurationException e) {
254                        e.printStackTrace();
255                } catch (SAXException e) {
256                        e.printStackTrace();
257                } catch (FileNotFoundException e) {
258                        e.printStackTrace();
259                }
260                if (inputSource != null) {
261                        inputSource.setSystemId("file://"
262                                        + new File(filename).getAbsolutePath());
263                        try {
264                                if (saxParser == null) {
265                                        throw new RuntimeException("SAXParser creation failed");
266                                }
267                                saxParser.parse(inputSource, this);
268                        } catch (SAXParseException e) {
269                                Console.printerrln("Failure parsing file in line "
270                                                + e.getLineNumber() + ", column " + e.getColumnNumber()
271                                                + ".");
272                                e.printStackTrace();
273                        } catch (SAXException e) {
274                                e.printStackTrace();
275                        } catch (IOException e) {
276                                e.printStackTrace();
277                        }
278                }
279        }
280
281}
Note: See TracBrowser for help on using the repository browser.